Skip to content

Commit 1d8c415

Browse files
committed
Move _increment_string into VersionInfo class
Makes removing deprecating functions easier as, for example, bump_prerelease is no longer dependant from an "external" function. * Adapt test cases * Move _LAST_NUMBER regex into VersionInfo class * Implement _increment_string as a staticmethod
1 parent 7a143e1 commit 1d8c415

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

semver.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
__maintainer_email__ = "s.celles@gmail.com"
1818

1919

20-
_LAST_NUMBER = re.compile(r"(?:[^\d]*(\d+)[^\d]*)+")
21-
2220
#: Contains the implemented semver.org version of the spec
2321
SEMVER_SPEC_VERSION = "2.0.0"
2422

@@ -141,6 +139,7 @@ class VersionInfo(object):
141139
"""
142140

143141
__slots__ = ("_major", "_minor", "_patch", "_prerelease", "_build")
142+
_LAST_NUMBER = re.compile(r"(?:[^\d]*(\d+)[^\d]*)+")
144143
_REGEX = re.compile(
145144
r"""
146145
^
@@ -268,6 +267,24 @@ def __iter__(self):
268267
for v in self.to_tuple():
269268
yield v
270269

270+
@staticmethod
271+
def _increment_string(string):
272+
"""
273+
Look for the last sequence of number(s) in a string and increment.
274+
275+
:param str string: the string to search for.
276+
:return: the incremented string
277+
278+
Source:
279+
http://code.activestate.com/recipes/442460-increment-numbers-in-a-string/#c1
280+
"""
281+
match = VersionInfo._LAST_NUMBER.search(string)
282+
if match:
283+
next_ = str(int(match.group(1)) + 1)
284+
start, end = match.span(1)
285+
string = string[: max(end - len(next_), start)] + next_ + string[end:]
286+
return string
287+
271288
def bump_major(self):
272289
"""
273290
Raise the major part of the version, return a new object but leave self
@@ -328,7 +345,7 @@ def bump_prerelease(self, token="rc"):
328345
build=None)
329346
"""
330347
cls = type(self)
331-
prerelease = _increment_string(self._prerelease or (token or "rc") + ".0")
348+
prerelease = cls._increment_string(self._prerelease or (token or "rc") + ".0")
332349
return cls(self._major, self._minor, self._patch, prerelease)
333350

334351
def bump_build(self, token="build"):
@@ -346,7 +363,7 @@ def bump_build(self, token="build"):
346363
build='build.10')
347364
"""
348365
cls = type(self)
349-
build = _increment_string(self._build or (token or "build") + ".0")
366+
build = cls._increment_string(self._build or (token or "build") + ".0")
350367
return cls(self._major, self._minor, self._patch, self._prerelease, build)
351368

352369
@comparator
@@ -692,20 +709,6 @@ def format_version(major, minor, patch, prerelease=None, build=None):
692709
return str(VersionInfo(major, minor, patch, prerelease, build))
693710

694711

695-
def _increment_string(string):
696-
"""
697-
Look for the last sequence of number(s) in a string and increment, from:
698-
699-
http://code.activestate.com/recipes/442460-increment-numbers-in-a-string/#c1
700-
"""
701-
match = _LAST_NUMBER.search(string)
702-
if match:
703-
next_ = str(int(match.group(1)) + 1)
704-
start, end = match.span(1)
705-
string = string[: max(end - len(next_), start)] + next_ + string[end:]
706-
return string
707-
708-
709712
@deprecated(version="2.9.2")
710713
def bump_major(version):
711714
"""

test_semver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ def does_not_raise(item):
5454
"string,expected", [("rc", "rc"), ("rc.1", "rc.2"), ("2x", "3x")]
5555
)
5656
def test_should_private_increment_string(string, expected):
57-
from semver import _increment_string
57+
# from semver import _increment_string
5858

59-
assert _increment_string(string) == expected
59+
assert VersionInfo._increment_string(string) == expected
6060

6161

6262
@pytest.fixture

0 commit comments

Comments
 (0)