def test_getParent_no_location_info(self): from zope.traversing.api import getParent test = self class Context(object): called = False def __conform__(self, iface): self.called = True test.assertEqual(iface, ILocationInfo) raise TypeError() context = Context() with self.assertRaisesRegex(TypeError, "Not enough context"): getParent(context) self.assertTrue(context.called) context.called = False # Now give it a parent context.__parent__ = self self.assertIs(self, getParent(context)) self.assertTrue(context.called) context.called = False # Now if it's a root, it has no parent interface.alsoProvides(context, IRoot) self.assertIsNone(getParent(context))
def getContextHelpTopic(self): """Retrieve a help topic based on the context of the help namespace. If the context is a view, try to find a matching help topic for the view and its context. If no help topic is found, try to get a help topic for the context only. If the context is not a view, try to retrieve a help topic based on the context. If nothing is found, return the onlinehelp root topic """ if self.topic is not None: return self.topic onlinehelp = self.context help_context = onlinehelp.context self.topic = None if IBrowserView.providedBy(help_context): # called from a view self.topic = getTopicFor(getParent(help_context), getName(help_context)) if self.topic is None: # nothing found for view try context only self.topic = getTopicFor(getParent(help_context)) else: # called without view self.topic = getTopicFor(help_context) if self.topic is None: self.topic = onlinehelp return self.topic
def singleBranchTree(self, root=''): parent = getParent(self.context) while parent is not None: if IReadContainer.providedBy(parent): view = queryMultiAdapter( (parent, self.request), name='singleBranchTree.xml') return view() else: parent = getParent(parent)
def getEvaluationsOfEvaluator(self, evaluator): """See interfaces.IEvaluations""" result = [(name, ev) for name, ev in self.items() if ev.evaluator == evaluator] result = Evaluations(result) location.locate(result, getParent(self), getName(self)) return result
def __str__(self): if IContainmentRoot.providedBy(self.context): return '' name = self.context.__name__ url = absoluteURL(getParent(self.context), self.request) url += '/' + name return url
def render(self): parent = getParent(self.context) name = self.context.__name__ del parent[name] self.flash((u'The photo %s has been deleted' % name)) return self.redirect(self.url(parent))
def getMethods(self): # remove the security proxy, so that `attr` is not proxied. We could # unproxy `attr` for each turn, but that would be less efficient. # # `getPermissionIds()` also expects the class's security checker not # to be proxied. klass = zope.security.proxy.removeSecurityProxy(self.klassView.context) obj = zope.security.proxy.removeSecurityProxy(self.context) for name in apidoc.utilities.getPublicAttributes(obj): val = getattr(obj, name) if not (inspect.ismethod(val) or inspect.ismethoddescriptor(val)): continue signature = apidoc.utilities.getFunctionSignature(val) entry = { 'name': name, 'signature': signature, 'doc': apidoc.utilities.renderText( val.__doc__ or '', getParent(self.klassView.context).getPath()), 'interface': apidoc.utilities.getInterfaceForAttribute( name, klass._Class__all_ifaces)} entry.update(apidoc.utilities.getPermissionIds( name, klass.getSecurityChecker())) yield entry
def render(self): parent = getParent(self.context) name = self.context.__name__ del parent[name] self.flash((u"The sponsor %s has been deleted" % name)) return self.redirect(self.url(parent))
def getParent(self): """Get the parent of the wrapped object. This might result in a `TypeError` if the wrapped object is not locatable in sense of ILocation, i.e. if it doesn't provide ``__parent__`` attribute. """ return getParent(self.obj)
def getEvaluationsForRequirement(self, req, recurse=True): """See interfaces.IEvaluations""" requirements = getRequirementList(req, recurse) result = [(name, ev) for name, ev in self.items() if ev.requirement in requirements] result = Evaluations(result) location.locate(result, getParent(self), getName(self)) return result
def getParentActiveViewletName(context, request, view, manager): parent = getParent(context) if parent is None: return None name = queryMultiAdapter((parent, request, view, manager), interfaces.IActiveViewletName, name='', default=None) return name
def getMenuLink(self, node): """Return the HTML link of the node that is displayed in the menu.""" obj = node.context apidoc_url = findAPIDocumentationRootURL(self.context, self.request) if isinstance(obj, Utility): iface = getParent(obj) return '%s/Utility/%s/%s/index.html' % (apidoc_url, getName(iface), getName(obj)) if isinstance(obj, UtilityInterface): return '%s/Interface/%s/index.html' % (apidoc_url, getName(obj))
def getMenuLink(self, node): """Return the HTML link of the node that is displayed in the menu.""" obj = node.context if isinstance(obj, Directive): ns = getParent(obj) apidoc_url = findAPIDocumentationRootURL(self.context, self.request) return '%s/ZCML/%s/%s/index.html' % ( apidoc_url, getName(ns), getName(obj)) return None
def getParentTitleContent(context, request, view): parent = getParent(context) if parent is None: return None providers = queryMultiAdapter((parent, request, view), flourish.interfaces.IContentProviders) if providers is None: return None content = providers.get("title") return content
def getParentActiveViewletName(context, request, view, manager): parent = getParent(context) if parent is None: return None name = queryMultiAdapter( (parent, request, view, manager), interfaces.IActiveViewletName, name='', default=None) return name
def getParentTitleContent(context, request, view): parent = getParent(context) if parent is None: return None providers = queryMultiAdapter( (parent, request, view), flourish.interfaces.IContentProviders) if providers is None: return None content = providers.get("title") return content
def matches(self, obj): """Decide whether the object is shown in the tree.""" matches = False if interfaces.IPreferenceCategory.providedBy(obj): # IPreferenceGroup objects dynamically implement # IPreferenceCategory matches = True elif interfaces.IPreferenceGroup.providedBy(obj): parent = getParent(obj) matches = interfaces.IPreferenceCategory.providedBy(parent) return matches
def handleSave(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return changes = self.applyChanges(data) if changes: self.status = self.successMessage else: self.status = self.noChangesMessage parent = getParent(self.context) self.redirect(self.url(parent, 'admin'))
def checkByAdaptation(self, permission, obj): crowd = queryAdapter(obj, ICrowd, name=permission, default=None) # If there is no crowd that has the given permission on this # object, try to look up a crowd that includes the parent. while crowd is None and obj is not None: obj = getParent(obj) crowd = queryAdapter(obj, ICrowd, name=permission, default=None) if crowd is None: # no crowds found raise AssertionError('no crowd found for', obj, permission) for participation in self.participations: if crowd.contains(participation.principal): return True else: return False
def __init__(self, context): self.context = context try: parent = getParent(self.context) except TypeError: parent = None if parent is not None: try: pp = getPath(parent) except TypeError: pp = "" if not pp.endswith("/"): pp += "/" self.pp = pp # parentpath else: self.pp = "" self.pplen = len(self.pp)
def __call__(self): """Create breadcrumbs for a module or an object in a module or package. We cannot reuse the system's bread crumbs, since they go all the way up to the root, but we just want to go to the root module. """ obj = self.context crumbs = [] while not IDocumentationModule.providedBy(obj): crumbs.append( {'name': getName(obj), 'url': absoluteURL(obj, self.request)} ) obj = getParent(obj) crumbs.append( {'name': _('[top]'), 'url': absoluteURL(obj, self.request)} ) crumbs.reverse() return crumbs
def url(self): directive = removeSecurityProxy(self.context) subDirective = None # Sub-directives are not directly documented, so use parent parent = getParent(directive) if not (IRootDirective.providedBy(parent) or IRootDirective.providedBy(directive)): subDirective = directive directive = parent ns, name = directive.name # Sometimes ns is `None`, especially in the slug files, where no # namespaces are used. ns = quoteNS(ns or 'ALL') zcml = getUtility(IDocumentationModule, 'ZCML') if name not in zcml[ns]: ns = 'ALL' link = '%s/ZCML/%s/%s/index.html' % ( findAPIDocumentationRootURL(self.context, self.request), ns, name) if subDirective: link += '#' + subDirective.name[1] return link
def evaluatee(self): try: return getParent(getParent(self)) except TypeError: raise ValueError('Evaluation is not yet assigned to a evaluatee')
def getNamespaceName(self): """Return the name of the namespace.""" name = getParent(self.context).getFullName() if name == 'ALL': return '<i>all namespaces</i>' return name
def getParent(self): return getParent(self.obj)
def __repr__(self): try: parent = getParent(self) except TypeError: parent = None return '<%s for %r>' % (self.__class__.__name__, parent)
def getBaseURL(self): """Return the URL for the API Documentation Tool.""" m = getUtility(IDocumentationModule, "Code") return absoluteURL(getParent(m), self.request)
def findDocModule(obj): if IDocumentationModule.providedBy(obj): return obj return findDocModule(getParent(obj))
def testGetParent(self): from zope.traversing.api import getParent self.assertEqual(getParent(self.item), self.folder)
def testGetParent(self): from zope.traversing.api import getParent self.assertEqual( getParent(self.item), self.folder )
def testGetParentFromRoot(self): from zope.traversing.api import getParent self.assertEqual(getParent(self.root), None)
def __call__(self, *args, **kwargs): """See interfaces.IEvaluationsQuery""" result = Evaluations(self._query(*args, **kwargs)) location.locate(result, getParent(self.context), getName(self.context)) return result
def parent(self): return getParent(self.context)
def getBaseURL(self): """Return the URL for the API Documentation Tool.""" apidoc = findAPIDocumentationRoot(self.context) m = apidoc['Code'] return absoluteURL(getParent(m), self.request)
def findAPIDocumentationRoot(context, request=None): if isinstance(context, APIDocumentation): return context return findAPIDocumentationRoot(getParent(context), request)
def getDoc(self): """Get the doc string of the class STX formatted.""" return renderText(self.context.getDocString() or '', getParent(self.context).getPath())
def getDocString(self): """Get the doc string of the function in a rendered format.""" return renderText(self.context.getDocString() or '', getParent(self.context).getPath())
def findAPIDocumentationRootURL(context, request): if isinstance(context, APIDocumentation): return absoluteURL(context, request) else: return findAPIDocumentationRootURL(getParent(context), request)
def testGetParentFromRoot(self): from zope.traversing.api import getParent self.assertEqual( getParent(self.root), None )