Mercurial > piecrust2
annotate piecrust/sources/autoconfig.py @ 239:f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Also add unit tests.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 15 Feb 2015 22:48:42 -0800 |
parents | 683baa977d97 |
children | f130365568ff |
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 |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
4 import glob |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 import logging |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
6 from piecrust.configuration import ConfigurationError |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
7 from piecrust.data.iterators import SettingSortIterator |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 from piecrust.sources.base import ( |
156
683baa977d97
Simplify `AutoConfigSource` by inheriting from `SimplePageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
148
diff
changeset
|
9 SimplePageSource, IPreparingSource, SimplePaginationSourceMixin, |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 PageNotFoundError, InvalidFileSystemEndpointError, |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 PageFactory, MODE_CREATING, MODE_PARSING) |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 |
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 logger = logging.getLogger(__name__) |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
17 class AutoConfigSourceBase(SimplePageSource, |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
18 SimplePaginationSourceMixin): |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
19 """ 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
|
20 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
|
21 """ |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 def __init__(self, app, name, config): |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
23 super(AutoConfigSourceBase, self).__init__(app, name, config) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
24 self.capture_mode = config.get('capture_mode', 'path') |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
25 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
|
26 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
|
27 "one of: path, dirname, filename" % |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
28 name) |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
29 |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
30 def buildPageFactories(self): |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
31 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
|
32 raise InvalidFileSystemEndpointError(self.name, |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
33 self.fs_endpoint_path) |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 for dirpath, dirnames, filenames in os.walk(self.fs_endpoint_path): |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 if not filenames: |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
37 continue |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
38 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
39 rel_dirpath = os.path.relpath(dirpath, self.fs_endpoint_path) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
40 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
41 # 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
|
42 # for each filename, so we do it here. |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
43 if self.capture_mode == 'dirname': |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
44 config = self.extractConfigFragment(rel_dirpath) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
45 |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 for f in filenames: |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
47 if self.capture_mode == 'path': |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
48 path = os.path.join(rel_dirpath, f) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
49 config = self.extractConfigFragment(path) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
50 elif self.capture_mode == 'filename': |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
51 config = self.extractConfigFragment(f) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
52 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
53 fac_path = f |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
54 if rel_dirpath != '.': |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
55 fac_path = os.path.join(rel_dirpath, f) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
56 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
57 slug = self.makeSlug(rel_dirpath, f) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
58 |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
59 metadata = { |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
60 'slug': slug, |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
61 'config': config} |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
62 yield PageFactory(self, fac_path, metadata) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
63 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
64 def makeSlug(self, rel_dirpath, filename): |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
65 raise NotImplementedError() |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
66 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
67 def extractConfigFragment(self, rel_path): |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
68 raise NotImplementedError() |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
69 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
70 def findPagePath(self, metadata, mode): |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
71 raise NotImplementedError() |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
72 |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
73 |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
74 class AutoConfigSource(AutoConfigSourceBase): |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
75 """ 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
|
76 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
|
77 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
|
78 """ |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
79 SOURCE_NAME = 'autoconfig' |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
80 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
81 def __init__(self, app, name, config): |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
82 config['capture_mode'] = 'dirname' |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
83 super(AutoConfigSource, self).__init__(app, name, config) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
84 self.setting_name = config.get('setting_name', name) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
85 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
|
86 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
|
87 False) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
88 self.supported_extensions = list( |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
89 app.config.get('site/auto_formats').keys()) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
90 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
91 def makeSlug(self, rel_dirpath, filename): |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
92 slug, ext = os.path.splitext(filename) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
93 if ext.lstrip('.') not in self.supported_extensions: |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
94 slug += ext |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
95 return slug |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
96 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
97 def extractConfigFragment(self, rel_path): |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
98 if rel_path == '.': |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
99 values = [] |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
100 else: |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
101 values = rel_path.split(os.sep) |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
102 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
103 if self.only_single_values: |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
104 if len(values) > 1: |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
105 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
|
106 "in source '%s'." % self.name) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
107 elif len(values) == 1: |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
108 values = values[0] |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
109 else: |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
110 values = None |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
111 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
112 if self.collapse_single_values: |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
113 if len(values) == 1: |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
114 values = values[0] |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
115 elif len(values) == 0: |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
116 values = None |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
117 |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
118 return {self.setting_name: values} |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
119 |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
120 def findPagePath(self, metadata, mode): |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
121 # 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
|
122 # find pages using a brute-force kinda way. |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
123 for dirpath, dirnames, filenames in os.walk(self.fs_endpoint_path): |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
124 for f in filenames: |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
125 slug, _ = os.path.splitext(f) |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
126 if slug == metadata['slug']: |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
127 path = os.path.join(dirpath, f) |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
128 rel_path = os.path.relpath(path, self.fs_endpoint_path) |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
129 config = self.extractConfigFragment(dirpath) |
148
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
130 metadata = {'slug': slug, 'config': config} |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
131 return rel_path, metadata |
432cd534ce08
Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
132 |
239
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
133 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
134 class OrderedPageSource(AutoConfigSourceBase): |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
135 """ 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
|
136 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
|
137 sort pages using that order. |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
138 """ |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
139 SOURCE_NAME = 'ordered' |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
140 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
141 re_pattern = re.compile(r'(^|/)(?P<num>\d+)_') |
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 def __init__(self, app, name, config): |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
144 config['capture_mode'] = 'filename' |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
145 super(OrderedPageSource, self).__init__(app, name, config) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
146 self.setting_name = config.get('setting_name', 'order') |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
147 self.default_value = config.get('default_value', 0) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
148 self.supported_extensions = list( |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
149 app.config.get('site/auto_formats').keys()) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
150 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
151 def makeSlug(self, rel_dirpath, filename): |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
152 slug, ext = os.path.splitext(filename) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
153 if ext.lstrip('.') not in self.supported_extensions: |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
154 slug += ext |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
155 slug = self.re_pattern.sub(r'\1', slug) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
156 slug = os.path.join(rel_dirpath, slug).replace('\\', '/') |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
157 if slug.startswith('./'): |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
158 slug = slug[2:] |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
159 return slug |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
160 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
161 def extractConfigFragment(self, rel_path): |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
162 m = self.re_pattern.match(rel_path) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
163 if m is not None: |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
164 val = int(m.group('num')) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
165 else: |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
166 val = self.default_value |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
167 return {self.setting_name: val} |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
168 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
169 def findPagePath(self, metadata, mode): |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
170 uri_path = metadata.get('slug', '') |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
171 if uri_path != '': |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
172 uri_parts = ['*_%s' % p for p in uri_path.split('/')] |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
173 else: |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
174 uri_parts = ['*__index'] |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
175 uri_parts.insert(0, self.fs_endpoint_path) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
176 path = os.path.join(*uri_parts) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
177 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
178 _, ext = os.path.splitext(uri_path) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
179 if ext == '': |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
180 path += '.*' |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
181 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
182 possibles = glob.glob(path) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
183 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
184 if len(possibles) == 0: |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
185 return None, None |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
186 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
187 if len(possibles) > 1: |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
188 raise Exception("More than one path matching: %s" % uri_path) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
189 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
190 path = possibles[0] |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
191 fac_path = os.path.relpath(path, self.fs_endpoint_path) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
192 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
193 _, filename = os.path.split(path) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
194 config = self.extractConfigFragment(filename) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
195 metadata = {'slug': uri_path, 'config': config} |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
196 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
197 return fac_path, metadata |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
198 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
199 def getSorterIterator(self, it): |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
200 accessor = self.getSettingAccessor() |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
201 return SettingSortIterator(it, self.setting_name, |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
202 value_accessor=accessor) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
203 |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
204 def _populateMetadata(self, rel_path, metadata, mode=None): |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
205 _, filename = os.path.split(rel_path) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
206 config = self.extractConfigFragment(filename) |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
207 metadata['config'] = config |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
208 slug = metadata['slug'] |
f43f19975671
sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents:
156
diff
changeset
|
209 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
|
210 |