morepath.traject – Routing

Implementation of routing.

The idea is to turn the routes into a tree, so that the routes:

a/b
a/b/c
a/d

become a tree like this:

a
  b
  b
    c
  d

Nodes in the tree can have a value attached that can be found through routing; in Morepath the value is a model instance factory.

When presented with a path, Traject traverses this internal tree.

For a description of a similar algorithm also read: http://littledev.nl/?p=99

class morepath.traject.Node

A node in the traject tree.

add(step)

Add a step into the tree as a child node of this node.

add_name_node(step)

Add a step into the tree as a node that doesn’t match variables.

add_variable_node(step)

Add a step into the tree as a node that matches variables.

get(segment)

Match a path segment, traversing this node.

Matches non-variable nodes before nodes with variables in them.

Segment:a path segment
Returns:a (bool, variables) tuple. Bool is True if matched, variables is a dictionary with matched variables.
class morepath.traject.Path(path)

Helper when registering paths.

Used by morepath.App.path() to register inverse paths used for link generation.

Also used by morepath.App.path() for creating discriminators.

Parameters:path – the route.
discriminator()

Creates a unique discriminator for the path.

interpolation_str()

Create a string for interpolating variables.

Used for link generation (inverse).

variables()

Get the variables used by the path.

Returns:a list of variable names
class morepath.traject.Step(s, converters=None)

A single step in the tree.

Parameters:
  • s – the path segment, such as 'foo' or '{variable}' or 'foo{variable}bar'.
  • converters – dict of converters for variables.
__eq__(other)

True if this step is the same as another.

__ge__(other)

x.__ge__(y) <==> x>=y

__gt__(other)

x.__gt__(y) <==> x>y

__le__(other)

x.__le__(y) <==> x<=y

__lt__(other)

Used for inserting steps in correct place in the tree.

The order in which a step is inserted into the tree compared to its siblings affects which step preferentially matches first.

In Traject, steps that contain no variables match before steps that do contain variables. Steps with more specific variables sort before steps with more general ones, i.e. prefix{foo} sorts before {foo} as prefix{foo} is more specific.

__ne__(other)

True if this step is not equal to another.

discriminator_info()

Information needed to construct path discriminator.

get_converter(name)

Get converter for a variable name.

If no converter is listed explicitly, do no conversion.

has_variables()

True if there are any variables in this step.

match(s)

Match this step with actual path segment.

Parameters:s – path segment to match with
Returns:(bool, variables) tuple. The bool indicates whether s with the step or not. variables is a dictionary that contains converted variables that matched in this segment.
validate()

Validate whether step makes sense.

Raises morepath.error.TrajectError if there is a problem with the segment.

validate_parts()

Check whether all non-variable parts of the segment are valid.

Raises morepath.error.TrajectError if there is a problem with the segment.

validate_variables()

Check whether all variables of the segment are valid.

Raises morepath.error.TrajectError if there is a problem with the variables.

class morepath.traject.StepNode(step)

A node that is also a step in that it can match.

Parameters:step – the step
match(segment)

Match a segment with the step.

class morepath.traject.TrajectRegistry

Tree of route steps.

add_pattern(path, value, converters=None, absorb=False)

Add a route to the tree.

Path:route to add.
Value:the value to store for the end step of the route.
Converters:converters to store with the end step of the route
Absorb:does this path absorb all segments
consume(stack)

Consume a stack given routes.

Parameters:stack – the stack of segments on a path, reversed so that the first segment of the path is on top.
Returns:value, stack, variables tuple: value is the value registered with the deepest node that matched, stack is the remaining segment stack and variables are the variables matched with the segments.
morepath.traject.create_path(segments)

Builds a path from a list of segments.

Parameters:stack – a list of segments
Returns:a path
morepath.traject.create_variables_re(s)

Create regular expression that matches variables from route segment.

Parameters:s – a route segment with variables in it.
Returns:a regular expression that matches with variables for the route.
morepath.traject.generalize_variables(s)

Generalize a route segment.

Parameters:s – a route segment.
Returns:a generalized route where all variables are empty ({}).
morepath.traject.interpolation_str(s)

Create a Python string with interpolation variables for a route segment.

Given a{foo}b, creates a%sb.

morepath.traject.is_identifier(s)

Check whether a variable name is a proper identifier.

Parameters:s – variable
Returns:True if variable is an identifier.
morepath.traject.normalize_path(path)

Converts path into normalized path.

Rules:

  • Collapse dots:

    >>> normalize_path('/../blog')
    '/blog'
    
  • Ensure absolute paths:

    >>> normalize_path('./site')
    '/site'
    
  • Remove double-slashes:

    >>> normalize_path('//index')
    '/index'
    

For example:

>>> normalize_path('../static//../app.py')
'/app.py'
Parameters:path – path string to parse
Returns:normalized path.
morepath.traject.parse_path(path)

Parses path, creates normalized segment list.

Dots are collapsed:

>>> parse_path('../static//../app.py')
['app.py']
Parameters:path – path string to parse
Returns:normalized list of path segments.
morepath.traject.parse_variables(s)

Parse variables out of a segment.

Raised a morepath.error.TrajectError` if a variable is not a valid identifier.

Parameters:s – a path segment
Returns:a list of variables.
morepath.traject.IDENTIFIER = <_sre.SRE_Pattern object>

regex for a valid variable name in a route.

same rule as for Python identifiers.

morepath.traject.PATH_VARIABLE = <_sre.SRE_Pattern object>

regex to find curly-brace marked variables {foo} in routes.