changeset 2548:5db66a25f0ba

introduce required_arg wrapper class
author Johannes Berg <johannes AT sipsolutions DOT net>
date Mon, 23 Jul 2007 19:43:40 +0200
parents c2ef5800253f
children fb88a3706761
files MoinMoin/_tests/test_wikiutil.py MoinMoin/wikiutil.py
diffstat 2 files changed, 31 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/_tests/test_wikiutil.py	Mon Jul 23 19:38:16 2007 +0200
+++ b/MoinMoin/_tests/test_wikiutil.py	Mon Jul 23 19:43:40 2007 +0200
@@ -506,6 +506,9 @@
     def _test_invoke_float_None(self, i=float):
         assert i == 1.4 or i is None
 
+    def _test_invoke_float_required(self, i=wikiutil.required_arg(float)):
+        assert i == 1.4
+
     def _test_invoke_choice(self, a, choice=[u'a', u'b', u'c']):
         assert a == 7
         assert choice == u'a'
@@ -576,5 +579,11 @@
             [{u'7 \xc3': 'test', 'test': u'x'}])
         ief(self.request, self._test_arbitrary_kw, u'7 \xc3=test, test= x ',
             [{u'7 \xc3': 'test', 'test': u'x'}])
+        py.test.raises(ValueError, ief, self.request,
+                       self._test_invoke_float_required, u'')
+        ief(self.request, self._test_invoke_float_required, u'1.4')
+        ief(self.request, self._test_invoke_float_required, u'i=1.4')
+        py.test.raises(ValueError, ief, self.request,
+                       self._test_invoke_float_required, u',')
 
 coverage_modules = ['MoinMoin.wikiutil']
--- a/MoinMoin/wikiutil.py	Mon Jul 23 19:38:16 2007 +0200
+++ b/MoinMoin/wikiutil.py	Mon Jul 23 19:43:40 2007 +0200
@@ -1486,6 +1486,22 @@
     return arg
 
 
+class required_arg:
+    """
+    Wrap a type in this class and give it as default argument
+    for a function passed to invoke_extension_function() in
+    order to get generic checking that the argument is given.
+    """
+    def __init__(self, argtype):
+        """
+        Initialise a required_arg
+        @param argtype: the type the argument should have
+        """
+        if not isinstance(argtype, type):
+            raise TypeError("argtype must be a type")
+        self.argtype = argtype
+
+
 def invoke_extension_function(request, function, args, fixed_args=[]):
     """
     Parses arguments for an extension call and calls the extension
@@ -1529,6 +1545,8 @@
             return get_int(request, value, name)
         elif default is float:
             return get_float(request, value, name)
+        elif isinstance(default, required_arg):
+            return _convert_arg(request, value, default.argtype, name)
         return value
 
     assert isinstance(fixed_args, list) or isinstance(fixed_args, tuple)
@@ -1612,6 +1630,10 @@
             # went wrong (if it does)
             kwargs[argname] = _convert_arg(request, kwargs[argname],
                                            defaults[argname], argname)
+            if (kwargs[argname] is None
+                and isinstance(defaults[argname], required_arg)):
+                raise ValueError(_('Argument "%s" is required') % argname)
+
         if not argname in argnames:
             # move argname into _kwargs parameter
             kwargs_to_pass[argname] = kwargs[argname]