changeset 5557:57f9d0f64da2

wikiutil: Fixed required_arg handling (no exception when trying to raise exception that choice is wrong).
author Eugene Syromyatnikov <evgsyr@gmail.com>
date Sat, 20 Feb 2010 19:16:36 +0300
parents 19cd37af7e99
children dd29947c1a31
files MoinMoin/wikiutil.py
diffstat 1 files changed, 14 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/wikiutil.py	Thu Feb 18 09:47:51 2010 +0100
+++ b/MoinMoin/wikiutil.py	Sat Feb 20 19:16:36 2010 +0300
@@ -1653,7 +1653,7 @@
     return arg
 
 
-def get_choice(request, arg, name=None, choices=[None]):
+def get_choice(request, arg, name=None, choices=[None], default_none=False):
     """
     For use with values returned from parse_quoted_separated or given
     as macro parameters, return a unicode string that must be in the
@@ -1664,12 +1664,19 @@
     @param arg: The argument, may be None or a unicode string
     @param name: Name of the argument, for error messages
     @param choices: the possible choices
+    @param default_none: If False (default), get_choice returns first available
+                         choice if arg is None. If True, get_choice returns
+                         None if arg is None. This is useful if some arg value
+                         is required (no default choice).
     @rtype: unicode or None
     @returns: the unicode string (or default value)
     """
     assert isinstance(choices, (tuple, list))
     if arg is None:
-        return choices[0]
+        if default_none:
+            return None
+        else:
+            return choices[0]
     elif not isinstance(arg, unicode):
         raise TypeError('Argument must be None or unicode')
     elif not arg in choices:
@@ -1677,11 +1684,12 @@
         if name:
             raise ValueError(
                 _('Argument "%s" must be one of "%s", not "%s"') % (
-                    name, '", "'.join(choices), arg))
+                    name, '", "'.join([repr(choice) for choice in choices]),
+                    arg))
         else:
             raise ValueError(
                 _('Argument must be one of "%s", not "%s"') % (
-                    '", "'.join(choices), arg))
+                    '", "'.join([repr(choice) for choice in choices]), arg))
 
     return arg
 
@@ -1830,8 +1838,8 @@
             if isinstance(default.argtype, (tuple, list)):
                 # treat choice specially and return None if no choice
                 # is given in the value
-                choices = [None] + list(default.argtype)
-                return get_choice(request, value, name, choices)
+                return get_choice(request, value, name, list(default.argtype),
+                       default_none=True)
             else:
                 return _convert_arg(request, value, default.argtype, name)
         return value