1
2 r"""
3 :Copyright:
4
5 Copyright 2015
6 Andr\xe9 Malo or his licensors, as applicable
7
8 :License:
9
10 Licensed under the Apache License, Version 2.0 (the "License");
11 you may not use this file except in compliance with the License.
12 You may obtain a copy of the License at
13
14 http://www.apache.org/licenses/LICENSE-2.0
15
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
21
22 ========================
23 Version Representation
24 ========================
25
26 Version representation.
27 """
28 if __doc__:
29
30 __doc__ = __doc__.encode('ascii').decode('unicode_escape')
31 __author__ = r"Andr\xe9 Malo".encode('ascii').decode('unicode_escape')
32 __docformat__ = "restructuredtext en"
33
34
35 try:
36 unicode
37 except NameError:
38 py3 = True
39 unicode = str
40 else:
41 py3 = False
42
43
45 """
46 Container representing the package version
47
48 :IVariables:
49 `major` : ``int``
50 The major version number
51
52 `minor` : ``int``
53 The minor version number
54
55 `patch` : ``int``
56 The patch level version number
57
58 `is_dev` : ``bool``
59 Is it a development version?
60
61 `revision` : ``int``
62 Internal revision
63 """
64 _str = "(unknown)"
65
66 - def __new__(cls, versionstring, is_dev, revision):
67 """
68 Construction
69
70 :Parameters:
71 `versionstring` : ``str``
72 The numbered version string (like ``"1.1.0"``)
73 It should contain at least three dot separated numbers
74
75 `is_dev` : ``bool``
76 Is it a development version?
77
78 `revision` : ``int``
79 Internal revision
80
81 :Return: New version instance
82 :Rtype: `Version`
83 """
84
85
86 tup = []
87 versionstring = versionstring.strip()
88 isuni = not(py3) and isinstance(versionstring, unicode)
89 strs = []
90 if versionstring:
91 for item in versionstring.split('.'):
92 try:
93 item = int(item)
94 strs.append(str(item))
95 except ValueError:
96 if py3:
97 strs.append(item)
98 elif isuni:
99 strs.append(item.encode('utf-8'))
100 else:
101 try:
102 item = item.decode('ascii')
103 strs.append(item.encode('ascii'))
104 except UnicodeError:
105 try:
106 item = item.decode('utf-8')
107 strs.append(item.encode('utf-8'))
108 except UnicodeError:
109 strs.append(item)
110 item = item.decode('latin-1')
111 tup.append(item)
112 while len(tup) < 3:
113 tup.append(0)
114 self = tuple.__new__(cls, tup)
115 self._str = ".".join(strs)
116 return self
117
118 - def __init__(self, versionstring, is_dev, revision):
119 """
120 Initialization
121
122 :Parameters:
123 `versionstring` : ``str``
124 The numbered version string (like ``1.1.0``)
125 It should contain at least three dot separated numbers
126
127 `is_dev` : ``bool``
128 Is it a development version?
129
130 `revision` : ``int``
131 Internal revision
132 """
133
134
135 super(Version, self).__init__()
136 self.major, self.minor, self.patch = self[:3]
137 self.is_dev = bool(is_dev)
138 self.revision = int(revision)
139
141 """
142 Create a development string representation
143
144 :Return: The string representation
145 :Rtype: ``str``
146 """
147 return "%s.%s(%r, is_dev=%r, revision=%r)" % (
148 self.__class__.__module__,
149 self.__class__.__name__,
150 self._str,
151 self.is_dev,
152 self.revision,
153 )
154
156 """
157 Create a version like string representation
158
159 :Return: The string representation
160 :Rtype: ``str``
161 """
162 return "%s%s" % (
163 self._str,
164 ("", ".dev%d" % self.revision)[self.is_dev],
165 )
166
168 """
169 Create a version like unicode representation
170
171 :Return: The unicode representation
172 :Rtype: ``unicode``
173 """
174 u = lambda x: x.decode('ascii')
175
176 return u("%s%s") % (
177 u(".").join(map(unicode, self)),
178 (u(""), u(".dev%d") % self.revision)[self.is_dev],
179 )
180
182 """
183 Create a version like bytes representation (utf-8 encoded)
184
185 :Return: The bytes representation
186 :Rtype: ``bytes``
187 """
188 return str(self).encode('utf-8')
189