Mercurial > libervia-backend
annotate libervia/backend/memory/persistent.py @ 4340:ea72364131d5 default tip @
doc (components): Update Email Gateway documentation:
A section has been added to explain how attachments are handled.
fix 453
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 03 Dec 2024 00:53:18 +0100 |
parents | 0d7bb4df2343 |
children |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
4 # SAT: a jabber client |
3479 | 5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
10 # (at your option) any later version. |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
15 # GNU Affero General Public License for more details. |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
20 from twisted.internet import defer |
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
21 from twisted.python import failure |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
22 from libervia.backend.core.i18n import _ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
23 from libervia.backend.core.log import getLogger |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
24 |
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
25 |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
26 log = getLogger(__name__) |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 |
592
e5a875a3311b
Fix pep8 support in src/memory.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
28 |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 class MemoryNotInitializedError(Exception): |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 pass |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 |
592
e5a875a3311b
Fix pep8 support in src/memory.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
32 |
3234
78fea49735c5
core (memory/persistent): added `aset` method in LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
3225
diff
changeset
|
33 class PersistentDict: |
592
e5a875a3311b
Fix pep8 support in src/memory.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
34 r"""A dictionary which save persistently each value assigned |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
35 |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 /!\ be careful, each assignment means a database write |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
37 /!\ Memory must be initialised before loading/setting value with instances of this class |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
38 """ |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
39 |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 storage = None |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
41 binary = False |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 def __init__(self, namespace, profile=None): |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
44 """ |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
45 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
46 @param namespace: unique namespace for this dictionary |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
47 @param profile(unicode, None): profile which *MUST* exists, or None for general values |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
48 """ |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 if not self.storage: |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
50 log.error(_("PersistentDict can't be used before memory initialisation")) |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 raise MemoryNotInitializedError |
2890
c652d079a9a1
core (memory/persistent): added clear method + initialise with a dict:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
52 self._cache = {} |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 self.namespace = namespace |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 self.profile = profile |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3661
diff
changeset
|
56 def _set_cache(self, data): |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
57 self._cache = data |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
58 |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 def load(self): |
1029
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
60 """Load persistent data from storage. |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
61 |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
62 need to be called before any other operation |
1029
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
63 @return: defers the PersistentDict instance itself |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 """ |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
65 d = defer.ensureDeferred( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
66 self.storage.get_privates( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
67 self.namespace, binary=self.binary, profile=self.profile |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
68 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
69 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3661
diff
changeset
|
70 d.addCallback(self._set_cache) |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2734
diff
changeset
|
71 d.addCallback(lambda __: self) |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
72 return d |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 |
1292
b29a065a66f0
core: added items() and iteritems() methods to PersistentDict
Goffi <goffi@goffi.org>
parents:
1029
diff
changeset
|
74 def iteritems(self): |
3028 | 75 return iter(self._cache.items()) |
1292
b29a065a66f0
core: added items() and iteritems() methods to PersistentDict
Goffi <goffi@goffi.org>
parents:
1029
diff
changeset
|
76 |
b29a065a66f0
core: added items() and iteritems() methods to PersistentDict
Goffi <goffi@goffi.org>
parents:
1029
diff
changeset
|
77 def items(self): |
3253
1af840e84af7
core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
3234
diff
changeset
|
78 return self._cache.items() |
1292
b29a065a66f0
core: added items() and iteritems() methods to PersistentDict
Goffi <goffi@goffi.org>
parents:
1029
diff
changeset
|
79 |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 def __repr__(self): |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 return self._cache.__repr__() |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 def __str__(self): |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 return self._cache.__str__() |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 def __lt__(self, other): |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 return self._cache.__lt__(other) |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 def __le__(self, other): |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 return self._cache.__le__(other) |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 def __eq__(self, other): |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 return self._cache.__eq__(other) |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 def __ne__(self, other): |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 return self._cache.__ne__(other) |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 def __gt__(self, other): |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 return self._cache.__gt__(other) |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 def __ge__(self, other): |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 return self._cache.__ge__(other) |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 def __cmp__(self, other): |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 return self._cache.__cmp__(other) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
106 |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 def __hash__(self): |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 return self._cache.__hash__() |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 |
3028 | 110 def __bool__(self): |
111 return self._cache.__len__() != 0 | |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 def __contains__(self, key): |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 return self._cache.__contains__(key) |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 |
979
8f8bdd31375a
core (memory): added __iter__ to PersistentDict
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
116 def __iter__(self): |
8f8bdd31375a
core (memory): added __iter__ to PersistentDict
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
117 return self._cache.__iter__() |
8f8bdd31375a
core (memory): added __iter__ to PersistentDict
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
118 |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 def __getitem__(self, key): |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 return self._cache.__getitem__(key) |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 def __setitem__(self, key, value): |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
123 defer.ensureDeferred( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3661
diff
changeset
|
124 self.storage.set_private_value( |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
125 self.namespace, key, value, self.binary, self.profile |
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
126 ) |
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
127 ) |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 return self._cache.__setitem__(key, value) |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 def __delitem__(self, key): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3661
diff
changeset
|
131 self.storage.del_private_value(self.namespace, key, self.binary, self.profile) |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 return self._cache.__delitem__(key) |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 |
2890
c652d079a9a1
core (memory/persistent): added clear method + initialise with a dict:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
134 def clear(self): |
c652d079a9a1
core (memory/persistent): added clear method + initialise with a dict:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
135 """Delete all values from this namespace""" |
c652d079a9a1
core (memory/persistent): added clear method + initialise with a dict:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
136 self._cache.clear() |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
137 return self.storage.del_private_namespace( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
138 self.namespace, self.binary, self.profile |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
139 ) |
2890
c652d079a9a1
core (memory/persistent): added clear method + initialise with a dict:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
140 |
442
ab9ddf6c7eaf
core: added get method to PersistentDictionary
Goffi <goffi@goffi.org>
parents:
439
diff
changeset
|
141 def get(self, key, default=None): |
ab9ddf6c7eaf
core: added get method to PersistentDictionary
Goffi <goffi@goffi.org>
parents:
439
diff
changeset
|
142 return self._cache.get(key, default) |
ab9ddf6c7eaf
core: added get method to PersistentDictionary
Goffi <goffi@goffi.org>
parents:
439
diff
changeset
|
143 |
3225
843a9279fb5a
core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
144 def aset(self, key, value): |
843a9279fb5a
core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
145 """Async set, return a Deferred fired when value is actually stored""" |
843a9279fb5a
core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
146 self._cache.__setitem__(key, value) |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
147 return defer.ensureDeferred( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3661
diff
changeset
|
148 self.storage.set_private_value( |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
149 self.namespace, key, value, self.binary, self.profile |
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
150 ) |
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
151 ) |
3225
843a9279fb5a
core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
152 |
843a9279fb5a
core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
153 def adel(self, key): |
843a9279fb5a
core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
154 """Async del, return a Deferred fired when value is actually deleted""" |
843a9279fb5a
core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
155 self._cache.__delitem__(key) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3661
diff
changeset
|
156 return self.storage.del_private_value( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
157 self.namespace, key, self.binary, self.profile |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
158 ) |
3225
843a9279fb5a
core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
159 |
1340
91e72da1d093
core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents:
1292
diff
changeset
|
160 def setdefault(self, key, default): |
91e72da1d093
core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents:
1292
diff
changeset
|
161 try: |
91e72da1d093
core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents:
1292
diff
changeset
|
162 return self._cache[key] |
91e72da1d093
core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents:
1292
diff
changeset
|
163 except: |
91e72da1d093
core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents:
1292
diff
changeset
|
164 self.__setitem__(key, default) |
91e72da1d093
core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents:
1292
diff
changeset
|
165 return default |
91e72da1d093
core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents:
1292
diff
changeset
|
166 |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 def force(self, name): |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 """Force saving of an attribute to storage |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
169 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
170 @return: deferred fired when data is actually saved |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
171 """ |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
172 return defer.ensureDeferred( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3661
diff
changeset
|
173 self.storage.set_private_value( |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
174 self.namespace, name, self._cache[name], self.binary, self.profile |
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
175 ) |
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
176 ) |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 class PersistentBinaryDict(PersistentDict): |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 """Persistent dict where value can be any python data (instead of string only)""" |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
181 |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
182 binary = True |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
183 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
184 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
185 class LazyPersistentBinaryDict(PersistentBinaryDict): |
3028 | 186 r"""PersistentBinaryDict which get key/value when needed |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
187 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
188 This Persistent need more database access, it is suitable for largest data, |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
189 to save memory. |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
190 /!\ most of methods return a Deferred |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
191 """ |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
192 |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
193 # TODO: missing methods should be implemented using database access |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
194 # TODO: a cache would be useful (which is deleted after a timeout) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
195 |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
196 def load(self): |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
197 # we show a warning as calling load on LazyPersistentBinaryDict sounds like a code mistake |
3028 | 198 log.warning(_("Calling load on LazyPersistentBinaryDict while it's not needed")) |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
199 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
200 def iteritems(self): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
201 raise NotImplementedError |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
202 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
203 def items(self): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
204 d = defer.ensureDeferred( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
205 self.storage.get_privates( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
206 self.namespace, binary=self.binary, profile=self.profile |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
207 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
208 ) |
3253
1af840e84af7
core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
3234
diff
changeset
|
209 d.addCallback(lambda data_dict: data_dict.items()) |
1af840e84af7
core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
3234
diff
changeset
|
210 return d |
1af840e84af7
core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
3234
diff
changeset
|
211 |
1af840e84af7
core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
3234
diff
changeset
|
212 def all(self): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
213 return defer.ensureDeferred( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
214 self.storage.get_privates( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
215 self.namespace, binary=self.binary, profile=self.profile |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
216 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
217 ) |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
218 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
219 def __repr__(self): |
3653
d2b17f0f2615
core (memory/persistent): implement `__repr__` (via `__str__`)
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
220 return self.__str__() |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
221 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
222 def __str__(self): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
223 return "LazyPersistentBinaryDict (namespace: {})".format(self.namespace) |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
224 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
225 def __lt__(self, other): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
226 raise NotImplementedError |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
227 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
228 def __le__(self, other): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
229 raise NotImplementedError |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
230 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
231 def __eq__(self, other): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
232 raise NotImplementedError |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
233 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
234 def __ne__(self, other): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
235 raise NotImplementedError |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
236 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
237 def __gt__(self, other): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
238 raise NotImplementedError |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
239 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
240 def __ge__(self, other): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
241 raise NotImplementedError |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
242 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
243 def __cmp__(self, other): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
244 raise NotImplementedError |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
245 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
246 def __hash__(self): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
247 return hash(str(self.__class__) + self.namespace + (self.profile or "")) |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
248 |
3028 | 249 def __bool__(self): |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
250 raise NotImplementedError |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
251 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
252 def __contains__(self, key): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
253 raise NotImplementedError |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
254 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
255 def __iter__(self): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
256 raise NotImplementedError |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
257 |
2644
e107089d6640
core (memory/persistent): better default value handling in LazyPersistentBinaryDict (don't jump to debugger in debug mode if value is not found)
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
258 def _data2value(self, data, key): |
e107089d6640
core (memory/persistent): better default value handling in LazyPersistentBinaryDict (don't jump to debugger in debug mode if value is not found)
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
259 try: |
e107089d6640
core (memory/persistent): better default value handling in LazyPersistentBinaryDict (don't jump to debugger in debug mode if value is not found)
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
260 return data[key] |
e107089d6640
core (memory/persistent): better default value handling in LazyPersistentBinaryDict (don't jump to debugger in debug mode if value is not found)
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
261 except KeyError as e: |
e107089d6640
core (memory/persistent): better default value handling in LazyPersistentBinaryDict (don't jump to debugger in debug mode if value is not found)
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
262 # we return a Failure here to avoid the jump |
e107089d6640
core (memory/persistent): better default value handling in LazyPersistentBinaryDict (don't jump to debugger in debug mode if value is not found)
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
263 # into debugger in debug mode. |
e107089d6640
core (memory/persistent): better default value handling in LazyPersistentBinaryDict (don't jump to debugger in debug mode if value is not found)
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
264 raise failure.Failure(e) |
e107089d6640
core (memory/persistent): better default value handling in LazyPersistentBinaryDict (don't jump to debugger in debug mode if value is not found)
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
265 |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
266 def __getitem__(self, key): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
267 """get the value as a Deferred""" |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
268 d = defer.ensureDeferred( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
269 self.storage.get_privates( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
270 self.namespace, keys=[key], binary=self.binary, profile=self.profile |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
271 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
272 ) |
2644
e107089d6640
core (memory/persistent): better default value handling in LazyPersistentBinaryDict (don't jump to debugger in debug mode if value is not found)
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
273 d.addCallback(self._data2value, key) |
2209
ea41cf1e6d29
memory (persistent, sqlite): fixed getPrivates bug with keys arguments and its use in LazyPersistentBinaryDict
Goffi <goffi@goffi.org>
parents:
2182
diff
changeset
|
274 return d |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
275 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
276 def __setitem__(self, key, value): |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
277 defer.ensureDeferred( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3661
diff
changeset
|
278 self.storage.set_private_value( |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
279 self.namespace, key, value, self.binary, self.profile |
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
280 ) |
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
281 ) |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
282 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
283 def __delitem__(self, key): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3661
diff
changeset
|
284 self.storage.del_private_value(self.namespace, key, self.binary, self.profile) |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
285 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3661
diff
changeset
|
286 def _default_or_exception(self, failure_, default): |
2644
e107089d6640
core (memory/persistent): better default value handling in LazyPersistentBinaryDict (don't jump to debugger in debug mode if value is not found)
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
287 failure_.trap(KeyError) |
e107089d6640
core (memory/persistent): better default value handling in LazyPersistentBinaryDict (don't jump to debugger in debug mode if value is not found)
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
288 return default |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
289 |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
290 def get(self, key, default=None): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
291 d = self.__getitem__(key) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3661
diff
changeset
|
292 d.addErrback(self._default_or_exception, default=default) |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
293 return d |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
294 |
3234
78fea49735c5
core (memory/persistent): added `aset` method in LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
3225
diff
changeset
|
295 def aset(self, key, value): |
78fea49735c5
core (memory/persistent): added `aset` method in LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
3225
diff
changeset
|
296 """Async set, return a Deferred fired when value is actually stored""" |
78fea49735c5
core (memory/persistent): added `aset` method in LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
3225
diff
changeset
|
297 # FIXME: redundant with force, force must be removed |
78fea49735c5
core (memory/persistent): added `aset` method in LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
3225
diff
changeset
|
298 # XXX: similar as PersistentDict.aset, but doesn't use cache |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
299 return defer.ensureDeferred( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3661
diff
changeset
|
300 self.storage.set_private_value( |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
301 self.namespace, key, value, self.binary, self.profile |
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
302 ) |
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
303 ) |
3234
78fea49735c5
core (memory/persistent): added `aset` method in LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
3225
diff
changeset
|
304 |
3253
1af840e84af7
core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
3234
diff
changeset
|
305 def adel(self, key): |
1af840e84af7
core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
3234
diff
changeset
|
306 """Async del, return a Deferred fired when value is actually deleted""" |
1af840e84af7
core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
3234
diff
changeset
|
307 # XXX: similar as PersistentDict.adel, but doesn't use cache |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3661
diff
changeset
|
308 return self.storage.del_private_value( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
309 self.namespace, key, self.binary, self.profile |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
310 ) |
3253
1af840e84af7
core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
3234
diff
changeset
|
311 |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
312 def setdefault(self, key, default): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
313 raise NotImplementedError |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
314 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
315 def force(self, name, value): |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
316 """Force saving of an attribute to storage |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
317 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
318 @param value(object): value is needed for LazyPersistentBinaryDict |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
319 @return: deferred fired when data is actually saved |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
320 """ |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
321 return defer.ensureDeferred( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3661
diff
changeset
|
322 self.storage.set_private_value( |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
323 self.namespace, name, value, self.binary, self.profile |
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
324 ) |
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
325 ) |
2734
9702025f6dff
core (memory/persistent): new LazyPersistentBinaryDict.remove method
Goffi <goffi@goffi.org>
parents:
2644
diff
changeset
|
326 |
9702025f6dff
core (memory/persistent): new LazyPersistentBinaryDict.remove method
Goffi <goffi@goffi.org>
parents:
2644
diff
changeset
|
327 def remove(self, key): |
9702025f6dff
core (memory/persistent): new LazyPersistentBinaryDict.remove method
Goffi <goffi@goffi.org>
parents:
2644
diff
changeset
|
328 """Delete a key from sotrage, and return a deferred called when it's done |
9702025f6dff
core (memory/persistent): new LazyPersistentBinaryDict.remove method
Goffi <goffi@goffi.org>
parents:
2644
diff
changeset
|
329 |
9702025f6dff
core (memory/persistent): new LazyPersistentBinaryDict.remove method
Goffi <goffi@goffi.org>
parents:
2644
diff
changeset
|
330 @param key(unicode): key to delete |
9702025f6dff
core (memory/persistent): new LazyPersistentBinaryDict.remove method
Goffi <goffi@goffi.org>
parents:
2644
diff
changeset
|
331 @return (D): A deferred fired when delete is done |
9702025f6dff
core (memory/persistent): new LazyPersistentBinaryDict.remove method
Goffi <goffi@goffi.org>
parents:
2644
diff
changeset
|
332 """ |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
333 return self.storage.del_private_value( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
334 self.namespace, key, self.binary, self.profile |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
335 ) |