mutable.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. # ext/mutable.py
  2. # Copyright (C) 2005-2022 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. r"""Provide support for tracking of in-place changes to scalar values,
  8. which are propagated into ORM change events on owning parent objects.
  9. .. _mutable_scalars:
  10. Establishing Mutability on Scalar Column Values
  11. ===============================================
  12. A typical example of a "mutable" structure is a Python dictionary.
  13. Following the example introduced in :ref:`types_toplevel`, we
  14. begin with a custom type that marshals Python dictionaries into
  15. JSON strings before being persisted::
  16. from sqlalchemy.types import TypeDecorator, VARCHAR
  17. import json
  18. class JSONEncodedDict(TypeDecorator):
  19. "Represents an immutable structure as a json-encoded string."
  20. impl = VARCHAR
  21. def process_bind_param(self, value, dialect):
  22. if value is not None:
  23. value = json.dumps(value)
  24. return value
  25. def process_result_value(self, value, dialect):
  26. if value is not None:
  27. value = json.loads(value)
  28. return value
  29. The usage of ``json`` is only for the purposes of example. The
  30. :mod:`sqlalchemy.ext.mutable` extension can be used
  31. with any type whose target Python type may be mutable, including
  32. :class:`.PickleType`, :class:`_postgresql.ARRAY`, etc.
  33. When using the :mod:`sqlalchemy.ext.mutable` extension, the value itself
  34. tracks all parents which reference it. Below, we illustrate a simple
  35. version of the :class:`.MutableDict` dictionary object, which applies
  36. the :class:`.Mutable` mixin to a plain Python dictionary::
  37. from sqlalchemy.ext.mutable import Mutable
  38. class MutableDict(Mutable, dict):
  39. @classmethod
  40. def coerce(cls, key, value):
  41. "Convert plain dictionaries to MutableDict."
  42. if not isinstance(value, MutableDict):
  43. if isinstance(value, dict):
  44. return MutableDict(value)
  45. # this call will raise ValueError
  46. return Mutable.coerce(key, value)
  47. else:
  48. return value
  49. def __setitem__(self, key, value):
  50. "Detect dictionary set events and emit change events."
  51. dict.__setitem__(self, key, value)
  52. self.changed()
  53. def __delitem__(self, key):
  54. "Detect dictionary del events and emit change events."
  55. dict.__delitem__(self, key)
  56. self.changed()
  57. The above dictionary class takes the approach of subclassing the Python
  58. built-in ``dict`` to produce a dict
  59. subclass which routes all mutation events through ``__setitem__``. There are
  60. variants on this approach, such as subclassing ``UserDict.UserDict`` or
  61. ``collections.MutableMapping``; the part that's important to this example is
  62. that the :meth:`.Mutable.changed` method is called whenever an in-place
  63. change to the datastructure takes place.
  64. We also redefine the :meth:`.Mutable.coerce` method which will be used to
  65. convert any values that are not instances of ``MutableDict``, such
  66. as the plain dictionaries returned by the ``json`` module, into the
  67. appropriate type. Defining this method is optional; we could just as well
  68. created our ``JSONEncodedDict`` such that it always returns an instance
  69. of ``MutableDict``, and additionally ensured that all calling code
  70. uses ``MutableDict`` explicitly. When :meth:`.Mutable.coerce` is not
  71. overridden, any values applied to a parent object which are not instances
  72. of the mutable type will raise a ``ValueError``.
  73. Our new ``MutableDict`` type offers a class method
  74. :meth:`~.Mutable.as_mutable` which we can use within column metadata
  75. to associate with types. This method grabs the given type object or
  76. class and associates a listener that will detect all future mappings
  77. of this type, applying event listening instrumentation to the mapped
  78. attribute. Such as, with classical table metadata::
  79. from sqlalchemy import Table, Column, Integer
  80. my_data = Table('my_data', metadata,
  81. Column('id', Integer, primary_key=True),
  82. Column('data', MutableDict.as_mutable(JSONEncodedDict))
  83. )
  84. Above, :meth:`~.Mutable.as_mutable` returns an instance of ``JSONEncodedDict``
  85. (if the type object was not an instance already), which will intercept any
  86. attributes which are mapped against this type. Below we establish a simple
  87. mapping against the ``my_data`` table::
  88. from sqlalchemy import mapper
  89. class MyDataClass(object):
  90. pass
  91. # associates mutation listeners with MyDataClass.data
  92. mapper(MyDataClass, my_data)
  93. The ``MyDataClass.data`` member will now be notified of in place changes
  94. to its value.
  95. There's no difference in usage when using declarative::
  96. from sqlalchemy.ext.declarative import declarative_base
  97. Base = declarative_base()
  98. class MyDataClass(Base):
  99. __tablename__ = 'my_data'
  100. id = Column(Integer, primary_key=True)
  101. data = Column(MutableDict.as_mutable(JSONEncodedDict))
  102. Any in-place changes to the ``MyDataClass.data`` member
  103. will flag the attribute as "dirty" on the parent object::
  104. >>> from sqlalchemy.orm import Session
  105. >>> sess = Session()
  106. >>> m1 = MyDataClass(data={'value1':'foo'})
  107. >>> sess.add(m1)
  108. >>> sess.commit()
  109. >>> m1.data['value1'] = 'bar'
  110. >>> assert m1 in sess.dirty
  111. True
  112. The ``MutableDict`` can be associated with all future instances
  113. of ``JSONEncodedDict`` in one step, using
  114. :meth:`~.Mutable.associate_with`. This is similar to
  115. :meth:`~.Mutable.as_mutable` except it will intercept all occurrences
  116. of ``MutableDict`` in all mappings unconditionally, without
  117. the need to declare it individually::
  118. MutableDict.associate_with(JSONEncodedDict)
  119. class MyDataClass(Base):
  120. __tablename__ = 'my_data'
  121. id = Column(Integer, primary_key=True)
  122. data = Column(JSONEncodedDict)
  123. Supporting Pickling
  124. --------------------
  125. The key to the :mod:`sqlalchemy.ext.mutable` extension relies upon the
  126. placement of a ``weakref.WeakKeyDictionary`` upon the value object, which
  127. stores a mapping of parent mapped objects keyed to the attribute name under
  128. which they are associated with this value. ``WeakKeyDictionary`` objects are
  129. not picklable, due to the fact that they contain weakrefs and function
  130. callbacks. In our case, this is a good thing, since if this dictionary were
  131. picklable, it could lead to an excessively large pickle size for our value
  132. objects that are pickled by themselves outside of the context of the parent.
  133. The developer responsibility here is only to provide a ``__getstate__`` method
  134. that excludes the :meth:`~MutableBase._parents` collection from the pickle
  135. stream::
  136. class MyMutableType(Mutable):
  137. def __getstate__(self):
  138. d = self.__dict__.copy()
  139. d.pop('_parents', None)
  140. return d
  141. With our dictionary example, we need to return the contents of the dict itself
  142. (and also restore them on __setstate__)::
  143. class MutableDict(Mutable, dict):
  144. # ....
  145. def __getstate__(self):
  146. return dict(self)
  147. def __setstate__(self, state):
  148. self.update(state)
  149. In the case that our mutable value object is pickled as it is attached to one
  150. or more parent objects that are also part of the pickle, the :class:`.Mutable`
  151. mixin will re-establish the :attr:`.Mutable._parents` collection on each value
  152. object as the owning parents themselves are unpickled.
  153. Receiving Events
  154. ----------------
  155. The :meth:`.AttributeEvents.modified` event handler may be used to receive
  156. an event when a mutable scalar emits a change event. This event handler
  157. is called when the :func:`.attributes.flag_modified` function is called
  158. from within the mutable extension::
  159. from sqlalchemy.ext.declarative import declarative_base
  160. from sqlalchemy import event
  161. Base = declarative_base()
  162. class MyDataClass(Base):
  163. __tablename__ = 'my_data'
  164. id = Column(Integer, primary_key=True)
  165. data = Column(MutableDict.as_mutable(JSONEncodedDict))
  166. @event.listens_for(MyDataClass.data, "modified")
  167. def modified_json(instance):
  168. print("json value modified:", instance.data)
  169. .. _mutable_composites:
  170. Establishing Mutability on Composites
  171. =====================================
  172. Composites are a special ORM feature which allow a single scalar attribute to
  173. be assigned an object value which represents information "composed" from one
  174. or more columns from the underlying mapped table. The usual example is that of
  175. a geometric "point", and is introduced in :ref:`mapper_composite`.
  176. As is the case with :class:`.Mutable`, the user-defined composite class
  177. subclasses :class:`.MutableComposite` as a mixin, and detects and delivers
  178. change events to its parents via the :meth:`.MutableComposite.changed` method.
  179. In the case of a composite class, the detection is usually via the usage of
  180. Python descriptors (i.e. ``@property``), or alternatively via the special
  181. Python method ``__setattr__()``. Below we expand upon the ``Point`` class
  182. introduced in :ref:`mapper_composite` to subclass :class:`.MutableComposite`
  183. and to also route attribute set events via ``__setattr__`` to the
  184. :meth:`.MutableComposite.changed` method::
  185. from sqlalchemy.ext.mutable import MutableComposite
  186. class Point(MutableComposite):
  187. def __init__(self, x, y):
  188. self.x = x
  189. self.y = y
  190. def __setattr__(self, key, value):
  191. "Intercept set events"
  192. # set the attribute
  193. object.__setattr__(self, key, value)
  194. # alert all parents to the change
  195. self.changed()
  196. def __composite_values__(self):
  197. return self.x, self.y
  198. def __eq__(self, other):
  199. return isinstance(other, Point) and \
  200. other.x == self.x and \
  201. other.y == self.y
  202. def __ne__(self, other):
  203. return not self.__eq__(other)
  204. The :class:`.MutableComposite` class uses a Python metaclass to automatically
  205. establish listeners for any usage of :func:`_orm.composite` that specifies our
  206. ``Point`` type. Below, when ``Point`` is mapped to the ``Vertex`` class,
  207. listeners are established which will route change events from ``Point``
  208. objects to each of the ``Vertex.start`` and ``Vertex.end`` attributes::
  209. from sqlalchemy.orm import composite, mapper
  210. from sqlalchemy import Table, Column
  211. vertices = Table('vertices', metadata,
  212. Column('id', Integer, primary_key=True),
  213. Column('x1', Integer),
  214. Column('y1', Integer),
  215. Column('x2', Integer),
  216. Column('y2', Integer),
  217. )
  218. class Vertex(object):
  219. pass
  220. mapper(Vertex, vertices, properties={
  221. 'start': composite(Point, vertices.c.x1, vertices.c.y1),
  222. 'end': composite(Point, vertices.c.x2, vertices.c.y2)
  223. })
  224. Any in-place changes to the ``Vertex.start`` or ``Vertex.end`` members
  225. will flag the attribute as "dirty" on the parent object::
  226. >>> from sqlalchemy.orm import Session
  227. >>> sess = Session()
  228. >>> v1 = Vertex(start=Point(3, 4), end=Point(12, 15))
  229. >>> sess.add(v1)
  230. >>> sess.commit()
  231. >>> v1.end.x = 8
  232. >>> assert v1 in sess.dirty
  233. True
  234. Coercing Mutable Composites
  235. ---------------------------
  236. The :meth:`.MutableBase.coerce` method is also supported on composite types.
  237. In the case of :class:`.MutableComposite`, the :meth:`.MutableBase.coerce`
  238. method is only called for attribute set operations, not load operations.
  239. Overriding the :meth:`.MutableBase.coerce` method is essentially equivalent
  240. to using a :func:`.validates` validation routine for all attributes which
  241. make use of the custom composite type::
  242. class Point(MutableComposite):
  243. # other Point methods
  244. # ...
  245. def coerce(cls, key, value):
  246. if isinstance(value, tuple):
  247. value = Point(*value)
  248. elif not isinstance(value, Point):
  249. raise ValueError("tuple or Point expected")
  250. return value
  251. Supporting Pickling
  252. --------------------
  253. As is the case with :class:`.Mutable`, the :class:`.MutableComposite` helper
  254. class uses a ``weakref.WeakKeyDictionary`` available via the
  255. :meth:`MutableBase._parents` attribute which isn't picklable. If we need to
  256. pickle instances of ``Point`` or its owning class ``Vertex``, we at least need
  257. to define a ``__getstate__`` that doesn't include the ``_parents`` dictionary.
  258. Below we define both a ``__getstate__`` and a ``__setstate__`` that package up
  259. the minimal form of our ``Point`` class::
  260. class Point(MutableComposite):
  261. # ...
  262. def __getstate__(self):
  263. return self.x, self.y
  264. def __setstate__(self, state):
  265. self.x, self.y = state
  266. As with :class:`.Mutable`, the :class:`.MutableComposite` augments the
  267. pickling process of the parent's object-relational state so that the
  268. :meth:`MutableBase._parents` collection is restored to all ``Point`` objects.
  269. """
  270. import weakref
  271. from .. import event
  272. from .. import inspect
  273. from .. import types
  274. from ..orm import Mapper
  275. from ..orm import mapper
  276. from ..orm.attributes import flag_modified
  277. from ..sql.base import SchemaEventTarget
  278. from ..util import memoized_property
  279. class MutableBase(object):
  280. """Common base class to :class:`.Mutable`
  281. and :class:`.MutableComposite`.
  282. """
  283. @memoized_property
  284. def _parents(self):
  285. """Dictionary of parent object's :class:`.InstanceState`->attribute
  286. name on the parent.
  287. This attribute is a so-called "memoized" property. It initializes
  288. itself with a new ``weakref.WeakKeyDictionary`` the first time
  289. it is accessed, returning the same object upon subsequent access.
  290. .. versionchanged:: 1.4 the :class:`.InstanceState` is now used
  291. as the key in the weak dictionary rather than the instance
  292. itself.
  293. """
  294. return weakref.WeakKeyDictionary()
  295. @classmethod
  296. def coerce(cls, key, value):
  297. """Given a value, coerce it into the target type.
  298. Can be overridden by custom subclasses to coerce incoming
  299. data into a particular type.
  300. By default, raises ``ValueError``.
  301. This method is called in different scenarios depending on if
  302. the parent class is of type :class:`.Mutable` or of type
  303. :class:`.MutableComposite`. In the case of the former, it is called
  304. for both attribute-set operations as well as during ORM loading
  305. operations. For the latter, it is only called during attribute-set
  306. operations; the mechanics of the :func:`.composite` construct
  307. handle coercion during load operations.
  308. :param key: string name of the ORM-mapped attribute being set.
  309. :param value: the incoming value.
  310. :return: the method should return the coerced value, or raise
  311. ``ValueError`` if the coercion cannot be completed.
  312. """
  313. if value is None:
  314. return None
  315. msg = "Attribute '%s' does not accept objects of type %s"
  316. raise ValueError(msg % (key, type(value)))
  317. @classmethod
  318. def _get_listen_keys(cls, attribute):
  319. """Given a descriptor attribute, return a ``set()`` of the attribute
  320. keys which indicate a change in the state of this attribute.
  321. This is normally just ``set([attribute.key])``, but can be overridden
  322. to provide for additional keys. E.g. a :class:`.MutableComposite`
  323. augments this set with the attribute keys associated with the columns
  324. that comprise the composite value.
  325. This collection is consulted in the case of intercepting the
  326. :meth:`.InstanceEvents.refresh` and
  327. :meth:`.InstanceEvents.refresh_flush` events, which pass along a list
  328. of attribute names that have been refreshed; the list is compared
  329. against this set to determine if action needs to be taken.
  330. .. versionadded:: 1.0.5
  331. """
  332. return {attribute.key}
  333. @classmethod
  334. def _listen_on_attribute(cls, attribute, coerce, parent_cls):
  335. """Establish this type as a mutation listener for the given
  336. mapped descriptor.
  337. """
  338. key = attribute.key
  339. if parent_cls is not attribute.class_:
  340. return
  341. # rely on "propagate" here
  342. parent_cls = attribute.class_
  343. listen_keys = cls._get_listen_keys(attribute)
  344. def load(state, *args):
  345. """Listen for objects loaded or refreshed.
  346. Wrap the target data member's value with
  347. ``Mutable``.
  348. """
  349. val = state.dict.get(key, None)
  350. if val is not None:
  351. if coerce:
  352. val = cls.coerce(key, val)
  353. state.dict[key] = val
  354. val._parents[state] = key
  355. def load_attrs(state, ctx, attrs):
  356. if not attrs or listen_keys.intersection(attrs):
  357. load(state)
  358. def set_(target, value, oldvalue, initiator):
  359. """Listen for set/replace events on the target
  360. data member.
  361. Establish a weak reference to the parent object
  362. on the incoming value, remove it for the one
  363. outgoing.
  364. """
  365. if value is oldvalue:
  366. return value
  367. if not isinstance(value, cls):
  368. value = cls.coerce(key, value)
  369. if value is not None:
  370. value._parents[target] = key
  371. if isinstance(oldvalue, cls):
  372. oldvalue._parents.pop(inspect(target), None)
  373. return value
  374. def pickle(state, state_dict):
  375. val = state.dict.get(key, None)
  376. if val is not None:
  377. if "ext.mutable.values" not in state_dict:
  378. state_dict["ext.mutable.values"] = []
  379. state_dict["ext.mutable.values"].append(val)
  380. def unpickle(state, state_dict):
  381. if "ext.mutable.values" in state_dict:
  382. for val in state_dict["ext.mutable.values"]:
  383. val._parents[state] = key
  384. event.listen(parent_cls, "load", load, raw=True, propagate=True)
  385. event.listen(
  386. parent_cls, "refresh", load_attrs, raw=True, propagate=True
  387. )
  388. event.listen(
  389. parent_cls, "refresh_flush", load_attrs, raw=True, propagate=True
  390. )
  391. event.listen(
  392. attribute, "set", set_, raw=True, retval=True, propagate=True
  393. )
  394. event.listen(parent_cls, "pickle", pickle, raw=True, propagate=True)
  395. event.listen(
  396. parent_cls, "unpickle", unpickle, raw=True, propagate=True
  397. )
  398. class Mutable(MutableBase):
  399. """Mixin that defines transparent propagation of change
  400. events to a parent object.
  401. See the example in :ref:`mutable_scalars` for usage information.
  402. """
  403. def changed(self):
  404. """Subclasses should call this method whenever change events occur."""
  405. for parent, key in self._parents.items():
  406. flag_modified(parent.obj(), key)
  407. @classmethod
  408. def associate_with_attribute(cls, attribute):
  409. """Establish this type as a mutation listener for the given
  410. mapped descriptor.
  411. """
  412. cls._listen_on_attribute(attribute, True, attribute.class_)
  413. @classmethod
  414. def associate_with(cls, sqltype):
  415. """Associate this wrapper with all future mapped columns
  416. of the given type.
  417. This is a convenience method that calls
  418. ``associate_with_attribute`` automatically.
  419. .. warning::
  420. The listeners established by this method are *global*
  421. to all mappers, and are *not* garbage collected. Only use
  422. :meth:`.associate_with` for types that are permanent to an
  423. application, not with ad-hoc types else this will cause unbounded
  424. growth in memory usage.
  425. """
  426. def listen_for_type(mapper, class_):
  427. if mapper.non_primary:
  428. return
  429. for prop in mapper.column_attrs:
  430. if isinstance(prop.columns[0].type, sqltype):
  431. cls.associate_with_attribute(getattr(class_, prop.key))
  432. event.listen(mapper, "mapper_configured", listen_for_type)
  433. @classmethod
  434. def as_mutable(cls, sqltype):
  435. """Associate a SQL type with this mutable Python type.
  436. This establishes listeners that will detect ORM mappings against
  437. the given type, adding mutation event trackers to those mappings.
  438. The type is returned, unconditionally as an instance, so that
  439. :meth:`.as_mutable` can be used inline::
  440. Table('mytable', metadata,
  441. Column('id', Integer, primary_key=True),
  442. Column('data', MyMutableType.as_mutable(PickleType))
  443. )
  444. Note that the returned type is always an instance, even if a class
  445. is given, and that only columns which are declared specifically with
  446. that type instance receive additional instrumentation.
  447. To associate a particular mutable type with all occurrences of a
  448. particular type, use the :meth:`.Mutable.associate_with` classmethod
  449. of the particular :class:`.Mutable` subclass to establish a global
  450. association.
  451. .. warning::
  452. The listeners established by this method are *global*
  453. to all mappers, and are *not* garbage collected. Only use
  454. :meth:`.as_mutable` for types that are permanent to an application,
  455. not with ad-hoc types else this will cause unbounded growth
  456. in memory usage.
  457. """
  458. sqltype = types.to_instance(sqltype)
  459. # a SchemaType will be copied when the Column is copied,
  460. # and we'll lose our ability to link that type back to the original.
  461. # so track our original type w/ columns
  462. if isinstance(sqltype, SchemaEventTarget):
  463. @event.listens_for(sqltype, "before_parent_attach")
  464. def _add_column_memo(sqltyp, parent):
  465. parent.info["_ext_mutable_orig_type"] = sqltyp
  466. schema_event_check = True
  467. else:
  468. schema_event_check = False
  469. def listen_for_type(mapper, class_):
  470. if mapper.non_primary:
  471. return
  472. for prop in mapper.column_attrs:
  473. if (
  474. schema_event_check
  475. and hasattr(prop.expression, "info")
  476. and prop.expression.info.get("_ext_mutable_orig_type")
  477. is sqltype
  478. ) or (prop.columns[0].type is sqltype):
  479. cls.associate_with_attribute(getattr(class_, prop.key))
  480. event.listen(mapper, "mapper_configured", listen_for_type)
  481. return sqltype
  482. class MutableComposite(MutableBase):
  483. """Mixin that defines transparent propagation of change
  484. events on a SQLAlchemy "composite" object to its
  485. owning parent or parents.
  486. See the example in :ref:`mutable_composites` for usage information.
  487. """
  488. @classmethod
  489. def _get_listen_keys(cls, attribute):
  490. return {attribute.key}.union(attribute.property._attribute_keys)
  491. def changed(self):
  492. """Subclasses should call this method whenever change events occur."""
  493. for parent, key in self._parents.items():
  494. prop = parent.mapper.get_property(key)
  495. for value, attr_name in zip(
  496. self.__composite_values__(), prop._attribute_keys
  497. ):
  498. setattr(parent.obj(), attr_name, value)
  499. def _setup_composite_listener():
  500. def _listen_for_type(mapper, class_):
  501. for prop in mapper.iterate_properties:
  502. if (
  503. hasattr(prop, "composite_class")
  504. and isinstance(prop.composite_class, type)
  505. and issubclass(prop.composite_class, MutableComposite)
  506. ):
  507. prop.composite_class._listen_on_attribute(
  508. getattr(class_, prop.key), False, class_
  509. )
  510. if not event.contains(Mapper, "mapper_configured", _listen_for_type):
  511. event.listen(Mapper, "mapper_configured", _listen_for_type)
  512. _setup_composite_listener()
  513. class MutableDict(Mutable, dict):
  514. """A dictionary type that implements :class:`.Mutable`.
  515. The :class:`.MutableDict` object implements a dictionary that will
  516. emit change events to the underlying mapping when the contents of
  517. the dictionary are altered, including when values are added or removed.
  518. Note that :class:`.MutableDict` does **not** apply mutable tracking to the
  519. *values themselves* inside the dictionary. Therefore it is not a sufficient
  520. solution for the use case of tracking deep changes to a *recursive*
  521. dictionary structure, such as a JSON structure. To support this use case,
  522. build a subclass of :class:`.MutableDict` that provides appropriate
  523. coercion to the values placed in the dictionary so that they too are
  524. "mutable", and emit events up to their parent structure.
  525. .. seealso::
  526. :class:`.MutableList`
  527. :class:`.MutableSet`
  528. """
  529. def __setitem__(self, key, value):
  530. """Detect dictionary set events and emit change events."""
  531. dict.__setitem__(self, key, value)
  532. self.changed()
  533. def setdefault(self, key, value):
  534. result = dict.setdefault(self, key, value)
  535. self.changed()
  536. return result
  537. def __delitem__(self, key):
  538. """Detect dictionary del events and emit change events."""
  539. dict.__delitem__(self, key)
  540. self.changed()
  541. def update(self, *a, **kw):
  542. dict.update(self, *a, **kw)
  543. self.changed()
  544. def pop(self, *arg):
  545. result = dict.pop(self, *arg)
  546. self.changed()
  547. return result
  548. def popitem(self):
  549. result = dict.popitem(self)
  550. self.changed()
  551. return result
  552. def clear(self):
  553. dict.clear(self)
  554. self.changed()
  555. @classmethod
  556. def coerce(cls, key, value):
  557. """Convert plain dictionary to instance of this class."""
  558. if not isinstance(value, cls):
  559. if isinstance(value, dict):
  560. return cls(value)
  561. return Mutable.coerce(key, value)
  562. else:
  563. return value
  564. def __getstate__(self):
  565. return dict(self)
  566. def __setstate__(self, state):
  567. self.update(state)
  568. class MutableList(Mutable, list):
  569. """A list type that implements :class:`.Mutable`.
  570. The :class:`.MutableList` object implements a list that will
  571. emit change events to the underlying mapping when the contents of
  572. the list are altered, including when values are added or removed.
  573. Note that :class:`.MutableList` does **not** apply mutable tracking to the
  574. *values themselves* inside the list. Therefore it is not a sufficient
  575. solution for the use case of tracking deep changes to a *recursive*
  576. mutable structure, such as a JSON structure. To support this use case,
  577. build a subclass of :class:`.MutableList` that provides appropriate
  578. coercion to the values placed in the dictionary so that they too are
  579. "mutable", and emit events up to their parent structure.
  580. .. versionadded:: 1.1
  581. .. seealso::
  582. :class:`.MutableDict`
  583. :class:`.MutableSet`
  584. """
  585. def __reduce_ex__(self, proto):
  586. return (self.__class__, (list(self),))
  587. # needed for backwards compatibility with
  588. # older pickles
  589. def __setstate__(self, state):
  590. self[:] = state
  591. def __setitem__(self, index, value):
  592. """Detect list set events and emit change events."""
  593. list.__setitem__(self, index, value)
  594. self.changed()
  595. def __setslice__(self, start, end, value):
  596. """Detect list set events and emit change events."""
  597. list.__setslice__(self, start, end, value)
  598. self.changed()
  599. def __delitem__(self, index):
  600. """Detect list del events and emit change events."""
  601. list.__delitem__(self, index)
  602. self.changed()
  603. def __delslice__(self, start, end):
  604. """Detect list del events and emit change events."""
  605. list.__delslice__(self, start, end)
  606. self.changed()
  607. def pop(self, *arg):
  608. result = list.pop(self, *arg)
  609. self.changed()
  610. return result
  611. def append(self, x):
  612. list.append(self, x)
  613. self.changed()
  614. def extend(self, x):
  615. list.extend(self, x)
  616. self.changed()
  617. def __iadd__(self, x):
  618. self.extend(x)
  619. return self
  620. def insert(self, i, x):
  621. list.insert(self, i, x)
  622. self.changed()
  623. def remove(self, i):
  624. list.remove(self, i)
  625. self.changed()
  626. def clear(self):
  627. list.clear(self)
  628. self.changed()
  629. def sort(self, **kw):
  630. list.sort(self, **kw)
  631. self.changed()
  632. def reverse(self):
  633. list.reverse(self)
  634. self.changed()
  635. @classmethod
  636. def coerce(cls, index, value):
  637. """Convert plain list to instance of this class."""
  638. if not isinstance(value, cls):
  639. if isinstance(value, list):
  640. return cls(value)
  641. return Mutable.coerce(index, value)
  642. else:
  643. return value
  644. class MutableSet(Mutable, set):
  645. """A set type that implements :class:`.Mutable`.
  646. The :class:`.MutableSet` object implements a set that will
  647. emit change events to the underlying mapping when the contents of
  648. the set are altered, including when values are added or removed.
  649. Note that :class:`.MutableSet` does **not** apply mutable tracking to the
  650. *values themselves* inside the set. Therefore it is not a sufficient
  651. solution for the use case of tracking deep changes to a *recursive*
  652. mutable structure. To support this use case,
  653. build a subclass of :class:`.MutableSet` that provides appropriate
  654. coercion to the values placed in the dictionary so that they too are
  655. "mutable", and emit events up to their parent structure.
  656. .. versionadded:: 1.1
  657. .. seealso::
  658. :class:`.MutableDict`
  659. :class:`.MutableList`
  660. """
  661. def update(self, *arg):
  662. set.update(self, *arg)
  663. self.changed()
  664. def intersection_update(self, *arg):
  665. set.intersection_update(self, *arg)
  666. self.changed()
  667. def difference_update(self, *arg):
  668. set.difference_update(self, *arg)
  669. self.changed()
  670. def symmetric_difference_update(self, *arg):
  671. set.symmetric_difference_update(self, *arg)
  672. self.changed()
  673. def __ior__(self, other):
  674. self.update(other)
  675. return self
  676. def __iand__(self, other):
  677. self.intersection_update(other)
  678. return self
  679. def __ixor__(self, other):
  680. self.symmetric_difference_update(other)
  681. return self
  682. def __isub__(self, other):
  683. self.difference_update(other)
  684. return self
  685. def add(self, elem):
  686. set.add(self, elem)
  687. self.changed()
  688. def remove(self, elem):
  689. set.remove(self, elem)
  690. self.changed()
  691. def discard(self, elem):
  692. set.discard(self, elem)
  693. self.changed()
  694. def pop(self, *arg):
  695. result = set.pop(self, *arg)
  696. self.changed()
  697. return result
  698. def clear(self):
  699. set.clear(self)
  700. self.changed()
  701. @classmethod
  702. def coerce(cls, index, value):
  703. """Convert plain set to instance of this class."""
  704. if not isinstance(value, cls):
  705. if isinstance(value, set):
  706. return cls(value)
  707. return Mutable.coerce(index, value)
  708. else:
  709. return value
  710. def __getstate__(self):
  711. return set(self)
  712. def __setstate__(self, state):
  713. self.update(state)
  714. def __reduce_ex__(self, proto):
  715. return (self.__class__, (list(self),))