Mercurial > piecrust2
annotate piecrust/sources/autoconfig.py @ 840:7f3043f9f26f
internal: Don't check for a page repository, there's always one.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 15 Feb 2017 22:17:13 -0800 |
parents | 58ebf50235a5 |
children | 4850f8c21b6e |
rev | line source |
---|---|
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
1 import re |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 import os |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 import os.path |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 import logging |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
5 from piecrust.configuration import ConfigurationError |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
505
diff
changeset
|
6 from piecrust.routing import RouteParameter |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 from piecrust.sources.base import ( |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
8 PageSource, PageFactory, InvalidFileSystemEndpointError) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
9 from piecrust.sources.default import ( |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
10 filter_page_dirname, filter_page_filename) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
11 from piecrust.sources.interfaces import IListableSource |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
12 from piecrust.sources.mixins import SimplePaginationSourceMixin |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 logger = logging.getLogger(__name__) |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
18 class AutoConfigSourceBase(PageSource, SimplePaginationSourceMixin, |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
19 IListableSource): |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
20 """ Base class for page sources that automatically apply configuration |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
21 settings to their generated pages based on those pages' paths. |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
22 """ |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 def __init__(self, app, name, config): |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
24 super(AutoConfigSourceBase, self).__init__(app, name, config) |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
25 self.fs_endpoint = config.get('fs_endpoint', name) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
26 self.fs_endpoint_path = os.path.join(self.root_dir, self.fs_endpoint) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
27 self.supported_extensions = list( |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
28 app.config.get('site/auto_formats').keys()) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
29 self.default_auto_format = app.config.get('site/default_auto_format') |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
30 |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
31 self.capture_mode = config.get('capture_mode', 'path') |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
32 if self.capture_mode not in ['path', 'dirname', 'filename']: |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
33 raise ConfigurationError("Capture mode in source '%s' must be " |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
34 "one of: path, dirname, filename" % |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
35 name) |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 |
792
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
505
diff
changeset
|
37 def getSupportedRouteParameters(self): |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
505
diff
changeset
|
38 return [ |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
505
diff
changeset
|
39 RouteParameter('slug', RouteParameter.TYPE_PATH)] |
58ebf50235a5
routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
505
diff
changeset
|
40 |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
41 def buildPageFactories(self): |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
42 logger.debug("Scanning for pages in: %s" % self.fs_endpoint_path) |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 if not os.path.isdir(self.fs_endpoint_path): |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
44 raise InvalidFileSystemEndpointError(self.name, |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
45 self.fs_endpoint_path) |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 for dirpath, dirnames, filenames in os.walk(self.fs_endpoint_path): |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
48 rel_dirpath = os.path.relpath(dirpath, self.fs_endpoint_path) |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
49 dirnames[:] = list(filter(filter_page_dirname, dirnames)) |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
50 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
51 # If `capture_mode` is `dirname`, we don't need to recompute it |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
52 # for each filename, so we do it here. |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
53 if self.capture_mode == 'dirname': |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
54 config = self._extractConfigFragment(rel_dirpath) |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
55 |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
56 for f in filter(filter_page_filename, filenames): |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
57 if self.capture_mode == 'path': |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
58 path = os.path.join(rel_dirpath, f) |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
59 config = self._extractConfigFragment(path) |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
60 elif self.capture_mode == 'filename': |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
61 config = self._extractConfigFragment(f) |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
62 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
63 fac_path = f |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
64 if rel_dirpath != '.': |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
65 fac_path = os.path.join(rel_dirpath, f) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
66 |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
67 slug = self._makeSlug(fac_path) |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
68 |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
69 metadata = { |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
70 'slug': slug, |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
71 'config': config} |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
72 yield PageFactory(self, fac_path, metadata) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
73 |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
74 def resolveRef(self, ref_path): |
363
dd25bd3ce1f9
serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
360
diff
changeset
|
75 path = os.path.normpath( |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
76 os.path.join(self.fs_endpoint_path, ref_path.lstrip("\\/"))) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
77 |
363
dd25bd3ce1f9
serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
360
diff
changeset
|
78 config = None |
dd25bd3ce1f9
serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
360
diff
changeset
|
79 if self.capture_mode == 'dirname': |
dd25bd3ce1f9
serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
360
diff
changeset
|
80 config = self._extractConfigFragment(os.path.dirname(ref_path)) |
dd25bd3ce1f9
serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
360
diff
changeset
|
81 elif self.capture_mode == 'path': |
dd25bd3ce1f9
serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
360
diff
changeset
|
82 config = self._extractConfigFragment(ref_path) |
dd25bd3ce1f9
serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
360
diff
changeset
|
83 elif self.capture_mode == 'filename': |
dd25bd3ce1f9
serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
360
diff
changeset
|
84 config = self._extractConfigFragment(os.path.basename(ref_path)) |
dd25bd3ce1f9
serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
360
diff
changeset
|
85 |
dd25bd3ce1f9
serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
360
diff
changeset
|
86 slug = self._makeSlug(ref_path) |
dd25bd3ce1f9
serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
360
diff
changeset
|
87 metadata = {'slug': slug, 'config': config} |
dd25bd3ce1f9
serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
360
diff
changeset
|
88 return path, metadata |
dd25bd3ce1f9
serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
360
diff
changeset
|
89 |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
90 def listPath(self, rel_path): |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
91 raise NotImplementedError() |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
92 |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
93 def getDirpath(self, rel_path): |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
94 return os.path.dirname(rel_path) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
95 |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
96 def getBasename(self, rel_path): |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
97 filename = os.path.basename(rel_path) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
98 name, _ = os.path.splitext(filename) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
99 return name |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
100 |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
101 def _makeSlug(self, rel_path): |
267
f512905ae812
sources: Generate proper slugs in the `autoconfig` and `ordered` sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
248
diff
changeset
|
102 slug = rel_path.replace('\\', '/') |
f512905ae812
sources: Generate proper slugs in the `autoconfig` and `ordered` sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
248
diff
changeset
|
103 slug = self._cleanSlug(slug) |
f512905ae812
sources: Generate proper slugs in the `autoconfig` and `ordered` sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
248
diff
changeset
|
104 slug, ext = os.path.splitext(slug) |
f512905ae812
sources: Generate proper slugs in the `autoconfig` and `ordered` sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
248
diff
changeset
|
105 if ext.lstrip('.') not in self.supported_extensions: |
f512905ae812
sources: Generate proper slugs in the `autoconfig` and `ordered` sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
248
diff
changeset
|
106 slug += ext |
f512905ae812
sources: Generate proper slugs in the `autoconfig` and `ordered` sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
248
diff
changeset
|
107 if slug.startswith('./'): |
f512905ae812
sources: Generate proper slugs in the `autoconfig` and `ordered` sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
248
diff
changeset
|
108 slug = slug[2:] |
f512905ae812
sources: Generate proper slugs in the `autoconfig` and `ordered` sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
248
diff
changeset
|
109 if slug == '_index': |
f512905ae812
sources: Generate proper slugs in the `autoconfig` and `ordered` sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
248
diff
changeset
|
110 slug = '' |
f512905ae812
sources: Generate proper slugs in the `autoconfig` and `ordered` sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
248
diff
changeset
|
111 return slug |
f512905ae812
sources: Generate proper slugs in the `autoconfig` and `ordered` sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
248
diff
changeset
|
112 |
f512905ae812
sources: Generate proper slugs in the `autoconfig` and `ordered` sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
248
diff
changeset
|
113 def _cleanSlug(self, slug): |
f512905ae812
sources: Generate proper slugs in the `autoconfig` and `ordered` sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
248
diff
changeset
|
114 return slug |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
115 |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
116 def _extractConfigFragment(self, rel_path): |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
117 raise NotImplementedError() |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
118 |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
119 |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
120 class AutoConfigSource(AutoConfigSourceBase): |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
121 """ Page source that extracts configuration settings from the sub-folders |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
122 each page resides in. This is ideal for setting tags or categories |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
123 on pages based on the folders they're in. |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
124 """ |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
125 SOURCE_NAME = 'autoconfig' |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
126 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
127 def __init__(self, app, name, config): |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
128 config['capture_mode'] = 'dirname' |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
129 super(AutoConfigSource, self).__init__(app, name, config) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
130 self.setting_name = config.get('setting_name', name) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
131 self.only_single_values = config.get('only_single_values', False) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
132 self.collapse_single_values = config.get('collapse_single_values', |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
133 False) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
134 self.supported_extensions = list( |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
135 app.config.get('site/auto_formats').keys()) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
136 |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
137 def _extractConfigFragment(self, rel_path): |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
138 if rel_path == '.': |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
139 values = [] |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
140 else: |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
141 values = rel_path.split(os.sep) |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
142 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
143 if self.only_single_values: |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
144 if len(values) > 1: |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
145 raise Exception("Only one folder level is allowed for pages " |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
146 "in source '%s'." % self.name) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
147 elif len(values) == 1: |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
148 values = values[0] |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
149 else: |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
150 values = None |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
151 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
152 if self.collapse_single_values: |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
153 if len(values) == 1: |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
154 values = values[0] |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
155 elif len(values) == 0: |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
156 values = None |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
157 |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
158 return {self.setting_name: values} |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
159 |
363
dd25bd3ce1f9
serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
360
diff
changeset
|
160 def findPageFactory(self, metadata, mode): |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
161 # Pages from this source are effectively flattened, so we need to |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
162 # find pages using a brute-force kinda way. |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
163 for dirpath, dirnames, filenames in os.walk(self.fs_endpoint_path): |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
164 for f in filenames: |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
165 slug, _ = os.path.splitext(f) |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
166 if slug == metadata['slug']: |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
167 path = os.path.join(dirpath, f) |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
168 rel_path = os.path.relpath(path, self.fs_endpoint_path) |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
169 config = self._extractConfigFragment(rel_path) |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
170 metadata = {'slug': slug, 'config': config} |
363
dd25bd3ce1f9
serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
360
diff
changeset
|
171 return PageFactory(self, rel_path, metadata) |
dd25bd3ce1f9
serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
360
diff
changeset
|
172 return None |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
173 |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
174 def listPath(self, rel_path): |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
175 rel_path = rel_path.lstrip('\\/') |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
176 path = os.path.join(self.fs_endpoint_path, rel_path) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
177 names = sorted(os.listdir(path)) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
178 items = [] |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
179 for name in names: |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
180 if os.path.isdir(os.path.join(path, name)): |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
181 if filter_page_dirname(name): |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
182 rel_subdir = os.path.join(rel_path, name) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
183 items.append((True, name, rel_subdir)) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
184 else: |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
185 if filter_page_filename(name): |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
186 cur_rel_path = os.path.join(rel_path, name) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
187 slug = self._makeSlug(cur_rel_path) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
188 config = self._extractConfigFragment(cur_rel_path) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
189 metadata = {'slug': slug, 'config': config} |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
190 fac = PageFactory(self, cur_rel_path, metadata) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
191 |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
192 name, _ = os.path.splitext(name) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
193 items.append((False, name, fac)) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
194 return items |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
195 |
267
f512905ae812
sources: Generate proper slugs in the `autoconfig` and `ordered` sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
248
diff
changeset
|
196 def _cleanSlug(self, slug): |
f512905ae812
sources: Generate proper slugs in the `autoconfig` and `ordered` sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
248
diff
changeset
|
197 return os.path.basename(slug) |
f512905ae812
sources: Generate proper slugs in the `autoconfig` and `ordered` sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
248
diff
changeset
|
198 |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
199 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
200 class OrderedPageSource(AutoConfigSourceBase): |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
201 """ A page source that assigns an "order" to its pages based on a |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
202 numerical prefix in their filename. Page iterators will automatically |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
203 sort pages using that order. |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
204 """ |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
205 SOURCE_NAME = 'ordered' |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
206 |
286
a2d283d1033d
tests: Fixes for running on Windows.
Ludovic Chabant <ludovic@chabant.com>
parents:
267
diff
changeset
|
207 re_pattern = re.compile(r'(^|[/\\])(?P<num>\d+)_') |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
208 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
209 def __init__(self, app, name, config): |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
210 config['capture_mode'] = 'path' |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
211 super(OrderedPageSource, self).__init__(app, name, config) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
212 self.setting_name = config.get('setting_name', 'order') |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
213 self.default_value = config.get('default_value', 0) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
214 self.supported_extensions = list( |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
215 app.config.get('site/auto_formats').keys()) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
216 |
363
dd25bd3ce1f9
serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
360
diff
changeset
|
217 def findPageFactory(self, metadata, mode): |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
218 uri_path = metadata.get('slug', '') |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
219 if uri_path == '': |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
220 uri_path = '_index' |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
221 |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
222 path = self.fs_endpoint_path |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
223 uri_parts = uri_path.split('/') |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
224 for i, p in enumerate(uri_parts): |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
225 if i == len(uri_parts) - 1: |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
226 # Last part, this is the filename. We need to check for either |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
227 # the name, or the name with the prefix, but also handle a |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
228 # possible extension. |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
229 p_pat = r'(\d+_)?' + re.escape(p) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
230 |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
231 _, ext = os.path.splitext(uri_path) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
232 if ext == '': |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
233 p_pat += r'\.[\w\d]+' |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
234 |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
235 found = False |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
236 for name in os.listdir(path): |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
237 if re.match(p_pat, name): |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
238 path = os.path.join(path, name) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
239 found = True |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
240 break |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
241 if not found: |
363
dd25bd3ce1f9
serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
360
diff
changeset
|
242 return None |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
243 else: |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
244 # Find each sub-directory. It can either be a directory with |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
245 # the name itself, or the name with a number prefix. |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
246 p_pat = r'(\d+_)?' + re.escape(p) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
247 found = False |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
248 for name in os.listdir(path): |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
249 if re.match(p_pat, name): |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
250 path = os.path.join(path, name) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
251 found = True |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
252 break |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
253 if not found: |
363
dd25bd3ce1f9
serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
360
diff
changeset
|
254 return None |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
255 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
256 fac_path = os.path.relpath(path, self.fs_endpoint_path) |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
257 config = self._extractConfigFragment(fac_path) |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
258 metadata = {'slug': uri_path, 'config': config} |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
259 |
363
dd25bd3ce1f9
serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
360
diff
changeset
|
260 return PageFactory(self, fac_path, metadata) |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
261 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
262 def getSorterIterator(self, it): |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
263 accessor = self.getSettingAccessor() |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
264 return OrderTrailSortIterator(it, self.setting_name + '_trail', |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
265 value_accessor=accessor) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
266 |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
267 def listPath(self, rel_path): |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
268 rel_path = rel_path.lstrip('/') |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
269 path = self.fs_endpoint_path |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
270 if rel_path != '': |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
271 parts = rel_path.split('/') |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
272 for p in parts: |
360
95874699ec2e
sources: Fix how the `autoconfig` source iterates over its structure.
Ludovic Chabant <ludovic@chabant.com>
parents:
286
diff
changeset
|
273 p_pat = r'(\d+_)?' + re.escape(p) + '$' |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
274 for name in os.listdir(path): |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
275 if re.match(p_pat, name): |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
276 path = os.path.join(path, name) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
277 break |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
278 else: |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
279 raise Exception("No such path: %s" % rel_path) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
280 |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
281 items = [] |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
282 names = sorted(os.listdir(path)) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
283 for name in names: |
248
3f740928043a
sources: The ordered source returns names without prefixes in `listPath`.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
284 clean_name = self.re_pattern.sub('', name) |
3f740928043a
sources: The ordered source returns names without prefixes in `listPath`.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
285 clean_name, _ = os.path.splitext(clean_name) |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
286 if os.path.isdir(os.path.join(path, name)): |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
287 if filter_page_dirname(name): |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
288 rel_subdir = os.path.join(rel_path, name) |
248
3f740928043a
sources: The ordered source returns names without prefixes in `listPath`.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
289 items.append((True, clean_name, rel_subdir)) |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
290 else: |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
291 if filter_page_filename(name): |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
292 slug = self._makeSlug(os.path.join(rel_path, name)) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
293 |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
294 fac_path = name |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
295 if rel_path != '.': |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
296 fac_path = os.path.join(rel_path, name) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
297 fac_path = fac_path.replace('\\', '/') |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
298 |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
299 config = self._extractConfigFragment(fac_path) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
300 metadata = {'slug': slug, 'config': config} |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
301 fac = PageFactory(self, fac_path, metadata) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
302 |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
303 name, _ = os.path.splitext(name) |
248
3f740928043a
sources: The ordered source returns names without prefixes in `listPath`.
Ludovic Chabant <ludovic@chabant.com>
parents:
242
diff
changeset
|
304 items.append((False, clean_name, fac)) |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
305 return items |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
306 |
267
f512905ae812
sources: Generate proper slugs in the `autoconfig` and `ordered` sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
248
diff
changeset
|
307 def _cleanSlug(self, slug): |
f512905ae812
sources: Generate proper slugs in the `autoconfig` and `ordered` sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
248
diff
changeset
|
308 return self.re_pattern.sub(r'\1', slug) |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
309 |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
310 def _extractConfigFragment(self, rel_path): |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
311 values = [] |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
312 for m in self.re_pattern.finditer(rel_path): |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
313 val = int(m.group('num')) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
314 values.append(val) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
315 |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
316 if len(values) == 0: |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
317 values.append(self.default_value) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
318 |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
319 return { |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
320 self.setting_name: values[-1], |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
321 self.setting_name + '_trail': values} |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
322 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
323 def _populateMetadata(self, rel_path, metadata, mode=None): |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
324 _, filename = os.path.split(rel_path) |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
325 config = self._extractConfigFragment(filename) |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
326 metadata['config'] = config |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
327 slug = metadata['slug'] |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
328 metadata['slug'] = self.re_pattern.sub(r'\1', slug) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
329 |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
330 |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
331 class OrderTrailSortIterator(object): |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
332 def __init__(self, it, trail_name, value_accessor): |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
333 self.it = it |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
334 self.trail_name = trail_name |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
335 self.value_accessor = value_accessor |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
336 |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
337 def __iter__(self): |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
338 return iter(sorted(self.it, key=self._key_getter)) |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
339 |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
340 def _key_getter(self, item): |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
341 values = self.value_accessor(item, self.trail_name) |
505
883a5544cd7f
bug: Fix a crash with the `ordered` page source when sorting pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
363
diff
changeset
|
342 key = ''.join(map(lambda v: str(v), values)) |
242
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
343 return key |
f130365568ff
internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents:
239
diff
changeset
|
344 |