annotate sat/memory/persistent.py @ 3697:842b522d525e

install: requirements update
author Goffi <goffi@goffi.org>
date Thu, 04 Nov 2021 18:13:55 +0100
parents d2b17f0f2615
children cbb988a6f507
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2890
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
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
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3253
diff changeset
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
771
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 609
diff changeset
20 from sat.core.i18n import _
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
21 from sat.core.log import getLogger
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
22 log = getLogger(__name__)
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
23 from twisted.python import failure
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
24
592
e5a875a3311b Fix pep8 support in src/memory.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
25
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
26 class MemoryNotInitializedError(Exception):
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
27 pass
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
28
592
e5a875a3311b Fix pep8 support in src/memory.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
29
3234
78fea49735c5 core (memory/persistent): added `aset` method in LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3225
diff changeset
30 class PersistentDict:
592
e5a875a3311b Fix pep8 support in src/memory.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
31 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
32
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
33 /!\ be careful, each assignment means a database write
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
34 /!\ Memory must be initialised before loading/setting value with instances of this class"""
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
35 storage = None
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
36 binary = False
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
37
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
38 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
39 """
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
40
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
41 @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
42 @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
43 """
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
44 if not self.storage:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
45 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
46 raise MemoryNotInitializedError
2890
c652d079a9a1 core (memory/persistent): added clear method + initialise with a dict:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
47 self._cache = {}
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
48 self.namespace = namespace
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
49 self.profile = profile
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
50
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
51 def _setCache(self, data):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
52 self._cache = data
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
53
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
54 def load(self):
1029
f6182f6418ea memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents: 993
diff changeset
55 """Load persistent data from storage.
f6182f6418ea memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents: 993
diff changeset
56
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
57 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
58 @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
59 """
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
60 d = self.storage.getPrivates(self.namespace, binary=self.binary, profile=self.profile)
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
61 d.addCallback(self._setCache)
2765
378188abe941 misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents: 2734
diff changeset
62 d.addCallback(lambda __: self)
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
63 return d
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
64
1292
b29a065a66f0 core: added items() and iteritems() methods to PersistentDict
Goffi <goffi@goffi.org>
parents: 1029
diff changeset
65 def iteritems(self):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2890
diff changeset
66 return iter(self._cache.items())
1292
b29a065a66f0 core: added items() and iteritems() methods to PersistentDict
Goffi <goffi@goffi.org>
parents: 1029
diff changeset
67
b29a065a66f0 core: added items() and iteritems() methods to PersistentDict
Goffi <goffi@goffi.org>
parents: 1029
diff changeset
68 def items(self):
3253
1af840e84af7 core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3234
diff changeset
69 return self._cache.items()
1292
b29a065a66f0 core: added items() and iteritems() methods to PersistentDict
Goffi <goffi@goffi.org>
parents: 1029
diff changeset
70
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
71 def __repr__(self):
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
72 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
73
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
74 def __str__(self):
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
75 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
76
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
77 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
78 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
79
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
80 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
81 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
82
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
83 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
84 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
85
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
86 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
87 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
88
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
89 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
90 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
91
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
92 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
93 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
94
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
95 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
96 return self._cache.__cmp__(other)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
97
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
98 def __hash__(self):
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.__hash__()
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
100
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2890
diff changeset
101 def __bool__(self):
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2890
diff changeset
102 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
103
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
104 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
105 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
106
979
8f8bdd31375a core (memory): added __iter__ to PersistentDict
Goffi <goffi@goffi.org>
parents: 811
diff changeset
107 def __iter__(self):
8f8bdd31375a core (memory): added __iter__ to PersistentDict
Goffi <goffi@goffi.org>
parents: 811
diff changeset
108 return self._cache.__iter__()
8f8bdd31375a core (memory): added __iter__ to PersistentDict
Goffi <goffi@goffi.org>
parents: 811
diff changeset
109
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
110 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
111 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
112
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
113 def __setitem__(self, key, value):
2890
c652d079a9a1 core (memory/persistent): added clear method + initialise with a dict:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
114 self.storage.setPrivateValue(self.namespace, key, value, self.binary,
c652d079a9a1 core (memory/persistent): added clear method + initialise with a dict:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
115 self.profile)
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
116 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
117
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
118 def __delitem__(self, key):
2302
f824b6026b73 core (memory/persistent): fixed calls to delPrivateValue
Goffi <goffi@goffi.org>
parents: 2209
diff changeset
119 self.storage.delPrivateValue(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
120 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
121
2890
c652d079a9a1 core (memory/persistent): added clear method + initialise with a dict:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
122 def clear(self):
c652d079a9a1 core (memory/persistent): added clear method + initialise with a dict:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
123 """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
124 self._cache.clear()
c652d079a9a1 core (memory/persistent): added clear method + initialise with a dict:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
125 return self.storage.delPrivateNamespace(self.namespace, self.binary, self.profile)
c652d079a9a1 core (memory/persistent): added clear method + initialise with a dict:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
126
442
ab9ddf6c7eaf core: added get method to PersistentDictionary
Goffi <goffi@goffi.org>
parents: 439
diff changeset
127 def get(self, key, default=None):
ab9ddf6c7eaf core: added get method to PersistentDictionary
Goffi <goffi@goffi.org>
parents: 439
diff changeset
128 return self._cache.get(key, default)
ab9ddf6c7eaf core: added get method to PersistentDictionary
Goffi <goffi@goffi.org>
parents: 439
diff changeset
129
3225
843a9279fb5a core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
130 def aset(self, key, value):
843a9279fb5a core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
131 """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
132 self._cache.__setitem__(key, value)
843a9279fb5a core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
133 return self.storage.setPrivateValue(self.namespace, key, value,
843a9279fb5a core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
134 self.binary, self.profile)
843a9279fb5a core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
135
843a9279fb5a core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
136 def adel(self, key):
843a9279fb5a core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
137 """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
138 self._cache.__delitem__(key)
843a9279fb5a core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
139 return self.storage.delPrivateValue(
843a9279fb5a core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
140 self.namespace, key, self.binary, self.profile)
843a9279fb5a core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
141
1340
91e72da1d093 core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents: 1292
diff changeset
142 def setdefault(self, key, default):
91e72da1d093 core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents: 1292
diff changeset
143 try:
91e72da1d093 core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents: 1292
diff changeset
144 return self._cache[key]
91e72da1d093 core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents: 1292
diff changeset
145 except:
91e72da1d093 core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents: 1292
diff changeset
146 self.__setitem__(key, default)
91e72da1d093 core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents: 1292
diff changeset
147 return default
91e72da1d093 core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents: 1292
diff changeset
148
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
149 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
150 """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
151
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
152 @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
153 """
2890
c652d079a9a1 core (memory/persistent): added clear method + initialise with a dict:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
154 return self.storage.setPrivateValue(self.namespace, name, self._cache[name],
c652d079a9a1 core (memory/persistent): added clear method + initialise with a dict:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
155 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
156
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
157
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
158 class PersistentBinaryDict(PersistentDict):
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
159 """Persistent dict where value can be any python data (instead of string only)"""
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
160 binary = True
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
161
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
162
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
163 class LazyPersistentBinaryDict(PersistentBinaryDict):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2890
diff changeset
164 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
165
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
166 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
167 to save memory.
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
168 /!\ most of methods return a Deferred
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 # 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
171 # 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
172
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
173 def load(self):
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
174 # we show a warning as calling load on LazyPersistentBinaryDict sounds like a code mistake
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2890
diff changeset
175 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
176
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
177 def iteritems(self):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
178 raise NotImplementedError
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
179
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
180 def items(self):
3253
1af840e84af7 core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3234
diff changeset
181 d = self.storage.getPrivates(self.namespace, binary=self.binary, profile=self.profile)
1af840e84af7 core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3234
diff changeset
182 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
183 return d
1af840e84af7 core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3234
diff changeset
184
1af840e84af7 core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3234
diff changeset
185 def all(self):
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
186 return self.storage.getPrivates(self.namespace, binary=self.binary, profile=self.profile)
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 def __repr__(self):
3653
d2b17f0f2615 core (memory/persistent): implement `__repr__` (via `__str__`)
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
189 return self.__str__()
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
190
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
191 def __str__(self):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
192 return "LazyPersistentBinaryDict (namespace: {})".format(self.namespace)
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
193
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
194 def __lt__(self, other):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
195 raise NotImplementedError
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
196
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
197 def __le__(self, other):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
198 raise NotImplementedError
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 __eq__(self, other):
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 __ne__(self, other):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
204 raise NotImplementedError
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
205
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
206 def __gt__(self, other):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
207 raise NotImplementedError
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
208
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
209 def __ge__(self, other):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
210 raise NotImplementedError
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
211
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
212 def __cmp__(self, other):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
213 raise NotImplementedError
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
214
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
215 def __hash__(self):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2890
diff changeset
216 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
217
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2890
diff changeset
218 def __bool__(self):
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
219 raise NotImplementedError
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
220
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
221 def __contains__(self, key):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
222 raise NotImplementedError
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
223
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
224 def __iter__(self):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
225 raise NotImplementedError
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
226
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
227 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
228 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
229 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
230 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
231 # 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
232 # 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
233 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
234
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
235 def __getitem__(self, key):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
236 """get the value as a Deferred"""
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
237 d = self.storage.getPrivates(self.namespace, keys=[key], binary=self.binary,
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
238 profile=self.profile)
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
239 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
240 return d
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
241
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
242 def __setitem__(self, key, value):
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
243 self.storage.setPrivateValue(self.namespace, key, value, self.binary,
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
244 self.profile)
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
245
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
246 def __delitem__(self, key):
2302
f824b6026b73 core (memory/persistent): fixed calls to delPrivateValue
Goffi <goffi@goffi.org>
parents: 2209
diff changeset
247 self.storage.delPrivateValue(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
248
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
249 def _defaultOrException(self, failure_, default):
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
250 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
251 return default
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
252
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
253 def get(self, key, default=None):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
254 d = self.__getitem__(key)
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
255 d.addErrback(self._defaultOrException, default=default)
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
256 return d
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
257
3234
78fea49735c5 core (memory/persistent): added `aset` method in LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3225
diff changeset
258 def aset(self, key, value):
78fea49735c5 core (memory/persistent): added `aset` method in LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3225
diff changeset
259 """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
260 # 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
261 # XXX: similar as PersistentDict.aset, but doesn't use cache
78fea49735c5 core (memory/persistent): added `aset` method in LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3225
diff changeset
262 return self.storage.setPrivateValue(self.namespace, key, value,
78fea49735c5 core (memory/persistent): added `aset` method in LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3225
diff changeset
263 self.binary, self.profile)
78fea49735c5 core (memory/persistent): added `aset` method in LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3225
diff changeset
264
3253
1af840e84af7 core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3234
diff changeset
265 def adel(self, key):
1af840e84af7 core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3234
diff changeset
266 """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
267 # XXX: similar as PersistentDict.adel, but doesn't use cache
1af840e84af7 core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3234
diff changeset
268 return self.storage.delPrivateValue(
1af840e84af7 core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3234
diff changeset
269 self.namespace, key, self.binary, self.profile)
1af840e84af7 core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3234
diff changeset
270
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
271 def setdefault(self, key, default):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
272 raise NotImplementedError
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
273
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
274 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
275 """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
276
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
277 @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
278 @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
279 """
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
280 return self.storage.setPrivateValue(self.namespace, name, value, self.binary, self.profile)
2734
9702025f6dff core (memory/persistent): new LazyPersistentBinaryDict.remove method
Goffi <goffi@goffi.org>
parents: 2644
diff changeset
281
9702025f6dff core (memory/persistent): new LazyPersistentBinaryDict.remove method
Goffi <goffi@goffi.org>
parents: 2644
diff changeset
282 def remove(self, key):
9702025f6dff core (memory/persistent): new LazyPersistentBinaryDict.remove method
Goffi <goffi@goffi.org>
parents: 2644
diff changeset
283 """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
284
9702025f6dff core (memory/persistent): new LazyPersistentBinaryDict.remove method
Goffi <goffi@goffi.org>
parents: 2644
diff changeset
285 @param key(unicode): key to delete
9702025f6dff core (memory/persistent): new LazyPersistentBinaryDict.remove method
Goffi <goffi@goffi.org>
parents: 2644
diff changeset
286 @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
287 """
9702025f6dff core (memory/persistent): new LazyPersistentBinaryDict.remove method
Goffi <goffi@goffi.org>
parents: 2644
diff changeset
288 return self.storage.delPrivateValue(self.namespace, key, self.binary, self.profile)