summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris McDonough <chrism@plope.com>2011-04-27 02:00:30 -0400
committerChris McDonough <chrism@plope.com>2011-04-27 02:00:30 -0400
commit10b8a7120f35497bfea83d2ade2c2915c96861d4 (patch)
tree95db547cf8f17dcde980ce821a686e64e8dac997
parent1b395e6d43b9450ea652d9addb675936b507ea52 (diff)
downloadpyramid-10b8a7120f35497bfea83d2ade2c2915c96861d4.tar.gz
pyramid-10b8a7120f35497bfea83d2ade2c2915c96861d4.tar.bz2
pyramid-10b8a7120f35497bfea83d2ade2c2915c96861d4.zip
use __doc__ instead of __text__ to represent predicate output; inject bfg.routes.matchdict into environ to prevent return of wrong subpath; expose __predicates__ as actual predicates rather than text for ease of copying (although i dont like it)
-rw-r--r--pyramid/config.py46
-rw-r--r--pyramid/paster.py13
-rw-r--r--pyramid/tests/test_paster.py49
3 files changed, 53 insertions, 55 deletions
diff --git a/pyramid/config.py b/pyramid/config.py
index 6206d64c4..a0c3f7a00 100644
--- a/pyramid/config.py
+++ b/pyramid/config.py
@@ -2440,17 +2440,16 @@ def _make_predicates(xhr=None, request_method=None, path_info=None,
if xhr:
def xhr_predicate(context, request):
+ """xhr = True"""
return request.is_xhr
- xhr_predicate.__text__ = "xhr = True"
weights.append(1 << 1)
predicates.append(xhr_predicate)
h.update('xhr:%r' % bool(xhr))
if request_method is not None:
def request_method_predicate(context, request):
+ """request_method = %s""" % request_method
return request.method == request_method
- msg = "request method = %s"
- request_method_predicate.__text__ = msg % request_method
weights.append(1 << 2)
predicates.append(request_method_predicate)
h.update('request_method:%r' % request_method)
@@ -2461,9 +2460,8 @@ def _make_predicates(xhr=None, request_method=None, path_info=None,
except re.error, why:
raise ConfigurationError(why[0])
def path_info_predicate(context, request):
+ """path_info = %s""" % path_info
return path_info_val.match(request.path_info) is not None
- msg = "path_info = %s"
- path_info_predicate.__text__ = msg % path_info
weights.append(1 << 3)
predicates.append(path_info_predicate)
h.update('path_info:%r' % path_info)
@@ -2472,15 +2470,15 @@ def _make_predicates(xhr=None, request_method=None, path_info=None,
request_param_val = None
if '=' in request_param:
request_param, request_param_val = request_param.split('=', 1)
- def request_param_predicate(context, request):
- if request_param_val is None:
- return request_param in request.params
- return request.params.get(request_param) == request_param_val
if request_param_val is None:
msg = "request_param %s" % request_param
else:
msg = "request_param %s = %s" % (request_param, request_param_val)
- request_param_predicate.__text__ = msg
+ def request_param_predicate(context, request):
+ """%s""" % msg
+ if request_param_val is None:
+ return request_param in request.params
+ return request.params.get(request_param) == request_param_val
weights.append(1 << 4)
predicates.append(request_param_predicate)
h.update('request_param:%r=%r' % (request_param, request_param_val))
@@ -2494,43 +2492,42 @@ def _make_predicates(xhr=None, request_method=None, path_info=None,
header_val = re.compile(header_val)
except re.error, why:
raise ConfigurationError(why[0])
+ if header_val is None:
+ msg = "header %s" % header_name
+ else:
+ msg = "header %s = %s" % (header_name, header_val)
def header_predicate(context, request):
+ """%s""" % msg
if header_val is None:
return header_name in request.headers
val = request.headers.get(header_name)
if val is None:
return False
return header_val.match(val) is not None
- if header_val is None:
- msg = "header %s" % header_name
- else:
- msg = "header %s = %s" % (header_name, header_val)
- header_predicate.__text__ = msg
weights.append(1 << 5)
predicates.append(header_predicate)
h.update('header:%r=%r' % (header_name, header_val))
if accept is not None:
def accept_predicate(context, request):
+ """accept = %s""" % accept
return accept in request.accept
- accept_predicate.__text__ = "accept = %s" % accept
weights.append(1 << 6)
predicates.append(accept_predicate)
h.update('accept:%r' % accept)
if containment is not None:
def containment_predicate(context, request):
+ """containment = %s""" % containment
return find_interface(context, containment) is not None
- containment_predicate.__text__ = "containment = %s" % containment
weights.append(1 << 7)
predicates.append(containment_predicate)
h.update('containment:%r' % hash(containment))
if request_type is not None:
def request_type_predicate(context, request):
+ """request_type = %s""" % request_type
return request_type.providedBy(request)
- msg = "request type = %s" % request_type
- request_type_predicate.__text__ = msg
weights.append(1 << 8)
predicates.append(request_type_predicate)
h.update('request_type:%r' % hash(request_type))
@@ -2549,7 +2546,6 @@ def _make_predicates(xhr=None, request_method=None, path_info=None,
tvalue = tgenerate(m)
m['traverse'] = traversal_path(tvalue)
return True
- traverse_predicate.__text__ = "traverse = True"
# This isn't actually a predicate, it's just a infodict
# modifier that injects ``traverse`` into the matchdict. As a
# result, the ``traverse_predicate`` function above always
@@ -2559,8 +2555,8 @@ def _make_predicates(xhr=None, request_method=None, path_info=None,
if custom:
for num, predicate in enumerate(custom):
- if not hasattr(predicate, '__text__'):
- predicate.__text__ = "custom predicate"
+ if getattr(predicate, '__doc__', None) is None:
+ predicate.__doc__ = "<unknown custom predicate>"
predicates.append(predicate)
# using hash() here rather than id() is intentional: we
# want to allow custom predicates that are part of
@@ -2680,8 +2676,10 @@ def preserve_view_attrs(view, wrapped_view):
pass
try:
wrapped_view.__predicated__ = view.__predicated__
- wrapped_view.__predicates__ = [p.__text__
- for p in view.__predicates__]
+ except AttributeError:
+ pass
+ try:
+ wrapped_view.__predicates__ = view.__predicates__
except AttributeError:
pass
try:
diff --git a/pyramid/paster.py b/pyramid/paster.py
index 4437db497..a7d813bbc 100644
--- a/pyramid/paster.py
+++ b/pyramid/paster.py
@@ -288,8 +288,7 @@ class PViewsCommand(PCommand):
IView, name='', default=None)
if view is None:
continue
- view.__predicates__ = [p.__text__
- for p in route.predicates]
+ view.__predicates__ = list(route.predicates)
view.__route_attrs__ = {'matchdict': match,
'matched_route': route,
'subpath': subpath}
@@ -321,7 +320,7 @@ class PViewsCommand(PCommand):
if route is not None:
attrs['matchdict'] = match
attrs['matched_route'] = route
-
+ request.environ['bfg.routes.matchdict'] = match
request_iface = registry.queryUtility(
IRouteRequest,
name=route.name,
@@ -394,8 +393,8 @@ class PViewsCommand(PCommand):
self.out(" required permission = %s" % permission)
predicates = getattr(view_wrapper, '__predicates__', None)
if predicates is not None:
- for text in predicates:
- self.out(" %s" % text)
+ for predicate in predicates:
+ self.out(" %s" % predicate.__doc__)
def output_view_info(self, view):
if view is not None:
@@ -413,8 +412,8 @@ class PViewsCommand(PCommand):
self.out(" required permission = %s" % permission)
predicates = getattr(view, '__predicates__', None)
if predicates is not None:
- for text in predicates:
- self.out(" %s" % text)
+ for predicate in predicates:
+ self.out(" %s" % predicate.__doc__)
def command(self):
from pyramid.interfaces import IMultiView
diff --git a/pyramid/tests/test_paster.py b/pyramid/tests/test_paster.py
index 85a79b681..4d7220524 100644
--- a/pyramid/tests/test_paster.py
+++ b/pyramid/tests/test_paster.py
@@ -350,8 +350,8 @@ class TestPViewsCommand(unittest.TestCase):
implements(IMyRoot)
def __init__(self, request):
pass
- routes = [DummyRoute('a', '/a', factory=Factory),
- DummyRoute('b', '/b', factory=Factory, will_match=False)]
+ routes = [DummyRoute('a', '/a', factory=Factory, matchdict={}),
+ DummyRoute('b', '/b', factory=Factory)]
self._register_mapper(registry, routes)
command = self._makeOne()
result = command._find_view('/a', registry)
@@ -380,8 +380,8 @@ class TestPViewsCommand(unittest.TestCase):
def __init__(self, request):
pass
registry.registerUtility(Factory, IRootFactory)
- routes = [DummyRoute('a', '/a'),
- DummyRoute('b', '/a')]
+ routes = [DummyRoute('a', '/a', matchdict={}),
+ DummyRoute('b', '/a', matchdict={})]
self._register_mapper(registry, routes)
command = self._makeOne()
result = command._find_view('/a', registry)
@@ -418,8 +418,8 @@ class TestPViewsCommand(unittest.TestCase):
def __init__(self, request):
pass
registry.registerUtility(Factory, IRootFactory)
- routes = [DummyRoute('a', '/a'),
- DummyRoute('b', '/a')]
+ routes = [DummyRoute('a', '/a', matchdict={}),
+ DummyRoute('b', '/a', matchdict={})]
self._register_mapper(registry, routes)
command = self._makeOne()
result = command._find_view('/a', registry)
@@ -431,29 +431,29 @@ class TestPViewsCommand(unittest.TestCase):
def test__find_multi_routes_all_match(self):
command = self._makeOne()
def factory(request): pass
- routes = [DummyRoute('a', '/a', factory=factory),
- DummyRoute('b', '/a', factory=factory)]
+ routes = [DummyRoute('a', '/a', factory=factory, matchdict={}),
+ DummyRoute('b', '/a', factory=factory, matchdict={})]
mapper = DummyMapper(*routes)
request = DummyRequest({'PATH_INFO':'/a'})
result = command._find_multi_routes(mapper, request)
- self.assertEqual(result, [{'match':True, 'route':routes[0]},
- {'match':True, 'route':routes[1]}])
+ self.assertEqual(result, [{'match':{}, 'route':routes[0]},
+ {'match':{}, 'route':routes[1]}])
def test__find_multi_routes_some_match(self):
command = self._makeOne()
def factory(request): pass
- routes = [DummyRoute('a', '/a', factory=factory, will_match=False),
- DummyRoute('b', '/a', factory=factory)]
+ routes = [DummyRoute('a', '/a', factory=factory),
+ DummyRoute('b', '/a', factory=factory, matchdict={})]
mapper = DummyMapper(*routes)
request = DummyRequest({'PATH_INFO':'/a'})
result = command._find_multi_routes(mapper, request)
- self.assertEqual(result, [{'match':True, 'route':routes[1]}])
+ self.assertEqual(result, [{'match':{}, 'route':routes[1]}])
def test__find_multi_routes_none_match(self):
command = self._makeOne()
def factory(request): pass
- routes = [DummyRoute('a', '/a', factory=factory, will_match=False),
- DummyRoute('b', '/a', factory=factory, will_match=False)]
+ routes = [DummyRoute('a', '/a', factory=factory),
+ DummyRoute('b', '/a', factory=factory)]
mapper = DummyMapper(*routes)
request = DummyRequest({'PATH_INFO':'/a'})
result = command._find_multi_routes(mapper, request)
@@ -541,8 +541,10 @@ class TestPViewsCommand(unittest.TestCase):
registry = Registry()
L = []
command.out = L.append
+ def predicate():
+ """predicate = x"""
view = DummyView(context='context', view_name='a')
- view.__predicates__ = ['predicate = x']
+ view.__predicates__ = [predicate]
command._find_view = lambda arg1, arg2: view
app = DummyApp()
app.registry = registry
@@ -563,7 +565,7 @@ class TestPViewsCommand(unittest.TestCase):
registry = Registry()
L = []
command.out = L.append
- route = DummyRoute('a', '/a')
+ route = DummyRoute('a', '/a', matchdict={})
view = DummyView(context='context', view_name='a',
matched_route=route, subpath='')
command._find_view = lambda arg1, arg2: view
@@ -637,10 +639,12 @@ class TestPViewsCommand(unittest.TestCase):
registry = Registry()
L = []
command.out = L.append
+ def predicate():
+ """predicate = x"""
view = DummyView(context='context')
view.__name__ = 'view'
view.__view_attr__ = 'call'
- view.__predicates__ = ['predicate = x']
+ view.__predicates__ = [predicate]
multiview = DummyMultiView(view, context='context', view_name='a')
command._find_view = lambda arg1, arg2: multiview
app = DummyApp()
@@ -748,19 +752,16 @@ class DummyMapper(object):
return self.routes
class DummyRoute(object):
- def __init__(self, name, pattern, factory=None, will_match=True):
+ def __init__(self, name, pattern, factory=None, matchdict=None):
self.name = name
self.path = pattern
self.pattern = pattern
self.factory = factory
- if not will_match:
- self.will_match = None
- else:
- self.will_match = will_match
+ self.matchdict = matchdict
self.predicates = []
def match(self, route):
- return self.will_match
+ return self.matchdict
class DummyRequest:
application_url = 'http://example.com:5432'