MoinMoin/util/version.py
author Thomas Waldmann <tw AT waldmann-edv DOT de>
Tue, 29 Jul 2014 22:17:58 +0200
changeset 2674 4ad3b2654ce3
parent 1975 384555088cab
permissions -rw-r--r--
fix jQuery-File-Upload version at 4.4.2 as newer one is not yet tested
     1 # Copyright: 2011 MoinMoin:ThomasWaldmann
     2 # License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
     3 
     4 """
     5     MoinMoin - dealing with version numbers
     6 """
     7 
     8 
     9 # see MoinMoin/_tests/test_version.py for examples how to use this:
    10 
    11 import re
    12 
    13 
    14 class Version(tuple):
    15     """
    16     Version objects store versions like 1.2.3a4 in a structured
    17     way and support version comparisons and direct version component access.
    18     1: major version (digits only)
    19     2: minor version (digits only)
    20     3: (maintenance) release version (digits only)
    21     a4: optional additional version specification (str)
    22 
    23     See PEP386 for more details.
    24     TODO: use 3rd party code for PEP386 version numbers later.
    25 
    26     You can create a Version instance either by giving the components, like:
    27         Version(1,2,3,'a4')
    28     or by giving the composite version string, like:
    29         Version(version="1.2.3a4").
    30 
    31     Version subclasses tuple, so comparisons to tuples should work.
    32     Also, we inherit all the comparison logic from tuple base class.
    33     """
    34     VERSION_RE = re.compile(
    35         r"""
    36         (?P<major>\d+)
    37         \.
    38         (?P<minor>\d+)
    39         \.
    40         (?P<release>\d+)
    41         (?P<additional>[abc]\d+)?""",
    42         re.VERBOSE)
    43 
    44     @classmethod
    45     def parse_version(cls, version):
    46         match = cls.VERSION_RE.match(version)
    47         if match is None:
    48             raise ValueError("Unexpected version string format: {0!r}".format(version))
    49         v = match.groupdict()
    50         return int(v['major']), int(v['minor']), int(v['release']), str(v['additional'] or 'd0')
    51 
    52     def __new__(cls, major=0, minor=0, release=0, additional='d0', version=None):
    53         # HACK: Use "d0" for release, as "d0" > "c99".
    54         if version:
    55             major, minor, release, additional = cls.parse_version(version)
    56         return tuple.__new__(cls, (major, minor, release, additional))
    57 
    58     # properties for easy access of version components
    59     major = property(lambda self: self[0])
    60     minor = property(lambda self: self[1])
    61     release = property(lambda self: self[2])
    62     additional = property(lambda self: self[3] if self[3] != 'd0' else '')
    63 
    64     def __str__(self):
    65         version_str = "{0}.{1}.{2}".format(self.major, self.minor, self.release)
    66         if self.additional != 'd0':
    67             version_str += self.additional
    68         return version_str