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.py134B
- middleware.py6KB
- util.pyc15.1KB
- util.py18.8KB
- base.pyc353B
- mapper.py45.9KB
- __init__.pyc5.4KB
- __init__.py5.3KB
- route.py28.7KB
- route.pyc16.8KB
- mapper.pyc38KB
- middleware.pyc4.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
评论



相关推荐
- api-development-tools:一系列有用的资源,用于构建RESTful HTTP + JSON APIapi-development-tools:一系列有用的资源,用于构建RESTful HTTP + JSON API
- cli2web:通过 Restful HTTP 的 CLI 应用程序即服务cli2web 通过 Restful HTTP 的 CLI 应用程序即服务
- ansible-api:Ansible的RESTful HTTP Api适用于ansible python版本> = 3.7的Restful HTTP API 它是什么? 是一个非常简单的IT自动化系统。 如果您要使用它而不是CLI,请立即尝试。 我可以通过RESTful HTTP Api和实时处理消息(websocket api)为您提供...
- httpdomain:记录RESTful HTTP APIsphinxcontrib.httpdomain 此contrib扩展sphinxcontrib.httpdomain提供了一个用于描述HTTP API的Sphinx域。 您可以从以下URL找到文档:
- hat:HAT(超媒体 API 工具包)可让您构建 RESTful HTTP CRUD API,这些 API 可与您的 ringHAT 允许您构建 RESTful HTTP CRUD API,这些 API 可以与您的 Ring 应用程序完美组合。 用法 ; Resource description ( defn hosts-description [database-connection] ( generate-description { :singular-name ...
- kaproxy:Kafka 的 Restful HTTP 代理kaproxy 是 kafka 的静态 HTTP 代理,提供以下功能: 基本队列原语:PUBLISH、CONSUME 授权,用户使用令牌访问组和主题 生产/消费者指标 kaproxy SDK 建造 要构建服务器二进制文件: $ make # target file would ...
- RESTful-Unity:RESTful-Unity旨在为Unity3d提供一个简单的RESTful HTTP服务器RESTful-Unity旨在为提供一个简单的RESTful HTTP服务器。 通过http请求从Unity场景访问并封装游戏对象的状态。 这个项目是在3D原型制作原型期间创建的,其中可以通过访问和控制游戏对象。 这是概念验证项目,因此...
- pandoc-http:Pandoc 的 RESTful HTTP API 封装在 docker 容器中Pandoc 的 RESTful HTTP API 是一个了不起的通用文档转换器。 不幸的是,它只有一个命令行界面。 在这个项目中,我们通过 RESTful HTTP API 启用 Pandoc,提供 Pandoc 类型标识符到常见的映射,并将所有内容包装在 ...
- junebug:一种用于通过RESTful HTTP接口管理文本消息传送的系统Junebug是一个用于通过RESTful HTTP接口管理文本消息传送的系统,该接口支持: 创建,自省,更新和删除传输 发送和接收短信 接收已发送短信的状态更新 监控运输运行状况和性能 检索最近的传输日志以调试传输问题。...
- mimerender:用于RESTful HTTP内容协商的Python模块mimerender是用于RESTful HTTP内容协商的Python模块。 它充当包装HTTP请求处理程序的装饰器,以为给定的HTTP Accept标头选择正确的呈现函数。 它使用解析接受字符串并选择最佳的可用表示形式。 对 ( ), , 和是...
最新资源