comparison piecrust/routing.py @ 430:21e26ed867b6

internal: Create full route metadata in one place. Instead of combining things at different moments to make up route metadata, build it once and for all up-front and use that.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 27 Jun 2015 08:27:35 -0700
parents dc0988d937b3
children cff70eeb1cc1
comparison
equal deleted inserted replaced
429:ca5a3c970263 430:21e26ed867b6
11 route_esc_re = re.compile(r'\\%((?P<qual>path)\\:)?(?P<name>\w+)\\%') 11 route_esc_re = re.compile(r'\\%((?P<qual>path)\\:)?(?P<name>\w+)\\%')
12 template_func_re = re.compile(r'^(?P<name>\w+)\((?P<first_arg>\w+)' 12 template_func_re = re.compile(r'^(?P<name>\w+)\((?P<first_arg>\w+)'
13 r'(?P<other_args>.*)\)\s*$') 13 r'(?P<other_args>.*)\)\s*$')
14 template_func_arg_re = re.compile(r',\s*(?P<arg>\w+)') 14 template_func_arg_re = re.compile(r',\s*(?P<arg>\w+)')
15 ugly_url_cleaner = re.compile(r'\.html$') 15 ugly_url_cleaner = re.compile(r'\.html$')
16
17
18 def create_route_metadata(page):
19 route_metadata = copy.deepcopy(page.source_metadata)
20 route_metadata.update(page.getRouteMetadata())
21
22 # TODO: fix this hard-coded shit
23 for key in ['year', 'month', 'day']:
24 if key in route_metadata and isinstance(route_metadata[key], str):
25 route_metadata[key] = int(route_metadata[key])
26
27 return route_metadata
16 28
17 29
18 class IRouteMetadataProvider(object): 30 class IRouteMetadataProvider(object):
19 def getRouteMetadata(self): 31 def getRouteMetadata(self):
20 raise NotImplementedError() 32 raise NotImplementedError()
120 for k in missing_keys: 132 for k in missing_keys:
121 route_metadata[k] = '' 133 route_metadata[k] = ''
122 134
123 return route_metadata 135 return route_metadata
124 136
125 def getUri(self, route_metadata, *, sub_num=1, provider=None): 137 def getUri(self, route_metadata, *, sub_num=1):
126 route_metadata = copy.deepcopy(route_metadata)
127 if provider:
128 route_metadata.update(provider.getRouteMetadata())
129
130 #TODO: fix this hard-coded shit
131 for key in ['year', 'month', 'day']:
132 if key in route_metadata and isinstance(route_metadata[key], str):
133 route_metadata[key] = int(route_metadata[key])
134
135 uri = self.uri_format % route_metadata 138 uri = self.uri_format % route_metadata
136 suffix = None 139 suffix = None
137 if sub_num > 1: 140 if sub_num > 1:
138 # Note that we know the pagination suffix starts with a slash. 141 # Note that we know the pagination suffix starts with a slash.
139 suffix = self.pagination_suffix_format % {'num': sub_num} 142 suffix = self.pagination_suffix_format % {'num': sub_num}