Mercurial > piecrust2
comparison piecrust/routing.py @ 730:8c3c2b949b82
routing: Fix problems with route functions.
Instead of using merged functions with multiblog sites, use different
functions. Add some options to customize the function names.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 01 Jun 2016 22:10:47 -0700 |
parents | 606f6d57b5df |
children | 3f01f63b7247 |
comparison
equal
deleted
inserted
replaced
729:e35407c60e00 | 730:8c3c2b949b82 |
---|---|
10 | 10 |
11 | 11 |
12 route_re = re.compile(r'%((?P<qual>[\w\d]+):)?(?P<name>\w+)%') | 12 route_re = re.compile(r'%((?P<qual>[\w\d]+):)?(?P<name>\w+)%') |
13 route_esc_re = re.compile(r'\\%((?P<qual>[\w\d]+)\\:)?(?P<name>\w+)\\%') | 13 route_esc_re = re.compile(r'\\%((?P<qual>[\w\d]+)\\:)?(?P<name>\w+)\\%') |
14 template_func_re = re.compile(r'^(?P<name>\w+)\((?P<args>.*)\)\s*$') | 14 template_func_re = re.compile(r'^(?P<name>\w+)\((?P<args>.*)\)\s*$') |
15 template_func_arg_re = re.compile(r'(?P<arg>\+?\w+)') | 15 template_func_arg_re = re.compile(r'((?P<qual>[\w\d]+):)?(?P<arg>\+?\w+)') |
16 ugly_url_cleaner = re.compile(r'\.html$') | 16 ugly_url_cleaner = re.compile(r'\.html$') |
17 | 17 |
18 | 18 |
19 class RouteNotFoundError(Exception): | 19 class RouteNotFoundError(Exception): |
20 pass | 20 pass |
286 | 286 |
287 self.template_func_name = m.group('name') | 287 self.template_func_name = m.group('name') |
288 self.template_func_args = [] | 288 self.template_func_args = [] |
289 arg_list = m.group('args') | 289 arg_list = m.group('args') |
290 if arg_list: | 290 if arg_list: |
291 self.template_func_args = template_func_arg_re.findall(arg_list) | 291 self.template_func_args = [] |
292 for m2 in template_func_arg_re.finditer(arg_list): | |
293 self.template_func_args.append(m2.group('arg')) | |
292 for i in range(len(self.template_func_args) - 1): | 294 for i in range(len(self.template_func_args) - 1): |
293 if self.template_func_args[i][0] == '+': | 295 if self.template_func_args[i][0] == '+': |
294 raise Exception("Only the last route parameter can be a " | 296 raise Exception("Only the last route parameter can be a " |
295 "variable argument (prefixed with `+`)") | 297 "variable argument (prefixed with `+`)") |
296 | 298 |
301 def template_func(*args): | 303 def template_func(*args): |
302 is_variable = (self.template_func_vararg is not None) | 304 is_variable = (self.template_func_vararg is not None) |
303 if not is_variable and len(args) != len(self.template_func_args): | 305 if not is_variable and len(args) != len(self.template_func_args): |
304 raise Exception( | 306 raise Exception( |
305 "Route function '%s' expected %d arguments, " | 307 "Route function '%s' expected %d arguments, " |
306 "got %d." % | 308 "got %d: %s" % |
307 (func_def, len(self.template_func_args), | 309 (func_def, len(self.template_func_args), |
308 len(args))) | 310 len(args), args)) |
309 elif is_variable and len(args) < len(self.template_func_args): | 311 elif is_variable and len(args) < len(self.template_func_args): |
310 raise Exception( | 312 raise Exception( |
311 "Route function '%s' expected at least %d arguments, " | 313 "Route function '%s' expected at least %d arguments, " |
312 "got %d." % | 314 "got %d: %s" % |
313 (func_def, len(self.template_func_args), | 315 (func_def, len(self.template_func_args), |
314 len(args))) | 316 len(args), args)) |
315 | 317 |
316 metadata = {} | 318 metadata = {} |
317 non_var_args = list(self.template_func_args) | 319 non_var_args = list(self.template_func_args) |
318 if is_variable: | 320 if is_variable: |
319 del non_var_args[-1] | 321 del non_var_args[-1] |