changeset 2457:e0b1fc786797

ParameterParser: cleanup: avoid using builtins as names, cosmetic changes
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 22 Jul 2007 00:01:18 +0200
parents 15502589ece6
children 01f2921e5fa4
files MoinMoin/wikiutil.py
diffstat 1 files changed, 32 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/wikiutil.py	Sat Jul 21 23:48:56 2007 +0200
+++ b/MoinMoin/wikiutil.py	Sun Jul 22 00:01:18 2007 +0200
@@ -1308,7 +1308,7 @@
     """
 
     def __init__(self, pattern):
-        #parameter_re = "([^\"',]*(\"[^\"]*\"|'[^']*')?[^\"',]*)[,)]"
+        # parameter_re = "([^\"',]*(\"[^\"]*\"|'[^']*')?[^\"',]*)[,)]"
         name = "(?P<%s>[a-zA-Z_][a-zA-Z0-9_]*)"
         int_re = r"(?P<int>-?\d+)"
         bool_re = r"(?P<bool>(([10])|([Tt]rue)|([Ff]alse)))"
@@ -1341,7 +1341,7 @@
                 named = True
                 self.param_dict[match.group('name')[1:-1]] = i
             elif named:
-                raise ValueError, "Named parameter expected"
+                raise ValueError("Named parameter expected")
             i += 1
 
     def __str__(self):
@@ -1349,7 +1349,7 @@
                                         self.optional)
 
     def parse_parameters(self, params):
-        #Default list to "None"s
+        # Default list/dict entries to None
         parameter_list = [None] * len(self.param_list)
         parameter_dict = dict([(key, None) for key in self.param_dict])
         check_list = [0] * len(self.param_list)
@@ -1365,24 +1365,23 @@
                 raise ValueError("malformed parameters")
             start += match.end()
             if match.group("int"):
-                value = int(match.group("int"))
-                type = 'i'
+                pvalue = int(match.group("int"))
+                ptype = 'i'
             elif match.group("bool"):
-                value = (match.group("bool") == "1") or (match.group("bool") == "True") or (match.group("bool") == "true")
-                type = 'b'
+                pvalue = (match.group("bool") == "1") or (match.group("bool") == "True") or (match.group("bool") == "true")
+                ptype = 'b'
             elif match.group("float"):
-                value = float(match.group("float"))
-                type = 'f'
+                pvalue = float(match.group("float"))
+                ptype = 'f'
             elif match.group("string"):
-                value = match.group("string")[1:-1]
-                type = 's'
+                pvalue = match.group("string")[1:-1]
+                ptype = 's'
             elif match.group("name_param"):
-                value = match.group("name_param")
-                type = 'n'
+                pvalue = match.group("name_param")
+                ptype = 'n'
             else:
                 raise ValueError("Parameter parser code does not fit param_re regex")
 
-            #parameter_list.append(value)
             name = match.group("name")
             if name:
                 if name not in self.param_dict:
@@ -1393,9 +1392,9 @@
                     raise ValueError("parameter '%s' specified twice" % name)
                 else:
                     check_list[nr] = 1
-                value = self._check_type(value, type, self.param_list[nr])
-                parameter_dict[name] = value
-                parameter_list[nr] = value
+                pvalue = self._check_type(pvalue, ptype, self.param_list[nr])
+                parameter_dict[name] = pvalue
+                parameter_list[nr] = pvalue
                 named = True
             elif named:
                 raise ValueError("only named parameters allowed after first named parameter")
@@ -1403,7 +1402,7 @@
                 nr = i
                 if nr not in self.param_dict.values():
                     fixed_count = nr + 1
-                parameter_list[nr] = self._check_type(value, type, self.param_list[nr])
+                parameter_list[nr] = self._check_type(pvalue, ptype, self.param_list[nr])
 
             # Let's populate and map our dictionary to what's been found
             for name in self.param_dict:
@@ -1417,31 +1416,31 @@
 
         return fixed_count, parameter_dict
 
-    def _check_type(self, value, type, format):
-        if type == 'n' and 's' in format: # n as s
-            return value
-
-        if type in format:
-            return value # x -> x
+    def _check_type(self, pvalue, ptype, format):
+        if ptype == 'n' and 's' in format: # n as s
+            return pvalue
 
-        if type == 'i':
+        if ptype in format:
+            return pvalue # x -> x
+
+        if ptype == 'i':
             if 'f' in format:
-                return float(value) # i -> f
+                return float(pvalue) # i -> f
             elif 'b' in format:
-                return value != 0 # i -> b
-        elif type == 's':
+                return pvalue != 0 # i -> b
+        elif ptype == 's':
             if 'b' in format:
-                if value.lower() == 'false':
+                if pvalue.lower() == 'false':
                     return False # s-> b
-                elif value.lower() == 'true':
+                elif pvalue.lower() == 'true':
                     return True # s-> b
                 else:
-                    raise ValueError('%r does not match format %r' % (value, format))
+                    raise ValueError('%r does not match format %r' % (pvalue, format))
 
         if 's' in format: # * -> s
-            return str(value)
+            return str(pvalue)
 
-        raise ValueError('%r does not match format %r' % (value, format))
+        raise ValueError('%r does not match format %r' % (pvalue, format))
 
 
 #############################################################################