1
2 u"""
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 __author__ = u"Andr\xe9 Malo"
29 __docformat__ = "restructuredtext en"
30
31
33 """
34 Container representing the package version
35
36 :IVariables:
37 `major` : ``int``
38 The major version number
39
40 `minor` : ``int``
41 The minor version number
42
43 `patch` : ``int``
44 The patch level version number
45
46 `is_dev` : ``bool``
47 Is it a development version?
48
49 `revision` : ``int``
50 Internal revision
51 """
52 _str = "(unknown)"
53
54 - def __new__(cls, versionstring, is_dev, revision):
55 """
56 Construction
57
58 :Parameters:
59 `versionstring` : ``str``
60 The numbered version string (like ``"1.1.0"``)
61 It should contain at least three dot separated numbers
62
63 `is_dev` : ``bool``
64 Is it a development version?
65
66 `revision` : ``int``
67 Internal revision
68
69 :Return: New version instance
70 :Rtype: `version`
71 """
72
73
74 tup = []
75 versionstring = versionstring.strip()
76 isuni = isinstance(versionstring, unicode)
77 strs = []
78 if versionstring:
79 for item in versionstring.split('.'):
80 try:
81 item = int(item)
82 strs.append(str(item))
83 except ValueError:
84 if isuni:
85 strs.append(item.encode('utf-8'))
86 else:
87 try:
88 item = item.decode('ascii')
89 strs.append(item.encode('ascii'))
90 except UnicodeError:
91 try:
92 item = item.decode('utf-8')
93 strs.append(item.encode('utf-8'))
94 except UnicodeError:
95 strs.append(item)
96 item = item.decode('latin-1')
97 tup.append(item)
98 while len(tup) < 3:
99 tup.append(0)
100 self = tuple.__new__(cls, tup)
101 self._str = ".".join(strs)
102 return self
103
104 - def __init__(self, versionstring, is_dev, revision):
105 """
106 Initialization
107
108 :Parameters:
109 `versionstring` : ``str``
110 The numbered version string (like ``1.1.0``)
111 It should contain at least three dot separated numbers
112
113 `is_dev` : ``bool``
114 Is it a development version?
115
116 `revision` : ``int``
117 Internal revision
118 """
119
120
121 super(Version, self).__init__()
122 self.major, self.minor, self.patch = self[:3]
123 self.is_dev = bool(is_dev)
124 self.revision = int(revision)
125
127 """
128 Create a development string representation
129
130 :Return: The string representation
131 :Rtype: ``str``
132 """
133 return "%s.%s(%r, is_dev=%r, revision=%r)" % (
134 self.__class__.__module__,
135 self.__class__.__name__,
136 self._str,
137 self.is_dev,
138 self.revision,
139 )
140
142 """
143 Create a version like string representation
144
145 :Return: The string representation
146 :Rtype: ``str``
147 """
148 return "%s%s" % (
149 self._str,
150 ("", "-dev-r%d" % self.revision)[self.is_dev],
151 )
152
154 """
155 Create a version like unicode representation
156
157 :Return: The unicode representation
158 :Rtype: ``unicode``
159 """
160 return u"%s%s" % (
161 u".".join(map(unicode, self)),
162 (u"", u"-dev-r%d" % self.revision)[self.is_dev],
163 )
164