translate.misc.selector.SimpleParser

Package translate :: Package misc :: Module selector :: Class SimpleParser |
[hide private]
[frames] | no frames]

# Class SimpleParser

source code

Callable to turn path expressions into regexes with named groups.

For instance "/hello/{name}" becomes r"^\/hello\/(?P<name>[^\^.]+)$"

For /hello/{name:pattern} you get whatever is in self.patterns['pattern'] instead of "[^\^.]+"

Optional portions of path expression can be expressed [like this]

/hello/{name}[/] (can have trailing slash or not)

Example:

/blog/archive/{year:digits}/{month:digits}[/[{article}[/]]]

This would catch any of these:

/blog/archive/2005/09 /blog/archive/2005/09/ /blog/archive/2005/09/1 /blog/archive/2005/09/1/

(I am not suggesting that this example is a best practice. I would probably have a separate mapping for listing the month and retrieving an individual entry. It depends, though.)

Instance Methods | [hide private]
__init__(self, patterns=None) Initialize with character class mappings. | source code
lookup(self, name) Return the replacement for the name found. | source code
lastly(self, regex) Process the result of __call__ right before it returns. | source code
openended(self, regex) Process the result of __call__ right before it returns. | source code
outermost_optionals_split(self, text) Split out optional portions by outermost matching delims. | source code
parse(self, text) Turn a path expression into regex. | source code
__call__(self, url_pattern) Turn a path expression into regex via parse and lastly. | source code
Inherited from `object`: `__delattr__`, `__getattribute__`, `__hash__`, `__new__`, `__reduce__`, `__reduce_ex__`, `__repr__`, `__setattr__`, `__str__`

Class Variables | [hide private]
| _patterns = ``{``'``alpha``'``:` `'``[a-zA-Z]+``'``,` `'``any``'``:` `'``.+``'``,` `'``chunk``'``:` `'``[^/^``...``
| default_pattern = ``'``chunk``'``
| end = ``'``}``'``
| oend = ``'``]``'``
| ostart = ``'``[``'``
| start = ``'``{``'``

Properties | [hide private]
Inherited from `object`: `__class__`

Method Details | [hide private]

### __init__(self, patterns=None) (Constructor) | source code
Initialize with character class mappings. Overrides: object.__init__

### lastly(self, regex) | source code
Process the result of __call__ right before it returns. Adds the ^ and the $ to the beginning and the end, respectively.

### openended(self, regex) | source code
Process the result of __call__ right before it returns. Adds the ^ to the beginning but no $ to the end. Called as a special alternative to lastly.

Class Variable Details | [hide private]

  ### _patterns Value:
      {'alpha': '[a-zA-Z]+',
       'any': '.+',
       'chunk': '[^/^.]+',
       'digits': '\\d+',
       'number': '\\d*.?\\d+',
       'segment': '[^/]+',
       'word': '\\w+'}