traversalkit.route

The module provides route descriptors.

The following classes should not be instantiated directly. They are used within traversalkit.resource.Resource to handle routes.

Node

class traversalkit.route.Node(class_, name=None, pattern=None, metaname=None, complies=None)

Route node descriptor.

Parameters:
  • class (Resource) – Resource class of the node.
  • name (str) – Name of the node. Optional.
  • pattern (regex) – Pattern of node name. Optional.
  • metaname (str) – Metaname of node. Optional.
  • complies (Condition) – Condition that route should complie. Optional.
class_

Resource class of the node. Should be a subclass of traversalkit.resource.Resource

name

Name of the node. It is specified, when the node is created by traversalkit.resource.Resource.mount().

pattern

Pattern of the node name. It is specified, when the node is created by traversalkit.resource.Resource.mount_set().

metaname

Metaname of the node. It is specified, when the node is created by traversalkit.resource.Resource.mount_set().

type

Type of the node.

If name is defined, the type will be "single", i.e. the node has been created using traversalkit.resource.Resource.mount().

If name is not defined, the type will be "set", i.e. the node has been created using traversalkit.resource.Resource.mount_set().

complies(route)

Checks whether the route complies node’s condition.

If the node has been created without complies parameter, this method will always return True.

If the node has been created with complies parameter, this method will run compiles(route + self) (i.e. passes the route concatenated with the node itself to the condition) and return the result. See traversalkit.condition for details.

Parameters:route (Route) – Route to test.
Returns:Result of the test.
Return type:bool
__str__()

String representation of the node.

It is mostly useful for documentation purposes. There are three possible representations:

>>> import re

>>> # Node describes single named resource
>>> node = Node(object, name='foo')
>>> str(node)
'foo'

>>> # Node describes anonymous set of resources
>>> node = Node(object, pattern=re.compile('.*'))
>>> str(node)
'{.*}'

>>> # Node describes named set of resources
>>> node = Node(object, pattern=re.compile('.*'),
...             metaname='foo')
>>> str(node)
'{foo}'

Route

class traversalkit.route.Route(*nodes)

Route descriptor.

In general, it is just a immutable sequence of nodes (see Node) with some syntactic surgar.

Parameters:*nodes (Node) –

Nodes of the route.

>>> import re
>>> route = Route(Node(object, name=''))
>>> route
<Route: />
>>> len(route)
1
>>> route.uri
'/'

>>> route += Node(object, name='foo')
>>> route
<Route: /foo/>
>>> len(route)
2
>>> route.uri
'/foo/'

>>> route += [
...     Node(object, pattern=re.compile(r'.*')),
...     Node(object, pattern=re.compile(r'.*'), metaname='bar'),
... ]
>>> route
<Route: /foo/{.*}/{bar}/>
>>> len(route)
4
>>> route.uri
'/foo/{.*}/{bar}/'