sqlobject 3.13.1__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. sqlobject/__init__.py +15 -0
  2. sqlobject/__version__.py +8 -0
  3. sqlobject/boundattributes.py +124 -0
  4. sqlobject/cache.py +376 -0
  5. sqlobject/classregistry.py +138 -0
  6. sqlobject/col.py +2187 -0
  7. sqlobject/compat.py +59 -0
  8. sqlobject/conftest.py +64 -0
  9. sqlobject/constraints.py +77 -0
  10. sqlobject/converters.py +278 -0
  11. sqlobject/dbconnection.py +1137 -0
  12. sqlobject/dberrors.py +54 -0
  13. sqlobject/declarative.py +211 -0
  14. sqlobject/events.py +365 -0
  15. sqlobject/firebird/__init__.py +8 -0
  16. sqlobject/firebird/firebirdconnection.py +463 -0
  17. sqlobject/include/__init__.py +1 -0
  18. sqlobject/include/hashcol.py +109 -0
  19. sqlobject/include/tests/__init__.py +1 -0
  20. sqlobject/include/tests/test_hashcol.py +84 -0
  21. sqlobject/index.py +189 -0
  22. sqlobject/inheritance/__init__.py +551 -0
  23. sqlobject/inheritance/iteration.py +106 -0
  24. sqlobject/inheritance/tests/__init__.py +1 -0
  25. sqlobject/inheritance/tests/test_aggregates.py +23 -0
  26. sqlobject/inheritance/tests/test_asdict.py +48 -0
  27. sqlobject/inheritance/tests/test_deep_inheritance.py +114 -0
  28. sqlobject/inheritance/tests/test_destroy_cascade.py +23 -0
  29. sqlobject/inheritance/tests/test_foreignKey.py +94 -0
  30. sqlobject/inheritance/tests/test_indexes.py +42 -0
  31. sqlobject/inheritance/tests/test_inheritance.py +164 -0
  32. sqlobject/inheritance/tests/test_inheritance_tree.py +50 -0
  33. sqlobject/joins.py +656 -0
  34. sqlobject/main.py +1880 -0
  35. sqlobject/manager/__init__.py +1 -0
  36. sqlobject/manager/command.py +1309 -0
  37. sqlobject/maxdb/__init__.py +8 -0
  38. sqlobject/maxdb/maxdbconnection.py +320 -0
  39. sqlobject/maxdb/readme.txt +27 -0
  40. sqlobject/mssql/__init__.py +8 -0
  41. sqlobject/mssql/mssqlconnection.py +417 -0
  42. sqlobject/mysql/__init__.py +8 -0
  43. sqlobject/mysql/mysqlconnection.py +562 -0
  44. sqlobject/postgres/__init__.py +8 -0
  45. sqlobject/postgres/pgconnection.py +619 -0
  46. sqlobject/sqlbuilder.py +1655 -0
  47. sqlobject/sqlite/__init__.py +8 -0
  48. sqlobject/sqlite/sqliteconnection.py +418 -0
  49. sqlobject/sresults.py +377 -0
  50. sqlobject/styles.py +174 -0
  51. sqlobject/sybase/__init__.py +8 -0
  52. sqlobject/sybase/sybaseconnection.py +183 -0
  53. sqlobject/tests/__init__.py +1 -0
  54. sqlobject/tests/dbtest.py +363 -0
  55. sqlobject/tests/test_ForeignKey.py +133 -0
  56. sqlobject/tests/test_ForeignKey_cascade.py +150 -0
  57. sqlobject/tests/test_NoneValuedResultItem.py +33 -0
  58. sqlobject/tests/test_SQLMultipleJoin.py +65 -0
  59. sqlobject/tests/test_SQLRelatedJoin.py +114 -0
  60. sqlobject/tests/test_SingleJoin.py +30 -0
  61. sqlobject/tests/test_aggregates.py +75 -0
  62. sqlobject/tests/test_aliases.py +49 -0
  63. sqlobject/tests/test_asdict.py +18 -0
  64. sqlobject/tests/test_auto.py +218 -0
  65. sqlobject/tests/test_basic.py +359 -0
  66. sqlobject/tests/test_blob.py +34 -0
  67. sqlobject/tests/test_boundattributes.py +67 -0
  68. sqlobject/tests/test_cache.py +57 -0
  69. sqlobject/tests/test_class_hash.py +22 -0
  70. sqlobject/tests/test_columns_order.py +18 -0
  71. sqlobject/tests/test_combining_joins.py +47 -0
  72. sqlobject/tests/test_comparison.py +24 -0
  73. sqlobject/tests/test_compat.py +10 -0
  74. sqlobject/tests/test_complex_sorting.py +77 -0
  75. sqlobject/tests/test_constraints.py +43 -0
  76. sqlobject/tests/test_converters.py +276 -0
  77. sqlobject/tests/test_create_drop.py +29 -0
  78. sqlobject/tests/test_csvexport.py +101 -0
  79. sqlobject/tests/test_csvimport.py +21 -0
  80. sqlobject/tests/test_cyclic_reference.py +55 -0
  81. sqlobject/tests/test_datetime.py +171 -0
  82. sqlobject/tests/test_dbconnection.py +17 -0
  83. sqlobject/tests/test_decimal.py +63 -0
  84. sqlobject/tests/test_declarative.py +83 -0
  85. sqlobject/tests/test_default_style.py +65 -0
  86. sqlobject/tests/test_delete.py +74 -0
  87. sqlobject/tests/test_distinct.py +36 -0
  88. sqlobject/tests/test_empty.py +22 -0
  89. sqlobject/tests/test_enum.py +75 -0
  90. sqlobject/tests/test_events.py +142 -0
  91. sqlobject/tests/test_exceptions.py +40 -0
  92. sqlobject/tests/test_expire.py +29 -0
  93. sqlobject/tests/test_groupBy.py +45 -0
  94. sqlobject/tests/test_identity.py +29 -0
  95. sqlobject/tests/test_indexes.py +100 -0
  96. sqlobject/tests/test_inheritance.py +35 -0
  97. sqlobject/tests/test_joins.py +160 -0
  98. sqlobject/tests/test_joins_conditional.py +143 -0
  99. sqlobject/tests/test_jsonbcol.py +53 -0
  100. sqlobject/tests/test_jsoncol.py +56 -0
  101. sqlobject/tests/test_lazy.py +165 -0
  102. sqlobject/tests/test_md5.py +13 -0
  103. sqlobject/tests/test_mysql.py +50 -0
  104. sqlobject/tests/test_new_joins.py +165 -0
  105. sqlobject/tests/test_parse_uri.py +147 -0
  106. sqlobject/tests/test_paste.py +92 -0
  107. sqlobject/tests/test_perConnection.py +20 -0
  108. sqlobject/tests/test_pickle.py +42 -0
  109. sqlobject/tests/test_picklecol.py +44 -0
  110. sqlobject/tests/test_postgres.py +82 -0
  111. sqlobject/tests/test_reparent_sqlmeta.py +33 -0
  112. sqlobject/tests/test_schema.py +30 -0
  113. sqlobject/tests/test_select.py +218 -0
  114. sqlobject/tests/test_select_through.py +63 -0
  115. sqlobject/tests/test_setters.py +23 -0
  116. sqlobject/tests/test_slice.py +59 -0
  117. sqlobject/tests/test_sorting.py +80 -0
  118. sqlobject/tests/test_sqlbuilder.py +134 -0
  119. sqlobject/tests/test_sqlbuilder_dbspecific.py +66 -0
  120. sqlobject/tests/test_sqlbuilder_importproxy.py +55 -0
  121. sqlobject/tests/test_sqlbuilder_joins_instances.py +90 -0
  122. sqlobject/tests/test_sqlite.py +160 -0
  123. sqlobject/tests/test_sqlmeta_idName.py +52 -0
  124. sqlobject/tests/test_sqlobject_admin.py +38 -0
  125. sqlobject/tests/test_string_id.py +79 -0
  126. sqlobject/tests/test_style.py +36 -0
  127. sqlobject/tests/test_subqueries.py +142 -0
  128. sqlobject/tests/test_transactions.py +122 -0
  129. sqlobject/tests/test_unicode.py +84 -0
  130. sqlobject/tests/test_uuidcol.py +40 -0
  131. sqlobject/tests/test_validation.py +125 -0
  132. sqlobject/tests/test_views.py +159 -0
  133. sqlobject/util/__init__.py +1 -0
  134. sqlobject/util/csvexport.py +210 -0
  135. sqlobject/util/csvimport.py +366 -0
  136. sqlobject/util/moduleloader.py +35 -0
  137. sqlobject/util/threadinglocal.py +6 -0
  138. sqlobject/versioning/__init__.py +120 -0
  139. sqlobject/versioning/test/__init__.py +0 -0
  140. sqlobject/versioning/test/test_version.py +183 -0
  141. sqlobject/views.py +151 -0
  142. sqlobject/wsgi_middleware.py +109 -0
  143. sqlobject-3.13.1.data/scripts/sqlobject-admin +36 -0
  144. sqlobject-3.13.1.data/scripts/sqlobject-convertOldURI +19 -0
  145. sqlobject-3.13.1.dist-info/METADATA +177 -0
  146. sqlobject-3.13.1.dist-info/RECORD +149 -0
  147. sqlobject-3.13.1.dist-info/WHEEL +6 -0
  148. sqlobject-3.13.1.dist-info/entry_points.txt +4 -0
  149. sqlobject-3.13.1.dist-info/top_level.txt +1 -0
sqlobject/__init__.py ADDED
@@ -0,0 +1,15 @@
1
+ """SQLObject"""
2
+
3
+ # Do import for namespace
4
+ # noqa is a directive for flake8 to ignore seemingly unused imports
5
+
6
+ from .__version__ import version, version_info # noqa
7
+
8
+ from .col import * # noqa
9
+ from .index import * # noqa
10
+ from .joins import * # noqa
11
+ from .main import * # noqa
12
+ from .sqlbuilder import AND, OR, NOT, IN, LIKE, RLIKE, DESC, CONTAINSSTRING, const, func # noqa
13
+ from .styles import * # noqa
14
+ from .dbconnection import connectionForURI # noqa
15
+ from . import dberrors # noqa
@@ -0,0 +1,8 @@
1
+
2
+ version = '3.13.1'
3
+ major = 3
4
+ minor = 13
5
+ micro = 1
6
+ release_level = 'final'
7
+ serial = 0
8
+ version_info = (major, minor, micro, release_level, serial)
@@ -0,0 +1,124 @@
1
+ """
2
+ Bound attributes are attributes that are bound to a specific class and
3
+ a specific name. In SQLObject a typical example is a column object,
4
+ which knows its name and class.
5
+
6
+ A bound attribute should define a method ``__addtoclass__(added_class,
7
+ name)`` (attributes without this method will simply be treated as
8
+ normal). The return value is ignored; if the attribute wishes to
9
+ change the value in the class, it must call ``setattr(added_class,
10
+ name, new_value)``.
11
+
12
+ BoundAttribute is a class that facilitates lazy attribute creation.
13
+ """
14
+ from __future__ import absolute_import
15
+
16
+ from . import declarative
17
+ from . import events
18
+
19
+ __all__ = ['BoundAttribute', 'BoundFactory']
20
+
21
+
22
+ class BoundAttribute(declarative.Declarative):
23
+
24
+ """
25
+ This is a declarative class that passes all the values given to it
26
+ to another object. So you can pass it arguments (via
27
+ __init__/__call__) or give it the equivalent of keyword arguments
28
+ through subclassing. Then a bound object will be added in its
29
+ place.
30
+
31
+ To hook this other object in, override ``make_object(added_class,
32
+ name, **attrs)`` and maybe ``set_object(added_class, name,
33
+ **attrs)`` (the default implementation of ``set_object``
34
+ just resets the attribute to whatever ``make_object`` returned).
35
+
36
+ Also see ``BoundFactory``.
37
+ """
38
+
39
+ _private_variables = (
40
+ '_private_variables',
41
+ '_all_attributes',
42
+ '__classinit__',
43
+ '__addtoclass__',
44
+ '_add_attrs',
45
+ 'set_object',
46
+ 'make_object',
47
+ 'clone_in_subclass',
48
+ )
49
+
50
+ _all_attrs = ()
51
+ clone_for_subclass = True
52
+
53
+ def __classinit__(cls, new_attrs):
54
+ declarative.Declarative.__classinit__(cls, new_attrs)
55
+ cls._all_attrs = cls._add_attrs(cls, new_attrs)
56
+
57
+ def __instanceinit__(self, new_attrs):
58
+ declarative.Declarative.__instanceinit__(self, new_attrs)
59
+ self.__dict__['_all_attrs'] = self._add_attrs(self, new_attrs)
60
+
61
+ @staticmethod
62
+ def _add_attrs(this_object, new_attrs):
63
+ private = this_object._private_variables
64
+ all_attrs = list(this_object._all_attrs)
65
+ for key in new_attrs.keys():
66
+ if key.startswith('_') or key in private:
67
+ continue
68
+ if key not in all_attrs:
69
+ all_attrs.append(key)
70
+ return tuple(all_attrs)
71
+
72
+ @declarative.classinstancemethod
73
+ def __addtoclass__(self, cls, added_class, attr_name):
74
+ me = self or cls
75
+ attrs = {}
76
+ for name in me._all_attrs:
77
+ attrs[name] = getattr(me, name)
78
+ attrs['added_class'] = added_class
79
+ attrs['attr_name'] = attr_name
80
+ obj = me.make_object(**attrs)
81
+
82
+ if self.clone_for_subclass:
83
+ def on_rebind(new_class_name, bases, new_attrs,
84
+ post_funcs, early_funcs):
85
+ def rebind(new_class):
86
+ me.set_object(
87
+ new_class, attr_name,
88
+ me.make_object(**attrs))
89
+ post_funcs.append(rebind)
90
+ events.listen(receiver=on_rebind, soClass=added_class,
91
+ signal=events.ClassCreateSignal, weak=False)
92
+
93
+ me.set_object(added_class, attr_name, obj)
94
+
95
+ @classmethod
96
+ def set_object(cls, added_class, attr_name, obj):
97
+ setattr(added_class, attr_name, obj)
98
+
99
+ @classmethod
100
+ def make_object(cls, added_class, attr_name, *args, **attrs):
101
+ raise NotImplementedError
102
+
103
+ def __setattr__(self, name, value):
104
+ self.__dict__['_all_attrs'] = self._add_attrs(self, {name: value})
105
+ self.__dict__[name] = value
106
+
107
+
108
+ class BoundFactory(BoundAttribute):
109
+
110
+ """
111
+ This will bind the attribute to whatever is given by
112
+ ``factory_class``. This factory should be a callable with the
113
+ signature ``factory_class(added_class, attr_name, *args, **kw)``.
114
+
115
+ The factory will be reinvoked (and the attribute rebound) for
116
+ every subclassing.
117
+ """
118
+
119
+ factory_class = None
120
+ _private_variables = (
121
+ BoundAttribute._private_variables + ('factory_class',))
122
+
123
+ def make_object(cls, added_class, attr_name, *args, **kw):
124
+ return cls.factory_class(added_class, attr_name, *args, **kw)
sqlobject/cache.py ADDED
@@ -0,0 +1,376 @@
1
+ """
2
+ This implements the instance caching in SQLObject. Caching is
3
+ relatively aggressive. All objects are retained so long as they are
4
+ in memory, by keeping weak references to objects. We also keep other
5
+ objects in a cache that doesn't allow them to be garbage collected
6
+ (unless caching is turned off).
7
+ """
8
+
9
+ import threading
10
+ from weakref import ref
11
+
12
+
13
+ class CacheFactory(object):
14
+
15
+ """
16
+ CacheFactory caches object creation. Each object should be
17
+ referenced by a single hashable ID (note tuples of hashable
18
+ values are also hashable).
19
+
20
+ """
21
+
22
+ def __init__(self, cullFrequency=100, cullFraction=2,
23
+ cache=True):
24
+ """
25
+ Every cullFrequency times that an item is retrieved from
26
+ this cache, the cull method is called.
27
+
28
+ The cull method then expires an arbitrary fraction of
29
+ the cached objects. The idea is at no time will the cache
30
+ be entirely emptied, placing a potentially high load at that
31
+ moment, but everything object will have its time to go
32
+ eventually. The fraction is given as an integer, and one
33
+ in that many objects are expired (i.e., the default is 1/2
34
+ of objects are expired).
35
+
36
+ By setting cache to False, items won't be cached.
37
+
38
+ However, in all cases a weak reference is kept to created
39
+ objects, and if the object hasn't been garbage collected
40
+ it will be returned.
41
+ """
42
+
43
+ self.cullFrequency = cullFrequency
44
+ self.cullCount = 0
45
+ self.cullOffset = 0
46
+ self.cullFraction = cullFraction
47
+ self.doCache = cache
48
+
49
+ if self.doCache:
50
+ self.cache = {}
51
+ self.expiredCache = {}
52
+ self.lock = threading.Lock()
53
+
54
+ def tryGet(self, id):
55
+ """
56
+ This returns None, or the object in cache.
57
+ """
58
+ value = self.expiredCache.get(id)
59
+ if value:
60
+ # it's actually a weakref:
61
+ return value()
62
+ if not self.doCache:
63
+ return None
64
+ return self.cache.get(id)
65
+
66
+ def get(self, id):
67
+ """
68
+ This method can cause deadlocks! tryGet is safer
69
+
70
+ This returns the object found in cache, or None. If None,
71
+ then the cache will remain locked! This is so that the
72
+ calling function can create the object in a threadsafe manner
73
+ before releasing the lock. You should use this like (note
74
+ that ``cache`` is actually a CacheSet object in this
75
+ example)::
76
+
77
+ obj = cache.get(some_id, my_class)
78
+ if obj is None:
79
+ try:
80
+ obj = create_object(some_id)
81
+ cache.put(some_id, my_class, obj)
82
+ finally:
83
+ cache.finishPut(cls)
84
+
85
+ This method checks both the main cache (which retains
86
+ references) and the 'expired' cache, which retains only weak
87
+ references.
88
+ """
89
+
90
+ if self.doCache:
91
+ if self.cullCount > self.cullFrequency:
92
+ # Two threads could hit the cull in a row, but
93
+ # that's not so bad. At least by setting cullCount
94
+ # back to zero right away we avoid this. The cull
95
+ # method has a lock, so it's threadsafe.
96
+ self.cullCount = 0
97
+ self.cull()
98
+ else:
99
+ self.cullCount = self.cullCount + 1
100
+
101
+ try:
102
+ return self.cache[id]
103
+ except KeyError:
104
+ pass
105
+ self.lock.acquire()
106
+ try:
107
+ val = self.cache[id]
108
+ except KeyError:
109
+ pass
110
+ else:
111
+ self.lock.release()
112
+ return val
113
+ try:
114
+ val = self.expiredCache[id]()
115
+ except KeyError:
116
+ return None
117
+ else:
118
+ del self.expiredCache[id]
119
+ if val is None:
120
+ return None
121
+ self.cache[id] = val
122
+ self.lock.release()
123
+ return val
124
+
125
+ else:
126
+ try:
127
+ val = self.expiredCache[id]()
128
+ if val is not None:
129
+ return val
130
+ except KeyError:
131
+ pass
132
+ self.lock.acquire()
133
+ try:
134
+ val = self.expiredCache[id]()
135
+ except KeyError:
136
+ return None
137
+ else:
138
+ if val is None:
139
+ del self.expiredCache[id]
140
+ return None
141
+ self.lock.release()
142
+ return val
143
+
144
+ def put(self, id, obj):
145
+ """
146
+ Puts an object into the cache. Should only be called after
147
+ .get(), so that duplicate objects don't end up in the cache.
148
+ """
149
+ if self.doCache:
150
+ self.cache[id] = obj
151
+ else:
152
+ self.expiredCache[id] = ref(obj)
153
+
154
+ def finishPut(self):
155
+ """
156
+ Releases the lock that is retained when .get() is called and
157
+ returns None.
158
+ """
159
+ self.lock.release()
160
+
161
+ def created(self, id, obj):
162
+ """
163
+ Inserts and object into the cache. Should be used when no one
164
+ else knows about the object yet, so there cannot be any object
165
+ already in the cache. After a database INSERT is an example
166
+ of this situation.
167
+ """
168
+ if self.doCache:
169
+ if self.cullCount > self.cullFrequency:
170
+ # Two threads could hit the cull in a row, but
171
+ # that's not so bad. At least by setting cullCount
172
+ # back to zero right away we avoid this. The cull
173
+ # method has a lock, so it's threadsafe.
174
+ self.cullCount = 0
175
+ self.cull()
176
+ else:
177
+ self.cullCount = self.cullCount + 1
178
+ self.cache[id] = obj
179
+ else:
180
+ self.expiredCache[id] = ref(obj)
181
+
182
+ def cull(self):
183
+ """Runs through the cache and expires objects
184
+
185
+ E.g., if ``cullFraction`` is 3, then every third object is moved to
186
+ the 'expired' (aka weakref) cache.
187
+
188
+ """
189
+ self.lock.acquire()
190
+ try:
191
+ # remove dead references from the expired cache
192
+ keys = list(self.expiredCache.keys())
193
+ for key in keys:
194
+ if self.expiredCache[key]() is None:
195
+ self.expiredCache.pop(key, None)
196
+
197
+ keys = list(self.cache.keys())
198
+ for i in range(self.cullOffset, len(keys), self.cullFraction):
199
+ id = keys[i]
200
+ # create a weakref, then remove from the cache
201
+ obj = ref(self.cache[id])
202
+ del self.cache[id]
203
+
204
+ # the object may have been gc'd when removed from the cache
205
+ # above, no need to place in expiredCache
206
+ if obj() is not None:
207
+ self.expiredCache[id] = obj
208
+ # This offset tries to balance out which objects we
209
+ # expire, so no object will just hang out in the cache
210
+ # forever.
211
+ self.cullOffset = (self.cullOffset + 1) % self.cullFraction
212
+ finally:
213
+ self.lock.release()
214
+
215
+ def clear(self):
216
+ """
217
+ Removes everything from the cache. Warning! This can cause
218
+ duplicate objects in memory.
219
+ """
220
+ if self.doCache:
221
+ self.cache.clear()
222
+ self.expiredCache.clear()
223
+
224
+ def expire(self, id):
225
+ """
226
+ Expires a single object. Typically called after a delete.
227
+ Doesn't even keep a weakref. (@@: bad name?)
228
+ """
229
+ if not self.doCache:
230
+ return
231
+ self.lock.acquire()
232
+ try:
233
+ if id in self.cache:
234
+ del self.cache[id]
235
+ if id in self.expiredCache:
236
+ del self.expiredCache[id]
237
+ finally:
238
+ self.lock.release()
239
+
240
+ def expireAll(self):
241
+ """
242
+ Expires all objects, moving them all into the expired/weakref
243
+ cache.
244
+ """
245
+ if not self.doCache:
246
+ return
247
+ self.lock.acquire()
248
+ try:
249
+ for key, value in self.cache.items():
250
+ self.expiredCache[key] = ref(value)
251
+ self.cache = {}
252
+ finally:
253
+ self.lock.release()
254
+
255
+ def allIDs(self):
256
+ """
257
+ Returns the IDs of all objects in the cache.
258
+ """
259
+ if self.doCache:
260
+ all = list(self.cache.keys())
261
+ else:
262
+ all = []
263
+ for id, value in self.expiredCache.items():
264
+ if value():
265
+ all.append(id)
266
+ return all
267
+
268
+ def getAll(self):
269
+ """
270
+ Return all the objects in the cache.
271
+ """
272
+ if self.doCache:
273
+ all = list(self.cache.values())
274
+ else:
275
+ all = []
276
+ for value in self.expiredCache.values():
277
+ if value():
278
+ all.append(value())
279
+ return all
280
+
281
+
282
+ class CacheSet(object):
283
+
284
+ """
285
+ A CacheSet is used to collect and maintain a series of caches. In
286
+ SQLObject, there is one CacheSet per connection, and one Cache
287
+ in the CacheSet for each class, since IDs are not unique across
288
+ classes. It contains methods similar to Cache, but that take
289
+ a ``cls`` argument.
290
+ """
291
+
292
+ def __init__(self, *args, **kw):
293
+ self.caches = {}
294
+ self.args = args
295
+ self.kw = kw
296
+
297
+ def get(self, id, cls):
298
+ try:
299
+ return self.caches[cls.__name__].get(id)
300
+ except KeyError:
301
+ self.caches[cls.__name__] = CacheFactory(*self.args, **self.kw)
302
+ return self.caches[cls.__name__].get(id)
303
+
304
+ def put(self, id, cls, obj):
305
+ self.caches[cls.__name__].put(id, obj)
306
+
307
+ def finishPut(self, cls):
308
+ self.caches[cls.__name__].finishPut()
309
+
310
+ def created(self, id, cls, obj):
311
+ try:
312
+ self.caches[cls.__name__].created(id, obj)
313
+ except KeyError:
314
+ self.caches[cls.__name__] = CacheFactory(*self.args, **self.kw)
315
+ self.caches[cls.__name__].created(id, obj)
316
+
317
+ def expire(self, id, cls):
318
+ try:
319
+ self.caches[cls.__name__].expire(id)
320
+ except KeyError:
321
+ pass
322
+
323
+ def clear(self, cls=None):
324
+ if cls is None:
325
+ for cache in self.caches.values():
326
+ cache.clear()
327
+ elif cls.__name__ in self.caches:
328
+ self.caches[cls.__name__].clear()
329
+
330
+ def tryGet(self, id, cls):
331
+ return self.tryGetByName(id, cls.__name__)
332
+
333
+ def tryGetByName(self, id, clsname):
334
+ try:
335
+ return self.caches[clsname].tryGet(id)
336
+ except KeyError:
337
+ return None
338
+
339
+ def allIDs(self, cls):
340
+ try:
341
+ self.caches[cls.__name__].allIDs()
342
+ except KeyError:
343
+ return []
344
+
345
+ def allSubCaches(self):
346
+ return self.caches.values()
347
+
348
+ def allSubCachesByClassNames(self):
349
+ return self.caches
350
+
351
+ def weakrefAll(self, cls=None):
352
+ """
353
+ Move all objects in the cls (or if not given, then in all
354
+ classes) to the weakref dictionary, where they can be
355
+ collected.
356
+ """
357
+ if cls is None:
358
+ for cache in self.caches.values():
359
+ cache.expireAll()
360
+ elif cls.__name__ in self.caches:
361
+ self.caches[cls.__name__].expireAll()
362
+
363
+ def getAll(self, cls=None):
364
+ """
365
+ Returns all instances in the cache for the given class or all
366
+ classes.
367
+ """
368
+ if cls is None:
369
+ results = []
370
+ for cache in self.caches.values():
371
+ results.extend(cache.getAll())
372
+ return results
373
+ elif cls.__name__ in self.caches:
374
+ return self.caches[cls.__name__].getAll()
375
+ else:
376
+ return []
@@ -0,0 +1,138 @@
1
+ """
2
+ classresolver.py
3
+ 2 February 2004, Ian Bicking <ianb@colorstudy.com>
4
+
5
+ Resolves strings to classes, and runs callbacks when referenced
6
+ classes are created.
7
+
8
+ Classes are referred to only by name, not by module. So that
9
+ identically-named classes can coexist, classes are put into individual
10
+ registries, which are keyed on strings (names). These registries are
11
+ created on demand.
12
+
13
+ Use like::
14
+
15
+ >>> import classregistry
16
+ >>> registry = classregistry.registry('MyModules')
17
+ >>> def afterMyClassExists(cls):
18
+ ... print('Class finally exists: %s' % cls)
19
+ >>> registry.addClassCallback('MyClass', afterMyClassExists)
20
+ >>> class MyClass:
21
+ ... pass
22
+ >>> registry.addClass(MyClass)
23
+ Class finally exists: MyClass
24
+
25
+ """
26
+
27
+
28
+ class ClassRegistry(object):
29
+ """
30
+ We'll be dealing with classes that reference each other, so
31
+ class C1 may reference C2 (in a join), while C2 references
32
+ C1 right back. Since classes are created in an order, there
33
+ will be a point when C1 exists but C2 doesn't. So we deal
34
+ with classes by name, and after each class is created we
35
+ try to fix up any references by replacing the names with
36
+ actual classes.
37
+
38
+ Here we keep a dictionaries of class names to classes -- note
39
+ that the classes might be spread among different modules, so
40
+ since we pile them together names need to be globally unique,
41
+ to just module unique.
42
+ Like needSet below, the container dictionary is keyed by the
43
+ class registry.
44
+ """
45
+
46
+ def __init__(self, name):
47
+ self.name = name
48
+ self.classes = {}
49
+ self.callbacks = {}
50
+ self.genericCallbacks = []
51
+
52
+ def addClassCallback(self, className, callback, *args, **kw):
53
+ """
54
+ Whenever a name is substituted for the class, you can register
55
+ a callback that will be called when the needed class is
56
+ created. If it's already been created, the callback will be
57
+ called immediately.
58
+ """
59
+ if className in self.classes:
60
+ callback(self.classes[className], *args, **kw)
61
+ else:
62
+ self.callbacks.setdefault(className, []).append(
63
+ (callback, args, kw))
64
+
65
+ def addCallback(self, callback, *args, **kw):
66
+ """
67
+ This callback is called for all classes, not just specific
68
+ ones (like addClassCallback).
69
+ """
70
+ self.genericCallbacks.append((callback, args, kw))
71
+ for cls in self.classes.values():
72
+ callback(cls, *args, **kw)
73
+
74
+ def addClass(self, cls):
75
+ """
76
+ Everytime a class is created, we add it to the registry, so
77
+ that other classes can find it by name. We also call any
78
+ callbacks that are waiting for the class.
79
+ """
80
+ if cls.__name__ in self.classes:
81
+ import sys
82
+ other = self.classes[cls.__name__]
83
+ raise ValueError(
84
+ "class %s is already in the registry (other class is "
85
+ "%r, from the module %s in %s; attempted new class is "
86
+ "%r, from the module %s in %s)"
87
+ % (cls.__name__,
88
+ other, other.__module__,
89
+ getattr(sys.modules.get(other.__module__),
90
+ '__file__', '(unknown)'),
91
+ cls, cls.__module__,
92
+ getattr(sys.modules.get(cls.__module__),
93
+ '__file__', '(unknown)')))
94
+ self.classes[cls.__name__] = cls
95
+ if cls.__name__ in self.callbacks:
96
+ for callback, args, kw in self.callbacks[cls.__name__]:
97
+ callback(cls, *args, **kw)
98
+ del self.callbacks[cls.__name__]
99
+ for callback, args, kw in self.genericCallbacks:
100
+ callback(cls, *args, **kw)
101
+
102
+ def getClass(self, className):
103
+ try:
104
+ return self.classes[className]
105
+ except KeyError:
106
+ all = sorted(self.classes.keys())
107
+ raise KeyError(
108
+ "No class %s found in the registry %s (these classes "
109
+ "exist: %s)"
110
+ % (className, self.name or '[default]', ', '.join(all)))
111
+
112
+ def allClasses(self):
113
+ return self.classes.values()
114
+
115
+
116
+ class _MasterRegistry(object):
117
+ """
118
+ This singleton holds all the class registries. There can be
119
+ multiple registries to hold different unrelated sets of classes
120
+ that reside in the same process. These registries are named with
121
+ strings, and are created on demand. The MasterRegistry module
122
+ global holds the singleton.
123
+ """
124
+
125
+ def __init__(self):
126
+ self.registries = {}
127
+
128
+ def registry(self, item):
129
+ if item not in self.registries:
130
+ self.registries[item] = ClassRegistry(item)
131
+ return self.registries[item]
132
+
133
+ MasterRegistry = _MasterRegistry()
134
+ registry = MasterRegistry.registry
135
+
136
+
137
+ def findClass(name, class_registry=None):
138
+ return registry(class_registry).getClass(name)