浏览代码

Bugfixes in PluginVersion comparison functions

Yann Weber 7 年前
父节点
当前提交
a0bf4c1176
共有 2 个文件被更改,包括 48 次插入34 次删除
  1. 3
    0
      lodel/plugin/exceptions.py
  2. 45
    34
      lodel/plugin/plugins.py

+ 3
- 0
lodel/plugin/exceptions.py 查看文件

@@ -1,6 +1,9 @@
1 1
 class PluginError(Exception):
2 2
     pass
3 3
 
4
+class PluginVersionError(PluginError):
5
+    pass
6
+
4 7
 class PluginTypeError(PluginError):
5 8
     pass
6 9
 

+ 45
- 34
lodel/plugin/plugins.py 查看文件

@@ -12,8 +12,8 @@ LodelContext.expose_modules(globals(), {
12 12
     'lodel.logger': 'logger',
13 13
     'lodel.settings.utils': ['SettingsError'],
14 14
     'lodel.plugin.hooks': ['LodelHook'],
15
-    'lodel.plugin.exceptions': ['PluginError', 'PluginTypeError',
16
-        'LodelScriptError', 'DatasourcePluginError'],
15
+    'lodel.plugin.exceptions': ['PluginError', 'PluginVersionError',
16
+        'PluginTypeError', 'LodelScriptError', 'DatasourcePluginError'],
17 17
     'lodel.exceptions': ['LodelException', 'LodelExceptions',
18 18
         'LodelFatalError', 'DataNoneValid', 'FieldValidationError']})
19 19
 
@@ -99,25 +99,25 @@ class PluginVersion(object):
99 99
                 spl = arg.split('.')
100 100
                 invalid = False
101 101
                 if len(spl) > 3:
102
-                    raise PluginError("The string '%s' is not a valid plugin \
102
+                    raise PluginVersionError("The string '%s' is not a valid plugin \
103 103
 version number" % arg)
104 104
                 if len(spl) < 3:
105 105
                     spl += [ 0 for _ in range(3-len(spl))]
106 106
                 try:
107 107
                     self.__version = [int(s) for s in spl]
108 108
                 except (ValueError, TypeError):
109
-                    raise PluginError("The string '%s' is not a valid lodel2 \
109
+                    raise PluginVersionError("The string '%s' is not a valid lodel2 \
110 110
 plugin version number" % arg)
111 111
             else:
112 112
                 try:
113 113
                     if len(arg) >= 1:
114 114
                         if len(arg) > 3:
115
-                            raise PluginError("Expected maximum 3 value to \
115
+                            raise PluginVersionError("Expected maximum 3 value to \
116 116
 create a plugin version number but found '%s' as argument" % arg)
117 117
                         for i, v in enumerate(arg):
118 118
                             self.__version[i] = int(arg[i])
119 119
                 except (TypeError, ValueError):
120
-                    raise PluginError("Unable to convert argument into plugin \
120
+                    raise PluginVersionError("Unable to convert argument into plugin \
121 121
 version number" % arg)
122 122
         elif len(args) > 3:
123 123
             raise PluginError("Expected between 1 and 3 positional arguments \
@@ -125,6 +125,10 @@ but %d arguments found" % len(args))
125 125
         else: 
126 126
             for i,v in enumerate(args):
127 127
                 self.__version[i] = int(v)
128
+        #Checks that version numbering is correct
129
+        for v in self.__version:
130
+            if v < 0:
131
+                raise PluginVersionError("No negative version number allowed !")
128 132
 
129 133
     ##@brief Property to access major version number
130 134
     @property
@@ -154,48 +158,55 @@ but %d arguments found" % len(args))
154 158
 a PluginVerison instance" % other)
155 159
         return other
156 160
     
157
-    ##@brief Generic comparison function
158
-    #@param other PluginVersion or iterable
159
-    #@param cmp_fun_name function : interger comparison function
160
-    def __generic_cmp(self, other, cmp_fun_name):
161
-        other = self.__cmp_check(other)
161
+    ##@brief Allow accessing to versions parts using interger index
162
+    #@param key int : index
163
+    #@return major for key == 0, minor for key == 1, revision for key == 2
164
+    def __getitem__(self, key):
162 165
         try:
163
-            cmpfun = getattr(int, cmp_fun_name)
164
-        except AttributeError:
165
-            raise LodelFatalError("Invalid comparison callback given \
166
-to generic PluginVersion comparison function : '%s'" % cmp_fun_name)
167
-        for property_name in self.PROPERTY_LIST:
168
-            if not cmpfun(
169
-                    getattr(self, property_name),
170
-                    getattr(other, property_name)):
171
-                if property_name == self.PROPERTY_LIST[-1]:
172
-                    return False
173
-        return True
166
+            key = int(key)
167
+        except (ValueError, TypeError):
168
+            raise ValueError("Expected an int as key")
169
+        if key < 0 or key > 2:
170
+            raise ValueError("Key is expected to be in [0..2]")
171
+        return self.__version[key]
174 172
 
175 173
     def __lt__(self, other):
176
-        return self.__generic_cmp(other, '__lt__')
177
-
178
-    def __le__(self, other):
179
-        return self.__generic_cmp(other, '__le__')
174
+        for i in range(3):
175
+            if self[i] < other[i]:
176
+                return True
177
+            elif self[i] > other[i]:
178
+                return False
179
+        return False
180 180
 
181 181
     def __eq__(self, other):
182
-        return self.__generic_cmp(other, '__eq__')
183
-
184
-    def __ne__(self, other):
185
-        return self.__generic_cmp(other, '__ne__')
182
+        for i in range(3):
183
+            if self[i] != other[i]:
184
+                return False
185
+        return True
186 186
 
187 187
     def __gt__(self, other):
188
-        return self.__generic_cmp(other, '__gt__')
188
+        for i in range(3):
189
+            if self[i] > other[i]:
190
+                return True
191
+            elif self[i] < other[i]:
192
+                return False
193
+        return False
194
+
195
+    def __le__(self, other):
196
+        return self < other or self == other
197
+
198
+    def __ne__(self, other):
199
+        return not(self == other)
189 200
 
190 201
     def __ge__(self, other):
191
-        return self.__generic_cmp(other, '__ge__')
202
+        return self > other or self == other
192 203
 
193 204
     def __str__(self):
194 205
         return '%d.%d.%d' % tuple(self.__version)
195 206
 
196 207
     def __repr__(self):
197
-        return "%s" % {'major': self.major, 'minor': self.minor,
198
-            'revision': self.revision}
208
+        return "{'major': %d, 'minor': %d, 'revision': %d}" % tuple(
209
+            self.__version)
199 210
 
200 211
 ##@brief Plugin metaclass that allows to "catch" child class declaration
201 212
 #@ingroup lodel2_plugins

正在加载...
取消
保存