comparison MoinMoin/macro/__init__.py @ 2529:df375ab9fbde

automatically convert macro args with defaults to the default's type
author Johannes Berg <johannes AT sipsolutions DOT net>
date Mon, 23 Jul 2007 12:32:06 +0200
parents ee8d1a2cc252
children 0a8fc701e40d
comparison
equal deleted inserted replaced
2528:1ae621d83c5b 2529:df375ab9fbde
22 22
23 import re, time, os 23 import re, time, os
24 from MoinMoin import action, config, util 24 from MoinMoin import action, config, util
25 from MoinMoin import wikiutil, i18n 25 from MoinMoin import wikiutil, i18n
26 from MoinMoin.Page import Page 26 from MoinMoin.Page import Page
27 from inspect import getargspec
28
27 29
28 names = ["TitleSearch", "WordIndex", "TitleIndex", "GoTo", 30 names = ["TitleSearch", "WordIndex", "TitleIndex", "GoTo",
29 # Macros with arguments 31 # Macros with arguments
30 "Icon", "PageList", "Date", "DateTime", "Anchor", "MailTo", "GetVal", "TemplateList", 32 "Icon", "PageList", "Date", "DateTime", "Anchor", "MailTo", "GetVal", "TemplateList",
31 ] 33 ]
89 self.cfg = self.request.cfg 91 self.cfg = self.request.cfg
90 92
91 # Initialized on execute 93 # Initialized on execute
92 self.name = None 94 self.name = None
93 95
96 def _convert_arg(self, value, default, name=None):
97 if isinstance(default, bool):
98 return wikiutil.get_boolean(self.request, value, name, default)
99 elif isinstance(default, int) or isinstance(default, long):
100 return wikiutil.get_int(self.request, value, name, default)
101 elif isinstance(default, float):
102 return wikiutil.get_float(self.request, value, name, default)
103 elif isinstance(default, unicode):
104 return wikiutil.get_unicode(self.request, value, name, default)
105 return value
106
94 def _wrap(self, macro_name, fn, args): 107 def _wrap(self, macro_name, fn, args):
95 """ 108 """
96 Parses arguments for a macro call and calls the macro 109 Parses arguments for a macro call and calls the macro
97 function with the arguments. 110 function with the arguments.
98 """ 111 """
126 139
127 else: 140 else:
128 positional = [] 141 positional = []
129 kwargs = {} 142 kwargs = {}
130 143
144 args, varargs, varkw, defaults = getargspec(fn)
145 argc = len(args) - 1
146 if not defaults:
147 defaults = []
148
149 # if the first (macro) parameter has a default too...
150 if argc < len(defaults):
151 defaults = defaults[1:]
152 defstart = argc - len(defaults)
153
131 try: 154 try:
155 for idx in range(defstart, argc):
156 argname = args[idx + 1]
157 default = defaults[idx - defstart]
158 if argname in kwargs:
159 kwargs[argname] = self._convert_arg(kwargs[argname],
160 default, argname)
161
162 if idx >= len(positional):
163 continue
164
165 if positional[idx] is None:
166 positional[idx] = default
167 else:
168 positional[idx] = self._convert_arg(positional[idx],
169 default)
170
132 return fn(self, *positional, **kwargs) 171 return fn(self, *positional, **kwargs)
133 except (ValueError, TypeError), e: 172 except (ValueError, TypeError), e:
134 return self.format_error(e) 173 return self.format_error(e)
135 174
136 def format_error(self, err): 175 def format_error(self, err):