|
1 """EditorConfig version tools |
|
2 |
|
3 Provides ``join_version`` and ``split_version`` classes for converting |
|
4 __version__ strings to VERSION tuples and vice versa. |
|
5 |
|
6 """ |
|
7 |
|
8 import re |
|
9 |
|
10 |
|
11 __all__ = ['join_version', 'split_version'] |
|
12 |
|
13 |
|
14 _version_re = re.compile(r'^(\d+)\.(\d+)\.(\d+)(\..*)?$', re.VERBOSE) |
|
15 |
|
16 |
|
17 def join_version(version_tuple): |
|
18 """Return a string representation of version from given VERSION tuple""" |
|
19 version = "%s.%s.%s" % version_tuple[:3] |
|
20 if version_tuple[3] != "final": |
|
21 version += "-%s" % version_tuple[3] |
|
22 return version |
|
23 |
|
24 |
|
25 def split_version(version): |
|
26 """Return VERSION tuple for given string representation of version""" |
|
27 match = _version_re.search(version) |
|
28 if not match: |
|
29 return None |
|
30 else: |
|
31 split_version = list(match.groups()) |
|
32 if split_version[3] is None: |
|
33 split_version[3] = "final" |
|
34 split_version = list(map(int, split_version[:3])) + split_version[3:] |
|
35 return tuple(split_version) |