routes源代码

  • Y0_221067
    了解作者
  • 57.6KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-11 19:16
    上传日期
Routes is a Python re-implementation of the Rails routes system for mapping URLs to application actions, and conversely to generate URLs. Routes makes it easy to create pretty and concise URLs that are RESTful with little effort. Routes allows conditional matching based on domain, cookies, HTTP method, or a custom function. Sub-domain support is built in. Routes comes with an extensive unit test suite.
routes.rar
  • routes
  • base.py
    134B
  • middleware.py
    6KB
  • util.pyc
    15.1KB
  • util.py
    18.8KB
  • base.pyc
    353B
  • mapper.py
    45.9KB
  • __init__.pyc
    5.4KB
  • __init__.py
    5.3KB
  • route.py
    28.7KB
  • route.pyc
    16.8KB
  • mapper.pyc
    38KB
  • middleware.pyc
    4.9KB
内容介绍
"""Mapper and Sub-Mapper""" import re import sys import threading import pkg_resources from repoze.lru import LRUCache from routes import request_config from routes.util import controller_scan, MatchException, RoutesException from routes.route import Route COLLECTION_ACTIONS = ['index', 'create', 'new'] MEMBER_ACTIONS = ['show', 'update', 'delete', 'edit'] def strip_slashes(name): """Remove slashes from the beginning and end of a part/URL.""" if name.startswith('/'): name = name[1:] if name.endswith('/'): name = name[:-1] return name class SubMapperParent(object): """Base class for Mapper and SubMapper, both of which may be the parent of SubMapper objects """ def submapper(self, **kargs): """Create a partial version of the Mapper with the designated options set This results in a :class:`routes.mapper.SubMapper` object. If keyword arguments provided to this method also exist in the keyword arguments provided to the submapper, their values will be merged with the saved options going first. In addition to :class:`routes.route.Route` arguments, submapper can also take a ``path_prefix`` argument which will be prepended to the path of all routes that are connected. Example:: >>> map = Mapper(controller_scan=None) >>> map.connect('home', '/', controller='home', action='splash') >>> map.matchlist[0].name == 'home' True >>> m = map.submapper(controller='home') >>> m.connect('index', '/index', action='index') >>> map.matchlist[1].name == 'index' True >>> map.matchlist[1].defaults['controller'] == 'home' True Optional ``collection_name`` and ``resource_name`` arguments are used in the generation of route names by the ``action`` and ``link`` methods. These in turn are used by the ``index``, ``new``, ``create``, ``show``, ``edit``, ``update`` and ``delete`` methods which may be invoked indirectly by listing them in the ``actions`` argument. If the ``formatted`` argument is set to ``True`` (the default), generated paths are given the suffix '{.format}' which matches or generates an optional format extension. Example:: >>> from routes.util import url_for >>> map = Mapper(controller_scan=None) >>> m = map.submapper(path_prefix='/entries', collection_name='entries', resource_name='entry', actions=['index', 'new']) >>> url_for('entries') == '/entries' True >>> url_for('new_entry', format='xml') == '/entries/new.xml' True """ return SubMapper(self, **kargs) def collection(self, collection_name, resource_name, path_prefix=None, member_prefix='/{id}', controller=None, collection_actions=COLLECTION_ACTIONS, member_actions = MEMBER_ACTIONS, member_options=None, **kwargs): """Create a submapper that represents a collection. This results in a :class:`routes.mapper.SubMapper` object, with a ``member`` property of the same type that represents the collection's member resources. Its interface is the same as the ``submapper`` together with ``member_prefix``, ``member_actions`` and ``member_options`` which are passed to the ``member` submatter as ``path_prefix``, ``actions`` and keyword arguments respectively. Example:: >>> from routes.util import url_for >>> map = Mapper(controller_scan=None) >>> c = map.collection('entries', 'entry') >>> c.member.link('ping', method='POST') >>> url_for('entries') == '/entries' True >>> url_for('edit_entry', id=1) == '/entries/1/edit' True >>> url_for('ping_entry', id=1) == '/entries/1/ping' True """ if controller is None: controller = resource_name or collection_name if path_prefix is None: path_prefix = '/' + collection_name collection = SubMapper(self, collection_name=collection_name, resource_name=resource_name, path_prefix=path_prefix, controller=controller, actions=collection_actions, **kwargs) collection.member = SubMapper(collection, path_prefix=member_prefix, actions=member_actions, **(member_options or {})) return collection class SubMapper(SubMapperParent): """Partial mapper for use with_options""" def __init__(self, obj, resource_name=None, collection_name=None, actions=None, formatted=None, **kwargs): self.kwargs = kwargs self.obj = obj self.collection_name = collection_name self.member = None self.resource_name = resource_name \ or getattr(obj, 'resource_name', None) \ or kwargs.get('controller', None) \ or getattr(obj, 'controller', None) if formatted is not None: self.formatted = formatted else: self.formatted = getattr(obj, 'formatted', None) if self.formatted is None: self.formatted = True self.add_actions(actions or []) def connect(self, *args, **kwargs): newkargs = {} newargs = args for key, value in self.kwargs.items(): if key == 'path_prefix': if len(args) > 1: newargs = (args[0], self.kwargs[key] + args[1]) else: newargs = (self.kwargs[key] + args[0],) elif key in kwargs: if isinstance(value, dict): newkargs[key] = dict(value, **kwargs[key]) # merge dicts else: newkargs[key] = value + kwargs[key] else: newkargs[key] = self.kwargs[key] for key in kwargs: if key not in self.kwargs: newkargs[key] = kwargs[key] return self.obj.connect(*newargs, **newkargs) def link(self, rel=None, name=None, action=None, method='GET', formatted=None, **kwargs): """Generates a named route for a subresource. Example:: >>> from routes.util import url_for >>> map = Mapper(controller_scan=None) >>> c = map.collection('entries', 'entry') >>> c.link('recent', name='recent_entries') >>> c.member.link('ping', method='POST', formatted=True) >>> url_for('entries') == '/entries' True >>> url_for('recent_entries') == '/entries/recent' True >>> url_for('ping_entry', id=1) == '/entries/1/ping' True >>> url_for('ping_entry', id=1, format='xml') == '/entries/1/ping.xml' True """ if formatted or (formatted is None and self.formatted): suffix = '{.format}' else: suffix = '' return self.connect(name or (rel + '_' + self.resource_name), '/' + (rel or name) + suffix, action=action or rel or name, **_kwargs_with_conditions(kwargs, method)) def new(self, **kwargs): """Generates the "new" link for a collection submapper.""" return self.link(rel='new', **kwargs) def edit(self, **kwargs): """Generates the "edit" link for a collection member s
评论
    相关推荐