template

class lucidity.template.Template(name, pattern, anchor=1, default_placeholder_expression='[\w_.\-]+', duplicate_placeholder_mode=1, template_resolver=None)[source]

Bases: object

A template.

__init__(name, pattern, anchor=1, default_placeholder_expression='[\\w_.\\-]+', duplicate_placeholder_mode=1, template_resolver=None)[source]

Initialise with name and pattern.

anchor determines how the pattern is anchored during a parse. A value of ANCHOR_START (the default) will match the pattern against the start of a path. ANCHOR_END will match against the end of a path. To anchor at both the start and end (a full path match) use ANCHOR_BOTH. Finally, None will try to match the pattern once anywhere in the path.

duplicate_placeholder_mode determines how duplicate placeholders will be handled during parsing. RELAXED mode extracts the last matching value without checking the other values. STRICT mode ensures that all duplicate placeholders extract the same value and raises ParseError if they do not.

If template_resolver is supplied, use it to resolve any template references in the pattern during operations. It should conform to the Resolver interface. It can be changed at any time on the instance to affect future operations.

name

Return name of template.

pattern

Return template pattern.

expanded_pattern()[source]

Return pattern with all referenced templates expanded recursively.

Raise lucidity.error.ResolveError if pattern contains a reference that cannot be resolved by currently set template_resolver.

parse(path)[source]

Return dictionary of data extracted from path using this template.

Raise ParseError if path is not parsable by this template.

format(data)[source]

Return a path formatted by applying data to this template.

Raise FormatError if data does not supply enough information to fill the template fields.

keys()[source]

Return unique set of placeholders in pattern.

references()[source]

Return unique set of referenced templates in pattern.

ANCHOR_BOTH = 3
ANCHOR_END = 2
ANCHOR_START = 1
RELAXED = 1
STRICT = 2
class lucidity.template.Resolver[source]

Bases: object

Template resolver interface.

get(template_name, default=None)[source]

Return template that matches template_name.

If no template matches then return default.