Coverage for /Users/coordt/Documents/code/bump-my-version/bumpversion/config/utils.py: 68%
34 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-06-12 09:26 -0500
« prev ^ index » next coverage.py v7.4.4, created at 2024-06-12 09:26 -0500
1"""Helper functions for the config module."""
3from __future__ import annotations
5import re
6from typing import Dict, List
8from wcmatch import glob
10from bumpversion.config.models import FileChange
11from bumpversion.exceptions import BumpVersionError
12from bumpversion.versioning.models import VersionComponentSpec
15def get_all_file_configs(config_dict: dict) -> List[FileChange]:
16 """Make sure all version parts are included."""
17 defaults = {
18 "parse": config_dict["parse"],
19 "serialize": config_dict["serialize"],
20 "search": config_dict["search"],
21 "replace": config_dict["replace"],
22 "ignore_missing_version": config_dict["ignore_missing_version"],
23 "ignore_missing_file": config_dict["ignore_missing_files"],
24 "regex": config_dict["regex"],
25 "include_bumps": tuple(config_dict["parts"]),
26 "exclude_bumps": (),
27 }
28 files = [{k: v for k, v in filecfg.items() if v is not None} for filecfg in config_dict["files"]]
29 for f in files:
30 f.update({k: v for k, v in defaults.items() if k not in f})
31 return [FileChange(**f) for f in files]
34def get_all_part_configs(config_dict: dict) -> Dict[str, VersionComponentSpec]:
35 """Make sure all version parts are included."""
36 try:
37 parsing_groups = list(re.compile(config_dict["parse"]).groupindex.keys())
38 except re.error as e:
39 raise BumpVersionError(f"Could not parse regex '{config_dict['parse']}': {e}") from e
40 parts = config_dict["parts"]
42 part_configs = {}
43 for label in parsing_groups:
44 is_independent = label.startswith("$")
45 part_configs[label] = (
46 VersionComponentSpec(**parts[label])
47 if label in parts
48 else VersionComponentSpec(independent=is_independent)
49 )
50 return part_configs
53def resolve_glob_files(file_cfg: FileChange) -> List[FileChange]:
54 """
55 Return a list of file configurations that match the glob pattern.
57 Args:
58 file_cfg: The file configuration containing the glob pattern
60 Returns:
61 A list of resolved file configurations according to the pattern.
62 """
63 files: List[FileChange] = []
64 exclude = file_cfg.glob_exclude or ()
65 glob_flags = glob.GLOBSTAR | glob.FORCEUNIX | glob.SPLIT
66 for filename_glob in glob.glob(file_cfg.glob, flags=glob_flags, exclude=exclude):
67 new_file_cfg = file_cfg.model_copy()
68 new_file_cfg.filename = filename_glob
69 new_file_cfg.glob = None
70 files.append(new_file_cfg)
71 return files