def __str__(self): if IContainmentRoot.providedBy(self.context): return "" name = self.context.__name__ url = zapi.absoluteURL(zapi.getParent(self.context), self.request) url += "/" + name return url
def traverse(self, name, ignored): # TODO: # This is here now to allow us to get site managers from a # separate namespace from the content. We add and etc # namespace to allow us to handle misc objects. We'll apply # YAGNI for now and hard code this. We'll want something more # general later. We were thinking of just calling "get" # methods, but this is probably too magic. In particular, we # will treat returned objects as sub-objects wrt security and # not all get methods may satisfy this assumption. It might be # best to introduce some sort of etc registry. ob = self.context if (name in ('process', 'ApplicationController') and IContainmentRoot.providedBy(ob)): return applicationController if name not in ('site',): raise TraversalError(ob, name) method_name = "getSiteManager" method = getattr(ob, method_name, None) if method is None: raise TraversalError(ob, name) try: return method() except ComponentLookupError: raise TraversalError(ob, name)
def getRoot(self): """Get the root location for a location. See IPhysicallyLocatable The root location is a location that contains the given location and that implements IContainmentRoot. >>> root = Location() >>> zope.interface.directlyProvides(root, IContainmentRoot) >>> LocationPhysicallyLocatable(root).getRoot() is root 1 >>> o1 = Location(); o1.__parent__ = root >>> LocationPhysicallyLocatable(o1).getRoot() is root 1 >>> o2 = Location(); o2.__parent__ = o1 >>> LocationPhysicallyLocatable(o2).getRoot() is root 1 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 IContainmentRoot.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 _findNextSiteManager(site): while True: if IContainmentRoot.providedBy(site): # we're the root site, return None return None try: site = zapi.getParent(site) except TypeError: # there was not enough context; probably run from a test return None if interfaces.ISite.providedBy(site): return site.getSiteManager()
def getPath(self): """Get the path of a location. See IPhysicallyLocatable This is an "absolute path", rooted at a root object. >>> root = Location() >>> zope.interface.directlyProvides(root, IContainmentRoot) >>> 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 IContainmentRoot.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")