Skip to content

Commit

Permalink
refactor GemVersion
Browse files Browse the repository at this point in the history
Signed-off-by: Keshav Priyadarshi <git@keshav.space>
  • Loading branch information
keshav-space committed Aug 2, 2022
1 parent 6ad300e commit f06c9af
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 80 deletions.
121 changes: 47 additions & 74 deletions src/univers/gem.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,19 +199,14 @@ def __init__(self, version):
if not self.is_correct(version):
raise InvalidVersionError(version)

# If version is an empty string convert it to 0
version = str(version).strip()

self.original = version

# If version is an empty string convert it to 0
if not version:
version = "0"

self.version = version.replace("-", ".pre.")
self._segments = ()
self._canonical_segments = ()
self._bump = None
self._release = None

def __str__(self):
return self.original
Expand Down Expand Up @@ -242,6 +237,32 @@ def __gt__(self, other):
def __ge__(self, other):
return self.__cmp__(other) >= 0

@property
def segments(self):
"""
Returns segments for this version where segments are
ints or strings parsed from the original version string.
"""
segments = []
find_segments = re.compile(r"[0-9]+|[a-z]+", re.IGNORECASE).findall
for seg in find_segments(self.version):
sub_segments = [int(seg) if seg.isdigit() else seg]
segments.extend(sub_segments)
return tuple(segments)

@property
def canonical_segments(self):
"""
Returns "canonical segments" for this version using
the Rubygems way for canonicalization.
"""
canonical_segments = []
for segments in self.split_segments():
# drop the section preceding the least significant zero (if exists)
segs = list(dropwhile(lambda s: s == 0, reversed(segments)))
canonical_segments.extend(reversed(segs))
return tuple(canonical_segments)

def bump(self):
"""
Return a new version object where the next to the last revision number
Expand All @@ -252,40 +273,33 @@ def bump(self):
>>> assert GemVersion("5.3.1").bump() == GemVersion("5.4"), repr(GemVersion("5.3.1").bump())
>>> assert GemVersion("5.3.1.4-2").bump() == GemVersion("5.3.2"), GemVersion("5.3.1.4-2").bump()
"""
if not self._bump:
segments = []
for seg in self.segments:
if isinstance(seg, str):
break
else:
segments.append(seg)

if len(segments) > 1:
segments.pop()
segments = []
for seg in self.segments:
if isinstance(seg, str):
break
else:
segments.append(seg)

segments[-1] += 1
segments = [str(r) for r in segments]
self._bump = GemVersion(".".join(segments))
if len(segments) > 1:
segments.pop()

return self._bump
segments[-1] += 1
segments = [str(r) for r in segments]
return GemVersion(".".join(segments))

def release(self):
"""
Return a new GemVersion which is the release for this version (e.g.,
1.2.0.a -> 1.2.0). Non-prerelease versions return themselves. A release
is composed only of numeric segments.
"""
if not self._release:
if self.prerelease():
segments = self.segments
while any(isinstance(s, str) for s in segments):
segments.pop()
segments = (str(s) for s in segments)
self._release = GemVersion(".".join(segments))
else:
self._release = self

return self._release
if self.prerelease():
segments = list(self.segments)
while any(isinstance(s, str) for s in segments):
segments.pop()
segments = (str(s) for s in segments)
return GemVersion(".".join(segments))
return self

def prerelease(self):
"""
Expand All @@ -294,47 +308,6 @@ def prerelease(self):
"""
return any(not str(s).isdigit() for s in self.segments)

@property
def segments(self):
"""
Return a new sequence of segments for this version where segments are
ints or strings parsed from the original version string.
"""
if not self._segments:
self._segments = self.get_segments()
return list(self._segments)

def get_segments(self):
"""
Return a sequence of segments for this version where segments are ints
or strings parsed from the original version string.
"""
find_segments = re.compile(r"[0-9]+|[a-z]+", re.IGNORECASE).findall
segments = []
for seg in find_segments(self.version):
if seg.isdigit():
seg = int(seg)
segments.append(seg)
return tuple(segments)

@property
def canonical_segments(self):
if not self._canonical_segments:
self._canonical_segments = self.get_canonical_segments()
return list(self._canonical_segments)

def get_canonical_segments(self):
"""
Return a new sequence of "canonical segments" for this version using
the Rubygems way.
"""
canonical_segments = []
for segments in self.split_segments():
segs = list(dropwhile(lambda s: s == 0, reversed(segments)))
segs = reversed(segs)
canonical_segments.extend(segs)
return tuple(canonical_segments)

def split_segments(self):
"""
Return a two-tuple of segments:
Expand Down Expand Up @@ -380,11 +353,11 @@ def __cmp__(self, other, trace=False):
if self.version == other.version:
return 0

lhsegments = self.canonical_segments
lhsegments = list(self.canonical_segments)
if trace:
print(f" lhsegments: canonical_segments: {lhsegments!r}")

rhsegments = other.canonical_segments
rhsegments = list(other.canonical_segments)
if trace:
print(f" rhsegments: canonical_segments: {rhsegments!r}")

Expand Down
12 changes: 6 additions & 6 deletions tests/test_rubygems_gem_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,22 +168,22 @@ def test_semver():

def test_segments():
# modifying the segments of a version should not affect the segments of the cached version object
ver = GemVersion("9.8.7")
secondseg = ver.segments[2]
ver_segment = GemVersion("9.8.7").segments
secondseg = ver_segment[2]
secondseg += 1

refute_version_eql("9.8.8", "9.8.7")
assert GemVersion("9.8.7").segments == [9, 8, 7]
assert GemVersion("9.8.7").segments == (9, 8, 7)


def test_split_segments():
assert GemVersion("3.2.4-2").split_segments() == ([3, 2, 4], ["pre", 2])


def test_canonical_segments():
assert GemVersion("1.0.0").canonical_segments == [1]
assert GemVersion("1.0.0.a.1.0").canonical_segments == [1, "a", 1]
assert GemVersion("1.2.3-1").canonical_segments == [1, 2, 3, "pre", 1]
assert GemVersion("1.0.0").canonical_segments == (1,)
assert GemVersion("1.0.0.a.1.0").canonical_segments == (1, "a", 1)
assert GemVersion("1.2.3-1").canonical_segments == (1, 2, 3, "pre", 1)


def test_frozen_version():
Expand Down

0 comments on commit f06c9af

Please sign in to comment.