Mercurial > libervia-backend
annotate src/memory/persistent.py @ 2307:8fa7edd0da24
plugin Pubsub Hook: first draft:
This new plugin allow to attach an external action to a Pubsub event (i.e. notification).
Hook can be persitent accross restarts, or temporary (will be deleted on profile disconnection).
Only Python files are handled for now.
In the future, it may make sense to move hooks in a generic plugin which could be used by ad-hoc commands, messages, pubsub, etc.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 05 Jul 2017 15:05:47 +0200 |
parents | f824b6026b73 |
children | 8b37a62336c3 |
rev | line source |
---|---|
1934
2daf7b4c6756
use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1 #!/usr/bin/env python2 |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
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 |
1766 | 5 # Copyright (C) 2009-2016 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 | 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__) |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 |
592
e5a875a3311b
Fix pep8 support in src/memory.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
24 |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 class MemoryNotInitializedError(Exception): |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 pass |
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 |
588
beaf6bec2fcd
Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
29 class PersistentDict(object): |
592
e5a875a3311b
Fix pep8 support in src/memory.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
30 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
|
31 |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 /!\ 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
|
33 /!\ 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
|
34 storage = None |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
35 binary = False |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 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
|
38 """ |
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 @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
|
41 @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
|
42 """ |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 if not self.storage: |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
979
diff
changeset
|
44 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
|
45 raise MemoryNotInitializedError |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
46 self._cache = None |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 self.namespace = namespace |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 self.profile = profile |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
50 def _setCache(self, data): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
51 self._cache = data |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
52 |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 def load(self): |
1029
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
54 """Load persistent data from storage. |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
993
diff
changeset
|
55 |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
56 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
|
57 @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
|
58 """ |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
59 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
|
60 d.addCallback(self._setCache) |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
61 d.addCallback(lambda dummy: self) |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
62 return d |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 |
1292
b29a065a66f0
core: added items() and iteritems() methods to PersistentDict
Goffi <goffi@goffi.org>
parents:
1029
diff
changeset
|
64 def iteritems(self): |
b29a065a66f0
core: added items() and iteritems() methods to PersistentDict
Goffi <goffi@goffi.org>
parents:
1029
diff
changeset
|
65 return self._cache.iteritems() |
b29a065a66f0
core: added items() and iteritems() methods to PersistentDict
Goffi <goffi@goffi.org>
parents:
1029
diff
changeset
|
66 |
b29a065a66f0
core: added items() and iteritems() methods to PersistentDict
Goffi <goffi@goffi.org>
parents:
1029
diff
changeset
|
67 def items(self): |
b29a065a66f0
core: added items() and iteritems() methods to PersistentDict
Goffi <goffi@goffi.org>
parents:
1029
diff
changeset
|
68 return self._cache.items() |
b29a065a66f0
core: added items() and iteritems() methods to PersistentDict
Goffi <goffi@goffi.org>
parents:
1029
diff
changeset
|
69 |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 def __repr__(self): |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 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
|
72 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 def __str__(self): |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 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
|
75 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 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
|
77 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
|
78 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 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
|
80 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
|
81 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 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
|
83 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
|
84 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 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
|
86 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
|
87 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 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
|
89 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
|
90 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 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
|
92 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
|
93 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 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
|
95 return self._cache.__cmp__(other) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
585
diff
changeset
|
96 |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 def __hash__(self): |
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.__hash__() |
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 __nonzero__(self): |
439
866dbb0d7d87
plugin maildir: maildir now use PersistentBinaryDictionary to store profile specific data
Goffi <goffi@goffi.org>
parents:
432
diff
changeset
|
101 return self._cache.__len__() |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 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
|
104 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
|
105 |
979
8f8bdd31375a
core (memory): added __iter__ to PersistentDict
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
106 def __iter__(self): |
8f8bdd31375a
core (memory): added __iter__ to PersistentDict
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
107 return self._cache.__iter__() |
8f8bdd31375a
core (memory): added __iter__ to PersistentDict
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
108 |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 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
|
110 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
|
111 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 def __setitem__(self, key, value): |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
113 self.storage.setPrivateValue(self.namespace, key, value, 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
|
114 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
|
115 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 def __delitem__(self, key): |
2302
f824b6026b73
core (memory/persistent): fixed calls to delPrivateValue
Goffi <goffi@goffi.org>
parents:
2209
diff
changeset
|
117 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
|
118 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
|
119 |
442
ab9ddf6c7eaf
core: added get method to PersistentDictionary
Goffi <goffi@goffi.org>
parents:
439
diff
changeset
|
120 def get(self, key, default=None): |
ab9ddf6c7eaf
core: added get method to PersistentDictionary
Goffi <goffi@goffi.org>
parents:
439
diff
changeset
|
121 return self._cache.get(key, default) |
ab9ddf6c7eaf
core: added get method to PersistentDictionary
Goffi <goffi@goffi.org>
parents:
439
diff
changeset
|
122 |
1340
91e72da1d093
core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents:
1292
diff
changeset
|
123 def setdefault(self, key, default): |
91e72da1d093
core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents:
1292
diff
changeset
|
124 try: |
91e72da1d093
core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents:
1292
diff
changeset
|
125 return self._cache[key] |
91e72da1d093
core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents:
1292
diff
changeset
|
126 except: |
91e72da1d093
core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents:
1292
diff
changeset
|
127 self.__setitem__(key, default) |
91e72da1d093
core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents:
1292
diff
changeset
|
128 return default |
91e72da1d093
core (persistent dict): added setdefault method
Goffi <goffi@goffi.org>
parents:
1292
diff
changeset
|
129 |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 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
|
131 """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
|
132 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
133 @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
|
134 """ |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
135 return self.storage.setPrivateValue(self.namespace, name, self._cache[name], 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
|
136 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 class PersistentBinaryDict(PersistentDict): |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 """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
|
140 binary = True |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
141 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
142 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
143 class LazyPersistentBinaryDict(PersistentBinaryDict): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
144 ur"""PersistentBinaryDict which get key/value when needed |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
145 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
146 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
|
147 to save memory. |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
148 /!\ most of methods return a Deferred |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
149 """ |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
150 # 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
|
151 # 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
|
152 |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 def load(self): |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
154 # we show a warning as calling load on LazyPersistentBinaryDict sounds like a code mistake |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
155 log.warning(_(u"Calling load on LazyPersistentBinaryDict while it's not needed")) |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
156 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
157 def iteritems(self): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
158 raise NotImplementedError |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
159 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
160 def items(self): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
161 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
|
162 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
163 def __repr__(self): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
164 raise NotImplementedError |
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 def __str__(self): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
167 return "LazyPersistentBinaryDict (namespace: {})".format(self.namespace) |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
168 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
169 def __lt__(self, other): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
170 raise NotImplementedError |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
171 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
172 def __le__(self, other): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
173 raise NotImplementedError |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
174 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
175 def __eq__(self, other): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
176 raise NotImplementedError |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
177 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
178 def __ne__(self, other): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
179 raise NotImplementedError |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
180 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
181 def __gt__(self, other): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
182 raise NotImplementedError |
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 def __ge__(self, other): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
185 raise NotImplementedError |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
186 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
187 def __cmp__(self, other): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
188 raise NotImplementedError |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
189 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
190 def __hash__(self): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
191 return hash(unicode(self.__class__) + self.namespace + (self.profile or u'')) |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
192 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
193 def __nonzero__(self): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
194 raise NotImplementedError |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
195 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
196 def __contains__(self, key): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
197 raise NotImplementedError |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
198 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
199 def __iter__(self): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
200 raise NotImplementedError |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
201 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
202 def __getitem__(self, key): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
203 """get the value as a Deferred""" |
2209
ea41cf1e6d29
memory (persistent, sqlite): fixed getPrivates bug with keys arguments and its use in LazyPersistentBinaryDict
Goffi <goffi@goffi.org>
parents:
2182
diff
changeset
|
204 d = self.storage.getPrivates(self.namespace, keys=[key], binary=self.binary, profile=self.profile) |
ea41cf1e6d29
memory (persistent, sqlite): fixed getPrivates bug with keys arguments and its use in LazyPersistentBinaryDict
Goffi <goffi@goffi.org>
parents:
2182
diff
changeset
|
205 d.addCallback(lambda data: data[key]) |
ea41cf1e6d29
memory (persistent, sqlite): fixed getPrivates bug with keys arguments and its use in LazyPersistentBinaryDict
Goffi <goffi@goffi.org>
parents:
2182
diff
changeset
|
206 return d |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
208 def __setitem__(self, key, value): |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
209 self.storage.setPrivateValue(self.namespace, key, value, 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
|
210 |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
211 def __delitem__(self, key): |
2302
f824b6026b73
core (memory/persistent): fixed calls to delPrivateValue
Goffi <goffi@goffi.org>
parents:
2209
diff
changeset
|
212 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
|
213 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
214 def _valueOrDefault(self, value, default): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
215 if value is None: |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
216 return default |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
217 return value |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
218 |
2182
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
219 def get(self, key, default=None): |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
220 d = self.__getitem__(key) |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
221 d.addCallback(self._valueOrDefault) |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
222 return d |
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 setdefault(self, key, default): |
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 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
|
228 """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
|
229 |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
230 @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
|
231 @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
|
232 """ |
087eec4c6c07
memory (persistent, sqlite): better private values handling + new LazyPersistentBinaryDict:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
233 return self.storage.setPrivateValue(self.namespace, name, value, self.binary, self.profile) |