morepath.traject – Routing

Implementation of routing.

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


become a tree like this:


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:

class morepath.traject.Node

A node in the traject tree.


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


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


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


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.

Creates a unique discriminator for the path.


Create a string for interpolating variables.

Used for link generation (inverse).


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.

  • s – the path segment, such as 'foo' or '{variable}' or 'foo{variable}bar'.
  • converters – dict of converters for variables.

True if this step is the same as another.


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


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


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


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.


True if this step is not equal to another.


Information needed to construct path discriminator.


Get converter for a variable name.

If no converter is listed explicitly, do no conversion.


True if there are any variables in this step.


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 whether step makes sense.

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


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

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


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 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 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.

Builds a path from a stack.

Parameters:stack – stack of steps, first segment on top
Returns:a path

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.

Generalize a route segment.

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

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

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


Check whether a variable name is a proper identifier.

Parameters:s – variable
Returns:True if variable is an identifier.

Normalizes the path as follows:

  • Collapses dots (/../blog -> /blog)
  • Ensures absolute paths (./site -> /site)
  • Removes double-slashes (//index -> /index)

For example:

../static//../ is turned into /

Parse a path /foo/bar/baz to a stack of steps.

A step is a string, such as ‘foo’, ‘bar’ and ‘baz’.

Parameters:path – the path string
Returns:a stack of steps, first segment on top

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_SEPARATOR = <_sre.SRE_Pattern object>

regex for path separator, a / or / repeated.

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

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