instrumentation.py 14 KB

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