module documentation

Functions for reversing a regular expression (used in reverse URL resolving). Used internally by Django and not intended for external use.

This is not, and is not intended to be, a complete reg-exp decompiler. It should be good enough for a large class of URLS, however.

Constant ESCAPE​_MAPPINGS Undocumented
Class ​Choice Represent multiple possibilities at this point in a pattern string.
Class ​Group Represent a capturing group in the pattern string.
Class ​Non​Capture Represent a non-capturing group in the pattern string.
Function ​_lazy​_re​_compile Lazily compile a regex with flags.
Function contains Return True if the "source" contains an instance of "inst". False, otherwise.
Function flatten​_result Turn the given source sequence into a list of reg-exp possibilities and their arguments. Return a list of strings and a list of argument lists. Each of the two lists will be of the same length.
Function get​_quantifier Parse a quantifier from the input, where "ch" is the first character in the quantifier.
Function next​_char No summary
Function normalize Given a reg-exp pattern, normalize it to an iterable of forms that suffice for reverse matching. This does the following:
Function walk​_to​_end The iterator is currently inside a capturing group. Walk to the close of this group, skipping over any nested groups and handling escaped parentheses correctly.
ESCAPE_MAPPINGS: dict =

Undocumented

Value
{'A': None,
 'b': None,
 'B': None,
 'd': '0',
 'D': 'x',
 's': ' ',
 'S': 'x',
...
def _lazy_re_compile(regex, flags=0):
Lazily compile a regex with flags.
def contains(source, inst):
Return True if the "source" contains an instance of "inst". False, otherwise.
def flatten_result(source):
Turn the given source sequence into a list of reg-exp possibilities and their arguments. Return a list of strings and a list of argument lists. Each of the two lists will be of the same length.
def get_quantifier(ch, input_iter):

Parse a quantifier from the input, where "ch" is the first character in the quantifier.

Return the minimum number of occurrences permitted by the quantifier and either None or the next character from the input_iter if the next character is not part of the quantifier.

def next_char(input_iter):

An iterator that yields the next character from "pattern_iter", respecting escape sequences. An escaped character is replaced by a representative of its class (e.g. w -> "x"). If the escaped character is one that is skipped, it is not returned (the next character is returned instead).

Yield the next character, along with a boolean indicating whether it is a raw (unescaped) character or not.

def normalize(pattern):

Given a reg-exp pattern, normalize it to an iterable of forms that suffice for reverse matching. This does the following:

  1. For any repeating sections, keeps the minimum number of occurrences permitted (this means zero for optional groups).
  2. If an optional group includes parameters, include one occurrence of that group (along with the zero occurrence case from step (1)).
  3. Select the first (essentially an arbitrary) element from any character class. Select an arbitrary character for any unordered class (e.g. '.' or 'w') in the pattern.
  4. Ignore look-ahead and look-behind assertions.
  5. Raise an error on any disjunctive ('|') constructs.

Django's URLs for forward resolving are either all positional arguments or all keyword arguments. That is assumed here, as well. Although reverse resolving can be done using positional args when keyword args are specified, the two cannot be mixed in the same reverse() call.

def walk_to_end(ch, input_iter):
The iterator is currently inside a capturing group. Walk to the close of this group, skipping over any nested groups and handling escaped parentheses correctly.