forked from gyger/OpenFilters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.py
139 lines (114 loc) · 5.3 KB
/
version.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# version.py
#
# A class to manage and compare versions.
#
# Copyright (c) 2012 Stephane Larouche.
#
# This file is part of OpenFilters.
#
# OpenFilters is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# OpenFilters is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
import string
########################################################################
# #
# version #
# #
########################################################################
class version(object):
"""A class to manage and compare versions
This class implements all the comparison operators to compare version
numbers."""
######################################################################
# #
# __init__ #
# #
######################################################################
def __init__(self, *args):
"""Initialize an instance of the target class
A version is composed of 2 or 3 digits, seperated by periods. For
example, 1.0.5 and 2.1 are valid version numbers.
This method either takes a single string argument defining the
version number, or 2 or 3 integer arguments."""
if len(args) < 1 or len(args) > 3:
raise TypeError
try:
if len(args) == 1 and isinstance(args[0], str):
number, _, self.build = args[0].partition("-")
numbers = string.split(number, ".")
self.major = int(numbers[0])
self.minor = int(numbers[1])
if len(numbers) == 2: self.revision = 0
elif len(numbers) == 3: self.revision = int(numbers[2])
else: raise ValueError
else:
self.major = int(args[0])
self.minor = int(args[1])
if len(args) > 2: self.revision = int(args[2])
else: self.revision = 0
except:
raise ValueError
######################################################################
# #
# __repr__ #
# #
######################################################################
def __repr__(self):
return "version(%i, %i, %i)" % (self.major, self.minor, self.revision)
######################################################################
# #
# __str__ #
# #
######################################################################
def __str__(self):
if self.revision:
return "%i.%i.%i" % (self.major, self.minor, self.revision)
else:
return "%i.%i" % (self.major, self.minor)
######################################################################
# #
# __lt__ #
# __le__ #
# __eq__ #
# __ne__ #
# __gt__ #
# __ge__ #
# #
######################################################################
def __lt__(self, other):
if self.major < other.major: return True
elif self.minor < other.minor: return True
elif self.revision < other.revision: return True
else: return False
def __le__(self, other):
if self.major > other.major: return False
elif self.minor > other.minor: return False
elif self.revision > other.revision: return False
else: return True
def __eq__(self, other):
if self.major == other.major and self.minor == other.minor and self.revision == other.revision: return True
else: return False
def __ne__(self, other):
if self.major != other.major or self.minor != other.minor or self.revision != other.revision: return True
else: return False
def __gt__(self, other):
if self.major > other.major: return True
elif self.minor > other.minor: return True
elif self.revision > other.revision: return True
else: return False
def __ge__(self, other):
if self.major < other.major: return False
elif self.minor < other.minor: return False
elif self.revision < other.revision: return False
else: return True