Skip to content

isimip_utils.patterns

Functions to match file names and extract ISIMIP specifiers.

match_dataset_path(pattern, dataset_path)

Match a dataset path against a pattern.

Parameters:

Name Type Description Default
pattern dict

Pattern dictionary containing regex patterns.

required
dataset_path Path

Path to the dataset to match.

required

Returns:

Type Description
tuple[Path, dict]

Tuple of (matched_path, specifiers_dict).

Raises:

Type Description
DidNotMatch

If the path doesn't match the pattern.

match_file_path(pattern, file_path)

Match a file path against a pattern.

Parameters:

Name Type Description Default
pattern dict

Pattern dictionary containing regex patterns.

required
file_path Path

Path to the file to match.

required

Returns:

Type Description
tuple[Path, dict]

Tuple of (matched_path, specifiers_dict).

Raises:

Type Description
DidNotMatch

If the path doesn't match the pattern.

match_path(pattern, path, dirname_pattern_key='path', filename_pattern_key='file')

Match both directory and filename components of a path against patterns.

Parameters:

Name Type Description Default
pattern dict

Pattern dictionary containing regex patterns and specifiers.

required
path Path

Path object to match.

required
dirname_pattern_key str

Key in pattern dict for directory pattern (default: 'path').

'path'
filename_pattern_key str

Key in pattern dict for filename pattern (default: 'file').

'file'

Returns:

Type Description
tuple[Path, dict]

Tuple of (matched_path, specifiers_dict) containing extracted specifiers.

Raises:

Type Description
DidNotMatch

If dirname and filename specifiers conflict.

match_dataset(pattern, path)

Match a dataset name against a pattern.

Parameters:

Name Type Description Default
pattern dict

Pattern dictionary containing regex patterns.

required
path Path

Path object with dataset name.

required

Returns:

Type Description
tuple[Path, dict]

Tuple of (matched_path, specifiers_dict).

Raises:

Type Description
DidNotMatch

If the dataset name doesn't match the pattern.

match_file(pattern, path)

Match a file name against a pattern.

Parameters:

Name Type Description Default
pattern dict

Pattern dictionary containing regex patterns.

required
path Path

Path object with file name.

required

Returns:

Type Description
tuple[Path, dict]

Tuple of (matched_path, specifiers_dict).

Raises:

Type Description
DidNotMatch

If the file name doesn't match the pattern.

match_string(pattern, string)

Match a string against a regex pattern and extract specifiers.

Parameters:

Name Type Description Default
pattern Pattern

Compiled regex pattern with named groups.

required
string str

String to match against the pattern.

required

Returns:

Type Description
Path

Tuple of (Path of matched portion, specifiers_dict).

dict

Year values (4-digit numbers) are converted to integers.

Raises:

Type Description
DidNotMatch

If the string doesn't match the pattern.