summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm/path_registry.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sqlalchemy/orm/path_registry.py')
-rw-r--r--lib/sqlalchemy/orm/path_registry.py70
1 files changed, 37 insertions, 33 deletions
diff --git a/lib/sqlalchemy/orm/path_registry.py b/lib/sqlalchemy/orm/path_registry.py
index bb4e2eda5..f33c209cc 100644
--- a/lib/sqlalchemy/orm/path_registry.py
+++ b/lib/sqlalchemy/orm/path_registry.py
@@ -56,8 +56,7 @@ class PathRegistry(object):
is_root = False
def __eq__(self, other):
- return other is not None and \
- self.path == other.path
+ return other is not None and self.path == other.path
def set(self, attributes, key, value):
log.debug("set '%s' on path '%s' to '%s'", key, self, value)
@@ -87,11 +86,8 @@ class PathRegistry(object):
yield path[i], path[i + 1]
def contains_mapper(self, mapper):
- for path_mapper in [
- self.path[i] for i in range(0, len(self.path), 2)
- ]:
- if path_mapper.is_mapper and \
- path_mapper.isa(mapper):
+ for path_mapper in [self.path[i] for i in range(0, len(self.path), 2)]:
+ if path_mapper.is_mapper and path_mapper.isa(mapper):
return True
else:
return False
@@ -100,40 +96,49 @@ class PathRegistry(object):
return (key, self.path) in attributes
def __reduce__(self):
- return _unreduce_path, (self.serialize(), )
+ return _unreduce_path, (self.serialize(),)
def serialize(self):
path = self.path
- return list(zip(
- [m.class_ for m in [path[i] for i in range(0, len(path), 2)]],
- [path[i].key for i in range(1, len(path), 2)] + [None]
- ))
+ return list(
+ zip(
+ [m.class_ for m in [path[i] for i in range(0, len(path), 2)]],
+ [path[i].key for i in range(1, len(path), 2)] + [None],
+ )
+ )
@classmethod
def deserialize(cls, path):
if path is None:
return None
- p = tuple(chain(*[(class_mapper(mcls),
- class_mapper(mcls).attrs[key]
- if key is not None else None)
- for mcls, key in path]))
+ p = tuple(
+ chain(
+ *[
+ (
+ class_mapper(mcls),
+ class_mapper(mcls).attrs[key]
+ if key is not None
+ else None,
+ )
+ for mcls, key in path
+ ]
+ )
+ )
if p and p[-1] is None:
p = p[0:-1]
return cls.coerce(p)
@classmethod
def per_mapper(cls, mapper):
- return EntityRegistry(
- cls.root, mapper
- )
+ return EntityRegistry(cls.root, mapper)
@classmethod
def coerce(cls, raw):
return util.reduce(lambda prev, next: prev[next], raw, cls.root)
def token(self, token):
- if token.endswith(':' + _WILDCARD_TOKEN):
+ if token.endswith(":" + _WILDCARD_TOKEN):
return TokenRegistry(self, token)
elif token.endswith(":" + _DEFAULT_TOKEN):
return TokenRegistry(self.root, token)
@@ -141,12 +146,10 @@ class PathRegistry(object):
raise exc.ArgumentError("invalid token: %s" % token)
def __add__(self, other):
- return util.reduce(
- lambda prev, next: prev[next],
- other.path, self)
+ return util.reduce(lambda prev, next: prev[next], other.path, self)
def __repr__(self):
- return "%s(%r)" % (self.__class__.__name__, self.path, )
+ return "%s(%r)" % (self.__class__.__name__, self.path)
class RootRegistry(PathRegistry):
@@ -154,6 +157,7 @@ class RootRegistry(PathRegistry):
paths are maintained per-root-mapper.
"""
+
path = ()
has_entity = False
is_aliased_class = False
@@ -162,6 +166,7 @@ class RootRegistry(PathRegistry):
def __getitem__(self, entity):
return entity._path_registry
+
PathRegistry.root = RootRegistry()
@@ -194,8 +199,10 @@ class PropRegistry(PathRegistry):
if not insp.is_aliased_class or insp._use_mapper_path:
parent = parent.parent[prop.parent]
elif insp.is_aliased_class and insp.with_polymorphic_mappers:
- if prop.parent is not insp.mapper and \
- prop.parent in insp.with_polymorphic_mappers:
+ if (
+ prop.parent is not insp.mapper
+ and prop.parent in insp.with_polymorphic_mappers
+ ):
subclass_entity = parent[-1]._entity_for_mapper(prop.parent)
parent = parent.parent[subclass_entity]
@@ -205,15 +212,13 @@ class PropRegistry(PathRegistry):
self._wildcard_path_loader_key = (
"loader",
- self.parent.path + self.prop._wildcard_token
+ self.parent.path + self.prop._wildcard_token,
)
self._default_path_loader_key = self.prop._default_path_loader_key
self._loader_key = ("loader", self.path)
def __str__(self):
- return " -> ".join(
- str(elem) for elem in self.path
- )
+ return " -> ".join(str(elem) for elem in self.path)
@util.memoized_property
def has_entity(self):
@@ -235,9 +240,7 @@ class PropRegistry(PathRegistry):
if isinstance(entity, (int, slice)):
return self.path[entity]
else:
- return EntityRegistry(
- self, entity
- )
+ return EntityRegistry(self, entity)
class EntityRegistry(PathRegistry, dict):
@@ -258,6 +261,7 @@ class EntityRegistry(PathRegistry, dict):
def __bool__(self):
return True
+
__nonzero__ = __bool__
def __getitem__(self, entity):