Track loaders and states for "post load" operations.
| 1534 | |
| 1535 | |
| 1536 | class _PostLoad: |
| 1537 | """Track loaders and states for "post load" operations.""" |
| 1538 | |
| 1539 | __slots__ = "loaders", "states", "load_keys" |
| 1540 | |
| 1541 | def __init__(self): |
| 1542 | self.loaders = {} |
| 1543 | self.states = util.OrderedDict() |
| 1544 | self.load_keys = None |
| 1545 | |
| 1546 | def add_state(self, state, overwrite): |
| 1547 | # the states for a polymorphic load here are all shared |
| 1548 | # within a single PostLoad object among multiple subtypes. |
| 1549 | # Filtering of callables on a per-subclass basis needs to be done at |
| 1550 | # the invocation level |
| 1551 | self.states[state] = overwrite |
| 1552 | |
| 1553 | def invoke(self, context, path): |
| 1554 | if not self.states: |
| 1555 | return |
| 1556 | path = path_registry.PathRegistry.coerce(path) |
| 1557 | for ( |
| 1558 | effective_context, |
| 1559 | token, |
| 1560 | limit_to_mapper, |
| 1561 | loader, |
| 1562 | arg, |
| 1563 | kw, |
| 1564 | ) in self.loaders.values(): |
| 1565 | states = [ |
| 1566 | (state, overwrite) |
| 1567 | for state, overwrite in self.states.items() |
| 1568 | if state.manager.mapper.isa(limit_to_mapper) |
| 1569 | ] |
| 1570 | if states: |
| 1571 | loader( |
| 1572 | effective_context, path, states, self.load_keys, *arg, **kw |
| 1573 | ) |
| 1574 | self.states.clear() |
| 1575 | |
| 1576 | @classmethod |
| 1577 | def for_context(cls, context, path, only_load_props): |
| 1578 | pl = context.post_load_paths.get(path.path) |
| 1579 | if pl is not None and only_load_props: |
| 1580 | pl.load_keys = only_load_props |
| 1581 | return pl |
| 1582 | |
| 1583 | @classmethod |
| 1584 | def path_exists(self, context, path, key): |
| 1585 | return ( |
| 1586 | path.path in context.post_load_paths |
| 1587 | and key in context.post_load_paths[path.path].loaders |
| 1588 | ) |
| 1589 | |
| 1590 | @classmethod |
| 1591 | def callable_for_path( |
| 1592 | cls, context, path, limit_to_mapper, token, loader_callable, *arg, **kw |
| 1593 | ): |