Modules

wheezy.routing

Package wheezy.routing.

wheezy.routing.builders

builders module.

wheezy.routing.builders.build_route(pattern, kwargs, route_builders)[source]

If pattern is an object drived from Route than it simply returned.

>>> pattern = PlainRoute(r'abc')
>>> r = build_route(pattern, None, [])
>>> assert pattern == r

If pattern is a string than try to find sutable route builder to create a route.

>>> from wheezy.routing.config import route_builders
>>> r = build_route(r'abc', {'a': 1}, route_builders)
>>> assert isinstance(r, PlainRoute)
>>> r.kwargs
{'a': 1}

Otherwise raise LookupError

>>> r = build_route(r'abc', None, [])
Traceback (most recent call last):
    ...
LookupError: No matching route factory found
wheezy.routing.builders.try_build_curly_route(pattern, kwargs=None)[source]

Convert pattern expression into regex with named groups and create regex route.

>>> r = try_build_curly_route('abc/{n}')
>>> assert isinstance(r, RegexRoute)

Otherwise return None.

>>> r = try_build_curly_route('abc')
wheezy.routing.builders.try_build_plain_route(pattern, kwargs=None)[source]

If the plain route regular expression match the pattern than create a PlainRoute instance.

>>> r = try_build_plain_route(r'abc')
>>> assert isinstance(r, PlainRoute)

Otherwise return None.

>>> r = try_build_plain_route(r'ab[c]')
>>> assert r is None
wheezy.routing.builders.try_build_regex_route(pattern, kwargs=None)[source]

There is no special tests to match regex selection strategy.

>>> r = try_build_regex_route(r'abc')
>>> assert isinstance(r, RegexRoute)

wheezy.routing.config

config module.

wheezy.routing.curly

curly module.

wheezy.routing.curly.convert(s)[source]

Convert curly expression into regex with named groups.

>>> convert(r'abc/{id}')
'abc/(?P<id>[^/]+)'
>>> convert(r'abc/{id:i}')
'abc/(?P<id>\\d+)'
>>> convert(r'abc/{n}/{x:w}')
'abc/(?P<n>[^/]+)/(?P<x>\\w+)'
wheezy.routing.curly.parse(s)[source]

Parse s according to group_name:pattern_name.

There is just group_name, return default pattern_name. >>> parse(‘abc’) (‘abc’, ‘s’)

Otherwise both. >>> parse(‘abc:i’) (‘abc’, ‘i’)

wheezy.routing.curly.replace(val)[source]

Replace {group_name:pattern_name} by regex with named groups.

If the val is not an expression in curly brackets simply return it. >>> replace(‘abc’) ‘abc’

If the pattern_name is not specified, use default one.

>>> replace('{abc}')
'(?P<abc>[^/]+)'

Replace the pattern_name with regex from patterns dict.

>>> replace('{abc:i}')
'(?P<abc>\\d+)'

The pattern_name not found raise KeyError.

>>> replace('{abc:x}')
Traceback (most recent call last):
    ...
KeyError: 'x'

wheezy.routing.route

route module.

class wheezy.routing.route.PlainRoute(pattern, kwargs=None)[source]

Route based on string equalty operation.

equals_match(path)[source]

If the path exactly equals pattern string, return end index of substring matched and a copy of self.kwargs.

>>> r = PlainRoute(r'abc')
>>> matched, kwargs = r.equals_match('abc')
>>> matched
3
>>> kwargs

Match returns self.kwargs.

>>> r = PlainRoute(r'abc', {'a': 1})
>>> matched, kwargs = r.equals_match('abc')
>>> matched
3
>>> kwargs
{'a': 1}

Otherwise return (-1, None).

>>> matched, kwargs = r.equals_match('abc/')
>>> matched
-1
>>> matched, kwargs = r.equals_match('bc')
>>> matched
-1
>>> kwargs
path(values=None)[source]

Build the path for given route by simply returning the pattern used during initialization.

>>> r = PlainRoute(r'abc')
>>> r.path()
'abc'
startswith_match(path)[source]

If the path starts with pattern string, return the end of substring matched and self.kwargs.

>>> r = PlainRoute(r'abc')
>>> matched, kwargs = r.startswith_match('abc')
>>> matched
3
>>> kwargs

Match returns self.kwargs.

>>> r = PlainRoute(r'abc', {'a': 1})
>>> matched, kwargs = r.startswith_match('abc/')
>>> matched
3
>>> kwargs
{'a': 1}

Otherwise return (None, None).

>>> matched, kwargs = r.startswith_match('bc')
>>> matched
-1
>>> kwargs
class wheezy.routing.route.RegexRoute(pattern, kwargs=None)[source]

Route based on regular expression matching.

match_no_kwargs(path)[source]

If the path match the regex pattern.

>>> r = RegexRoute(r'abc/(?P<id>\d+$)')
>>> matched, kwargs = r.match_no_kwargs('abc/1234')
>>> matched
8
>>> kwargs
{'id': '1234'}

Otherwise return (-1, None).

>>> matched, kwargs = r.match_no_kwargs('abc/x')
>>> matched
-1
>>> kwargs
match_with_kwargs(path)[source]

If the path match the regex pattern.

>>> r = RegexRoute(r'abc/\d+', {'lang': 'en'})
>>> matched, kwargs = r.match_with_kwargs('abc/1234')
>>> matched
8
>>> kwargs
{'lang': 'en'}
>>> r = RegexRoute(r'abc/(?P<id>\d+$)', {
...     'lang': 'en'
... })
>>> matched, kwargs = r.match_with_kwargs('abc/1234')
>>> kwargs
{'lang': 'en', 'id': '1234'}

kwargs from pattern match must override defaults.

>>> r = RegexRoute(r'abc/?(?P<id>\d*$)', {'id': '1'})
>>> matched, kwargs = r.match_with_kwargs('abc')
>>> kwargs
{'id': '1'}
>>> matched, kwargs = r.match_with_kwargs('abc/1234')
>>> kwargs
{'id': '1234'}

Otherwise return (-1, None).

>>> matched, kwargs = r.match_with_kwargs('abc/x')
>>> matched
-1
>>> kwargs
path_no_kwargs(values=None)[source]

Build the path for the given route by substituting the named places of the regual expression.

>>> r = RegexRoute(
...     r'abc/(?P<month>\d+)/(?P<day>\d+)'
... )
>>> r.path_no_kwargs(dict(month=6, day=9))
'abc/6/9'
>>> r.path_no_kwargs(dict(month=6))
'abc/6'
>>> r.path()
'abc'
path_with_kwargs(values=None)[source]

Build the path for the given route by substituting the named places of the regual expression.

>>> r = RegexRoute(
...     r'abc/(?P<month>\d+)/(?P<day>\d+)',
...     dict(month=1, day=1)
... )
>>> r.path_with_kwargs(dict(month=6, day=9))
'abc/6/9'
>>> r.path_with_kwargs(dict(month=6))
'abc/6/1'
>>> r.path()
'abc/1/1'
class wheezy.routing.route.Route[source]

Route abstract contract.

match(path)[source]

if the path matches, return the end of substring matched and kwargs. Otherwise return (-1, None).

>>> r = Route()
>>> matched, kwargs = r.match('x')
Traceback (most recent call last):
    ...
NotImplementedError
path(values=None)[source]

Build the path for given route.

>>> r = Route()
>>> r.path(dict(id=1234))
Traceback (most recent call last):
    ...
NotImplementedError

wheezy.routing.router

router module.

class wheezy.routing.router.PathRouter(route_builders=None)[source]
add_route(pattern, handler, kwargs=None, name=None)[source]

Adds a pattern to route table

>>> r = PathRouter()
>>> class Login: pass
>>> r.add_route(r'login', Login)
>>> assert r.route_map['login']

You can override the generated name by suppling optional name parameter

>>> r.add_route(r'login', Login, name='signin')
>>> assert r.route_map['signin']
>>> r.path_for('signin')
'login'
add_routes(mapping)[source]

Adds routes represented as a list of tuple (pattern, handler) to route table

>>> r = PathRouter()
>>> class Login: pass
>>> r.add_routes([
...     (r'login', Login)
... ])
>>> assert r.mapping
>>> assert r.route_map

If handler is tuple, list or an instance of PathRouter than we proceed with include function

>>> r = PathRouter()
>>> class Login: pass
>>> admin_routes = [(r'login', Login)]
>>> r.add_routes([
...     (r'admin/', admin_routes)
... ])
>>> len(r.routers)
1
>>> len(r.mapping)
1
include(pattern, included, kwargs=None)[source]

Includes nested routes below the current.

>>> r = PathRouter()
>>> class Login: pass
>>> admin_routes = [
...     (r'login', Login)
... ]
>>> r.include(r'admin/', admin_routes)
>>> route, inner = r.mapping[0]
>>> inner2, route2 = r.routers[0]
>>> assert route == route2
>>> assert inner == inner2
>>> assert isinstance(inner, PathRouter)
match(path)[source]

Tries to find a match for the given path in route table. Returns a tupple of (handler, kwargs)

>>> r = PathRouter()
>>> class Login: pass
>>> class Message: pass
>>> r.add_route(r'login', Login)
>>> handler, kwargs = r.match(r'login')
>>> assert handler == Login
>>> kwargs

Tries to find inner match

>>> r = PathRouter()
>>> admin_routes = [
...     (r'login', Login
... )]
>>> r.add_routes([
...     (r'admin/', admin_routes)
... ])
>>> handler, kwargs = r.match(r'admin/login')
>>> assert handler == Login

Merge kwargs

>>> r = PathRouter()
>>> admin_routes = [
...     (r'msg', Message, {'id': 1})
... ]
>>> r.add_routes([
...     (r'en/', admin_routes, {'lang': 'en'})
... ])
>>> handler, kwargs = r.match(r'en/msg')
>>> assert handler == Message
>>> kwargs
{'lang': 'en', 'id': 1}

Otherwise return (None, None)

>>> r = PathRouter()
>>> handler, kwargs = r.match(r'')
>>> handler
>>> kwargs
path_for(name, **kwargs)[source]

Returns the url for the given route name.

>>> r = PathRouter()
>>> class Login: pass
>>> r.add_route(r'login', Login)
>>> r.path_for(r'login')
'login'

Path for inner router

>>> r = PathRouter()
>>> admin_routes = [
...     (r'login', Login, None, 'signin')
... ]
>>> r.add_routes([
...     (r'admin/', admin_routes)
... ])
>>> r.path_for(r'signin')
'admin/login'

Otherwise None

>>> r.path_for(r'unknown')
wheezy.routing.router.url(pattern, handler, kwargs=None, name=None)[source]

Converts parameters to tupple of length four. Used for convenience to name parameters and skip unused.

>>> url(r'msg', 'handler', {'id': 1}, name='message')
('msg', 'handler', {'id': 1}, 'message')

Usage:

>>> class Login: pass
>>> admin_routes = [
...     url(r'login', Login, name='signin')
... ]
>>> r = PathRouter()
>>> r.add_routes([
...     url(r'admin/', admin_routes, kwargs={'site_id': 1})
... ])

wheezy.routing.utils

utils module.

wheezy.routing.utils.camelcase_to_underscore(s)[source]

Convert CamelCase to camel_case. >>> camelcase_to_underscore(‘MainPage’) ‘main_page’ >>> camelcase_to_underscore(‘Login’) ‘login’

wheezy.routing.utils.merge(d1, d2)[source]

Update d1 from d2 only if a value in d2 is evaluated to boolean True.

>>> d1 = dict(a=1,b=None,c=2)
>>> d2 = dict(a=None,b=2,c=1)
>>> merge(d1, d2)
{'a': 1, 'c': 1, 'b': 2}
wheezy.routing.utils.route_name(handler)[source]

Return a name for the given handler. handler can be an object, class or callable.

>>> class Login: pass
>>> route_name(Login)
'login'
>>> l = Login()
>>> route_name(l)
'login'
wheezy.routing.utils.strip_name(s)[source]

Strips the name per RE_STRIP_NAME regex.

>>> strip_name('Login')
'Login'
>>> strip_name('LoginHandler')
'Login'
>>> strip_name('LoginController')
'Login'
>>> strip_name('LoginHandler2')
'LoginHandler2'

Table Of Contents

Previous topic

User Guide

This Page