filefinder.group#

Group management.

Module Attributes

GroupKey

Can be used to select one or more groups in a pattern.

Classes

Group(definition, idx)

Manage a group inside the filename pattern.

Exceptions

GroupParseError(message[, group])

Custom errors when parsing group definition.

exception GroupParseError(message, group=None)#

Custom errors when parsing group definition.

Parameters:
  • message (str) –

  • group (Group | None) –

class Group(definition, idx)#

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.:

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.

definition#

The string that created the group %(definition).

discard: bool#

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

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.

GroupKey = int | str#

Can be used to select one or more groups in a pattern.