|
Size: 25900
Comment:
|
← Revision 3 as of 2009-12-25 07:14:50 ⇥
Size: 25833
Comment: converted to 1.6 markup
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 4: | Line 4: |
| [[TableOfContents]] | <<TableOfContents>> |
| Line 6: | Line 6: |
| [[Include(ZPyUGnav)]] | <<Include(ZPyUGnav)>> |
| Line 694: | Line 694: |
| 创建 by -- ZoomQuiet [[[DateTime(2008-04-16T06:43:37Z)]]] ||<^>[[PageComment2]]||<^>[:/PageCommentData:PageCommentData]''|| |
创建 by -- ZoomQuiet [<<DateTime(2008-04-16T06:43:37Z)>>] |
Contents
特化使用 Routes
limodou <[email protected]> reply-to [email protected], to [email protected], date Wed, Apr 16, 2008 at 2:38 PM subject [CPyUG:47564] Re: 回复:[CPyUG:47543] Re: 回复:[CPyUG:47524] Re: Routes + mod_python + cheetah
2008/4/16 iven <[email protected]>:
我想有个个性化的URL,还有我不想让别人随便访问我的py. 因此,想自己做个请求分派.
附件是我原来在web2py中加的,不过人家没有采用,它是一个Routes的变形,只有你想要的url映射的功能。定义Route的格式与Routes完全一样,定义形式有所差别:
map = URLMapping()
#创建map类
map.add('favicon.ico', '/examples/static/favicon.ico'),
map.add('archives/:year/:month', 'init/blog/view',
requirements=dict(year='\d{2,4}', month='\d{1,2}'))
map.add('feeds/:category/atom.xml', 'ppp/blog/category')
#配置测试
print map.match('/archives/2003/10')
{'function': 'init/blog/view', 'args': ['2003', '10'], 'kwargs':
{'month': '10', 'year': '2003'}}
args为顺序参数,kwargs为字典形参数,与Route定义中的:year, :month对应。
print map.match('/archives/20033/10')
None
没有匹配上
print map.match('/feeds/perl/atom.xml')
{'function': 'ppp/blog/category', 'args': ['perl'], 'kwargs':
{'category': 'perl'}}
#生成反向url
print map.url(month='04', year='2004')
/archives/2004/04
print map.url(category='python')
/feeds/python/atom.xmlRoute是从Routes中提取出来了,去除了我认为暂时用不上的东西。有兴趣可以试一试。
urlmapping.py
#coding=utf-8
import re, urllib
def url_quote(string, encoding):
"""A Unicode handling version of urllib.quote_plus."""
if encoding:
if isinstance(string, unicode):
s = string.encode(encoding)
elif isinstance(string, str):
# assume the encoding is already correct
s = string
else:
s = unicode(string).encode(encoding)
else:
s = str(string)
return urllib.quote_plus(s, '/')
class Route(object):
"""The Route object holds a route recognition and generation routine.
See Route.__init__ docs for usage.
"""
def __init__(self, routepath, **kargs):
"""Initialize a route, with a given routepath for matching/generation
The set of keyword args will be used as defaults.
Usage::
>>> from routes.base import Route
>>> newroute = Route(':controller/:action/:id')
>>> newroute.defaults
{'action': 'index', 'id': None}
>>> newroute = Route('date/:year/:month/:day', controller="blog",
... action="view")
>>> newroute = Route('archives/:page', controller="blog",
... action="by_page", requirements = { 'page':'\d{1,2}' })
>>> newroute.reqs
{'page': '\\\d{1,2}'}
.. Note::
Route is generally not called directly, a Mapper instance connect
method should be used to add routes.
"""
self.routepath = routepath
self.sub_domains = False
self.prior = None
self.encoding = kargs.pop('_encoding', 'utf-8')
self.decode_errors = 'replace'
# Don't bother forming stuff we don't need if its a static route
self.static = kargs.get('_static', False)
self.filter = kargs.pop('_filter', None)
self.absolute = kargs.pop('_absolute', False)
# Pull out the member/collection name if present, this applies only to
# map.resource
self.member_name = kargs.pop('_member_name', None)
self.collection_name = kargs.pop('_collection_name', None)
self.parent_resource = kargs.pop('_parent_resource', None)
# Pull out route conditions
self.conditions = kargs.pop('conditions', None)
# Determine if explicit behavior should be used
self.explicit = kargs.pop('_explicit', False)
# reserved keys that don't count
reserved_keys = ['requirements']
# special chars to indicate a natural split in the URL
self.done_chars = ('/', ',', ';', '.', '#')
# Strip preceding '/' if present
if routepath.startswith('/'):
routepath = routepath[1:]
# Build our routelist, and the keys used in the route
self.routelist = routelist = self._pathkeys(routepath)
routekeys = frozenset([key['name'] for key in routelist \
if isinstance(key, dict)])
# Build a req list with all the regexp requirements for our args
self.reqs = kargs.get('requirements', {})
self.req_regs = {}
for key, val in self.reqs.iteritems():
self.req_regs[key] = re.compile('^' + val + '$')
# Update our defaults and set new default keys if needed. defaults
# needs to be saved
(self.defaults, defaultkeys) = self._defaults(routekeys,
reserved_keys, kargs)
# Save the maximum keys we could utilize
self.maxkeys = defaultkeys | routekeys
# Populate our minimum keys, and save a copy of our backward keys for
# quicker generation later
(self.minkeys, self.routebackwards) = self._minkeys(routelist[:])
# Populate our hardcoded keys, these are ones that are set and don't
# exist in the route
self.hardcoded = frozenset([key for key in self.maxkeys \
if key not in routekeys and self.defaults[key] is not None])
def make_unicode(self, s):
"""Transform the given argument into a unicode string."""
if isinstance(s, unicode):
return s
elif isinstance(s, str):
return s.decode(self.encoding)
else:
return unicode(s)
def _pathkeys(self, routepath):
"""Utility function to walk the route, and pull out the valid
dynamic/wildcard keys."""
collecting = False
current = ''
done_on = ''
var_type = ''
just_started = False
routelist = []
for char in routepath:
if char in [':', '*'] and not collecting:
just_started = True
collecting = True
var_type = char
if len(current) > 0:
routelist.append(current)
current = ''
elif collecting and just_started:
just_started = False
if char == '(':
done_on = ')'
else:
current = char
done_on = self.done_chars + ('-',)
elif collecting and char not in done_on:
current += char
elif collecting:
collecting = False
routelist.append(dict(type=var_type, name=current))
if char in self.done_chars:
routelist.append(char)
done_on = var_type = current = ''
else:
current += char
if collecting:
routelist.append(dict(type=var_type, name=current))
elif current:
routelist.append(current)
return routelist
def _minkeys(self, routelist):
"""Utility function to walk the route backwards
Will also determine the minimum keys we can handle to generate a
working route.
routelist is a list of the '/' split route path
defaults is a dict of all the defaults provided for the route
"""
minkeys = []
backcheck = routelist[:]
gaps = False
backcheck.reverse()
for part in backcheck:
if not isinstance(part, dict) and part not in self.done_chars:
gaps = True
continue
elif not isinstance(part, dict):
continue
key = part['name']
if self.defaults.has_key(key) and not gaps:
continue
minkeys.append(key)
gaps = True
return (frozenset(minkeys), backcheck)
def _defaults(self, routekeys, reserved_keys, kargs):
"""Creates default set with values stringified
Put together our list of defaults, stringify non-None values
and add in our action/id default if they use it and didn't specify it
defaultkeys is a list of the currently assumed default keys
routekeys is a list of the keys found in the route path
reserved_keys is a list of keys that are not
"""
defaults = {}
# Add in a controller/action default if they don't exist
if 'controller' not in routekeys and 'controller' not in kargs \
and not self.explicit:
kargs['controller'] = 'content'
if 'action' not in routekeys and 'action' not in kargs \
and not self.explicit:
kargs['action'] = 'index'
defaultkeys = frozenset([key for key in kargs.keys() \
if key not in reserved_keys])
for key in defaultkeys:
if kargs[key] is not None:
defaults[key] = self.make_unicode(kargs[key])
else:
defaults[key] = None
if 'action' in routekeys and not defaults.has_key('action') \
and not self.explicit:
defaults['action'] = 'index'
if 'id' in routekeys and not defaults.has_key('id') \
and not self.explicit:
defaults['id'] = None
newdefaultkeys = frozenset([key for key in defaults.keys() \
if key not in reserved_keys])
return (defaults, newdefaultkeys)
def makeregexp(self, clist):
"""Create a regular expression for matching purposes
Note: This MUST be called before match can function properly.
clist should be a list of valid controller strings that can be
matched, for this reason makeregexp should be called by the web
framework after it knows all available controllers that can be
utilized.
"""
(reg, noreqs, allblank) = self.buildnextreg(self.routelist, clist)
if not reg:
reg = '/'
reg = reg + '(/)?' + '$'
if not reg.startswith('/'):
reg = '/' + reg
reg = '^' + reg
self.regexp = reg
self.regmatch = re.compile(reg)
def buildnextreg(self, path, clist):
"""Recursively build our regexp given a path, and a controller list.
Returns the regular expression string, and two booleans that can be
ignored as they're only used internally by buildnextreg.
"""
if path:
part = path[0]
else:
part = ''
reg = ''
# noreqs will remember whether the remainder has either a string
# match, or a non-defaulted regexp match on a key, allblank remembers
# if the rest could possible be completely empty
(rest, noreqs, allblank) = ('', True, True)
if len(path[1:]) > 0:
self.prior = part
(rest, noreqs, allblank) = self.buildnextreg(path[1:], clist)
if isinstance(part, dict) and part['type'] == ':':
var = part['name']
partreg = ''
# First we plug in the proper part matcher
if self.reqs.has_key(var):
partreg = '(?P<' + var + '>' + self.reqs[var] + ')'
elif var == 'controller':
partreg = '(?P<' + var + '>' + '|'.join(map(re.escape, clist))
partreg += ')'
elif self.prior in ['/', '#']:
partreg = '(?P<' + var + '>[^' + self.prior + ']+?)'
else:
if not rest:
partreg = '(?P<' + var + '>[^%s]+?)' % '/'
else:
end = ''.join(self.done_chars)
rem = rest
if rem[0] == '\\' and len(rem) > 1:
rem = rem[1]
elif rem.startswith('(\\') and len(rem) > 2:
rem = rem[2]
else:
rem = end
rem = frozenset(rem) | frozenset(['/'])
partreg = '(?P<' + var + '>[^%s]+?)' % ''.join(rem)
if self.reqs.has_key(var):
noreqs = False
if not self.defaults.has_key(var):
allblank = False
noreqs = False
# Now we determine if its optional, or required. This changes
# depending on what is in the rest of the match. If noreqs is
# true, then its possible the entire thing is optional as there's
# no reqs or string matches.
if noreqs:
# The rest is optional, but now we have an optional with a
# regexp. Wrap to ensure that if we match anything, we match
# our regexp first. It's still possible we could be completely
# blank as we have a default
if self.reqs.has_key(var) and self.defaults.has_key(var):
reg = '(' + partreg + rest + ')?'
# Or we have a regexp match with no default, so now being
# completely blank form here on out isn't possible
elif self.reqs.has_key(var):
allblank = False
reg = partreg + rest
# If the character before this is a special char, it has to be
# followed by this
elif self.defaults.has_key(var) and \
self.prior in (',', ';', '.'):
reg = partreg + rest
# Or we have a default with no regexp, don't touch the allblank
elif self.defaults.has_key(var):
reg = partreg + '?' + rest
# Or we have a key with no default, and no reqs. Not possible
# to be all blank from here
else:
allblank = False
reg = partreg + rest
# In this case, we have something dangling that might need to be
# matched
else:
# If they can all be blank, and we have a default here, we know
# its safe to make everything from here optional. Since
# something else in the chain does have req's though, we have
# to make the partreg here required to continue matching
if allblank and self.defaults.has_key(var):
reg = '(' + partreg + rest + ')?'
# Same as before, but they can't all be blank, so we have to
# require it all to ensure our matches line up right
else:
reg = partreg + rest
elif isinstance(part, dict) and part['type'] == '*':
var = part['name']
if noreqs:
if self.defaults.has_key(var):
reg = '(?P<' + var + '>.*)' + rest
else:
reg = '(?P<' + var + '>.*)' + rest
allblank = False
noreqs = False
else:
if allblank and self.defaults.has_key(var):
reg = '(?P<' + var + '>.*)' + rest
elif self.defaults.has_key(var):
reg = '(?P<' + var + '>.*)' + rest
else:
allblank = False
noreqs = False
reg = '(?P<' + var + '>.*)' + rest
elif part and part[-1] in self.done_chars:
if allblank:
reg = re.escape(part[:-1]) + '(' + re.escape(part[-1]) + rest
reg += ')?'
else:
allblank = False
reg = re.escape(part) + rest
# We have a normal string here, this is a req, and it prevents us from
# being all blank
else:
noreqs = False
allblank = False
reg = re.escape(part) + rest
return (reg, noreqs, allblank)
def match(self, url, environ=None, sub_domains=False,
sub_domains_ignore=None, domain_match=''):
"""Match a url to our regexp.
While the regexp might match, this operation isn't
guaranteed as there's other factors that can cause a match to fail
even though the regexp succeeds (Default that was relied on wasn't
given, requirement regexp doesn't pass, etc.).
Therefore the calling function shouldn't assume this will return a
valid dict, the other possible return is False if a match doesn't work
out.
"""
# Static routes don't match, they generate only
if self.static:
return False
if url.endswith('/') and len(url) > 1:
url = url[:-1]
match = self.regmatch.match(url)
if not match:
return False
if not environ:
environ = {}
sub_domain = None
if environ.get('HTTP_HOST') and sub_domains:
host = environ['HTTP_HOST'].split(':')[0]
sub_match = re.compile('^(.+?)\.%s$' % domain_match)
subdomain = re.sub(sub_match, r'\1', host)
if subdomain not in sub_domains_ignore and host != subdomain:
sub_domain = subdomain
if self.conditions:
if self.conditions.has_key('method') and \
environ.get('REQUEST_METHOD') not in self.conditions['method']:
return False
# Check sub-domains?
use_sd = self.conditions.get('sub_domain')
if use_sd and not sub_domain:
return False
if isinstance(use_sd, list) and sub_domain not in use_sd:
return False
matchdict = match.groupdict()
result = {}
extras = frozenset(self.defaults.keys()) - frozenset(matchdict.keys())
for key, val in matchdict.iteritems():
if key != 'path_info' and self.encoding:
# change back into python unicode objects from the URL
# representation
try:
val = val and val.decode(self.encoding, self.decode_errors)
except UnicodeDecodeError:
return False
if not val and self.defaults.has_key(key) and self.defaults[key]:
result[key] = self.defaults[key]
else:
result[key] = val
for key in extras:
result[key] = self.defaults[key]
# Add the sub-domain if there is one
if sub_domains:
result['sub_domain'] = sub_domain
# If there's a function, call it with environ and expire if it
# returns False
if self.conditions and self.conditions.has_key('function') and \
not self.conditions['function'](environ, result):
return False
return result
def generate(self, _ignore_req_list=False, _append_slash=False, **kargs):
"""Generate a URL from ourself given a set of keyword arguments
Toss an exception if this
set of keywords would cause a gap in the url.
"""
# Verify that our args pass any regexp requirements
if not _ignore_req_list:
for key in self.reqs.keys():
val = kargs.get(key)
if val and not self.req_regs[key].match(self.make_unicode(val)):
return False
# Verify that if we have a method arg, its in the method accept list.
# Also, method will be changed to _method for route generation
meth = kargs.get('method')
if meth:
if self.conditions and 'method' in self.conditions \
and meth.upper() not in self.conditions['method']:
return False
kargs.pop('method')
routelist = self.routebackwards
urllist = []
gaps = False
for part in routelist:
if isinstance(part, dict) and part['type'] == ':':
arg = part['name']
# For efficiency, check these just once
has_arg = kargs.has_key(arg)
has_default = self.defaults.has_key(arg)
# Determine if we can leave this part off
# First check if the default exists and wasn't provided in the
# call (also no gaps)
if has_default and not has_arg and not gaps:
continue
# Now check to see if there's a default and it matches the
# incoming call arg
if (has_default and has_arg) and self.make_unicode(kargs[arg]) == \
self.make_unicode(self.defaults[arg]) and not gaps:
continue
# We need to pull the value to append, if the arg is None and
# we have a default, use that
if has_arg and kargs[arg] is None and has_default and not gaps:
continue
# Otherwise if we do have an arg, use that
elif has_arg:
val = kargs[arg]
elif has_default and self.defaults[arg] is not None:
val = self.defaults[arg]
# No arg at all? This won't work
else:
return False
urllist.append(url_quote(val, self.encoding))
if has_arg:
del kargs[arg]
gaps = True
elif isinstance(part, dict) and part['type'] == '*':
arg = part['name']
kar = kargs.get(arg)
if kar is not None:
urllist.append(url_quote(kar, self.encoding))
gaps = True
elif part and part[-1] in self.done_chars:
if not gaps and part in self.done_chars:
continue
elif not gaps:
urllist.append(part[:-1])
gaps = True
else:
gaps = True
urllist.append(part)
else:
gaps = True
urllist.append(part)
urllist.reverse()
url = ''.join(urllist)
if not url.startswith('/'):
url = '/' + url
extras = frozenset(kargs.keys()) - self.maxkeys
if extras:
if _append_slash and not url.endswith('/'):
url += '/'
url += '?'
fragments = []
for key in extras:
if key == 'action' or key == 'controller':
continue
val = kargs[key]
if isinstance(val, (tuple, list)):
for value in val:
fragments.append((key, value))
else:
fragments.append((key, val))
url += urllib.urlencode(fragments)
elif _append_slash and not url.endswith('/'):
url += '/'
return url
class URLMapping(object):
def __init__(self, encoding='utf-8'):
self.routes = []
self.keys = {}
self.encoding = encoding
def add(self, p, f, **kwargs):
name = kwargs.pop('_name', None)
r = Route(p, function=f, **kwargs)
c = 0
for i in r.routelist:
if isinstance(i, dict):
c += 1
r.count = c
if name:
self.keys[name] = r
r.makeregexp([])
self.routes.append(r)
def match(self, url):
for u in self.routes:
result = u.match(url)
if result:
result.pop('action')
result.pop('controller')
for k, v in result.items():
result[k] = v.encode(self.encoding)
function = result.pop('function')
args = []
for k in u.routelist:
if isinstance(k, dict):
args.append(result[k['name']])
r = {'function':function, 'kwargs':result, 'args':args}
return r
def url(self, *name, **kwargs):
if len(name)>0:
n = name[0]
else:
n = None
if n and n in self.keys:
r = self.keys[n]
return r.generate(**kwargs)
for u in self.routes:
if not u.count:
continue
result = u.generate(**kwargs)
if result:
return result
if __name__ == '__main__':
# urls = [
# ('archives/:year/:month', 'init.blog.view'),
# ('feeds/:category/atom.xml', 'ppp.blog.category'),
# ]
map = URLMapping()
map.add('favicon.ico', '/examples/static/favicon.ico'),
map.add('archives/:year/:month', 'init/blog/view', requirements=dict(year='\d{2,4}', month='\d{1,2}'))
map.add('feeds/:category/atom.xml', 'ppp/blog/category')
print map.match('/archives/2003/10')
print map.match('/archives/20033/10')
print map.match('/feeds/perl/atom.xml')
print map.url(month='04', year='2004')
print map.url(category='python')反馈
创建 by -- ZoomQuiet [2008-04-16 06:43:37]
