wsme.protocols.expose now accept templated paths, and can expose a single function several times
This commit is contained in:
parent
21b5ccc632
commit
d3b6268720
@ -27,7 +27,7 @@ class expose(object):
|
|||||||
func.exposed = True
|
func.exposed = True
|
||||||
cfg = _cfg(func)
|
cfg = _cfg(func)
|
||||||
cfg['content-type'] = self.content_type
|
cfg['content-type'] = self.content_type
|
||||||
cfg['path'] = self.path
|
cfg.setdefault('paths', []).append(self.path)
|
||||||
return func
|
return func
|
||||||
|
|
||||||
|
|
||||||
@ -44,17 +44,33 @@ class CallContext(object):
|
|||||||
return self._request()
|
return self._request()
|
||||||
|
|
||||||
|
|
||||||
|
class ObjectDict(object):
|
||||||
|
def __init__(self, obj):
|
||||||
|
self.obj = obj
|
||||||
|
|
||||||
|
def __getitem__(self, name):
|
||||||
|
return getattr(self.obj, name)
|
||||||
|
|
||||||
|
|
||||||
class Protocol(object):
|
class Protocol(object):
|
||||||
name = None
|
name = None
|
||||||
displayname = None
|
displayname = None
|
||||||
dataformat = None
|
dataformat = None
|
||||||
content_types = []
|
content_types = []
|
||||||
|
|
||||||
|
def resolve_path(self, path):
|
||||||
|
if '$' in path:
|
||||||
|
from string import Template
|
||||||
|
s = Template(path)
|
||||||
|
path = s.substitute(ObjectDict(self))
|
||||||
|
return path
|
||||||
|
|
||||||
def iter_routes(self):
|
def iter_routes(self):
|
||||||
for attrname in dir(self):
|
for attrname in dir(self):
|
||||||
attr = getattr(self, attrname)
|
attr = getattr(self, attrname)
|
||||||
if getattr(attr, 'exposed', False):
|
if getattr(attr, 'exposed', False):
|
||||||
yield _cfg(attr)['path'], attr
|
for path in _cfg(attr)['paths']:
|
||||||
|
yield self.resolve_path(path), attr
|
||||||
|
|
||||||
def accept(self, request):
|
def accept(self, request):
|
||||||
if request.path.endswith('.' + self.dataformat):
|
if request.path.endswith('.' + self.dataformat):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user