Group#

class Group(definition, idx)#

Bases: object

Manage a group inside the filename pattern.

Parameters:
  • definition (str) – Group definition.

  • idx (int) – Index of the group in the filename pattern.

Raises:

GroupParseError – Invalid group definition.

_check_duplicates(m)#

Check if the definition does not contain duplicates.

The matching pattern (PATTERN) is written so that specs (rgx, fmt, …) can be given in any order, while still matching the full string.

The pattern is made of every possible spec in a OR list, which can be repeated up to 5 times. Regex only keep the last captured group. We must be a bit sly to check duplicates. For that I check that the groups we have account for the whole string. If part of the string is not in our match, something has been overwritten.

Parameters:

m (Match)

_parse_group_definition()#

Parse group definition against a regex to retrieve specs.

See _check_duplicate() for details on the pattern matching.

Return type:

None

_replace_regex_defaults(regex)#

Recursively replace defaults regexes of the form %[a-zA-Z].

Replacements are taken from Group.DEFAULT_GROUPS.

A ‘%’ in the regex should be escaped by another: ‘%%’.

Raises:

KeyError – Unknown replacement.

Parameters:

regex (str)

Return type:

str

fix_value(fix)#

Fix the group regex to a specific value.

Parameters:

fix (Any | bool | str) – A string is directly used as a regular expression, otherwise the value is formatted according to the group ‘format’ specification.

format(value)#

Return formatted string from value.

Parameters:

value (Any)

Return type:

str

get_regex()#

Get group regex.

Returns the fixed value if previously specified. Insert the regex into a capturing group, and make it optional if the :opt was indicated

Return type:

str

parse(string)#

Return parsed value from string.

Parameters:

string (str)

Return type:

Any

unfix()#

Unfix value.

DEFAULT_GROUPS = {'B': ['[a-zA-Z]*', 's'], 'F': ['%Y-%m-%d', 's'], 'H': ['\\d\\d', '02d'], 'I': ['\\d+', 'd'], 'M': ['\\d\\d', '02d'], 'S': ['\\d\\d', '02d'], 'X': ['%H%M%S', '06d'], 'Y': ['\\d{4}', '04d'], 'char': ['\\S*', 's'], 'd': ['\\d\\d', '02d'], 'j': ['\\d{3}', '03d'], 'm': ['\\d\\d', '02d'], 'text': ['\\w', 's'], 'x': ['%Y%m%d', '08d']}#

Regex and format strings for various default groups.

See the Name section of documentation for details.

PATTERN = re.compile('(?P<name>[^:]+?)(?:(?P<fmt>:fmt=.+?)|(?P<rgx>:rgx=.*?)|(?P<bool>:bool=.*?(?::.*?)??)|(?P<opt>:opt)|(?P<discard>:discard)){,5}')#

Pattern used to find properties in group definition.

See _check_duplicates() for details on the pattern matching.

definition#

The string that created the group %(definition).

discard: bool#

If the group should not be used when retrieving values from matches.

property fixed: bool#

True if the group has fixed value(s).

fmt: FormatAbstract#

Format string object.

idx: int#

Index inside the pre-regex.

name: str#

Group name.

optional: bool#

If True, the whole group is marked as optional (()?). Is set to False unless specification ‘:opt’ is indicated.

options: tuple[str, str] | None#

Tuple of the two possibilities indicated by the full :bool specification, in order (False, True), so that a simple getitem works.

rgx: str#

Regex.