Mercurial > piecrust2
comparison piecrust/sources/autoconfig.py @ 148:432cd534ce08
Add `autoconfig` page source.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 29 Nov 2014 21:00:54 -0800 |
parents | |
children | 683baa977d97 |
comparison
equal
deleted
inserted
replaced
147:ab6e7e0e9d44 | 148:432cd534ce08 |
---|---|
1 import os | |
2 import os.path | |
3 import logging | |
4 from piecrust.sources.base import ( | |
5 PageSource, IPreparingSource, SimplePaginationSourceMixin, | |
6 PageNotFoundError, InvalidFileSystemEndpointError, | |
7 PageFactory, MODE_CREATING, MODE_PARSING) | |
8 | |
9 | |
10 logger = logging.getLogger(__name__) | |
11 | |
12 | |
13 class AutoConfigSource(PageSource, | |
14 SimplePaginationSourceMixin): | |
15 SOURCE_NAME = 'autoconfig' | |
16 PATH_FORMAT = '%(values)s/%(slug)s.%(ext)s' | |
17 | |
18 def __init__(self, app, name, config): | |
19 super(AutoConfigSource, self).__init__(app, name, config) | |
20 self.fs_endpoint = config.get('fs_endpoint', name) | |
21 self.fs_endpoint_path = os.path.join(self.root_dir, self.fs_endpoint) | |
22 self.supported_extensions = list(app.config.get('site/auto_formats').keys()) | |
23 self.default_auto_format = app.config.get('site/default_auto_format') | |
24 self.setting_name = config.get('setting_name', name) | |
25 self.collapse_single_values = config.get('collapse_single_values', False) | |
26 self.only_single_values = config.get('only_single_values', False) | |
27 | |
28 def buildPageFactories(self): | |
29 if not os.path.isdir(self.fs_endpoint_path): | |
30 raise InvalidFileSystemEndpointError(self.name, self.fs_endpoint_path) | |
31 | |
32 for dirpath, dirnames, filenames in os.walk(self.fs_endpoint_path): | |
33 if not filenames: | |
34 continue | |
35 config = self._extractConfigFragment(dirpath) | |
36 for f in filenames: | |
37 slug, ext = os.path.splitext(f) | |
38 path = os.path.join(dirpath, f) | |
39 metadata = { | |
40 'slug': slug, | |
41 'config': config} | |
42 yield PageFactory(self, path, metadata) | |
43 | |
44 def _extractConfigFragment(self, path): | |
45 rel_path = os.path.relpath(path, self.fs_endpoint_path) | |
46 if rel_path == '.': | |
47 values = [] | |
48 else: | |
49 values = rel_path.split(os.sep) | |
50 if self.only_single_values and len(values) > 1: | |
51 raise Exception("Only one folder level is allowed for pages " | |
52 "in source '%s'." % self.name) | |
53 if self.collapse_single_values and len(values) == 1: | |
54 values = values[0] | |
55 return {self.setting_name: values} | |
56 | |
57 def resolveRef(self, ref_path): | |
58 return os.path.normpath( | |
59 os.path.join(self.fs_endpoint_path, ref_path)) | |
60 | |
61 def findPagePath(self, metadata, mode): | |
62 for dirpath, dirnames, filenames in os.walk(self.fs_endpoint_path): | |
63 for f in filenames: | |
64 slug, _ = os.path.splitext(f) | |
65 if slug == metadata['slug']: | |
66 path = os.path.join(dirpath, f) | |
67 rel_path = os.path.relpath(path, self.fs_endpoint_path) | |
68 config = self._extractConfigFragment(dirpath) | |
69 metadata = {'slug': slug, 'config': config} | |
70 return rel_path, metadata | |
71 |