diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-03-20 02:02:02 +0100 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-03-20 02:02:02 +0100 |
commit | f84d754af1ae86aaa9a891445d6ae5be36668a85 (patch) | |
tree | d56cb28242391a2d05b3601efd03202298e00cb3 /src | |
parent | 658ac42850e3b4c4923365287d55ec17be4806de (diff) | |
download | semantic-version-f84d754af1ae86aaa9a891445d6ae5be36668a85.tar.gz |
Fix handling of partial versions (Closes #1).
Diffstat (limited to 'src')
-rw-r--r-- | src/semantic_version/base.py | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/src/semantic_version/base.py b/src/semantic_version/base.py index 932614c..b8e7fcf 100644 --- a/src/semantic_version/base.py +++ b/src/semantic_version/base.py @@ -58,7 +58,7 @@ def identifier_list_cmp(a, b): class Version(object): version_re = re.compile('^(\d+)\.(\d+)\.(\d+)(?:-([0-9a-zA-Z.-]+))?(?:\+([0-9a-zA-Z.-]+))?$') - partial_version_re = re.compile('^(\d+)\.(\d+)\.(\d+)(?:-([0-9a-zA-Z.-]*))?(?:\+([0-9a-zA-Z.-]*))?$') + partial_version_re = re.compile('^(\d+)(?:\.(\d+)(?:\.(\d+))?)?(?:-([0-9a-zA-Z.-]*))?(?:\+([0-9a-zA-Z.-]*))?$') def __init__(self, version_string, partial=False): major, minor, patch, prerelease, build = self.parse(version_string, partial) @@ -72,6 +72,12 @@ class Version(object): self.partial = partial @classmethod + def _coerce(cls, value, allow_none=False): + if value is None and allow_none: + return value + return int(value) + + @classmethod def parse(cls, version_string, partial=False): if not version_string: raise ValueError('Invalid empty version string: %r' % version_string) @@ -88,8 +94,8 @@ class Version(object): major, minor, patch, prerelease, build = match.groups() major = int(major) - minor = int(minor) - patch = int(patch) + minor = cls._coerce(minor, partial) + patch = cls._coerce(patch, partial) if prerelease is None: if partial and (build is None): @@ -118,7 +124,11 @@ class Version(object): return iter((self.major, self.minor, self.patch, self.prerelease, self.build)) def __str__(self): - version = '%d.%d.%d' % (self.major, self.minor, self.patch) + version = '%d' % self.major + if self.minor is not None: + version = '%s.%d' % (version, self.minor) + if self.patch is not None: + version = '%s.%d' % (version, self.patch) if self.prerelease or (self.partial and self.prerelease == () and self.build is None): version = '%s-%s' % (version, '.'.join(self.prerelease)) |