Coverage for /Users/OORDCOR/Documents/code/bump-my-version/bumpversion/config/utils.py: 75%

1"""Helper functions for the config module."""

2

3 from __future__ import annotations

4

5 import glob

6 from typing import Dict, List

7

8 from bumpversion.config.models import FileChange

9 from bumpversion.exceptions import BumpVersionError

10 from bumpversion.versioning.models import VersionComponentSpec

11

12

13 def get_all_file_configs(config_dict: dict) -> List[FileChange]:

14 """Make sure all version parts are included."""

15 defaults = {

16 "parse": config_dict["parse"],

17 "serialize": config_dict["serialize"],

18 "search": config_dict["search"],

19 "replace": config_dict["replace"],

20 "ignore_missing_version": config_dict["ignore_missing_version"],

21 "ignore_missing_file": config_dict["ignore_missing_files"],

22 "regex": config_dict["regex"],

23 }

24 files = [{k: v for k, v in filecfg.items() if v is not None} for filecfg in config_dict["files"]]

25 for f in files:

26 f.update({k: v for k, v in defaults.items() if k not in f})

27 return [FileChange(**f) for f in files]

28

29

30 def get_all_part_configs(config_dict: dict) -> Dict[str, VersionComponentSpec]:

31 """Make sure all version parts are included."""

32 import re

33

34 try:

35 parsing_groups = list(re.compile(config_dict["parse"]).groupindex.keys())

36 except re.error as e:

37 raise BumpVersionError(f"Could not parse regex '{config_dict['parse']}': {e}") from e

38 parts = config_dict["parts"]

39

40 part_configs = {}

41 for label in parsing_groups:

42 is_independent = label.startswith("$")

43 part_configs[label] = (

44 VersionComponentSpec(**parts[label])

45 if label in parts

46 else VersionComponentSpec(independent=is_independent)

47 )

48 return part_configs

49

50

51 def resolve_glob_files(file_cfg: FileChange) -> List[FileChange]:

52 """

53 Return a list of file configurations that match the glob pattern.

54

55 Args:

56 file_cfg: The file configuration containing the glob pattern

57

58 Returns:

59 A list of resolved file configurations according to the pattern.

60 """

61 files = []

62 for filename_glob in glob.glob(file_cfg.glob, recursive=True):

63 new_file_cfg = file_cfg.model_copy()

64 new_file_cfg.filename = filename_glob

65 new_file_cfg.glob = None

66 files.append(new_file_cfg)

67 return files