diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2019-08-23 23:05:33 +0200 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2019-08-24 15:13:09 +0200 |
commit | 13902f1812697bac01ea1172663a6eedd20ac9d5 (patch) | |
tree | 832e7d87df696e5590e4400782f8801aa55d657b /semantic_version/base.py | |
parent | f0899127486265fc66632631cf542da5ff7c9b6f (diff) | |
download | semantic-version-13902f1812697bac01ea1172663a6eedd20ac9d5.tar.gz |
Add `Version.truncate()`.
This simplifies computing neighbouring versions.
Diffstat (limited to 'semantic_version/base.py')
-rw-r--r-- | semantic_version/base.py | 36 |
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. |