def traverse(self, name): new_obj = None # Try to get name as dict entry... keygetter = getattr(self.obj, 'keys', None) if inspect.ismethod(keygetter): if name in keygetter(): new_obj = self.obj[name] # Try to get name as sequence entry... if not new_obj: # This is not the appropriate way to handle iterators. We # must find somehing to handle them too. try: name_int = int(name) if name_int in range(0, len(self.obj)): new_obj = self.obj[name_int] except ValueError: pass # Get name as obj attribute... if not new_obj and hasattr(self.obj, name): new_obj = getattr(self.obj, name, None) # Get name as annotation... if not new_obj: naked = zope.security.proxy.removeSecurityProxy(self.obj) try: annotations = IAnnotations(naked) new_obj = name and name in annotations and annotations[name] if not new_obj: new_obj = annotations except TypeError: pass # Give obj a location... if new_obj: if not IPhysicallyLocatable(new_obj, False): new_obj = location.LocationProxy( new_obj, self.obj, name) new_info = ZopeObjectInfo(new_obj) new_info.__parent__ = self new_info.__name__ = name return new_info # name not found... return
def traverse(self, path): namespace = 'anno' print "TRAVERSE", path if path.startswith(namespace): name = path[len(namespace):] naked = removeSecurityProxy(self.context) annotations = IAnnotations(naked) print annotations.items() #obj = name and annotations[name] or annotations #obj = path and annotations[name] or annotations obj = ObjectInfo("Hello") if not IPhysicallyLocatable(obj, False): #obj = LocationProxy( # obj, self.context, namespace + name) obj = LocationProxy(obj, self.context, 'anno' + name) return obj return
def annotateTransaction(self, txn, request, ob): """Set some useful meta-information on the transaction. This information is used by the undo framework, for example. This method is not part of the `IPublication` interface, since it's specific to this particular implementation. """ if request.principal is not None: txn.setUser(request.principal.id) # Work around methods that are usually used for views bare = removeSecurityProxy(ob) if isinstance(bare, instancemethod): ob = bare.im_self # set the location path path = None locatable = IPhysicallyLocatable(ob, None) if locatable is not None: # Views are made children of their contexts, but that # doesn't necessarily mean that we can fully resolve the # path. E.g. the family tree of a resource cannot be # resolved completely, as the site manager is a dead end. try: path = locatable.getPath() except (AttributeError, TypeError): pass if path is not None: txn.setExtendedInfo('location', path) # set the request type iface = IRequest for iface in providedBy(request): if iface.extends(IRequest): break iface_dotted = iface.__module__ + '.' + iface.getName() txn.setExtendedInfo('request_type', iface_dotted) return txn