def getParent(obj): """ Returns the container the object was traversed via. See `ITraversalAPI` for details. """ try: location_info = ILocationInfo(obj) except TypeError: pass else: return location_info.getParent() # XXX Keep the old implementation as the fallback behaviour in the case # that obj doesn't have a location parent. This seems advisable as the # 'parent' is sometimes taken to mean the traversal parent, and the # __parent__ attribute is used for both. if IRoot.providedBy(obj): return None parent = getattr(obj, '__parent__', None) if parent is not None: return parent raise TypeError("Not enough context information to get parent", obj)
def getParent(obj): """Returns the container the object was traversed via. Returns None if the object is a containment root. Raises TypeError if the object doesn't have enough context to get the parent. """ try: location_info = ILocationInfo(obj) except TypeError: pass else: return location_info.getParent() # XXX Keep the old implementation as the fallback behaviour in the case # that obj doesn't have a location parent. This seems advisable as the # 'parent' is sometimes taken to mean the traversal parent, and the # __parent__ attribute is used for both. if IRoot.providedBy(obj): return None parent = getattr(obj, '__parent__', None) if parent is not None: return parent raise TypeError("Not enough context information to get parent", obj)
def getRoot(self): """Get the root location for a location. See ILocationInfo The root location is a location that contains the given location and that implements IContainmentRoot. >>> root = Location() >>> zope.interface.directlyProvides(root, IRoot) >>> LocationPhysicallyLocatable(root).getRoot() is root True >>> o1 = Location(); o1.__parent__ = root >>> LocationPhysicallyLocatable(o1).getRoot() is root True >>> o2 = Location(); o2.__parent__ = o1 >>> LocationPhysicallyLocatable(o2).getRoot() is root True We'll get a TypeError if we try to get the location fo a rootless object: >>> o1.__parent__ = None >>> LocationPhysicallyLocatable(o1).getRoot() Traceback (most recent call last): ... TypeError: Not enough context to determine location root >>> LocationPhysicallyLocatable(o2).getRoot() Traceback (most recent call last): ... TypeError: Not enough context to determine location root If we screw up and create a location cycle, it will be caught: >>> o1.__parent__ = o2 >>> LocationPhysicallyLocatable(o1).getRoot() Traceback (most recent call last): ... TypeError: Maximum location depth exceeded, """ \ """probably due to a a location cycle. """ context = self.context max = 9999 while context is not None: if IRoot.providedBy(context): return context context = context.__parent__ max -= 1 if max < 1: raise TypeError("Maximum location depth exceeded, " "probably due to a a location cycle.") raise TypeError("Not enough context to determine location root")
def getRoot(self): """See ILocationInfo. """ context = self.context max = 9999 while context is not None: if IRoot.providedBy(context): return context context = context.__parent__ max -= 1 if max < 1: raise TypeError("Maximum location depth exceeded, " "probably due to a a location cycle.") raise TypeError("Not enough context to determine location root")
def getParents(self): """See ILocationInfo. """ # XXX Merge this implementation with getPath. This was refactored # from zope.traversing. parents = [] w = self.context while 1: w = getattr(w, '__parent__', None) if w is None: break parents.append(w) if parents and IRoot.providedBy(parents[-1]): return parents raise TypeError("Not enough context information to get all parents")
def get_parent(obj, default=_marker): """Returns the container the object was traversed via. Returns None if the object is a containment root. Raises TypeError if the object doesn't have enough context to get the parent. """ if IRoot.providedBy(obj): return None parent = aq_parent(aq_inner(obj)) if parent is not None: return parent if default != _marker: return default raise TypeError("Not enough context information to get parent", obj)
def getPath(self): """See ILocationInfo. """ path = [] context = self.context max = 9999 while context is not None: if IRoot.providedBy(context): if path: path.append('') path.reverse() return u'/'.join(path) return u'/' path.append(context.__name__) context = context.__parent__ max -= 1 if max < 1: raise TypeError("Maximum location depth exceeded, " "probably due to a a location cycle.") raise TypeError("Not enough context to determine location root")
def getParents(self): """Returns a list starting with the object's parent followed by each of its parents. Raises a TypeError if the object is not connected to a containment root. >>> root = Location() >>> zope.interface.directlyProvides(root, IRoot) >>> o1 = Location() >>> o2 = Location() >>> o1.__parent__ = root >>> o2.__parent__ = o1 >>> LocationPhysicallyLocatable(o2).getParents() == [o1, root] True If the last parent is not an IRoot object, TypeError will be raised as statet before. >>> zope.interface.noLongerProvides(root, IRoot) >>> LocationPhysicallyLocatable(o2).getParents() Traceback (most recent call last): ... TypeError: Not enough context information to get all parents """ # XXX Merge this implementation with getPath. This was refactored # from zope.traversing. parents = [] w = self.context while 1: w = w.__parent__ if w is None: break parents.append(w) if parents and IRoot.providedBy(parents[-1]): return parents raise TypeError("Not enough context information to get all parents")
def getPath(self): """Get the path of a location. See ILocationInfo This is an "absolute path", rooted at a root object. >>> root = Location() >>> zope.interface.directlyProvides(root, IRoot) >>> LocationPhysicallyLocatable(root).getPath() u'/' >>> o1 = Location(); o1.__parent__ = root; o1.__name__ = 'o1' >>> LocationPhysicallyLocatable(o1).getPath() u'/o1' >>> o2 = Location(); o2.__parent__ = o1; o2.__name__ = u'o2' >>> LocationPhysicallyLocatable(o2).getPath() u'/o1/o2' It is an error to get the path of a rootless location: >>> o1.__parent__ = None >>> LocationPhysicallyLocatable(o1).getPath() Traceback (most recent call last): ... TypeError: Not enough context to determine location root >>> LocationPhysicallyLocatable(o2).getPath() Traceback (most recent call last): ... TypeError: Not enough context to determine location root If we screw up and create a location cycle, it will be caught: >>> o1.__parent__ = o2 >>> LocationPhysicallyLocatable(o1).getPath() Traceback (most recent call last): ... TypeError: Maximum location depth exceeded, """ \ """probably due to a a location cycle. """ path = [] context = self.context max = 9999 while context is not None: if IRoot.providedBy(context): if path: path.append('') path.reverse() return u'/'.join(path) else: return u'/' path.append(context.__name__) context = context.__parent__ max -= 1 if max < 1: raise TypeError("Maximum location depth exceeded, " "probably due to a a location cycle.") raise TypeError("Not enough context to determine location root")