Mercurial > piecrust2
annotate piecrust/routing.py @ 1195:ae9387338db1 draft default tip
admin: add option to publish immediately
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Fri, 30 Dec 2022 16:48:04 -0800 |
parents | 10520472cc73 |
children |
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 |
1168
10520472cc73
routing: Fix breakages with routing on some versions of Python.
Ludovic Chabant <ludovic@chabant.com>
parents:
1159
diff
changeset
|
3 import sys |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
4 import copy |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 import logging |
520
bab91fcef741
bake/serve: Improve support for unicode, add slugification options.
Ludovic Chabant <ludovic@chabant.com>
parents:
515
diff
changeset
|
6 import urllib.parse |
711
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
661
diff
changeset
|
7 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
|
8 |
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 logger = logging.getLogger(__name__) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 |
787
f6f9a284a5f3
routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents:
734
diff
changeset
|
13 route_re = re.compile(r'%((?P<qual>[\w\d]+):)?(?P<var>\+)?(?P<name>\w+)%') |
1168
10520472cc73
routing: Fix breakages with routing on some versions of Python.
Ludovic Chabant <ludovic@chabant.com>
parents:
1159
diff
changeset
|
14 if sys.hexversion >= 0x3070000: |
10520472cc73
routing: Fix breakages with routing on some versions of Python.
Ludovic Chabant <ludovic@chabant.com>
parents:
1159
diff
changeset
|
15 route_esc_re = re.compile( |
10520472cc73
routing: Fix breakages with routing on some versions of Python.
Ludovic Chabant <ludovic@chabant.com>
parents:
1159
diff
changeset
|
16 r'%((?P<qual>[\w\d]+)\\:)?(?P<var>\\\+)?(?P<name>\w+)%') |
10520472cc73
routing: Fix breakages with routing on some versions of Python.
Ludovic Chabant <ludovic@chabant.com>
parents:
1159
diff
changeset
|
17 else: |
10520472cc73
routing: Fix breakages with routing on some versions of Python.
Ludovic Chabant <ludovic@chabant.com>
parents:
1159
diff
changeset
|
18 route_esc_re = re.compile( |
10520472cc73
routing: Fix breakages with routing on some versions of Python.
Ludovic Chabant <ludovic@chabant.com>
parents:
1159
diff
changeset
|
19 r'\\%((?P<qual>[\w\d]+)\\:)?(?P<var>\\\+)?(?P<name>\w+)\\%') |
10520472cc73
routing: Fix breakages with routing on some versions of Python.
Ludovic Chabant <ludovic@chabant.com>
parents:
1159
diff
changeset
|
20 |
256
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
21 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
|
22 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 |
555
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
520
diff
changeset
|
24 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
|
25 pass |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
520
diff
changeset
|
26 |
daf8df5ade7d
serve: Refactor the server to make pieces usable by the debugging middleware.
Ludovic Chabant <ludovic@chabant.com>
parents:
520
diff
changeset
|
27 |
711
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
661
diff
changeset
|
28 class InvalidRouteError(Exception): |
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
661
diff
changeset
|
29 pass |
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
661
diff
changeset
|
30 |
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
661
diff
changeset
|
31 |
1100
1ce67d2fae0a
routing: Fix URL generation bug with ugly URLs and index pages in sub-folders.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
32 class InvalidRouteParameterError(Exception): |
1ce67d2fae0a
routing: Fix URL generation bug with ugly URLs and index pages in sub-folders.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
33 pass |
1ce67d2fae0a
routing: Fix URL generation bug with ugly URLs and index pages in sub-folders.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
34 |
1ce67d2fae0a
routing: Fix URL generation bug with ugly URLs and index pages in sub-folders.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
35 |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
36 class RouteParameter(object): |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
37 TYPE_STRING = 0 |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
38 TYPE_PATH = 1 |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
39 TYPE_INT2 = 2 |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
40 TYPE_INT4 = 3 |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
41 |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
42 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
|
43 self.param_name = param_name |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
44 self.param_type = param_type |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
45 self.variadic = variadic |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
46 |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
47 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 class Route(object): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 """ 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
|
50 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
|
51 content sources. |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
52 """ |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
53 def __init__(self, app, cfg): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
54 self.app = app |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
55 self.config = copy.deepcopy(cfg) |
256
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
56 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
57 self.source_name = cfg['source'] |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
58 self.uri_pattern = cfg['url'].lstrip('/') |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
854
diff
changeset
|
59 self.pass_num = cfg.get('pass', 1) |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
60 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
61 self.supported_params = self.source.getSupportedRouteParameters() |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
62 |
256
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
63 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
|
64 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
|
65 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
|
66 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
|
67 '__cache/pagination_suffix_format') |
329
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
68 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
|
69 |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
70 self.uri_params = [] |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
71 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
|
72 |
0a86a7a6b284
routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents:
154
diff
changeset
|
73 # 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
|
74 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
|
75 re.escape(self.uri_pattern)) + '$' |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
76 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
|
77 |
0a86a7a6b284
routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents:
154
diff
changeset
|
78 # 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
|
79 # 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
|
80 # 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
|
81 # 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
|
82 # 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
|
83 # (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
|
84 # right now) |
0a86a7a6b284
routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents:
154
diff
changeset
|
85 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
|
86 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
|
87 .replace('//', '/') |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
88 .rstrip('/')) |
173
0a86a7a6b284
routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents:
154
diff
changeset
|
89 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
|
90 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
|
91 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
|
92 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
|
93 else: |
0a86a7a6b284
routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents:
154
diff
changeset
|
94 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
|
95 |
787
f6f9a284a5f3
routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents:
734
diff
changeset
|
96 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
|
97 self.func_has_variadic_parameter = False |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
98 for p in self.uri_params[:-1]: |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
99 param = self.getParameter(p) |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
100 if param.variadic: |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
101 raise Exception( |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
102 "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
|
103 "Got: %s" % self.uri_pattern) |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
104 if len(self.uri_params) > 0: |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
105 last_param = self.getParameter(self.uri_params[-1]) |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
106 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
|
107 |
711
ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
661
diff
changeset
|
108 @cached_property |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
109 def source(self): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
110 for src in self.app.sources: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
111 if src.name == self.source_name: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
112 return src |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
113 raise Exception( |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
114 "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
|
115 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
|
116 |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
117 def hasParameter(self, name): |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
118 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
|
119 |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
120 def getParameter(self, name): |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
121 for p in self.supported_params: |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
122 if p.param_name == name: |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
123 return p |
1100
1ce67d2fae0a
routing: Fix URL generation bug with ugly URLs and index pages in sub-folders.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
124 raise InvalidRouteParameterError( |
1ce67d2fae0a
routing: Fix URL generation bug with ugly URLs and index pages in sub-folders.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
125 "No such supported route parameter '%s' in: %s" % |
1ce67d2fae0a
routing: Fix URL generation bug with ugly URLs and index pages in sub-folders.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
126 (name, self.uri_pattern)) |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
127 |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
128 def getParameterType(self, name): |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
129 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
|
130 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
131 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
|
132 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
|
133 |
380
f33712c4cfab
routing: Fix bugs with matching URLs with correct route but missing metadata.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
134 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
|
135 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
|
136 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
|
137 uri = uri[len(self.uri_root):] |
422052d2e978
internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents:
262
diff
changeset
|
138 |
256
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
139 if not self.pretty_urls: |
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
140 uri = ugly_url_cleaner.sub('', uri) |
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
141 elif self.trailing_slash: |
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
142 uri = uri.rstrip('/') |
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
143 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
144 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
|
145 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
|
146 if m: |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
147 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
|
148 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
|
149 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
|
150 if m: |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
151 route_params = m.groupdict() |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
152 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
|
153 return None |
f33712c4cfab
routing: Fix bugs with matching URLs with correct route but missing metadata.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
154 |
f33712c4cfab
routing: Fix bugs with matching URLs with correct route but missing metadata.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
155 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
|
156 # 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
|
157 # 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
|
158 # 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
|
159 # URL like `/foo`. |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
160 matched_keys = set(route_params.keys()) |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
161 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
|
162 for k in missing_keys: |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
163 if self.getParameterType(k) != RouteParameter.TYPE_PATH: |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
164 return None |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
165 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
|
166 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
167 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
|
168 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
|
169 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
|
170 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
171 return route_params |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
172 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
173 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
|
174 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
|
175 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
|
176 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
|
177 k, route_params[k]) |
723
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
178 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
179 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
|
180 suffix = None |
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
181 if sub_num > 1: |
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
182 # 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
|
183 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
|
184 |
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
185 if self.pretty_urls: |
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
186 # Output will be: |
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
187 # - `subdir/name` |
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
188 # - `subdir/name/2` |
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
189 # - `subdir/name.ext` |
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
190 # - `subdir/name.ext/2` |
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
191 if suffix: |
262
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
256
diff
changeset
|
192 if uri == '': |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
256
diff
changeset
|
193 uri = suffix.lstrip('/') |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
256
diff
changeset
|
194 else: |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
256
diff
changeset
|
195 uri = uri.rstrip('/') + suffix |
337
49408002798e
internal: Fix stupid routing bug.
Ludovic Chabant <ludovic@chabant.com>
parents:
334
diff
changeset
|
196 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
|
197 uri = uri.rstrip('/') + '/' |
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
198 else: |
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
199 # Output will be: |
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
200 # - `subdir/name.html` |
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
201 # - `subdir/name/2.html` |
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
202 # - `subdir/name.ext` |
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
203 # - `subdir/name/2.ext` |
262
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
256
diff
changeset
|
204 if uri == '': |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
256
diff
changeset
|
205 if suffix: |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
256
diff
changeset
|
206 uri = suffix.lstrip('/') + '.html' |
256
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
207 else: |
262
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
256
diff
changeset
|
208 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
|
209 if not ext: |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
256
diff
changeset
|
210 ext = '.html' |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
256
diff
changeset
|
211 if suffix: |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
256
diff
changeset
|
212 uri = base_uri + suffix + ext |
61145dcd56e0
routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents:
256
diff
changeset
|
213 else: |
1100
1ce67d2fae0a
routing: Fix URL generation bug with ugly URLs and index pages in sub-folders.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
214 # If we just have the extension to add to the URL, we |
1ce67d2fae0a
routing: Fix URL generation bug with ugly URLs and index pages in sub-folders.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
215 # strip any trailing slash to prevent, say, the index |
1ce67d2fae0a
routing: Fix URL generation bug with ugly URLs and index pages in sub-folders.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
216 # page of a source from generating an URL like |
1ce67d2fae0a
routing: Fix URL generation bug with ugly URLs and index pages in sub-folders.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
217 # `subdir/.html`. Instead, it does `subdir.html`. |
1ce67d2fae0a
routing: Fix URL generation bug with ugly URLs and index pages in sub-folders.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
218 uri = base_uri.rstrip('/') + ext |
256
da5e6e00fb41
bake/serve: Make previewed and baked URLs consistent.
Ludovic Chabant <ludovic@chabant.com>
parents:
235
diff
changeset
|
219 |
568
6b6c5442c790
bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents:
561
diff
changeset
|
220 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
|
221 |
dc0988d937b3
serve: Fix bug where `?!debug` doesn't get appending correctly.
Ludovic Chabant <ludovic@chabant.com>
parents:
380
diff
changeset
|
222 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
|
223 uri += '?!debug' |
dc0988d937b3
serve: Fix bug where `?!debug` doesn't get appending correctly.
Ludovic Chabant <ludovic@chabant.com>
parents:
380
diff
changeset
|
224 |
235
55087da9a72e
bake: Don't include the site root when building output paths.
Ludovic Chabant <ludovic@chabant.com>
parents:
225
diff
changeset
|
225 return uri |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
226 |
787
f6f9a284a5f3
routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents:
734
diff
changeset
|
227 def execTemplateFunc(self, *args): |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
228 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
|
229 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
|
230 fixed_param_count -= 1 |
f6f9a284a5f3
routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents:
734
diff
changeset
|
231 |
f6f9a284a5f3
routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents:
734
diff
changeset
|
232 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
|
233 raise Exception( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
234 "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
|
235 "got %d: %s" % |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
236 (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
|
237 |
f6f9a284a5f3
routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents:
734
diff
changeset
|
238 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
|
239 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
|
240 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
|
241 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
|
242 coerced_args.append(var_arg) |
f6f9a284a5f3
routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents:
734
diff
changeset
|
243 else: |
f6f9a284a5f3
routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents:
734
diff
changeset
|
244 coerced_args = args |
f6f9a284a5f3
routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents:
734
diff
changeset
|
245 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
246 route_params = {} |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
247 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
|
248 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
|
249 arg_name, arg_val) |
787
f6f9a284a5f3
routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents:
734
diff
changeset
|
250 |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
251 self.source.onRouteFunctionUsed(route_params) |
787
f6f9a284a5f3
routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents:
734
diff
changeset
|
252 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
253 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
|
254 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
255 def _uriFormatRepl(self, m): |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
256 if m.group('qual') or m.group('var'): |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
257 # 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
|
258 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
|
259 if print_warning: |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
260 logger.warning("Route '%s' specified parameter types -- " |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
261 "they're not needed anymore." % |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
262 self.uri_pattern) |
790
4cbe057a8b6a
routing: Add some backwards compatibility support for parameter types.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
263 |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
264 name = m.group('name') |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
265 self.uri_params.append(name) |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
266 try: |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
267 param_type = self.getParameterType(name) |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
268 if param_type == RouteParameter.TYPE_INT4: |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
269 return '%%(%s)04d' % name |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
270 elif param_type == RouteParameter.TYPE_INT2: |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
271 return '%%(%s)02d' % name |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
272 return '%%(%s)s' % name |
1100
1ce67d2fae0a
routing: Fix URL generation bug with ugly URLs and index pages in sub-folders.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
273 except InvalidRouteParameterError: |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
274 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
|
275 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
|
276 "Must be one of: %s'" % |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
277 (name, self.uri_pattern, known)) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
278 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
279 def _uriPatternRepl(self, m): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
280 name = m.group('name') |
792
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 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
|
283 return r'(?P<%s>[^\?]*)' % name |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
284 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
|
285 return r'(?P<%s>\d{4})' % name |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
286 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
|
287 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
|
288 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
|
289 |
0a86a7a6b284
routes: Actually match metadata when finding routes, fix problems with paths.
Ludovic Chabant <ludovic@chabant.com>
parents:
154
diff
changeset
|
290 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
|
291 name = m.group('name') |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
292 param_type = self.getParameterType(name) |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
293 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
|
294 return '' |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
295 return r'(?P<%s>[^/\?]+)' % name |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
296 |
723
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
297 def _coerceRouteParameter(self, name, val): |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
298 try: |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
299 param_type = self.getParameterType(name) |
1100
1ce67d2fae0a
routing: Fix URL generation bug with ugly URLs and index pages in sub-folders.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
300 except InvalidRouteParameterError: |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
301 # Unknown parameter... just leave it. |
723
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
302 return val |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
303 |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
304 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
|
305 try: |
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
306 return int(val) |
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
307 except ValueError: |
606f6d57b5df
routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents:
711
diff
changeset
|
308 raise Exception( |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
309 "Expected route parameter '%s' to be an integer, " |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
310 "but was: %s" % (name, param_type, val)) |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
791
diff
changeset
|
311 return val |
790
4cbe057a8b6a
routing: Add some backwards compatibility support for parameter types.
Ludovic Chabant <ludovic@chabant.com>
parents:
787
diff
changeset
|
312 |
787
f6f9a284a5f3
routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents:
734
diff
changeset
|
313 def _validateFuncName(self, name): |
f6f9a284a5f3
routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents:
734
diff
changeset
|
314 if not name: |
f6f9a284a5f3
routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents:
734
diff
changeset
|
315 return None |
f6f9a284a5f3
routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents:
734
diff
changeset
|
316 i = name.find('(') |
f6f9a284a5f3
routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents:
734
diff
changeset
|
317 if i >= 0: |
f6f9a284a5f3
routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents:
734
diff
changeset
|
318 name = name[:i] |
f6f9a284a5f3
routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents:
734
diff
changeset
|
319 logger.warning( |
f6f9a284a5f3
routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents:
734
diff
changeset
|
320 "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
|
321 "anymore -- just specify '%s'." % name) |
f6f9a284a5f3
routing: Simplify how route functions are declared and handled.
Ludovic Chabant <ludovic@chabant.com>
parents:
734
diff
changeset
|
322 return name |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
323 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
324 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
325 class RouteFunction: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
326 def __init__(self, route): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
792
diff
changeset
|
327 self._route = route |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
328 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
329 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
|
330 return self._route.execTemplateFunc(*args, **kwargs) |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
331 |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
332 def _isCompatibleRoute(self, route): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
333 return self._route.uri_pattern == route.uri_pattern |