1
1
"""Module for managing Versions and their internal parts."""
2
2
import re
3
- import string
4
- from copy import copy
5
3
from typing import Any , Dict , List , MutableMapping , Optional , Tuple
6
4
7
5
from click import UsageError
8
- from versioning .models import VersionComponentConfig
9
6
10
- from bumpversion .exceptions import FormattingError , MissingValueError
11
7
from bumpversion .ui import get_indented_logger
12
8
from bumpversion .utils import labels_for_format
13
- from bumpversion .versioning .models import Version , VersionComponent , VersionSpec
14
- from bumpversion .versioning .serialization import parse_version
9
+ from bumpversion .versioning .models import Version , VersionComponentConfig , VersionSpec
10
+ from bumpversion .versioning .serialization import parse_version , serialize
15
11
16
12
logger = get_indented_logger (__name__ )
17
13
@@ -85,117 +81,6 @@ def parse(self, version_string: Optional[str] = None) -> Optional[Version]:
85
81
version .original = version_string
86
82
return version
87
83
88
- # _parsed = {
89
- # key: VersionComponent(self.part_configs[key], value)
90
- # for key, value in parsed.items()
91
- # if key in self.part_configs
92
- # }
93
- # return Version(_parsed, version_string)
94
-
95
- def _serialize (
96
- self , version : Version , serialize_format : str , context : MutableMapping , raise_if_incomplete : bool = False
97
- ) -> str :
98
- """
99
- Attempts to serialize a version with the given serialization format.
100
-
101
- Args:
102
- version: The version to serialize
103
- serialize_format: The serialization format to use, using Python's format string syntax
104
- context: The context to use when serializing the version
105
- raise_if_incomplete: Whether to raise an error if the version is incomplete
106
-
107
- Raises:
108
- FormattingError: if not serializable
109
- MissingValueError: if not all parts required in the format have values
110
-
111
- Returns:
112
- The serialized version as a string
113
- """
114
- values = copy (context )
115
- for k in version :
116
- values [k ] = version [k ]
117
-
118
- # TODO dump complete context on debug level
119
-
120
- try :
121
- # test whether all parts required in the format have values
122
- serialized = serialize_format .format (** values )
123
-
124
- except KeyError as e :
125
- missing_key = getattr (e , "message" , e .args [0 ])
126
- raise MissingValueError (
127
- f"Did not find key { missing_key !r} in { version !r} when serializing version number"
128
- ) from e
129
-
130
- keys_needing_representation = set ()
131
-
132
- keys = list (self .order )
133
- for i , k in enumerate (keys ):
134
- v = values [k ]
135
-
136
- if not isinstance (v , VersionComponent ):
137
- # values coming from environment variables don't need
138
- # representation
139
- continue
140
-
141
- if not v .is_optional :
142
- keys_needing_representation = set (keys [: i + 1 ])
143
-
144
- required_by_format = set (labels_for_format (serialize_format ))
145
-
146
- # try whether all parsed keys are represented
147
- if raise_if_incomplete and not keys_needing_representation <= required_by_format :
148
- missing_keys = keys_needing_representation ^ required_by_format
149
- raise FormattingError (
150
- f"""Could not represent '{ "', '" .join (missing_keys )} ' in format '{ serialize_format } '"""
151
- )
152
-
153
- return serialized
154
-
155
- def _choose_serialize_format (self , version : Version , context : MutableMapping ) -> str :
156
- """
157
- Choose a serialization format for the given version and context.
158
-
159
- Args:
160
- version: The version to serialize
161
- context: The context to use when serializing the version
162
-
163
- Returns:
164
- The serialized version as a string
165
-
166
- Raises:
167
- MissingValueError: if not all parts required in the format have values
168
- """
169
- chosen = None
170
-
171
- logger .debug ("Evaluating serialization formats" )
172
- logger .indent ()
173
- for serialize_format in self .serialize_formats :
174
- try :
175
- self ._serialize (version , serialize_format , context , raise_if_incomplete = True )
176
- # Prefer shorter or first search expression.
177
- chosen_part_count = len (list (string .Formatter ().parse (chosen ))) if chosen else None
178
- serialize_part_count = len (list (string .Formatter ().parse (serialize_format )))
179
- if not chosen or chosen_part_count > serialize_part_count :
180
- chosen = serialize_format
181
- logger .debug ("Found '%s' to be a usable serialization format" , chosen )
182
- else :
183
- logger .debug ("Found '%s' usable serialization format, but it's longer" , serialize_format )
184
- except FormattingError :
185
- # If chosen, prefer shorter
186
- if not chosen :
187
- chosen = serialize_format
188
- except MissingValueError as e :
189
- logger .info (e .message )
190
- raise e
191
-
192
- if not chosen :
193
- raise KeyError ("Did not find suitable serialization format" )
194
- logger .dedent ()
195
- logger .debug ("Selected serialization format '%s'" , chosen )
196
-
197
- return chosen
198
-
199
84
def serialize (self , version : Version , context : MutableMapping ) -> str :
200
85
"""
201
86
Serialize a version to a string.
@@ -207,9 +92,4 @@ def serialize(self, version: Version, context: MutableMapping) -> str:
207
92
Returns:
208
93
The serialized version as a string
209
94
"""
210
- logger .debug ("Serializing version '%s'" , version )
211
- logger .indent ()
212
- serialized = self ._serialize (version , self ._choose_serialize_format (version , context ), context )
213
- logger .debug ("Serialized to '%s'" , serialized )
214
- logger .dedent ()
215
- return serialized
95
+ return serialize (version , list (self .serialize_formats ), context )
0 commit comments