VirtualMailManager/common.py
branchv0.6.x
changeset 347 586367ee042b
parent 328 85972d3ba936
child 350 b7a4d7828608
equal deleted inserted replaced
346:95d45e4ec1a6 347:586367ee042b
    15 from VirtualMailManager import ENCODING
    15 from VirtualMailManager import ENCODING
    16 from VirtualMailManager.constants import NOT_EXECUTABLE, NO_SUCH_BINARY
    16 from VirtualMailManager.constants import NOT_EXECUTABLE, NO_SUCH_BINARY
    17 from VirtualMailManager.errors import VMMError
    17 from VirtualMailManager.errors import VMMError
    18 
    18 
    19 
    19 
    20 _version_re = re.compile(r'^(\d+)\.(\d+)\.(?:(\d+)|(alpha|beta|rc)(\d+))$')
    20 VERSION_RE = re.compile(r'^(\d+)\.(\d+)\.(?:(\d+)|(alpha|beta|rc)(\d+))$')
       
    21 
    21 _version_level = dict(alpha=0xA, beta=0xB, rc=0xC)
    22 _version_level = dict(alpha=0xA, beta=0xB, rc=0xC)
    22 _version_cache = {}
    23 _version_cache = {}
    23 _ = lambda msg: msg
    24 _ = lambda msg: msg
    24 
    25 
    25 
    26 
    71     Raises a `ValueError` if the *version_string* has the wrong™ format.
    72     Raises a `ValueError` if the *version_string* has the wrong™ format.
    72 
    73 
    73     version_hex('1.2.3') -> 270548736
    74     version_hex('1.2.3') -> 270548736
    74     hex(version_hex('1.2.3')) -> '0x10203f00'
    75     hex(version_hex('1.2.3')) -> '0x10203f00'
    75     """
    76     """
    76     global _version_cache, _version_level, _version_re
    77     global _version_cache
    77     if version_string in _version_cache:
    78     if version_string in _version_cache:
    78         return _version_cache[version_string]
    79         return _version_cache[version_string]
    79     version = 0
    80     version = 0
    80     version_mo = _version_re.match(version_string)
    81     version_mo = VERSION_RE.match(version_string)
    81     if not version_mo:
    82     if not version_mo:
    82         raise ValueError('Invalid version string: %r' % version_string)
    83         raise ValueError('Invalid version string: %r' % version_string)
    83     major, minor, patch, level, serial = version_mo.groups()
    84     major, minor, patch, level, serial = version_mo.groups()
    84     major = int(major)
    85     major = int(major)
    85     minor = int(minor)
    86     minor = int(minor)
   108     """Converts a Dovecot version previously converted with version_hex back to
   109     """Converts a Dovecot version previously converted with version_hex back to
   109     a string.
   110     a string.
   110     Raises a `TypeError` if *version* is not an int/long.
   111     Raises a `TypeError` if *version* is not an int/long.
   111     Raises a `ValueError` if *version* is an incorrect int version.
   112     Raises a `ValueError` if *version* is an incorrect int version.
   112     """
   113     """
   113     global _version_cache, _version_level
   114     global _version_cache
   114     if version in _version_cache:
   115     if version in _version_cache:
   115         return _version_cache[version]
   116         return _version_cache[version]
   116     if not isinstance(version, (int, long)):
   117     if not isinstance(version, (int, long)):
   117         raise TypeError('Argument is not a int/long: %r', version)
   118         raise TypeError('Argument is not a int/long: %r', version)
   118     major = (version >> 28) & 0xFF
   119     major = (version >> 28) & 0xFF