-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatch.py
37 lines (28 loc) · 841 Bytes
/
dispatch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from werkzeug.routing import Map, Rule
from werkzeug import Local, LocalManager
url_map = Map()
local = Local()
local_manager = LocalManager([local])
application = local('application')
_api_prefix = '/_<any(json,xml):api>'
def expose(rule, with_api=True, **kw):
def decorator(f):
kw['endpoint'] = f.__name__
if with_api:
url_map.add(Rule(_api_prefix + rule, **kw))
else:
url_map.add(Rule(rule, **kw))
return f
return decorator
def url_for(endpoint, _external=False, **values):
return local.url_adapter.build(endpoint, values, force_external=_external)
def set2path(s, *args):
if isinstance(s, set):
s2 = s.union(args)
else:
s2 = s + list(args)
return u'/'.join((unicode(x) for x in sorted(s2)))
def get_adapter(environ):
return url_map.bind_to_environ(environ)