def traverse_predicate(context, request): if 'traverse' in context: return True m = context['match'] tvalue = tgenerate(m) m['traverse'] = traversal_path(tvalue) return True
def traverse_predicate(context, request): if 'traverse' in context: return True m = context['match'] tvalue = tgenerate(m) # tvalue will be urlquoted string m['traverse'] = traversal_path(tvalue) # will be seq of unicode return True
def __call__(self, request): path = request.environ['PATH_INFO'] ob = resources[path] traversed = traversal_path(path) return {'context':ob, 'view_name':'','subpath':(), 'traversed':traversed, 'virtual_root':ob, 'virtual_root_path':(), 'root':ob}
def url_to_resource(self, url): """ Returns the resource that is addressed by the given URL. :param str url: URL to convert :return: member or collection resource :note: If the query string in the URL has multiple values for a query parameter, the last definition in the query string wins. """ parsed = urlparse.urlparse(url) parsed_path = parsed.path # namedtupble problem pylint: disable=E1101 rc = find_resource(self.__request.root, traversal_path(parsed_path)) if ICollectionResource in provided_by(rc): # In case we found a collection, we have to filter, order, slice. parsed_query = parsed.query # namedtuple problem pylint: disable=E1101 params = dict(parse_qsl(parsed_query)) filter_string = params.get('q') if not filter_string is None: rc.filter = \ UrlPartsConverter.make_filter_specification(filter_string) order_string = params.get('sort') if not order_string is None: rc.order = \ UrlPartsConverter.make_order_specification(order_string) start_string = params.get('start') size_string = params.get('size') if not (start_string is None or size_string is None): rc.slice = \ UrlPartsConverter.make_slice_key(start_string, size_string) elif not IMemberResource in provided_by(rc): raise ValueError('Traversal found non-resource object "%s".' % rc) return rc
def __call__(self, context, request): if self.use_subpath: path_tuple = request.subpath else: path_tuple = traversal_path(request.path_info) path = _secure_path(path_tuple) if path is None: return HTTPNotFound('Out of bounds: %s' % request.url) if self.package_name: # package resource resource_path ='%s/%s' % (self.docroot.rstrip('/'), path) if resource_isdir(self.package_name, resource_path): if not request.path_url.endswith('/'): return self.add_slash_redirect(request) resource_path = '%s/%s' % (resource_path.rstrip('/'),self.index) if not resource_exists(self.package_name, resource_path): return HTTPNotFound(request.url) filepath = resource_filename(self.package_name, resource_path) else: # filesystem file # os.path.normpath converts / to \ on windows filepath = normcase(normpath(join(self.norm_docroot, path))) if isdir(filepath): if not request.path_url.endswith('/'): return self.add_slash_redirect(request) filepath = join(filepath, self.index) if not exists(filepath): return HTTPNotFound(request.url) return _FileResponse(filepath ,self.cache_max_age)
def __call__(self, context, request): if self.use_subpath: path_tuple = request.subpath else: path_tuple = traversal_path(request.path_info) path = _secure_path(path_tuple) if path is None: return HTTPNotFound('Out of bounds: %s' % request.url) if self.package_name: # package resource resource_path = '%s/%s' % (self.docroot.rstrip('/'), path) if resource_isdir(self.package_name, resource_path): if not request.path_url.endswith('/'): return self.add_slash_redirect(request) resource_path = '%s/%s' % (resource_path.rstrip('/'), self.index) if not resource_exists(self.package_name, resource_path): return HTTPNotFound(request.url) filepath = resource_filename(self.package_name, resource_path) else: # filesystem file # os.path.normpath converts / to \ on windows filepath = normcase(normpath(join(self.norm_docroot, path))) if isdir(filepath): if not request.path_url.endswith('/'): return self.add_slash_redirect(request) filepath = join(filepath, self.index) if not exists(filepath): return HTTPNotFound(request.url) return _FileResponse(filepath, self.cache_max_age)
def __call__(self, context, request): if 'traverse' in context: return True m = context['match'] tvalue = self.tgenerate(m) # tvalue will be urlquoted string m['traverse'] = traversal_path(tvalue) # This isn't actually a predicate, it's just a infodict modifier that # injects ``traverse`` into the matchdict. As a result, we just # return True. return True
def __call__(self, request): path = request.environ['PATH_INFO'] ob = resources[path] traversed = traversal_path(path) return { 'context': ob, 'view_name': '', 'subpath': (), 'traversed': traversed, 'virtual_root': ob, 'virtual_root_path': (), 'root': ob }
def __call__(self, request): """ 1. Split the PATH_INFO into traversal segments. 2. The 'current controller' is the root controller. 2. While there are segments: a) Pop the first segment off the traversal stack. This is the 'current segment'. b) Ask the 'current controller' for an attribute named after the current segment. This becomes the 'next controller'. c) If the 'next controller' is not a Controller instance, stop traversing and return the 'current controller' as the context. d) If the 'next controller' *is* a Controller instance, the 'next controller' becomes the 'current controller'. Goto a). 3. If we run out of path segments before running out of controllers to traverse, return the last controller found as the context. . """ path_info = request.path_info path = list(traversal_path(path_info)) traversed = [] controller = self.root controllers = [controller] while path: segment = path[-1] next = getattr(controller, segment, None) if next is None or not IController.providedBy(next): break controller = next controllers.append(next) traversed.append(segment) path.pop(0) return dict( context=controller, view_name='', subpath=tuple(path), traversed=tuple(traversed), virtual_root=self.root, virtual_root_path=None, controllers=controllers, )
def matcher(path): m = match(path) if m is None: return m d = {} for k, v in m.groupdict().iteritems(): if k == star: d[k] = traversal_path(v) else: encoded = unquote(v) try: d[k] = encoded.decode('utf-8') except UnicodeDecodeError, e: raise URLDecodeError(e.encoding, e.object, e.start, e.end, e.reason)
def matcher(path): m = match(path) if m is None: return m d = {} for k, v in m.groupdict().iteritems(): if k == star: d[k] = traversal_path(v) else: encoded = unquote(v) try: d[k] = encoded.decode('utf-8') except UnicodeDecodeError, e: raise URLDecodeError( e.encoding, e.object, e.start, e.end, e.reason )
def __call__(self, environ): if 'bfg.routes.matchdict' in environ: matchdict = environ['bfg.routes.matchdict'] path = matchdict.get('traverse', '/') subpath = matchdict.get('subpath', '') subpath = tuple(filter(None, subpath.split('/'))) else: # this request did not match a Routes route subpath = () try: path = environ['PATH_INFO'] or '/' except KeyError: path = '/' try: vroot_path = environ[VH_ROOT_KEY] except KeyError: vroot_tuple = () vpath = path vroot_idx = -1 else: vroot_tuple = traversal_path(vroot_path) vpath = vroot_path + path vroot_idx = len(vroot_tuple) -1 ob = vroot = LocationProxy(self.root) if vpath == '/' or (not vpath): # prevent a call to traversal_path if we know it's going # to return the empty tuple vpath_tuple = () else: # we do dead reckoning here via tuple slicing instead of # pushing and popping temporary lists for speed purposes # and this hurts readability; apologies i = 0 vpath_tuple = traversal_path(vpath) for segment in vpath_tuple: if segment[:2] =='@@': return dict(context=ob, view_name=segment[2:], subpath=vpath_tuple[i+1:], traversed=vpath_tuple[:vroot_idx+i+1], virtual_root=vroot, virtual_root_path=vroot_tuple, root=self.root) try: getitem = ob.__getitem__ except AttributeError: return dict(context=ob, view_name=segment, subpath=vpath_tuple[i+1:], traversed=vpath_tuple[:vroot_idx+i+1], virtual_root=vroot, virtual_root_path=vroot_tuple, root=self.root) try: next = getitem(segment) except KeyError: return dict(context=ob, view_name=segment, subpath=vpath_tuple[i+1:], traversed=vpath_tuple[:vroot_idx+i+1], virtual_root=vroot, virtual_root_path=vroot_tuple, root=self.root) next = LocationProxy(next, ob, segment) if i == vroot_idx: vroot = next ob = next i += 1 return dict(context=ob, view_name=u'', subpath=subpath, traversed=vpath_tuple, virtual_root=vroot, virtual_root_path=vroot_tuple, root=self.root)
def mgmt_url(self, obj, *arg, **kw): request = self.request traverse = ('', ) + traversal_path(request.resource_path(obj)) kw['traverse'] = traverse return request.route_url(MANAGE_ROUTE_NAME, *arg, **kw)
def __call__(self, request, queries=_path_queries): environ = request.environ context = root = self.root if root.__default_root__ and 'bfg.routes.route' in environ: return ResourceTreeTraverser(root)(request) path = '/%s/' % '/'.join(traversal_path(environ.get('PATH_INFO', '/'))) vroot_tuple = () l_path = len(root.__root_path__) # build paths for all parents in content hierarchy idx = 0 paths = {} current = root.__path__ for sec in path[l_path:].split('/'): if sec: current = '%s%s/' % (current, sec) paths[str(idx)] = current idx += 1 if idx: if idx not in queries: bindparams = [sql.bindparam(str(p)) for p in range(idx)] queries[idx] = ptah.QueryFreezer( lambda: ptah.get_session().query(BaseContent)\ .filter(BaseContent.__path__.in_(bindparams))) parents = sorted(queries[idx].all(**paths), reverse=True, key=lambda item: item.__path__) else: parents = [] if parents: # set __parent__ parents[-1].__parent__ = root for idx in range(len(parents) - 1): parents[idx].__parent__ = parents[idx + 1] context = parents[0] node = context.__path__[len(root.__path__):] leaf = path[l_path + len(node):].split('/') leaf, subpath = leaf[0], leaf[1:] return { 'context': context, 'view_name': leaf, 'subpath': subpath, 'traversed': traversal_path(node), 'virtual_root': root, 'virtual_root_path': vroot_tuple, 'root': root } else: vpath_tuple = () leaf = path[l_path:].split('/') leaf, subpath = leaf[0], leaf[1:] return { 'context': context, 'view_name': leaf, 'subpath': subpath, 'traversed': vpath_tuple, 'virtual_root': root, 'virtual_root_path': (), 'root': root }
def _callFUT(self, path): from pyramid.traversal import traversal_path return traversal_path(path)
def __call__(self, request, queries=_path_queries): environ = request.environ context = root = self.root if root.__default_root__ and "bfg.routes.route" in environ: return ResourceTreeTraverser(root)(request) path = "/%s/" % "/".join(traversal_path(environ.get("PATH_INFO", "/"))) vroot_tuple = () vpath = path vroot_idx = -1 l_path = len(root.__root_path__) # build paths for all parents in content hierarchy idx = 0 paths = {} current = root.__path__ for sec in path[l_path:].split("/"): if sec: current = "%s%s/" % (current, sec) paths[str(idx)] = current idx += 1 if idx: if idx not in queries: bindparams = [sql.bindparam(str(p)) for p in range(idx)] queries[idx] = ptah.QueryFreezer( lambda: Session.query(Content) .filter(Content.__path__.in_(bindparams)) .order_by(sql.desc(Content.__path__)) ) parents = queries[idx].all(**paths) else: parents = [] if parents: # set __parent__ parents[-1].__parent__ = root for idx in range(len(parents) - 1): parents[idx].__parent__ = parents[idx + 1] context = parents[0] node = context.__path__[len(root.__path__) :] leaf = path[l_path + len(node) :].split("/") leaf, subpath = leaf[0], leaf[1:] return { "context": context, "view_name": leaf, "subpath": subpath, "traversed": traversal_path(node), "virtual_root": root, "virtual_root_path": vroot_tuple, "root": root, } else: vpath_tuple = () leaf = path[l_path:].split("/") leaf, subpath = leaf[0], leaf[1:] return { "context": context, "view_name": leaf, "subpath": subpath, "traversed": vpath_tuple, "virtual_root": root, "virtual_root_path": (), "root": root, }
def __call__(self, request): environ = request.environ if 'bfg.routes.matchdict' in environ: matchdict = environ['bfg.routes.matchdict'] path = matchdict.get('traverse', '/') if hasattr(path, '__iter__'): # this is a *traverse stararg (not a :traverse) path = '/'.join([quote_path_segment(x) for x in path]) or '/' subpath = matchdict.get('subpath', ()) if not hasattr(subpath, '__iter__'): # this is not a *subpath stararg (just a :subpath) subpath = traversal_path(subpath) else: # this request did not match a route subpath = () try: path = environ['PATH_INFO'] or '/' except KeyError: path = '/' if VH_ROOT_KEY in environ: vroot_path = environ[VH_ROOT_KEY] vroot_tuple = traversal_path(vroot_path) vpath = vroot_path + path vroot_idx = len(vroot_tuple) -1 else: vroot_tuple = () vpath = path vroot_idx = -1 root = self.root ob = vroot = root if vpath == '/' or (not vpath): vpath_tuple = () else: i = 0 view_selector = self.VIEW_SELECTOR vpath_tuple = traversal_path(vpath) for segment in vpath_tuple: if segment[:2] == view_selector: return {'context':ob, 'view_name':segment[2:], 'subpath':vpath_tuple[i+1:], 'traversed':vpath_tuple[:vroot_idx+i+1], 'virtual_root':vroot, 'virtual_root_path':vroot_tuple, 'root':root} cont = IContainer(ob, None) if cont is None: return {'context':ob, 'view_name':segment, 'subpath':vpath_tuple[i+1:], 'traversed':vpath_tuple[:vroot_idx+i+1], 'virtual_root':vroot, 'virtual_root_path':vroot_tuple, 'root':root} else: try: next = LocationProxy(cont[segment], ob, segment) except KeyError: return {'context':ob, 'view_name':segment, 'subpath':vpath_tuple[i+1:], 'traversed':vpath_tuple[:vroot_idx+i+1], 'virtual_root':vroot, 'virtual_root_path':vroot_tuple, 'root':root} if i == vroot_idx: vroot = next ob = next i += 1 return {'context':ob, 'view_name':u'', 'subpath':subpath, 'traversed':vpath_tuple, 'virtual_root':vroot, 'virtual_root_path':vroot_tuple, 'root':root}
def __call__(self, request, queries=_path_queries): environ = request.environ context = root = self.root if root.__default_root__ and 'bfg.routes.route' in environ: return ResourceTreeTraverser(root)(request) path = '/%s/'%'/'.join(traversal_path(environ.get('PATH_INFO','/'))) vroot_tuple = () l_path = len(root.__root_path__) # build paths for all parents in content hierarchy idx = 0 paths = {} current = root.__path__ for sec in path[l_path:].split('/'): if sec: current = '%s%s/'%(current, sec) paths[str(idx)] = current idx += 1 if idx: if idx not in queries: bindparams = [sql.bindparam(str(p)) for p in range(idx)] queries[idx] = ptah.QueryFreezer( lambda: ptah.get_session().query(BaseContent)\ .filter(BaseContent.__path__.in_(bindparams))) parents = sorted(queries[idx].all(**paths), reverse=True, key = lambda item: item.__path__) else: parents = [] if parents: # set __parent__ parents[-1].__parent__ = root for idx in range(len(parents)-1): parents[idx].__parent__ = parents[idx+1] context = parents[0] node = context.__path__[len(root.__path__):] leaf = path[l_path+len(node):].split('/') leaf, subpath = leaf[0], leaf[1:] return {'context': context, 'view_name': leaf, 'subpath': subpath, 'traversed': traversal_path(node), 'virtual_root': root, 'virtual_root_path': vroot_tuple, 'root': root} else: vpath_tuple = () leaf = path[l_path:].split('/') leaf, subpath = leaf[0], leaf[1:] return {'context': context, 'view_name': leaf, 'subpath': subpath, 'traversed': vpath_tuple, 'virtual_root': root, 'virtual_root_path': (), 'root': root}
def __call__(self, environ): if 'bfg.routes.matchdict' in environ: matchdict = environ['bfg.routes.matchdict'] path = matchdict.get('traverse', '/') subpath = matchdict.get('subpath', '') subpath = tuple(filter(None, subpath.split('/'))) else: # this request did not match a Routes route subpath = () try: path = environ['PATH_INFO'] or '/' except KeyError: path = '/' try: vroot_path = environ[VH_ROOT_KEY] except KeyError: vroot_tuple = () vpath = path vroot_idx = -1 else: vroot_tuple = traversal_path(vroot_path) vpath = vroot_path + path vroot_idx = len(vroot_tuple) - 1 ob = vroot = LocationProxy(self.root) if vpath == '/' or (not vpath): # prevent a call to traversal_path if we know it's going # to return the empty tuple vpath_tuple = () else: # we do dead reckoning here via tuple slicing instead of # pushing and popping temporary lists for speed purposes # and this hurts readability; apologies i = 0 vpath_tuple = traversal_path(vpath) for segment in vpath_tuple: if segment[:2] == '@@': return dict(context=ob, view_name=segment[2:], subpath=vpath_tuple[i + 1:], traversed=vpath_tuple[:vroot_idx + i + 1], virtual_root=vroot, virtual_root_path=vroot_tuple, root=self.root) try: getitem = ob.__getitem__ except AttributeError: return dict(context=ob, view_name=segment, subpath=vpath_tuple[i + 1:], traversed=vpath_tuple[:vroot_idx + i + 1], virtual_root=vroot, virtual_root_path=vroot_tuple, root=self.root) try: next = getitem(segment) except KeyError: return dict(context=ob, view_name=segment, subpath=vpath_tuple[i + 1:], traversed=vpath_tuple[:vroot_idx + i + 1], virtual_root=vroot, virtual_root_path=vroot_tuple, root=self.root) next = LocationProxy(next, ob, segment) if i == vroot_idx: vroot = next ob = next i += 1 return dict(context=ob, view_name=u'', subpath=subpath, traversed=vpath_tuple, virtual_root=vroot, virtual_root_path=vroot_tuple, root=self.root)
def mgmt_url(self, obj, *arg, **kw): request = self.request traverse = ('',) + traversal_path(request.resource_path(obj)) kw['traverse'] = traverse return request.route_url(MANAGE_ROUTE_NAME, *arg, **kw)
def __call__(self, request): environ = request.environ registry = request.registry if 'bfg.routes.matchdict' in environ: matchdict = environ['bfg.routes.matchdict'] path = matchdict.get('traverse', '/') subpath = matchdict.get('subpath', '') subpath = tuple(filter(None, subpath.split('/'))) else: # this request did not match a Routes route subpath = () try: path = environ['PATH_INFO'] or '/' except KeyError: path = '/' try: vroot_path = environ[VH_ROOT_KEY] except KeyError: vroot_tuple = () vpath = path vroot_idx = -1 else: vroot_tuple = traversal_path(vroot_path) vpath = vroot_path + path vroot_idx = len(vroot_tuple) -1 ob = self.root proxy = registry.queryUtility(ISecurityProxyFactory, default=None) if proxy: ob = proxy(ob) vroot = root = ob if vpath == '/' or (not vpath): # prevent a call to traversal_path if we know it's going # to return the empty tuple vpath_tuple = () else: # we do dead reckoning here via tuple slicing instead of # pushing and popping temporary lists for speed purposes # and this hurts readability; apologies i = 0 vpath_tuple = traversal_path(vpath) marker = object() for segment in vpath_tuple: if segment[:2] == self.VIEW_SELECTOR: return dict(context=ob, view_name=segment[2:], subpath=vpath_tuple[i+1:], traversed=vpath_tuple[:vroot_idx+i+1], virtual_root=vroot, virtual_root_path=vroot_tuple, root=self.root) ns_match = namespace_pattern.match(segment) namespace = u"" if ns_match: prefix, namespace = ns_match.group(0, 1) segment = segment[len(prefix):] adapter = registry.getMultiAdapter((ob, request), IPluggableTraverser, name=namespace) try: next = adapter[segment] except KeyError: return dict(context=ob, view_name=segment, subpath=vpath_tuple[i+1:], traversed=vpath_tuple[:vroot_idx+i+1], virtual_root=vroot, virtual_root_path=vroot_tuple, root=self.root) if not is_locateable(next): next = LocationProxy(next, ob, segment) if proxy: next = proxy(next) if i == vroot_idx: vroot = next ob = next i += 1 default_view = registry.getMultiAdapter((ob, request), IDefaultView) return dict(context=ob, view_name=default_view.view_name, subpath=subpath, traversed=vpath_tuple, virtual_root=vroot, virtual_root_path=vroot_tuple, root=self.root)