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

1"""Bump My Version configuration models."""

2 from __future__ import annotations

3

4 import re

5 from collections import defaultdict

6 from itertools import chain

7 from typing import TYPE_CHECKING, Dict, List, MutableMapping, Optional, Tuple, Union

8

9 from pydantic import BaseModel, Field

10 from pydantic_settings import BaseSettings, SettingsConfigDict

11

12 from bumpversion.ui import get_indented_logger

13

14 if TYPE_CHECKING: 14 ↛ 15line 14 didn't jump to line 15, because the condition on line 14 was never true

15 from bumpversion.scm import SCMInfo

16 from bumpversion.version_part import VersionConfig

17

18 logger = get_indented_logger(__name__)

19

20

21 class VersionPartConfig(BaseModel):

22 """Configuration of a part of the version."""

23

24 values: Optional[list] = None # Optional. Numeric is used if missing or no items in list

25 optional_value: Optional[str] = None # Optional.

26 # Defaults to first value. 0 in the case of numeric. Empty string means nothing is optional.

27 first_value: Union[str, int, None] = None # Optional. Defaults to first value in values

28 independent: bool = False

29

30

31 class FileChange(BaseModel):

32 """A change to make to a file."""

33

34 parse: str

35 serialize: tuple

36 search: str

37 replace: str

38 regex: bool

39 ignore_missing_version: bool

40 filename: Optional[str] = None

41 glob: Optional[str] = None # Conflicts with filename. If both are specified, glob wins

42 key_path: Optional[str] = None # If specified, and has an appropriate extension, will be treated as a data file

43

44 def __hash__(self):

45 return hash(tuple(sorted(self.model_dump().items())))

46

47 def get_search_pattern(self, context: MutableMapping) -> Tuple[re.Pattern, str]:

48 """

49 Render the search pattern and return the compiled regex pattern and the raw pattern.

50

51 Args:

52 context: The context to use for rendering the search pattern

53

54 Returns:

55 A tuple of the compiled regex pattern and the raw pattern as a string.

56 """

57 logger.debug("Rendering search pattern with context")

58 logger.indent()

59 # the default search pattern is escaped, so we can still use it in a regex

60 raw_pattern = self.search.format(**context)

61 default = re.compile(re.escape(raw_pattern), re.MULTILINE | re.DOTALL)

62 if not self.regex:

63 logger.debug("No RegEx flag detected. Searching for the default pattern: '%s'", default.pattern)

64 logger.dedent()

65 return default, raw_pattern

66

67 re_context = {key: re.escape(str(value)) for key, value in context.items()}

68 regex_pattern = self.search.format(**re_context)

69 try:

70 search_for_re = re.compile(regex_pattern, re.MULTILINE | re.DOTALL)

71 logger.debug("Searching for the regex: '%s'", search_for_re.pattern)

72 logger.dedent()

73 return search_for_re, raw_pattern

74 except re.error as e:

75 logger.error("Invalid regex '%s': %s.", default, e)

76

77 logger.debug("Invalid regex. Searching for the default pattern: '%s'", raw_pattern)

78 logger.dedent()

79

80 return default, raw_pattern

81

82

83 class Config(BaseSettings):

84 """Bump Version configuration."""

85

86 current_version: Optional[str]

87 parse: str

88 serialize: tuple = Field(min_length=1)

89 search: str

90 replace: str

91 regex: bool

92 ignore_missing_version: bool

93 tag: bool

94 sign_tags: bool

95 tag_name: str

96 tag_message: Optional[str]

97 allow_dirty: bool

98 commit: bool

99 message: str

100 commit_args: Optional[str]

101 scm_info: Optional["SCMInfo"]

102 parts: Dict[str, VersionPartConfig]

103 files: List[FileChange]

104 included_paths: List[str] = Field(default_factory=list)

105 excluded_paths: List[str] = Field(default_factory=list)

106 model_config = SettingsConfigDict(env_prefix="bumpversion_")

107 _resolved_filemap: Optional[Dict[str, List[FileChange]]] = None

108

109 def add_files(self, filename: Union[str, List[str]]) -> None:

110 """Add a filename to the list of files."""

111 filenames = [filename] if isinstance(filename, str) else filename

112 print(f"before adding files: {[x.filename for x in self.files]}")

113 for name in filenames:

114 self.files.append(

115 FileChange(

116 filename=name,

117 glob=None,

118 key_path=None,

119 parse=self.parse,

120 serialize=self.serialize,

121 search=self.search,

122 replace=self.replace,

123 regex=self.regex,

124 ignore_missing_version=self.ignore_missing_version,

125 )

126 )

127 print(f"after adding files: {[x.filename for x in self.files]}")

128

129 self._resolved_filemap = None

130

131 @property

132 def resolved_filemap(self) -> Dict[str, List[FileChange]]:

133 """Return the cached resolved filemap."""

134 if self._resolved_filemap is None:

135 self._resolved_filemap = self._resolve_filemap()

136 return self._resolved_filemap

137

138 def _resolve_filemap(self) -> Dict[str, List[FileChange]]:

139 """Return a map of filenames to file configs, expanding any globs."""

140 from bumpversion.config.utils import resolve_glob_files

141

142 output = defaultdict(list)

143 new_files = []

144 for file_cfg in self.files:

145 if file_cfg.glob:

146 new_files.extend(resolve_glob_files(file_cfg))

147 else:

148 new_files.append(file_cfg)

149

150 for file_cfg in new_files:

151 output[file_cfg.filename].append(file_cfg)

152 return output

153

154 @property

155 def files_to_modify(self) -> List[FileChange]:

156 """Return a list of files to modify."""

157 files_not_excluded = [filename for filename in self.resolved_filemap if filename not in self.excluded_paths]

158 inclusion_set = set(self.included_paths) | set(files_not_excluded)

159 return list(

160 chain.from_iterable(

161 file_cfg_list for key, file_cfg_list in self.resolved_filemap.items() if key in inclusion_set

162 )

163 )

164

165 @property

166 def version_config(self) -> "VersionConfig":

167 """Return the version configuration."""

168 from bumpversion.version_part import VersionConfig

169

170 return VersionConfig(self.parse, self.serialize, self.search, self.replace, self.parts)