本文整理汇总了Python中pyramid.compat.decode_path_info函数的典型用法代码示例。如果您正苦于以下问题:Python decode_path_info函数的具体用法?Python decode_path_info怎么用?Python decode_path_info使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了decode_path_info函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __call__
def __call__(self, context, request):
path = decode_path_info(request.environ['PATH_INFO'] or '/')
registry = request.registry
mapper = registry.queryUtility(IRoutesMapper)
if mapper is not None and not path.endswith('/'):
slashpath = path + '/'
for route in mapper.get_routes():
if route.match(slashpath) is not None:
qs = request.query_string
if qs:
qs = '?' + qs
return self.redirect_class(location=request.path + '/' + qs)
return self.notfound_view(context, request)
示例2: __call__
def __call__(self, context, request):
path = decode_path_info(request.environ["PATH_INFO"] or "/")
registry = request.registry
mapper = registry.queryUtility(IRoutesMapper)
if mapper is not None and not path.endswith("/"):
slashpath = path + "/"
for route in mapper.get_routes():
if route.match(slashpath) is not None:
qs = request.query_string
if qs:
qs = "?" + qs
return HTTPFound(location=request.path + "/" + qs)
return self.notfound_view(context, request)
示例3: extract_tender
def extract_tender(request):
try:
# empty if mounted under a path in mod_wsgi, for example
path = decode_path_info(request.environ['PATH_INFO'] or '/')
except KeyError:
path = '/'
except UnicodeDecodeError as e:
raise URLDecodeError(e.encoding, e.object, e.start, e.end, e.reason)
tender_id = ""
# extract tender id
parts = path.split('/')
if len(parts) < 4 or parts[3] != 'tenders':
return
tender_id = parts[4]
return extract_tender_adapter(request, tender_id)
示例4: notfound
def notfound(request):
if request.method == 'POST':
request.response.status = 404
return dict(msg=request.exception)
path = decode_path_info(request.environ['PATH_INFO'] or '/')
registry = request.registry
mapper = registry.queryUtility(IRoutesMapper)
if mapper is not None and path.endswith('/'):
# redirect URLs with a trailing slash to URLs without one, if there
# is a matching route
nonslashpath = path.rstrip('/')
for route in mapper.get_routes():
if route.match(nonslashpath) is not None:
qs = request.query_string
if qs:
qs = '?' + qs
return HTTPFound(location=nonslashpath + qs)
request.response.status = 404
return dict(msg=request.exception)
示例5: __call__
def __call__(self, request):
environ = request.environ
try:
# empty if mounted under a path in mod_wsgi, for example
path = decode_path_info(environ['PATH_INFO'] or '/')
except KeyError:
path = '/'
except UnicodeDecodeError as e:
raise URLDecodeError(e.encoding, e.object, e.start, e.end, e.reason)
for route in self.routelist:
match = route.match(path)
if match is not None:
preds = route.predicates
info = {'match':match, 'route':route}
if preds and not all((p(info, request) for p in preds)):
continue
return info
return {'route':None, 'match':None}
示例6: _extract_from_request
def _extract_from_request(request): # pragma: no cover
""" Extract subpath, vpath and vroot_tuple from the request. The
contents of this method is just a copy from the base class'
implementation.
:param request: Current request
:type request: :class:`pyramid.request.Request`
:return: (subpath, vpath, vroot_tuple)
:rtype: tuple
"""
environ = request.environ
matchdict = request.matchdict
if matchdict is not None:
path = matchdict.get('traverse', slash) or slash
if is_nonstr_iter(path):
path = '/' + slash.join(path) or slash
subpath = matchdict.get('subpath', ())
if not is_nonstr_iter(subpath):
subpath = split_path_info(subpath)
else:
subpath = ()
try:
path = request.path_info or slash
except KeyError:
path = slash
except UnicodeDecodeError as e:
raise URLDecodeError(e.encoding, e.object, e.start, e.end,
e.reason)
if VH_ROOT_KEY in environ:
vroot_path = decode_path_info(environ[VH_ROOT_KEY])
vroot_tuple = split_path_info(vroot_path)
vpath = vroot_path + path
else:
vroot_tuple = ()
vpath = path
return subpath, vpath, vroot_tuple
示例7: __call__
def __call__(self, request):
environ = request.environ
matchdict = request.matchdict
if matchdict is not None:
path = matchdict.get("traverse", slash) or slash
if is_nonstr_iter(path):
# this is a *traverse stararg (not a {traverse})
# routing has already decoded these elements, so we just
# need to join them
path = "/" + slash.join(path) or slash
subpath = matchdict.get("subpath", ())
if not is_nonstr_iter(subpath):
# this is not a *subpath stararg (just a {subpath})
# routing has already decoded this string, so we just need
# to split it
subpath = split_path_info(subpath)
else:
# this request did not match a route
subpath = ()
try:
# empty if mounted under a path in mod_wsgi, for example
path = request.path_info or slash
except KeyError:
# if environ['PATH_INFO'] is just not there
path = slash
except UnicodeDecodeError as e:
raise URLDecodeError(e.encoding, e.object, e.start, e.end, e.reason)
if VH_ROOT_KEY in environ:
# HTTP_X_VHM_ROOT
vroot_path = decode_path_info(environ[VH_ROOT_KEY])
vroot_tuple = split_path_info(vroot_path)
vpath = vroot_path + path # both will (must) be unicode or asciistr
vroot_idx = len(vroot_tuple) - 1
else:
vroot_tuple = ()
vpath = path
vroot_idx = -1
root = self.root
ob = vroot = root
if vpath == slash: # invariant: vpath must not be empty
# 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
view_selector = self.VIEW_SELECTOR
vpath_tuple = split_path_info(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,
}
try:
getitem = ob.__getitem__
except AttributeError:
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,
}
try:
next = getitem(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,
#.........这里部分代码省略.........
示例8: traversal_path_info
def traversal_path_info(path):
""" Given``path``, return a tuple representing that path which can be
used to traverse a resource tree. ``path`` is assumed to be an
already-URL-decoded ``str`` type as if it had come to us from an upstream
WSGI server as the ``PATH_INFO`` environ variable.
The ``path`` is first decoded to from its WSGI representation to Unicode;
it is decoded differently depending on platform:
- On Python 2, ``path`` is decoded to Unicode from bytes using the UTF-8
decoding directly; a :exc:`pyramid.exc.URLDecodeError` is raised if a the
URL cannot be decoded.
- On Python 3, as per the PEP 3333 spec, ``path`` is first encoded to
bytes using the Latin-1 encoding; the resulting set of bytes is
subsequently decoded to text using the UTF-8 encoding; a
:exc:`pyramid.exc.URLDecodeError` is raised if a the URL cannot be
decoded.
The ``path`` is split on slashes, creating a list of segments. If a
segment name is empty or if it is ``.``, it is ignored. If a segment
name is ``..``, the previous segment is deleted, and the ``..`` is
ignored.
Examples:
``/``
()
``/foo/bar/baz``
(u'foo', u'bar', u'baz')
``foo/bar/baz``
(u'foo', u'bar', u'baz')
``/foo/bar/baz/``
(u'foo', u'bar', u'baz')
``/foo//bar//baz/``
(u'foo', u'bar', u'baz')
``/foo/bar/baz/..``
(u'foo', u'bar')
``/my%20archives/hello``
(u'my archives', u'hello')
``/archives/La%20Pe%C3%B1a``
(u'archives', u'<unprintable unicode>')
.. note::
This function does not generate the same type of tuples that
:func:`pyramid.traversal.resource_path_tuple` does. In particular, the
leading empty string is not present in the tuple it returns, unlike tuples
returned by :func:`pyramid.traversal.resource_path_tuple`. As a result,
tuples generated by ``traversal_path`` are not resolveable by the
:func:`pyramid.traversal.find_resource` API. ``traversal_path`` is a
function mostly used by the internals of :app:`Pyramid` and by people
writing their own traversal machinery, as opposed to users writing
applications in :app:`Pyramid`.
"""
try:
path = decode_path_info(path) # result will be Unicode
except UnicodeDecodeError as e:
raise URLDecodeError(e.encoding, e.object, e.start, e.end, e.reason)
return split_path_info(path) # result will be tuple of Unicode
示例9: __call__
def __call__(self, request):
try:
environ = request.environ
except AttributeError:
# In BFG 1.0 and before, this API expected an environ
# rather than a request; some bit of code may still be
# passing us an environ. If so, deal.
environ = request
depwarn = ('Passing an environ dictionary directly to a traverser '
'is deprecated in Pyramid 1.1. Pass a request object '
'instead.')
warnings.warn(depwarn, DeprecationWarning, 2)
if 'bfg.routes.matchdict' in environ:
matchdict = environ['bfg.routes.matchdict']
path = matchdict.get('traverse', '/') or '/'
if is_nonstr_iter(path):
# this is a *traverse stararg (not a {traverse})
# routing has already decoded these elements, so we just
# need to join them
path = '/'.join(path) or '/'
subpath = matchdict.get('subpath', ())
if not is_nonstr_iter(subpath):
# this is not a *subpath stararg (just a {subpath})
# routing has already decoded this string, so we just need
# to split it
subpath = split_path_info(subpath)
else:
# this request did not match a route
subpath = ()
try:
# empty if mounted under a path in mod_wsgi, for example
path = decode_path_info(environ['PATH_INFO'] or '/')
except KeyError:
path = '/'
except UnicodeDecodeError as e:
raise URLDecodeError(e.encoding, e.object, e.start, e.end,
e.reason)
if VH_ROOT_KEY in environ:
# HTTP_X_VHM_ROOT
vroot_path = decode_path_info(environ[VH_ROOT_KEY])
vroot_tuple = split_path_info(vroot_path)
vpath = vroot_path + path # both will (must) be unicode or asciistr
vroot_idx = len(vroot_tuple) -1
else:
vroot_tuple = ()
vpath = path
vroot_idx = -1
root = self.root
ob = vroot = root
if vpath == '/': # invariant: vpath must not be empty
# 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
view_selector = self.VIEW_SELECTOR
vpath_tuple = split_path_info(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}
try:
getitem = ob.__getitem__
except AttributeError:
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}
try:
next = getitem(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
#.........这里部分代码省略.........
示例10: __call__
def __call__(self, request):
environ = request.environ
matchdict = request.matchdict
if matchdict is not None:
path = matchdict.get('traverse', SLASH) or SLASH
if is_nonstr_iter(path):
# this is a *traverse stararg (not a {traverse})
# routing has already decoded these elements, so we just
# need to join them
path = '/' + SLASH.join(path) or SLASH
subpath = matchdict.get('subpath', ())
if not is_nonstr_iter(subpath):
# this is not a *subpath stararg (just a {subpath})
# routing has already decoded this string, so we just need
# to split it
subpath = split_path_info(subpath)
else:
# this request did not match a route
subpath = ()
try:
# empty if mounted under a path in mod_wsgi, for example
path = request.path_info or SLASH
except KeyError:
# if environ['PATH_INFO'] is just not there
path = SLASH
except UnicodeDecodeError as e:
raise URLDecodeError(e.encoding, e.object, e.start, e.end,
e.reason)
if VH_ROOT_KEY in environ:
# HTTP_X_VHM_ROOT
vroot_path = decode_path_info(environ[VH_ROOT_KEY])
vroot_tuple = split_path_info(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 == SLASH:
vpath_tuple = ()
else:
i = 0
view_selector = self.VIEW_SELECTOR
vpath_tuple = split_path_info(vpath)
return traverse(
i,
ob,
view_selector,
vpath_tuple,
vroot_idx,
vroot,
vroot_tuple,
root,
subpath,
)
return {
'context': ob,
'view_name': "",
'subpath': subpath,
'traversed': vpath_tuple,
'virtual_root': vroot,
'virtual_root_path': vroot_tuple,
'root': root
}