comparison MoinMoin/macro/__init__.py @ 2536:72a31f09d5a1

allow types as defaults for macro args to force conversion to that type
author Johannes Berg <johannes AT sipsolutions DOT net>
date Mon, 23 Jul 2007 13:11:10 +0200
parents cc125871c8a0
children 070294763d3e
comparison
equal deleted inserted replaced
2535:52961e122768 2536:72a31f09d5a1
106 return wikiutil.get_int(self.request, value, name, default) 106 return wikiutil.get_int(self.request, value, name, default)
107 elif isinstance(default, float): 107 elif isinstance(default, float):
108 return wikiutil.get_float(self.request, value, name, default) 108 return wikiutil.get_float(self.request, value, name, default)
109 elif isinstance(default, unicode): 109 elif isinstance(default, unicode):
110 return wikiutil.get_unicode(self.request, value, name, default) 110 return wikiutil.get_unicode(self.request, value, name, default)
111 elif default is bool:
112 return wikiutil.get_bool(self.request, value, name)
113 elif default is int or default is long:
114 return wikiutil.get_int(self.request, value, name)
115 elif default is float:
116 return wikiutil.get_float(self.request, value, name)
111 return value 117 return value
112 118
113 def _wrap(self, macro_name, fn, args): 119 def _wrap(self, macro_name, fn, args):
114 """ 120 """
115 Parses arguments for a macro call and calls the macro 121 Parses arguments for a macro call and calls the macro
119 int, long, float or unicode object, then the given value 125 int, long, float or unicode object, then the given value
120 is converted to the type of that default value before passing 126 is converted to the type of that default value before passing
121 it to the macro function. That way, macros need not call the 127 it to the macro function. That way, macros need not call the
122 wikiutil.get_* functions for any arguments that have a default. 128 wikiutil.get_* functions for any arguments that have a default.
123 """ 129 """
130 _ = self.request.getText
124 if args: 131 if args:
125 positional, keyword, trailing = \ 132 positional, keyword, trailing = \
126 wikiutil.parse_quoted_separated(args) 133 wikiutil.parse_quoted_separated(args)
127 134
128 kwargs = {} 135 kwargs = {}
181 default, argname) 188 default, argname)
182 189
183 # iterate through all arguments, but if no more positional 190 # iterate through all arguments, but if no more positional
184 # arguments are given then skip converting them 191 # arguments are given then skip converting them
185 if idx >= len(positional): 192 if idx >= len(positional):
193 if isinstance(default, type):
194 raise ValueError(_("Not enough arguments"))
186 continue 195 continue
187 196
188 # use the default right away if it's not given, 197 # use the default right away if it's not given,
189 # otherwise convert to the type of the default 198 # otherwise convert to the type of the default
190 if positional[idx] is None: 199 if positional[idx] is None:
459 468
460 def macro_Anchor(self, anchor=None): 469 def macro_Anchor(self, anchor=None):
461 anchor = wikiutil.get_unicode(self.request, anchor, 'anchor', u'anchor') 470 anchor = wikiutil.get_unicode(self.request, anchor, 'anchor', u'anchor')
462 return self.formatter.anchordef(anchor) 471 return self.formatter.anchordef(anchor)
463 472
464 def macro_MailTo(self, email, text=u''): 473 def macro_MailTo(self, email=unicode, text=u''):
465 if not email: 474 if not email:
466 raise ValueError("You need to give an (obfuscated) email address") 475 raise ValueError("You need to give an (obfuscated) email address")
467 476
468 from MoinMoin.mail.sendmail import decodeSpamSafeEmail 477 from MoinMoin.mail.sendmail import decodeSpamSafeEmail
469 478