annotate piecrust/sources/autoconfig.py @ 893:14cca285f73b

clean: PEP8.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 02 Jul 2017 22:20:48 -0700
parents f070a4fc033c
children 45ad976712ec
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
6 from piecrust.sources.base import ContentItem
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
7 from piecrust.sources.default import DefaultContentSource
148
432cd534ce08 Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8
432cd534ce08 Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9
432cd534ce08 Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 logger = logging.getLogger(__name__)
432cd534ce08 Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11
432cd534ce08 Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
13 class AutoConfigContentSourceBase(DefaultContentSource):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
14 """ Base class for content sources that automatically apply configuration
239
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
15 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
16 """
148
432cd534ce08 Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 def __init__(self, app, name, config):
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
18 super().__init__(app, name, config)
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
19
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
20 config.setdefault('data_type', 'page_iterator')
242
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
21
239
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
22 self.capture_mode = config.get('capture_mode', 'path')
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
23 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
24 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
25 "one of: path, dirname, filename" %
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
26 name)
148
432cd534ce08 Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
28 def _finalizeContent(self, parent_group, items, groups):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
29 DefaultContentSource._finalizeContent(parent_group, items, groups)
239
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
30
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
31 # If `capture_mode` is `dirname`, we don't need to recompute it
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
32 # for each filename, so we do it here.
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
33 if self.capture_mode == 'dirname':
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
34 rel_dirpath = os.path.relpath(parent_group.spec,
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
35 self.fs_endpoint_path)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
36 config = self._extractConfigFragment(rel_dirpath)
239
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
37
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
38 for i in items:
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
39 # Compute the confif for the other capture modes.
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
40 if self.capture_mode == 'path':
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
41 rel_path = os.path.relpath(i.spec, self.fs_endpoint_path)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
42 config = self._extractConfigFragment(rel_path)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
43 elif self.capture_mode == 'filename':
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
44 fname = os.path.basename(i.spec)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
45 config = self._extractConfigFragment(fname)
239
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
46
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
47 # Set the config on the content item's metadata.
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
48 i.metadata.setdefault('config', {}).update(config)
239
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
49
242
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
50 def _extractConfigFragment(self, rel_path):
239
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
51 raise NotImplementedError()
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
52
148
432cd534ce08 Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
54 class AutoConfigContentSource(AutoConfigContentSourceBase):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
55 """ Content source that extracts configuration settings from the sub-folders
239
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
56 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
57 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
58 """
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
59 SOURCE_NAME = 'autoconfig'
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
60
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
61 def __init__(self, app, name, config):
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
62 config['capture_mode'] = 'dirname'
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
63 AutoConfigContentSourceBase.__init__(app, name, config)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
64
239
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
65 self.setting_name = config.get('setting_name', name)
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
66 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
67 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
68 False)
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
69
242
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
70 def _extractConfigFragment(self, rel_path):
148
432cd534ce08 Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71 if rel_path == '.':
432cd534ce08 Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72 values = []
432cd534ce08 Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 else:
432cd534ce08 Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74 values = rel_path.split(os.sep)
239
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
75
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
76 if self.only_single_values:
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
77 if len(values) > 1:
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
78 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
79 "in source '%s'." % self.name)
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
80 elif len(values) == 1:
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
81 values = values[0]
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
82 else:
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
83 values = None
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
84
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
85 if self.collapse_single_values:
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
86 if len(values) == 1:
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
87 values = values[0]
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
88 elif len(values) == 0:
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
89 values = None
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
90
148
432cd534ce08 Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 return {self.setting_name: values}
432cd534ce08 Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
93 def findContent(self, route_params):
239
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
94 # 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
95 # find pages using a brute-force kinda way.
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
96 route_slug = route_params.get('slug', '')
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
97 if not route_slug:
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
98 route_slug = '_index'
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
99
148
432cd534ce08 Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100 for dirpath, dirnames, filenames in os.walk(self.fs_endpoint_path):
432cd534ce08 Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101 for f in filenames:
432cd534ce08 Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
102 slug, _ = os.path.splitext(f)
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
103 if slug == route_slug:
148
432cd534ce08 Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 path = os.path.join(dirpath, f)
432cd534ce08 Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105 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
106 config = self._extractConfigFragment(rel_path)
148
432cd534ce08 Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
107 metadata = {'slug': slug, 'config': config}
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
108 return ContentItem(path, metadata)
363
dd25bd3ce1f9 serve: Refactoring and fixes to be able to serve taxonomy pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 360
diff changeset
109 return None
148
432cd534ce08 Add `autoconfig` page source.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
110
242
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
111
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
112 class OrderedContentSource(AutoConfigContentSourceBase):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
113 """ A content source that assigns an "order" to its pages based on a
239
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
114 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
115 sort pages using that order.
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
116 """
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
117 SOURCE_NAME = 'ordered'
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
118
286
a2d283d1033d tests: Fixes for running on Windows.
Ludovic Chabant <ludovic@chabant.com>
parents: 267
diff changeset
119 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
120
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
121 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
122 config['capture_mode'] = 'path'
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
123 AutoConfigContentSourceBase.__init__(app, name, config)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
124
239
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
125 self.setting_name = config.get('setting_name', 'order')
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
126 self.default_value = config.get('default_value', 0)
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
127
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
128 def findContent(self, route_params):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
129 uri_path = route_params.get('slug', '')
242
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
130 if uri_path == '':
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
131 uri_path = '_index'
239
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
132
242
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
133 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
134 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
135 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
136 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
137 # 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
138 # 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
139 # possible extension.
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
140 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
141
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
142 _, 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
143 if ext == '':
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
144 p_pat += r'\.[\w\d]+'
239
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
145
242
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
146 found = False
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
147 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
148 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
149 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
150 found = True
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
151 break
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
152 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
153 return None
242
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
154 else:
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
155 # 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
156 # 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
157 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
158 found = False
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
159 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
160 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
161 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
162 found = True
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
163 break
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
164 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
165 return None
239
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
166
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
167 rel_path = os.path.relpath(path, self.fs_endpoint_path)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
168 config = self._extractConfigFragment(rel_path)
239
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
169 metadata = {'slug': uri_path, 'config': config}
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
170 return ContentItem(path, metadata)
239
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
171
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
172 def getSorterIterator(self, it):
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
173 accessor = self.getSettingAccessor()
242
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
174 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
175 value_accessor=accessor)
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
176
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
177 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
178 values = []
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
179 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
180 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
181 values.append(val)
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
182
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
183 if len(values) == 0:
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
184 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
185
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
186 return {
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
187 self.setting_name: values[-1],
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
188 self.setting_name + '_trail': values}
239
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
189
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
190 def _makeSlug(self, path):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
191 slug = super()._makeSlug(path)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
192 return self.re_pattern.sub(r'\1', slug)
239
f43f19975671 sources: Refactor `autoconfig` source, add `OrderedPageSource`.
Ludovic Chabant <ludovic@chabant.com>
parents: 156
diff changeset
193
242
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
194
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
195 class OrderTrailSortIterator(object):
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
196 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
197 self.it = it
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
198 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
199 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
200
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
201 def __iter__(self):
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
202 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
203
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
204 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
205 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
206 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
207 return key
f130365568ff internal: Code reorganization to put less stuff in `sources.base`.
Ludovic Chabant <ludovic@chabant.com>
parents: 239
diff changeset
208