comparison piecrust/sources/autoconfig.py @ 363:dd25bd3ce1f9

serve: Refactoring and fixes to be able to serve taxonomy pages. * Page sources' `findPagePath` is renamed to `findPageFactory`, so that it also returns source metadata. * Page refs now store possible hits more properly, and use the previous point to also store metadata. As a result, they can also return a proper factory. * Implement `findPageFactory` correctly in all built-in sources. * When the Chef server matches a taxonomy page, get the source metadata from the page ref in order to make a more proper page. * Make the `getRoute(s)` functions explicitely say if they want taxonomy routes or not.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 26 Apr 2015 15:07:40 -0700
parents 95874699ec2e
children 883a5544cd7f
comparison
equal deleted inserted replaced
362:ff595828a364 363:dd25bd3ce1f9
65 'slug': slug, 65 'slug': slug,
66 'config': config} 66 'config': config}
67 yield PageFactory(self, fac_path, metadata) 67 yield PageFactory(self, fac_path, metadata)
68 68
69 def resolveRef(self, ref_path): 69 def resolveRef(self, ref_path):
70 return os.path.normpath( 70 path = os.path.normpath(
71 os.path.join(self.fs_endpoint_path, ref_path.lstrip("\\/"))) 71 os.path.join(self.fs_endpoint_path, ref_path.lstrip("\\/")))
72
73 config = None
74 if self.capture_mode == 'dirname':
75 config = self._extractConfigFragment(os.path.dirname(ref_path))
76 elif self.capture_mode == 'path':
77 config = self._extractConfigFragment(ref_path)
78 elif self.capture_mode == 'filename':
79 config = self._extractConfigFragment(os.path.basename(ref_path))
80
81 slug = self._makeSlug(ref_path)
82 metadata = {'slug': slug, 'config': config}
83 return path, metadata
72 84
73 def listPath(self, rel_path): 85 def listPath(self, rel_path):
74 raise NotImplementedError() 86 raise NotImplementedError()
75 87
76 def getDirpath(self, rel_path): 88 def getDirpath(self, rel_path):
138 elif len(values) == 0: 150 elif len(values) == 0:
139 values = None 151 values = None
140 152
141 return {self.setting_name: values} 153 return {self.setting_name: values}
142 154
143 def findPagePath(self, metadata, mode): 155 def findPageFactory(self, metadata, mode):
144 # Pages from this source are effectively flattened, so we need to 156 # Pages from this source are effectively flattened, so we need to
145 # find pages using a brute-force kinda way. 157 # find pages using a brute-force kinda way.
146 for dirpath, dirnames, filenames in os.walk(self.fs_endpoint_path): 158 for dirpath, dirnames, filenames in os.walk(self.fs_endpoint_path):
147 for f in filenames: 159 for f in filenames:
148 slug, _ = os.path.splitext(f) 160 slug, _ = os.path.splitext(f)
149 if slug == metadata['slug']: 161 if slug == metadata['slug']:
150 path = os.path.join(dirpath, f) 162 path = os.path.join(dirpath, f)
151 rel_path = os.path.relpath(path, self.fs_endpoint_path) 163 rel_path = os.path.relpath(path, self.fs_endpoint_path)
152 config = self._extractConfigFragment(rel_path) 164 config = self._extractConfigFragment(rel_path)
153 metadata = {'slug': slug, 'config': config} 165 metadata = {'slug': slug, 'config': config}
154 return rel_path, metadata 166 return PageFactory(self, rel_path, metadata)
167 return None
155 168
156 def listPath(self, rel_path): 169 def listPath(self, rel_path):
157 rel_path = rel_path.lstrip('\\/') 170 rel_path = rel_path.lstrip('\\/')
158 path = os.path.join(self.fs_endpoint_path, rel_path) 171 path = os.path.join(self.fs_endpoint_path, rel_path)
159 names = sorted(os.listdir(path)) 172 names = sorted(os.listdir(path))
194 self.setting_name = config.get('setting_name', 'order') 207 self.setting_name = config.get('setting_name', 'order')
195 self.default_value = config.get('default_value', 0) 208 self.default_value = config.get('default_value', 0)
196 self.supported_extensions = list( 209 self.supported_extensions = list(
197 app.config.get('site/auto_formats').keys()) 210 app.config.get('site/auto_formats').keys())
198 211
199 def findPagePath(self, metadata, mode): 212 def findPageFactory(self, metadata, mode):
200 uri_path = metadata.get('slug', '') 213 uri_path = metadata.get('slug', '')
201 if uri_path == '': 214 if uri_path == '':
202 uri_path = '_index' 215 uri_path = '_index'
203 216
204 path = self.fs_endpoint_path 217 path = self.fs_endpoint_path
219 if re.match(p_pat, name): 232 if re.match(p_pat, name):
220 path = os.path.join(path, name) 233 path = os.path.join(path, name)
221 found = True 234 found = True
222 break 235 break
223 if not found: 236 if not found:
224 return None, None 237 return None
225 else: 238 else:
226 # Find each sub-directory. It can either be a directory with 239 # Find each sub-directory. It can either be a directory with
227 # the name itself, or the name with a number prefix. 240 # the name itself, or the name with a number prefix.
228 p_pat = r'(\d+_)?' + re.escape(p) 241 p_pat = r'(\d+_)?' + re.escape(p)
229 found = False 242 found = False
231 if re.match(p_pat, name): 244 if re.match(p_pat, name):
232 path = os.path.join(path, name) 245 path = os.path.join(path, name)
233 found = True 246 found = True
234 break 247 break
235 if not found: 248 if not found:
236 return None, None 249 return None
237 250
238 fac_path = os.path.relpath(path, self.fs_endpoint_path) 251 fac_path = os.path.relpath(path, self.fs_endpoint_path)
239 config = self._extractConfigFragment(fac_path) 252 config = self._extractConfigFragment(fac_path)
240 metadata = {'slug': uri_path, 'config': config} 253 metadata = {'slug': uri_path, 'config': config}
241 254
242 return fac_path, metadata 255 return PageFactory(self, fac_path, metadata)
243 256
244 def getSorterIterator(self, it): 257 def getSorterIterator(self, it):
245 accessor = self.getSettingAccessor() 258 accessor = self.getSettingAccessor()
246 return OrderTrailSortIterator(it, self.setting_name + '_trail', 259 return OrderTrailSortIterator(it, self.setting_name + '_trail',
247 value_accessor=accessor) 260 value_accessor=accessor)