Skip to content

Instantly share code, notes, and snippets.

@merwok
Last active January 17, 2020 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save merwok/c17b11dfa4e462efc6ea0d8ea9585c0c to your computer and use it in GitHub Desktop.
Save merwok/c17b11dfa4e462efc6ea0d8ea9585c0c to your computer and use it in GitHub Desktop.
test app for custom predicate docs — https://github.com/Pylons/pyramid/pull/3560
[app:main]
use = call:helloapp:main
pyramid.debug_notfound = true
pyramid.debug_routematch = true
pyramid.includes =
pyramid_debugtoolbar
[server:main]
use = egg:waitress#main
listen = localhost:6455
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
class AnyOfPredicate:
def __init__(self, val, info):
self.segment_name = val[0]
self.allowed = tuple(val[0:])
def text(self):
args = (self.segment_name,) + self.allowed
return "any_of = %s" % (args,)
phash = text
def __call__(self, info, request):
return info["match"][self.segment_name] in self.allowed
class IntegersPredicate:
def __init__(self, val, info):
self.segment_names = val
def text(self):
return "integers = %s" % (self.segment_names,)
phash = text
def __call__(self, info, request):
match = info["match"]
for segment_name in self.segment_names:
match[segment_name] = int(match[segment_name])
return True
class TwentyTenPredicate:
def __init__(self, val, info):
pass
def text(self):
return "twenty_ten = True"
phash = text
def __call__(self, info, request):
if info["route"].name in ("ymd", "ym", "y"):
return info["match"]["year"] == "2010"
def hello_world(request):
return Response("Hello World!")
def includeme(config):
# make sure app works
config.add_route("hello", "/")
config.add_view(hello_world, route_name="hello")
# test 1
config.add_route_predicate("any_of", AnyOfPredicate)
config.add_route(
"route_to_num", "/{num}", any_of=("num", "one", "two", "three")
)
# test 2
config.add_route_predicate("integers", IntegersPredicate)
config.add_route("ymd2", r"/{year:\d+}/{month:\d+}/{day:\d+}",
integers=("year", "month", "day"))
# test 3
config.add_route_predicate("twenty_ten", TwentyTenPredicate)
config.add_route("y", "/{year}", twenty_ten=True)
config.add_route("ym", "/{year}/{month}", twenty_ten=True)
config.add_route("ymd", "/{year}/{month}/{day}", twenty_ten=True)
def main(global_config, **settings):
config = Configurator(settings=settings)
config.include(includeme)
return config.make_wsgi_app()
if __name__ == "__main__":
global_config = {}
settings = {
"pyramid.debug_notfound": True,
"pyramid.debug_routematch": True,
}
app = main(global_config, **settings)
server = make_server("localhost", 0, app)
print(f"Serving helloapp on http://localhost:{server.server_port}/")
server.serve_forever()
@merwok
Copy link
Author

merwok commented Jan 17, 2020

proutes output:

Name                         Pattern                              View                            Method    
----                         -------                              ----                            ------    
hello                        /                                    helloapp.hello_world            *         
route_to_num                 /{num}                               <unknown>                       *         
ymd2                         /{year:\d+}/{month:\d+}/{day:\d+}    <unknown>                       *         
y                            /{year}                              <unknown>                       *         
ym                           /{year}/{month}                      <unknown>                       *         
ymd                          /{year}/{month}/{day}                <unknown>                       *         
debugtoolbar                 /_debug_toolbar/*subpath             <unknown>                       *         
__/_debug_toolbar/static/    /_debug_toolbar/static/*subpath      pyramid_debugtoolbar:static/    *  

@merwok
Copy link
Author

merwok commented Jan 17, 2020

Debug toolbar 1:
dbtb1

Debug toolbar 2 (partial):
dbtb2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment