annotate piecrust/routing.py @ 853:f070a4fc033c

core: Continue PieCrust3 refactor, simplify pages. The asset pipeline is still the only function pipeline at this point. * No more `QualifiedPage`, and several other pieces of code deleted. * Data providers are simpler and more focused. For instance, the page iterator doesn't try to support other types of items. * Route parameters are proper known source metadata to remove the confusion between the two. * Make the baker and pipeline more correctly manage records and record histories. * Add support for record collapsing and deleting stale outputs in the asset pipeline.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 21 May 2017 00:06:59 -0700
parents 4850f8c21b6e
children 08e02c2a2a1a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import re
256
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
2 import os.path
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import logging
520
bab91fcef741 bake/serve: Improve support for unicode, add slugification options.
Ludovic Chabant <ludovic@chabant.com>
parents: 515
diff changeset
4 import urllib.parse
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 661
diff changeset
5 from werkzeug.utils import cached_property
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 logger = logging.getLogger(__name__)
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10
787
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
11 route_re = re.compile(r'%((?P<qual>[\w\d]+):)?(?P<var>\+)?(?P<name>\w+)%')
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
12 route_esc_re = re.compile(
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
13 r'\\%((?P<qual>[\w\d]+)\\:)?(?P<var>\\\+)?(?P<name>\w+)\\%')
256
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
14 ugly_url_cleaner = re.compile(r'\.html$')
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16
555
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 520
diff changeset
17 class RouteNotFoundError(Exception):
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 520
diff changeset
18 pass
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 520
diff changeset
19
daf8df5ade7d serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents: 520
diff changeset
20
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 661
diff changeset
21 class InvalidRouteError(Exception):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 661
diff changeset
22 pass
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 661
diff changeset
23
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 661
diff changeset
24
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
25 class RouteParameter(object):
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
26 TYPE_STRING = 0
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
27 TYPE_PATH = 1
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
28 TYPE_INT2 = 2
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
29 TYPE_INT4 = 3
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
30
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
31 def __init__(self, param_name, param_type=TYPE_STRING, *, variadic=False):
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
32 self.param_name = param_name
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
33 self.param_type = param_type
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
34 self.variadic = variadic
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
35
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
36
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 class Route(object):
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 """ Information about a route for a PieCrust application.
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 Each route defines the "shape" of an URL and how it maps to
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
40 content sources.
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 """
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 def __init__(self, app, cfg):
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 self.app = app
256
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
44
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
45 self.source_name = cfg['source']
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
46 self.uri_pattern = cfg['url'].lstrip('/')
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
47
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
48 self.supported_params = self.source.getSupportedRouteParameters()
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
49
256
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
50 self.pretty_urls = app.config.get('site/pretty_urls')
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
51 self.trailing_slash = app.config.get('site/trailing_slash')
396
dc0988d937b3 serve: Fix bug where `?!debug` doesn't get appending correctly.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
52 self.show_debug_info = app.config.get('site/show_debug_info')
256
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
53 self.pagination_suffix_format = app.config.get(
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
54 '__cache/pagination_suffix_format')
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
55 self.uri_root = app.config.get('site/root')
256
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
56
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
57 self.uri_params = []
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 self.uri_format = route_re.sub(self._uriFormatRepl, self.uri_pattern)
173
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
59
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
60 # Get the straight-forward regex for matching this URI pattern.
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
61 p = route_esc_re.sub(self._uriPatternRepl,
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
62 re.escape(self.uri_pattern)) + '$'
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 self.uri_re = re.compile(p)
173
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
64
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
65 # If the URI pattern has a 'path'-type component, we'll need to match
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
66 # the versions for which that component is empty. So for instance if
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
67 # we have `/foo/%path:bar%`, we may need to match `/foo` (note the
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
68 # lack of a trailing slash). We have to build a special pattern (in
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
69 # this case without that trailing slash) to match those situations.
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
70 # (maybe there's a better way to do it but I can't think of any
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
71 # right now)
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
72 uri_pattern_no_path = (
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
73 route_re.sub(self._uriNoPathRepl, self.uri_pattern)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
74 .replace('//', '/')
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
75 .rstrip('/'))
173
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
76 if uri_pattern_no_path != self.uri_pattern:
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
77 p = route_esc_re.sub(self._uriPatternRepl,
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
78 re.escape(uri_pattern_no_path)) + '$'
173
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
79 self.uri_re_no_path = re.compile(p)
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
80 else:
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
81 self.uri_re_no_path = None
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
82
787
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
83 self.func_name = self._validateFuncName(cfg.get('func'))
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
84 self.func_has_variadic_parameter = False
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
85 for p in self.uri_params[:-1]:
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
86 param = self.getParameter(p)
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
87 if param.variadic:
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
88 raise Exception(
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
89 "Only the last route URL parameter can be variadic. "
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
90 "Got: %s" % self.uri_pattern)
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
91 if len(self.uri_params) > 0:
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
92 last_param = self.getParameter(self.uri_params[-1])
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
93 self.func_has_variadic_parameter = last_param.variadic
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 661
diff changeset
95 @cached_property
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 def source(self):
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 for src in self.app.sources:
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98 if src.name == self.source_name:
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99 return src
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
100 raise Exception(
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
101 "Can't find source '%s' for route '%s'." % (
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
102 self.source_name, self.uri_pattern))
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
104 def hasParameter(self, name):
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
105 return any(lambda p: p.param_name == name, self.supported_params)
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
106
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
107 def getParameter(self, name):
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
108 for p in self.supported_params:
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
109 if p.param_name == name:
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
110 return p
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
111 raise Exception("No such supported route parameter '%s' in: %s" %
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
112 (name, self.uri_pattern))
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
113
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
114 def getParameterType(self, name):
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
115 return self.getParameter(name).param_type
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
116
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
117 def matchesParameters(self, route_params):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
118 return set(self.uri_params).issubset(route_params.keys())
173
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
119
380
f33712c4cfab routing: Fix bugs with matching URLs with correct route but missing metadata.
Ludovic Chabant <ludovic@chabant.com>
parents: 369
diff changeset
120 def matchUri(self, uri, strict=False):
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
121 if not uri.startswith(self.uri_root):
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
122 raise Exception("The given URI is not absolute: %s" % uri)
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
123 uri = uri[len(self.uri_root):]
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
124
256
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
125 if not self.pretty_urls:
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
126 uri = ugly_url_cleaner.sub('', uri)
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
127 elif self.trailing_slash:
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
128 uri = uri.rstrip('/')
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
129
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
130 route_params = None
173
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
131 m = self.uri_re.match(uri)
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
132 if m:
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
133 route_params = m.groupdict()
173
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
134 if self.uri_re_no_path:
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
135 m = self.uri_re_no_path.match(uri)
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
136 if m:
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
137 route_params = m.groupdict()
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
138 if route_params is None:
380
f33712c4cfab routing: Fix bugs with matching URLs with correct route but missing metadata.
Ludovic Chabant <ludovic@chabant.com>
parents: 369
diff changeset
139 return None
f33712c4cfab routing: Fix bugs with matching URLs with correct route but missing metadata.
Ludovic Chabant <ludovic@chabant.com>
parents: 369
diff changeset
140
f33712c4cfab routing: Fix bugs with matching URLs with correct route but missing metadata.
Ludovic Chabant <ludovic@chabant.com>
parents: 369
diff changeset
141 if not strict:
f33712c4cfab routing: Fix bugs with matching URLs with correct route but missing metadata.
Ludovic Chabant <ludovic@chabant.com>
parents: 369
diff changeset
142 # When matching URIs, if the URI is a match but is missing some
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
143 # parameters, fill those up with empty strings. This can happen if,
380
f33712c4cfab routing: Fix bugs with matching URLs with correct route but missing metadata.
Ludovic Chabant <ludovic@chabant.com>
parents: 369
diff changeset
144 # say, a route's pattern is `/foo/%slug%`, and we're matching an
f33712c4cfab routing: Fix bugs with matching URLs with correct route but missing metadata.
Ludovic Chabant <ludovic@chabant.com>
parents: 369
diff changeset
145 # URL like `/foo`.
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
146 matched_keys = set(route_params.keys())
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
147 missing_keys = set(self.uri_params) - matched_keys
380
f33712c4cfab routing: Fix bugs with matching URLs with correct route but missing metadata.
Ludovic Chabant <ludovic@chabant.com>
parents: 369
diff changeset
148 for k in missing_keys:
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
149 if self.getParameterType(k) != RouteParameter.TYPE_PATH:
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
150 return None
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
151 route_params[k] = ''
380
f33712c4cfab routing: Fix bugs with matching URLs with correct route but missing metadata.
Ludovic Chabant <ludovic@chabant.com>
parents: 369
diff changeset
152
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
153 for k in route_params:
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
154 route_params[k] = self._coerceRouteParameter(
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
155 k, route_params[k])
448
a17774094db8 serve: Fix bug with creating routing metadata from the URL.
Ludovic Chabant <ludovic@chabant.com>
parents: 438
diff changeset
156
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
157 return route_params
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
158
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
159 def getUri(self, route_params, *, sub_num=1):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
160 route_params = dict(route_params)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
161 for k in route_params:
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
162 route_params[k] = self._coerceRouteParameter(
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
163 k, route_params[k])
723
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
164
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
165 uri = self.uri_format % route_params
256
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
166 suffix = None
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
167 if sub_num > 1:
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
168 # Note that we know the pagination suffix starts with a slash.
262
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 256
diff changeset
169 suffix = self.pagination_suffix_format % {'num': sub_num}
256
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
170
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
171 if self.pretty_urls:
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
172 # Output will be:
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
173 # - `subdir/name`
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
174 # - `subdir/name/2`
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
175 # - `subdir/name.ext`
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
176 # - `subdir/name.ext/2`
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
177 if suffix:
262
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 256
diff changeset
178 if uri == '':
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 256
diff changeset
179 uri = suffix.lstrip('/')
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 256
diff changeset
180 else:
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 256
diff changeset
181 uri = uri.rstrip('/') + suffix
337
49408002798e internal: Fix stupid routing bug.
Ludovic Chabant <ludovic@chabant.com>
parents: 334
diff changeset
182 if self.trailing_slash and uri != '':
256
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
183 uri = uri.rstrip('/') + '/'
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
184 else:
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
185 # Output will be:
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
186 # - `subdir/name.html`
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
187 # - `subdir/name/2.html`
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
188 # - `subdir/name.ext`
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
189 # - `subdir/name/2.ext`
262
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 256
diff changeset
190 if uri == '':
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 256
diff changeset
191 if suffix:
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 256
diff changeset
192 uri = suffix.lstrip('/') + '.html'
256
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
193 else:
262
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 256
diff changeset
194 base_uri, ext = os.path.splitext(uri)
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 256
diff changeset
195 if not ext:
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 256
diff changeset
196 ext = '.html'
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 256
diff changeset
197 if suffix:
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 256
diff changeset
198 uri = base_uri + suffix + ext
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 256
diff changeset
199 else:
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 256
diff changeset
200 uri = base_uri + ext
256
da5e6e00fb41 bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents: 235
diff changeset
201
568
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 561
diff changeset
202 uri = self.uri_root + urllib.parse.quote(uri)
396
dc0988d937b3 serve: Fix bug where `?!debug` doesn't get appending correctly.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
203
dc0988d937b3 serve: Fix bug where `?!debug` doesn't get appending correctly.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
204 if self.show_debug_info:
dc0988d937b3 serve: Fix bug where `?!debug` doesn't get appending correctly.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
205 uri += '?!debug'
dc0988d937b3 serve: Fix bug where `?!debug` doesn't get appending correctly.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
206
235
55087da9a72e bake: Don't include the site root when building output paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 225
diff changeset
207 return uri
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
208
787
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
209 def execTemplateFunc(self, *args):
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
210 fixed_param_count = len(self.uri_params)
787
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
211 if self.func_has_variadic_parameter:
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
212 fixed_param_count -= 1
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
213
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
214 if len(args) < fixed_param_count:
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
215 raise Exception(
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
216 "Route function '%s' expected %d arguments, "
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
217 "got %d: %s" %
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
218 (self.func_name, fixed_param_count, len(args), args))
787
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
219
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
220 if self.func_has_variadic_parameter:
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
221 coerced_args = list(args[:fixed_param_count])
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
222 if len(args) > fixed_param_count:
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
223 var_arg = tuple(args[fixed_param_count:])
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
224 coerced_args.append(var_arg)
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
225 else:
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
226 coerced_args = args
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
227
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
228 route_params = {}
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
229 for arg_name, arg_val in zip(self.uri_params, coerced_args):
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
230 route_params[arg_name] = self._coerceRouteParameter(
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
231 arg_name, arg_val)
787
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
232
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
233 self.source.onRouteFunctionUsed(self, route_params)
787
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
234
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
235 return self.getUri(route_params)
787
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
236
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
237 def _uriFormatRepl(self, m):
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
238 if m.group('qual') or m.group('var'):
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
239 # Print a warning only if we're not in a worker process.
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
240 print_warning = not self.app.config.has('baker/worker_id')
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
241 if print_warning:
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
242 logger.warning("Route '%s' specified parameter types -- "
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
243 "they're not needed anymore." %
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
244 self.uri_pattern)
790
4cbe057a8b6a routing: Add some backwards compatibility support for parameter types.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
245
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
246 name = m.group('name')
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
247 self.uri_params.append(name)
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
248 try:
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
249 param_type = self.getParameterType(name)
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
250 if param_type == RouteParameter.TYPE_INT4:
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
251 return '%%(%s)04d' % name
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
252 elif param_type == RouteParameter.TYPE_INT2:
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
253 return '%%(%s)02d' % name
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
254 return '%%(%s)s' % name
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
255 except:
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
256 known = [p.name for p in self.supported_params]
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
257 raise Exception("Unknown route parameter '%s' for route '%s'. "
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
258 "Must be one of: %s'" %
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
259 (name, self.uri_pattern, known))
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
260
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
261 def _uriPatternRepl(self, m):
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
262 name = m.group('name')
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
263 param_type = self.getParameterType(name)
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
264 if param_type == RouteParameter.TYPE_PATH:
173
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
265 return r'(?P<%s>[^\?]*)' % name
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
266 elif param_type == RouteParameter.TYPE_INT4:
723
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
267 return r'(?P<%s>\d{4})' % name
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
268 elif param_type == RouteParameter.TYPE_INT2:
723
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
269 return r'(?P<%s>\d{2})' % name
173
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
270 return r'(?P<%s>[^/\?]+)' % name
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
271
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
272 def _uriNoPathRepl(self, m):
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
273 name = m.group('name')
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
274 param_type = self.getParameterType(name)
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
275 if param_type == RouteParameter.TYPE_PATH:
173
0a86a7a6b284 routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents: 154
diff changeset
276 return ''
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
277 return r'(?P<%s>[^/\?]+)' % name
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
278
723
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
279 def _coerceRouteParameter(self, name, val):
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
280 try:
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
281 param_type = self.getParameterType(name)
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
282 except:
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
283 # Unknown parameter... just leave it.
723
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
284 return val
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
285
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
286 if param_type in [RouteParameter.TYPE_INT2, RouteParameter.TYPE_INT4]:
723
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
287 try:
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
288 return int(val)
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
289 except ValueError:
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
290 raise Exception(
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
291 "Expected route parameter '%s' to be an integer, "
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
292 "but was: %s" % (name, param_type, val))
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 791
diff changeset
293 return val
790
4cbe057a8b6a routing: Add some backwards compatibility support for parameter types.
Ludovic Chabant <ludovic@chabant.com>
parents: 787
diff changeset
294
787
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
295 def _validateFuncName(self, name):
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
296 if not name:
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
297 return None
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
298 i = name.find('(')
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
299 if i >= 0:
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
300 name = name[:i]
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
301 logger.warning(
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
302 "Route function names shouldn't contain the list of arguments "
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
303 "anymore -- just specify '%s'." % name)
f6f9a284a5f3 routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 734
diff changeset
304 return name
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
305
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
306
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
307 class RouteFunction:
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
308 def __init__(self, route):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
309 self._route = route
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
310
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
311 def __call__(self, *args, **kwargs):
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
312 return self._route.execTemplateFunc(*args, **kwargs)