annotate sat/memory/persistent.py @ 3582:71516731d0aa

core (memory/sqla): database migration using Alembic: Alembic database migration tool, which is the recommended one for SQLAlchemy has been integrated. When a database is created, it will be used to stamp to current (head) revision, otherwise, DB will be checked to see if it needs to be updated, and upgrade will be triggered if necessary.
author Goffi <goffi@goffi.org>
date Fri, 25 Jun 2021 17:55:23 +0200
parents 888109774673
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
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
771
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 609
diff changeset
22 from sat.core.i18n import _
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
23 from sat.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
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
37 /!\ 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
38 storage = None
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
39 binary = False
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
40
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
41 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
42 """
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
43
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
44 @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
45 @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
46 """
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
47 if not self.storage:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
48 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
49 raise MemoryNotInitializedError
2890
c652d079a9a1 core (memory/persistent): added clear method + initialise with a dict:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
50 self._cache = {}
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
51 self.namespace = namespace
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
52 self.profile = profile
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
53
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
54 def _setCache(self, data):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
55 self._cache = data
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
56
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
57 def load(self):
1029
f6182f6418ea memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents: 993
diff changeset
58 """Load persistent data from storage.
f6182f6418ea memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents: 993
diff changeset
59
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
60 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
61 @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
62 """
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
63 d = defer.ensureDeferred(self.storage.getPrivates(
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
64 self.namespace, binary=self.binary, profile=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
65 ))
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
66 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
67 d.addCallback(lambda __: self)
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
68 return d
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
69
1292
b29a065a66f0 core: added items() and iteritems() methods to PersistentDict
Goffi <goffi@goffi.org>
parents: 1029
diff changeset
70 def iteritems(self):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2890
diff changeset
71 return iter(self._cache.items())
1292
b29a065a66f0 core: added items() and iteritems() methods to PersistentDict
Goffi <goffi@goffi.org>
parents: 1029
diff changeset
72
b29a065a66f0 core: added items() and iteritems() methods to PersistentDict
Goffi <goffi@goffi.org>
parents: 1029
diff changeset
73 def items(self):
3253
1af840e84af7 core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3234
diff changeset
74 return self._cache.items()
1292
b29a065a66f0 core: added items() and iteritems() methods to PersistentDict
Goffi <goffi@goffi.org>
parents: 1029
diff changeset
75
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
76 def __repr__(self):
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
77 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
78
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
79 def __str__(self):
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
80 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
81
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
82 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
83 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
84
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
85 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
86 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
87
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
88 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
89 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
90
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
91 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
92 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
93
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
94 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
95 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
96
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
97 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
98 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
99
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
100 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
101 return self._cache.__cmp__(other)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 585
diff changeset
102
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
103 def __hash__(self):
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
104 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
105
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2890
diff changeset
106 def __bool__(self):
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2890
diff changeset
107 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
108
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
109 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
110 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
111
979
8f8bdd31375a core (memory): added __iter__ to PersistentDict
Goffi <goffi@goffi.org>
parents: 811
diff changeset
112 def __iter__(self):
8f8bdd31375a core (memory): added __iter__ to PersistentDict
Goffi <goffi@goffi.org>
parents: 811
diff changeset
113 return self._cache.__iter__()
8f8bdd31375a core (memory): added __iter__ to PersistentDict
Goffi <goffi@goffi.org>
parents: 811
diff changeset
114
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
115 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
116 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
117
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
118 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
119 defer.ensureDeferred(
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
120 self.storage.setPrivateValue(
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
121 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
122 )
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
123 )
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
124 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
125
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
126 def __delitem__(self, key):
2302
f824b6026b73 core (memory/persistent): fixed calls to delPrivateValue
Goffi <goffi@goffi.org>
parents: 2209
diff changeset
127 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
128 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
129
2890
c652d079a9a1 core (memory/persistent): added clear method + initialise with a dict:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
130 def clear(self):
c652d079a9a1 core (memory/persistent): added clear method + initialise with a dict:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
131 """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
132 self._cache.clear()
c652d079a9a1 core (memory/persistent): added clear method + initialise with a dict:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
133 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
134
442
ab9ddf6c7eaf core: added get method to PersistentDictionary
Goffi <goffi@goffi.org>
parents: 439
diff changeset
135 def get(self, key, default=None):
ab9ddf6c7eaf core: added get method to PersistentDictionary
Goffi <goffi@goffi.org>
parents: 439
diff changeset
136 return self._cache.get(key, default)
ab9ddf6c7eaf core: added get method to PersistentDictionary
Goffi <goffi@goffi.org>
parents: 439
diff changeset
137
3225
843a9279fb5a core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
138 def aset(self, key, value):
843a9279fb5a core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
139 """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
140 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
141 return defer.ensureDeferred(
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
142 self.storage.setPrivateValue(
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
143 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
144 )
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
145 )
3225
843a9279fb5a core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
146
843a9279fb5a core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
147 def adel(self, key):
843a9279fb5a core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
148 """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
149 self._cache.__delitem__(key)
843a9279fb5a core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
150 return self.storage.delPrivateValue(
843a9279fb5a core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
151 self.namespace, key, self.binary, self.profile)
843a9279fb5a core (memory/persistent): implemented `aset` and `adel`:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
152
1340
91e72da1d093 core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents: 1292
diff changeset
153 def setdefault(self, key, default):
91e72da1d093 core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents: 1292
diff changeset
154 try:
91e72da1d093 core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents: 1292
diff changeset
155 return self._cache[key]
91e72da1d093 core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents: 1292
diff changeset
156 except:
91e72da1d093 core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents: 1292
diff changeset
157 self.__setitem__(key, default)
91e72da1d093 core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents: 1292
diff changeset
158 return default
91e72da1d093 core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents: 1292
diff changeset
159
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
160 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
161 """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
162
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
163 @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
164 """
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
165 return defer.ensureDeferred(
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
166 self.storage.setPrivateValue(
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
167 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
168 )
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
169 )
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
170
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
171
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
172 class PersistentBinaryDict(PersistentDict):
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
173 """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
174 binary = True
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
175
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 class LazyPersistentBinaryDict(PersistentBinaryDict):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2890
diff changeset
178 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
179
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
180 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
181 to save memory.
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
182 /!\ most of methods return a Deferred
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 # 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
185 # 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
186
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
187 def load(self):
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
188 # 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
189 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
190
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
191 def iteritems(self):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
192 raise NotImplementedError
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 items(self):
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
195 d = defer.ensureDeferred(self.storage.getPrivates(
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
196 self.namespace, binary=self.binary, profile=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
197 ))
3253
1af840e84af7 core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3234
diff changeset
198 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
199 return d
1af840e84af7 core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3234
diff changeset
200
1af840e84af7 core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3234
diff changeset
201 def all(self):
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
202 return defer.ensureDeferred(self.storage.getPrivates(
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
203 self.namespace, binary=self.binary, profile=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
204 ))
2182
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 __repr__(self):
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 __str__(self):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
210 return "LazyPersistentBinaryDict (namespace: {})".format(self.namespace)
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 __lt__(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 __le__(self, other):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
216 raise NotImplementedError
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
217
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
218 def __eq__(self, other):
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 __ne__(self, other):
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 __gt__(self, other):
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
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
227 def __ge__(self, other):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
228 raise NotImplementedError
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
229
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
230 def __cmp__(self, other):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
231 raise NotImplementedError
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
232
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
233 def __hash__(self):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2890
diff changeset
234 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
235
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2890
diff changeset
236 def __bool__(self):
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
237 raise NotImplementedError
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
238
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
239 def __contains__(self, key):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
240 raise NotImplementedError
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
241
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
242 def __iter__(self):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
243 raise NotImplementedError
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
244
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
245 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
246 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
247 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
248 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
249 # 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
250 # 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
251 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
252
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
253 def __getitem__(self, key):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
254 """get the value as a Deferred"""
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
255 d = defer.ensureDeferred(self.storage.getPrivates(
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
256 self.namespace, keys=[key], binary=self.binary, profile=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
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 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
259 return d
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
260
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
261 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
262 defer.ensureDeferred(
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
263 self.storage.setPrivateValue(
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
264 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
265 )
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
266 )
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
267
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
268 def __delitem__(self, key):
2302
f824b6026b73 core (memory/persistent): fixed calls to delPrivateValue
Goffi <goffi@goffi.org>
parents: 2209
diff changeset
269 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
270
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
271 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
272 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
273 return default
432
31e8c48b5f5d core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff changeset
274
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
275 def get(self, key, default=None):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
276 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
277 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
278 return d
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
279
3234
78fea49735c5 core (memory/persistent): added `aset` method in LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3225
diff changeset
280 def aset(self, key, value):
78fea49735c5 core (memory/persistent): added `aset` method in LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3225
diff changeset
281 """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
282 # 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
283 # 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
284 return defer.ensureDeferred(
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
285 self.storage.setPrivateValue(
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
286 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
287 )
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
288 )
3234
78fea49735c5 core (memory/persistent): added `aset` method in LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3225
diff changeset
289
3253
1af840e84af7 core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3234
diff changeset
290 def adel(self, key):
1af840e84af7 core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3234
diff changeset
291 """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
292 # 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
293 return self.storage.delPrivateValue(
1af840e84af7 core (memory/persistent): fixed "items", added "all" + "adel" to LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 3234
diff changeset
294 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
295
2182
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
296 def setdefault(self, key, default):
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
297 raise NotImplementedError
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
298
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
299 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
300 """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
301
087eec4c6c07 memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
302 @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
303 @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
304 """
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
305 return defer.ensureDeferred(
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
306 self.storage.setPrivateValue(
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
307 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
308 )
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
309 )
2734
9702025f6dff core (memory/persistent): new LazyPersistentBinaryDict.remove method
Goffi <goffi@goffi.org>
parents: 2644
diff changeset
310
9702025f6dff core (memory/persistent): new LazyPersistentBinaryDict.remove method
Goffi <goffi@goffi.org>
parents: 2644
diff changeset
311 def remove(self, key):
9702025f6dff core (memory/persistent): new LazyPersistentBinaryDict.remove method
Goffi <goffi@goffi.org>
parents: 2644
diff changeset
312 """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
313
9702025f6dff core (memory/persistent): new LazyPersistentBinaryDict.remove method
Goffi <goffi@goffi.org>
parents: 2644
diff changeset
314 @param key(unicode): key to delete
9702025f6dff core (memory/persistent): new LazyPersistentBinaryDict.remove method
Goffi <goffi@goffi.org>
parents: 2644
diff changeset
315 @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
316 """
9702025f6dff core (memory/persistent): new LazyPersistentBinaryDict.remove method
Goffi <goffi@goffi.org>
parents: 2644
diff changeset
317 return self.storage.delPrivateValue(self.namespace, key, self.binary, self.profile)