|
2 | 2 | import re
|
3 | 3 | import string
|
4 | 4 | from copy import copy
|
5 |
| -from typing import Any, Dict, List, MutableMapping, Optional, Tuple, Union |
| 5 | +from typing import Any, Dict, List, MutableMapping, Optional, Tuple |
6 | 6 |
|
7 | 7 | from click import UsageError
|
8 | 8 |
|
9 | 9 | from bumpversion.config.models import VersionPartConfig
|
10 |
| -from bumpversion.exceptions import FormattingError, InvalidVersionPartError, MissingValueError |
11 |
| -from bumpversion.functions import NumericFunction, PartFunction, ValuesFunction |
| 10 | +from bumpversion.exceptions import FormattingError, MissingValueError |
12 | 11 | from bumpversion.ui import get_indented_logger
|
13 | 12 | from bumpversion.utils import key_val_string, labels_for_format
|
| 13 | +from bumpversion.versioning.models import Version, VersionPart |
14 | 14 |
|
15 | 15 | logger = get_indented_logger(__name__)
|
16 | 16 |
|
17 | 17 |
|
18 |
| -class VersionPart: |
19 |
| - """ |
20 |
| - Represent part of a version number. |
21 |
| -
|
22 |
| - Determines the PartFunction that rules how the part behaves when increased or reset |
23 |
| - based on the configuration given. |
24 |
| - """ |
25 |
| - |
26 |
| - def __init__(self, config: VersionPartConfig, value: Union[str, int, None] = None): |
27 |
| - self._value = str(value) if value is not None else None |
28 |
| - self.config = config |
29 |
| - self.func: Optional[PartFunction] = None |
30 |
| - if config.values: |
31 |
| - str_values = [str(v) for v in config.values] |
32 |
| - str_optional_value = str(config.optional_value) if config.optional_value is not None else None |
33 |
| - str_first_value = str(config.first_value) if config.first_value is not None else None |
34 |
| - self.func = ValuesFunction(str_values, str_optional_value, str_first_value) |
35 |
| - else: |
36 |
| - self.func = NumericFunction(config.optional_value, config.first_value or "0") |
37 |
| - |
38 |
| - @property |
39 |
| - def value(self) -> str: |
40 |
| - """Return the value of the part.""" |
41 |
| - return self._value or self.func.optional_value |
42 |
| - |
43 |
| - def copy(self) -> "VersionPart": |
44 |
| - """Return a copy of the part.""" |
45 |
| - return VersionPart(self.config, self._value) |
46 |
| - |
47 |
| - def bump(self) -> "VersionPart": |
48 |
| - """Return a part with bumped value.""" |
49 |
| - return VersionPart(self.config, self.func.bump(self.value)) |
50 |
| - |
51 |
| - def null(self) -> "VersionPart": |
52 |
| - """Return a part with first value.""" |
53 |
| - return VersionPart(self.config, self.func.first_value) |
54 |
| - |
55 |
| - @property |
56 |
| - def is_optional(self) -> bool: |
57 |
| - """Is the part optional?""" |
58 |
| - return self.value == self.func.optional_value |
59 |
| - |
60 |
| - @property |
61 |
| - def is_independent(self) -> bool: |
62 |
| - """Is the part independent of the other parts?""" |
63 |
| - return self.config.independent |
64 |
| - |
65 |
| - def __format__(self, format_spec: str) -> str: |
66 |
| - try: |
67 |
| - val = int(self.value) |
68 |
| - except ValueError: |
69 |
| - return self.value |
70 |
| - else: |
71 |
| - return int.__format__(val, format_spec) |
72 |
| - |
73 |
| - def __repr__(self) -> str: |
74 |
| - return f"<bumpversion.VersionPart:{self.func.__class__.__name__}:{self.value}>" |
75 |
| - |
76 |
| - def __eq__(self, other: Any) -> bool: |
77 |
| - return self.value == other.value if isinstance(other, VersionPart) else False |
78 |
| - |
79 |
| - |
80 |
| -class Version: |
81 |
| - """The specification of a version and its parts.""" |
82 |
| - |
83 |
| - def __init__(self, values: Dict[str, VersionPart], original: Optional[str] = None): |
84 |
| - self.values = values |
85 |
| - self.original = original |
86 |
| - |
87 |
| - def __getitem__(self, key: str) -> VersionPart: |
88 |
| - return self.values[key] |
89 |
| - |
90 |
| - def __len__(self) -> int: |
91 |
| - return len(self.values) |
92 |
| - |
93 |
| - def __iter__(self): |
94 |
| - return iter(self.values) |
95 |
| - |
96 |
| - def __repr__(self): |
97 |
| - return f"<bumpversion.Version:{key_val_string(self.values)}>" |
98 |
| - |
99 |
| - def __eq__(self, other: Any) -> bool: |
100 |
| - return ( |
101 |
| - all(value == other.values[key] for key, value in self.values.items()) |
102 |
| - if isinstance(other, Version) |
103 |
| - else False |
104 |
| - ) |
105 |
| - |
106 |
| - def bump(self, part_name: str, order: List[str]) -> "Version": |
107 |
| - """Increase the value of the given part.""" |
108 |
| - bumped = False |
109 |
| - |
110 |
| - new_values = {} |
111 |
| - |
112 |
| - for label in order: |
113 |
| - if label not in self.values: |
114 |
| - continue |
115 |
| - if label == part_name: |
116 |
| - new_values[label] = self.values[label].bump() |
117 |
| - bumped = True |
118 |
| - elif bumped and not self.values[label].is_independent: |
119 |
| - new_values[label] = self.values[label].null() |
120 |
| - else: |
121 |
| - new_values[label] = self.values[label].copy() |
122 |
| - |
123 |
| - if not bumped: |
124 |
| - raise InvalidVersionPartError(f"No part named {part_name!r}") |
125 |
| - |
126 |
| - return Version(new_values) |
127 |
| - |
128 |
| - |
129 | 18 | class VersionConfig:
|
130 | 19 | """
|
131 | 20 | Hold a complete representation of a version string.
|
|
0 commit comments