summaryrefslogtreecommitdiff
path: root/semantic_version
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2019-08-23 23:05:33 +0200
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2019-08-24 15:13:09 +0200
commit13902f1812697bac01ea1172663a6eedd20ac9d5 (patch)
tree832e7d87df696e5590e4400782f8801aa55d657b /semantic_version
parentf0899127486265fc66632631cf542da5ff7c9b6f (diff)
downloadsemantic-version-13902f1812697bac01ea1172663a6eedd20ac9d5.tar.gz
Add `Version.truncate()`.
This simplifies computing neighbouring versions.
Diffstat (limited to 'semantic_version')
-rw-r--r--semantic_version/base.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/semantic_version/base.py b/semantic_version/base.py
index 48fdfbf..8251efa 100644
--- a/semantic_version/base.py
+++ b/semantic_version/base.py
@@ -165,6 +165,42 @@ class Version:
partial=self.partial,
)
+ def truncate(self, level='patch'):
+ """Return a new Version object, truncated up to the selected level."""
+ if level == 'build':
+ return self
+ elif level == 'prerelease':
+ return Version(
+ major=self.major,
+ minor=self.minor,
+ patch=self.patch,
+ prerelease=self.prerelease,
+ partial=self.partial,
+ )
+ elif level == 'patch':
+ return Version(
+ major=self.major,
+ minor=self.minor,
+ patch=self.patch,
+ partial=self.partial,
+ )
+ elif level == 'minor':
+ return Version(
+ major=self.major,
+ minor=self.minor,
+ patch=None if self.partial else 0,
+ partial=self.partial,
+ )
+ elif level == 'major':
+ return Version(
+ major=self.major,
+ minor=None if self.partial else 0,
+ patch=None if self.partial else 0,
+ partial=self.partial,
+ )
+ else:
+ raise ValueError("Invalid truncation level `%s`." % level)
+
@classmethod
def coerce(cls, version_string, partial=False):
"""Coerce an arbitrary version string into a semver-compatible one.