instrumentation.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. """Extensible class instrumentation.
  2. The :mod:`sqlalchemy.ext.instrumentation` package provides for alternate
  3. systems of class instrumentation within the ORM. Class instrumentation
  4. refers to how the ORM places attributes on the class which maintain
  5. data and track changes to that data, as well as event hooks installed
  6. on the class.
  7. .. note::
  8. The extension package is provided for the benefit of integration
  9. with other object management packages, which already perform
  10. their own instrumentation. It is not intended for general use.
  11. For examples of how the instrumentation extension is used,
  12. see the example :ref:`examples_instrumentation`.
  13. .. versionchanged:: 0.8
  14. The :mod:`sqlalchemy.orm.instrumentation` was split out so
  15. that all functionality having to do with non-standard
  16. instrumentation was moved out to :mod:`sqlalchemy.ext.instrumentation`.
  17. When imported, the module installs itself within
  18. :mod:`sqlalchemy.orm.instrumentation` so that it
  19. takes effect, including recognition of
  20. ``__sa_instrumentation_manager__`` on mapped classes, as
  21. well :data:`.instrumentation_finders`
  22. being used to determine class instrumentation resolution.
  23. """
  24. from ..orm import instrumentation as orm_instrumentation
  25. from ..orm.instrumentation import (
  26. ClassManager, InstrumentationFactory, _default_state_getter,
  27. _default_dict_getter, _default_manager_getter
  28. )
  29. from ..orm import attributes, collections, base as orm_base
  30. from .. import util
  31. from ..orm import exc as orm_exc
  32. import weakref
  33. INSTRUMENTATION_MANAGER = '__sa_instrumentation_manager__'
  34. """Attribute, elects custom instrumentation when present on a mapped class.
  35. Allows a class to specify a slightly or wildly different technique for
  36. tracking changes made to mapped attributes and collections.
  37. Only one instrumentation implementation is allowed in a given object
  38. inheritance hierarchy.
  39. The value of this attribute must be a callable and will be passed a class
  40. object. The callable must return one of:
  41. - An instance of an InstrumentationManager or subclass
  42. - An object implementing all or some of InstrumentationManager (TODO)
  43. - A dictionary of callables, implementing all or some of the above (TODO)
  44. - An instance of a ClassManager or subclass
  45. This attribute is consulted by SQLAlchemy instrumentation
  46. resolution, once the :mod:`sqlalchemy.ext.instrumentation` module
  47. has been imported. If custom finders are installed in the global
  48. instrumentation_finders list, they may or may not choose to honor this
  49. attribute.
  50. """
  51. def find_native_user_instrumentation_hook(cls):
  52. """Find user-specified instrumentation management for a class."""
  53. return getattr(cls, INSTRUMENTATION_MANAGER, None)
  54. instrumentation_finders = [find_native_user_instrumentation_hook]
  55. """An extensible sequence of callables which return instrumentation
  56. implementations
  57. When a class is registered, each callable will be passed a class object.
  58. If None is returned, the
  59. next finder in the sequence is consulted. Otherwise the return must be an
  60. instrumentation factory that follows the same guidelines as
  61. sqlalchemy.ext.instrumentation.INSTRUMENTATION_MANAGER.
  62. By default, the only finder is find_native_user_instrumentation_hook, which
  63. searches for INSTRUMENTATION_MANAGER. If all finders return None, standard
  64. ClassManager instrumentation is used.
  65. """
  66. class ExtendedInstrumentationRegistry(InstrumentationFactory):
  67. """Extends :class:`.InstrumentationFactory` with additional
  68. bookkeeping, to accommodate multiple types of
  69. class managers.
  70. """
  71. _manager_finders = weakref.WeakKeyDictionary()
  72. _state_finders = weakref.WeakKeyDictionary()
  73. _dict_finders = weakref.WeakKeyDictionary()
  74. _extended = False
  75. def _locate_extended_factory(self, class_):
  76. for finder in instrumentation_finders:
  77. factory = finder(class_)
  78. if factory is not None:
  79. manager = self._extended_class_manager(class_, factory)
  80. return manager, factory
  81. else:
  82. return None, None
  83. def _check_conflicts(self, class_, factory):
  84. existing_factories = self._collect_management_factories_for(class_).\
  85. difference([factory])
  86. if existing_factories:
  87. raise TypeError(
  88. "multiple instrumentation implementations specified "
  89. "in %s inheritance hierarchy: %r" % (
  90. class_.__name__, list(existing_factories)))
  91. def _extended_class_manager(self, class_, factory):
  92. manager = factory(class_)
  93. if not isinstance(manager, ClassManager):
  94. manager = _ClassInstrumentationAdapter(class_, manager)
  95. if factory != ClassManager and not self._extended:
  96. # somebody invoked a custom ClassManager.
  97. # reinstall global "getter" functions with the more
  98. # expensive ones.
  99. self._extended = True
  100. _install_instrumented_lookups()
  101. self._manager_finders[class_] = manager.manager_getter()
  102. self._state_finders[class_] = manager.state_getter()
  103. self._dict_finders[class_] = manager.dict_getter()
  104. return manager
  105. def _collect_management_factories_for(self, cls):
  106. """Return a collection of factories in play or specified for a
  107. hierarchy.
  108. Traverses the entire inheritance graph of a cls and returns a
  109. collection of instrumentation factories for those classes. Factories
  110. are extracted from active ClassManagers, if available, otherwise
  111. instrumentation_finders is consulted.
  112. """
  113. hierarchy = util.class_hierarchy(cls)
  114. factories = set()
  115. for member in hierarchy:
  116. manager = self.manager_of_class(member)
  117. if manager is not None:
  118. factories.add(manager.factory)
  119. else:
  120. for finder in instrumentation_finders:
  121. factory = finder(member)
  122. if factory is not None:
  123. break
  124. else:
  125. factory = None
  126. factories.add(factory)
  127. factories.discard(None)
  128. return factories
  129. def unregister(self, class_):
  130. if class_ in self._manager_finders:
  131. del self._manager_finders[class_]
  132. del self._state_finders[class_]
  133. del self._dict_finders[class_]
  134. super(ExtendedInstrumentationRegistry, self).unregister(class_)
  135. def manager_of_class(self, cls):
  136. if cls is None:
  137. return None
  138. try:
  139. finder = self._manager_finders.get(cls, _default_manager_getter)
  140. except TypeError:
  141. # due to weakref lookup on invalid object
  142. return None
  143. else:
  144. return finder(cls)
  145. def state_of(self, instance):
  146. if instance is None:
  147. raise AttributeError("None has no persistent state.")
  148. return self._state_finders.get(
  149. instance.__class__, _default_state_getter)(instance)
  150. def dict_of(self, instance):
  151. if instance is None:
  152. raise AttributeError("None has no persistent state.")
  153. return self._dict_finders.get(
  154. instance.__class__, _default_dict_getter)(instance)
  155. orm_instrumentation._instrumentation_factory = \
  156. _instrumentation_factory = ExtendedInstrumentationRegistry()
  157. orm_instrumentation.instrumentation_finders = instrumentation_finders
  158. class InstrumentationManager(object):
  159. """User-defined class instrumentation extension.
  160. :class:`.InstrumentationManager` can be subclassed in order
  161. to change
  162. how class instrumentation proceeds. This class exists for
  163. the purposes of integration with other object management
  164. frameworks which would like to entirely modify the
  165. instrumentation methodology of the ORM, and is not intended
  166. for regular usage. For interception of class instrumentation
  167. events, see :class:`.InstrumentationEvents`.
  168. The API for this class should be considered as semi-stable,
  169. and may change slightly with new releases.
  170. .. versionchanged:: 0.8
  171. :class:`.InstrumentationManager` was moved from
  172. :mod:`sqlalchemy.orm.instrumentation` to
  173. :mod:`sqlalchemy.ext.instrumentation`.
  174. """
  175. # r4361 added a mandatory (cls) constructor to this interface.
  176. # given that, perhaps class_ should be dropped from all of these
  177. # signatures.
  178. def __init__(self, class_):
  179. pass
  180. def manage(self, class_, manager):
  181. setattr(class_, '_default_class_manager', manager)
  182. def dispose(self, class_, manager):
  183. delattr(class_, '_default_class_manager')
  184. def manager_getter(self, class_):
  185. def get(cls):
  186. return cls._default_class_manager
  187. return get
  188. def instrument_attribute(self, class_, key, inst):
  189. pass
  190. def post_configure_attribute(self, class_, key, inst):
  191. pass
  192. def install_descriptor(self, class_, key, inst):
  193. setattr(class_, key, inst)
  194. def uninstall_descriptor(self, class_, key):
  195. delattr(class_, key)
  196. def install_member(self, class_, key, implementation):
  197. setattr(class_, key, implementation)
  198. def uninstall_member(self, class_, key):
  199. delattr(class_, key)
  200. def instrument_collection_class(self, class_, key, collection_class):
  201. return collections.prepare_instrumentation(collection_class)
  202. def get_instance_dict(self, class_, instance):
  203. return instance.__dict__
  204. def initialize_instance_dict(self, class_, instance):
  205. pass
  206. def install_state(self, class_, instance, state):
  207. setattr(instance, '_default_state', state)
  208. def remove_state(self, class_, instance):
  209. delattr(instance, '_default_state')
  210. def state_getter(self, class_):
  211. return lambda instance: getattr(instance, '_default_state')
  212. def dict_getter(self, class_):
  213. return lambda inst: self.get_instance_dict(class_, inst)
  214. class _ClassInstrumentationAdapter(ClassManager):
  215. """Adapts a user-defined InstrumentationManager to a ClassManager."""
  216. def __init__(self, class_, override):
  217. self._adapted = override
  218. self._get_state = self._adapted.state_getter(class_)
  219. self._get_dict = self._adapted.dict_getter(class_)
  220. ClassManager.__init__(self, class_)
  221. def manage(self):
  222. self._adapted.manage(self.class_, self)
  223. def dispose(self):
  224. self._adapted.dispose(self.class_)
  225. def manager_getter(self):
  226. return self._adapted.manager_getter(self.class_)
  227. def instrument_attribute(self, key, inst, propagated=False):
  228. ClassManager.instrument_attribute(self, key, inst, propagated)
  229. if not propagated:
  230. self._adapted.instrument_attribute(self.class_, key, inst)
  231. def post_configure_attribute(self, key):
  232. super(_ClassInstrumentationAdapter, self).post_configure_attribute(key)
  233. self._adapted.post_configure_attribute(self.class_, key, self[key])
  234. def install_descriptor(self, key, inst):
  235. self._adapted.install_descriptor(self.class_, key, inst)
  236. def uninstall_descriptor(self, key):
  237. self._adapted.uninstall_descriptor(self.class_, key)
  238. def install_member(self, key, implementation):
  239. self._adapted.install_member(self.class_, key, implementation)
  240. def uninstall_member(self, key):
  241. self._adapted.uninstall_member(self.class_, key)
  242. def instrument_collection_class(self, key, collection_class):
  243. return self._adapted.instrument_collection_class(
  244. self.class_, key, collection_class)
  245. def initialize_collection(self, key, state, factory):
  246. delegate = getattr(self._adapted, 'initialize_collection', None)
  247. if delegate:
  248. return delegate(key, state, factory)
  249. else:
  250. return ClassManager.initialize_collection(self, key,
  251. state, factory)
  252. def new_instance(self, state=None):
  253. instance = self.class_.__new__(self.class_)
  254. self.setup_instance(instance, state)
  255. return instance
  256. def _new_state_if_none(self, instance):
  257. """Install a default InstanceState if none is present.
  258. A private convenience method used by the __init__ decorator.
  259. """
  260. if self.has_state(instance):
  261. return False
  262. else:
  263. return self.setup_instance(instance)
  264. def setup_instance(self, instance, state=None):
  265. self._adapted.initialize_instance_dict(self.class_, instance)
  266. if state is None:
  267. state = self._state_constructor(instance, self)
  268. # the given instance is assumed to have no state
  269. self._adapted.install_state(self.class_, instance, state)
  270. return state
  271. def teardown_instance(self, instance):
  272. self._adapted.remove_state(self.class_, instance)
  273. def has_state(self, instance):
  274. try:
  275. self._get_state(instance)
  276. except orm_exc.NO_STATE:
  277. return False
  278. else:
  279. return True
  280. def state_getter(self):
  281. return self._get_state
  282. def dict_getter(self):
  283. return self._get_dict
  284. def _install_instrumented_lookups():
  285. """Replace global class/object management functions
  286. with ExtendedInstrumentationRegistry implementations, which
  287. allow multiple types of class managers to be present,
  288. at the cost of performance.
  289. This function is called only by ExtendedInstrumentationRegistry
  290. and unit tests specific to this behavior.
  291. The _reinstall_default_lookups() function can be called
  292. after this one to re-establish the default functions.
  293. """
  294. _install_lookups(
  295. dict(
  296. instance_state=_instrumentation_factory.state_of,
  297. instance_dict=_instrumentation_factory.dict_of,
  298. manager_of_class=_instrumentation_factory.manager_of_class
  299. )
  300. )
  301. def _reinstall_default_lookups():
  302. """Restore simplified lookups."""
  303. _install_lookups(
  304. dict(
  305. instance_state=_default_state_getter,
  306. instance_dict=_default_dict_getter,
  307. manager_of_class=_default_manager_getter
  308. )
  309. )
  310. _instrumentation_factory._extended = False
  311. def _install_lookups(lookups):
  312. global instance_state, instance_dict, manager_of_class
  313. instance_state = lookups['instance_state']
  314. instance_dict = lookups['instance_dict']
  315. manager_of_class = lookups['manager_of_class']
  316. orm_base.instance_state = attributes.instance_state = \
  317. orm_instrumentation.instance_state = instance_state
  318. orm_base.instance_dict = attributes.instance_dict = \
  319. orm_instrumentation.instance_dict = instance_dict
  320. orm_base.manager_of_class = attributes.manager_of_class = \
  321. orm_instrumentation.manager_of_class = manager_of_class