Last active
January 17, 2020 21:29
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
proutes output: