Mercurial > piecrust2
annotate piecrust/sources/fs.py @ 1089:a4d7ff2cdc5c
sources: Update prose source to correctly use the new source API.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Fri, 16 Feb 2018 00:14:27 -0800 |
parents | a6618fdab37e |
children | 4ea64255eadf |
rev | line source |
---|---|
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 import os.path |
853
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
2 import re |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
3 import glob |
853
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
4 import fnmatch |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 import logging |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 from piecrust import osutil |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 from piecrust.routing import RouteParameter |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
8 from piecrust.sources.base import ( |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
9 ContentItem, ContentGroup, ContentSource, |
1078
a6618fdab37e
sources: Fix some invalid relationship name.
Ludovic Chabant <ludovic@chabant.com>
parents:
1060
diff
changeset
|
10 REL_PARENT_GROUP, REL_LOGICAL_PARENT_ITEM, REL_LOGICAL_CHILD_GROUP) |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 logger = logging.getLogger(__name__) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 class InvalidFileSystemEndpointError(Exception): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 def __init__(self, source_name, fs_endpoint): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
18 super(InvalidFileSystemEndpointError, self).__init__( |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 "Invalid file-system endpoint for source '%s': %s" % |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 (source_name, fs_endpoint)) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 def _filter_crap_files(f): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 return (f[-1] != '~' and # Vim temp files and what-not |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
25 f not in ['.DS_Store', 'Thumbs.db']) # OSX and Windows bullshit |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
27 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 class FSContentSourceBase(ContentSource): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
29 """ Implements some basic stuff for a `ContentSource` that stores its |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
30 items as files on disk. |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
31 """ |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
32 def __init__(self, app, name, config): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
33 super().__init__(app, name, config) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
34 self.fs_endpoint = config.get('fs_endpoint', name) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
35 self.fs_endpoint_path = os.path.join(self.root_dir, self.fs_endpoint) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
36 |
855
448710d84121
refactor: Get the taxonomy support back to a functional state.
Ludovic Chabant <ludovic@chabant.com>
parents:
854
diff
changeset
|
37 @property |
448710d84121
refactor: Get the taxonomy support back to a functional state.
Ludovic Chabant <ludovic@chabant.com>
parents:
854
diff
changeset
|
38 def root_dir(self): |
448710d84121
refactor: Get the taxonomy support back to a functional state.
Ludovic Chabant <ludovic@chabant.com>
parents:
854
diff
changeset
|
39 if self.is_theme_source: |
448710d84121
refactor: Get the taxonomy support back to a functional state.
Ludovic Chabant <ludovic@chabant.com>
parents:
854
diff
changeset
|
40 return self.app.theme_dir |
448710d84121
refactor: Get the taxonomy support back to a functional state.
Ludovic Chabant <ludovic@chabant.com>
parents:
854
diff
changeset
|
41 return self.app.root_dir |
448710d84121
refactor: Get the taxonomy support back to a functional state.
Ludovic Chabant <ludovic@chabant.com>
parents:
854
diff
changeset
|
42 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 def _checkFSEndpoint(self): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 if not os.path.isdir(self.fs_endpoint_path): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 if self.config.get('ignore_missing_dir'): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 return False |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 raise InvalidFileSystemEndpointError(self.name, |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 self.fs_endpoint_path) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 return True |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 |
933
7e7fc7926307
sources: File-system sources accept all `open` arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
51 def openItem(self, item, mode='r', **kwargs): |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
52 for m in 'wxa+': |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
53 if m in mode: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
54 # If opening the file for writing, let's make sure the |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
55 # directory exists. |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
56 dirname = os.path.dirname(item.spec) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
57 if not os.path.exists(dirname): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
58 os.makedirs(dirname, 0o755) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
59 break |
933
7e7fc7926307
sources: File-system sources accept all `open` arguments.
Ludovic Chabant <ludovic@chabant.com>
parents:
866
diff
changeset
|
60 return open(item.spec, mode, **kwargs) |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
61 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
62 def getItemMtime(self, item): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
63 return os.path.getmtime(item.spec) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
64 |
853
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
65 def describe(self): |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
66 return {'endpoint_path': self.fs_endpoint_path} |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
67 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
68 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
69 class FSContentSource(FSContentSourceBase): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
70 """ Implements a `ContentSource` that simply returns files on disk |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
71 under a given root directory. |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
72 """ |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
73 SOURCE_NAME = 'fs' |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
74 |
853
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
75 def __init__(self, app, name, config): |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
76 super().__init__(app, name, config) |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
77 |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
78 config.setdefault('data_type', 'asset_iterator') |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
79 |
1042
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
80 self._ignore = _parse_patterns(config.get('ignore')) |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
81 self._filter = _parse_patterns(config.get('filter')) |
853
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
82 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
83 def getContents(self, group): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
84 if not self._checkFSEndpoint(): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
85 return None |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
86 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
87 parent_path = self.fs_endpoint_path |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
88 if group is not None: |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
89 parent_path = group.spec |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
90 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
91 names = filter(_filter_crap_files, osutil.listdir(parent_path)) |
853
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
92 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
93 items = [] |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
94 groups = [] |
1042
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
95 for name in names: |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
96 path = os.path.join(parent_path, name) |
1042
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
97 if self._filterPath(path): |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
98 if os.path.isdir(path): |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
99 metadata = self._createGroupMetadata(path) |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
100 groups.append(ContentGroup(path, metadata)) |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
101 else: |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
102 metadata = self._createItemMetadata(path) |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
103 items.append(ContentItem(path, metadata)) |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
104 self._finalizeContent(group, items, groups) |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
105 return items + groups |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
106 |
1042
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
107 def _filterPath(self, path): |
853
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
108 rel_path = os.path.relpath(path, self.fs_endpoint_path) |
1042
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
109 |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
110 if self._ignore is not None: |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
111 if _matches_patterns(self._ignore, rel_path): |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
112 return False |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
113 |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
114 if self._filter is not None: |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
115 if _matches_patterns(self._filter, rel_path): |
853
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
116 return True |
1042
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
117 return False |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
118 |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
119 return True |
853
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
120 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
121 def _createGroupMetadata(self, path): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
122 return {} |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
123 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
124 def _createItemMetadata(self, path): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
125 return {} |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
126 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
127 def _finalizeContent(self, parent_group, items, groups): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
128 pass |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
129 |
989
8adc27285d93
bake: Big pass on bake performance.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
130 def findContentFromSpec(self, spec): |
8adc27285d93
bake: Big pass on bake performance.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
131 if os.path.isdir(spec): |
8adc27285d93
bake: Big pass on bake performance.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
132 metadata = self._createGroupMetadata(spec) |
8adc27285d93
bake: Big pass on bake performance.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
133 return ContentGroup(spec, metadata) |
8adc27285d93
bake: Big pass on bake performance.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
134 elif os.path.isfile(spec): |
8adc27285d93
bake: Big pass on bake performance.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
135 metadata = self._createItemMetadata(spec) |
8adc27285d93
bake: Big pass on bake performance.
Ludovic Chabant <ludovic@chabant.com>
parents:
979
diff
changeset
|
136 return ContentItem(spec, metadata) |
866
d9059257743c
refactor: Make the linker work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
862
diff
changeset
|
137 return None |
d9059257743c
refactor: Make the linker work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
862
diff
changeset
|
138 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
139 def getRelatedContents(self, item, relationship): |
977
84fc72a17f7a
sources: Changes in related contents management.
Ludovic Chabant <ludovic@chabant.com>
parents:
933
diff
changeset
|
140 if relationship == REL_PARENT_GROUP: |
84fc72a17f7a
sources: Changes in related contents management.
Ludovic Chabant <ludovic@chabant.com>
parents:
933
diff
changeset
|
141 parent_dir = os.path.dirname(item.spec) |
84fc72a17f7a
sources: Changes in related contents management.
Ludovic Chabant <ludovic@chabant.com>
parents:
933
diff
changeset
|
142 if len(parent_dir) >= len(self.fs_endpoint_path): |
84fc72a17f7a
sources: Changes in related contents management.
Ludovic Chabant <ludovic@chabant.com>
parents:
933
diff
changeset
|
143 metadata = self._createGroupMetadata(parent_dir) |
84fc72a17f7a
sources: Changes in related contents management.
Ludovic Chabant <ludovic@chabant.com>
parents:
933
diff
changeset
|
144 return ContentGroup(parent_dir, metadata) |
84fc72a17f7a
sources: Changes in related contents management.
Ludovic Chabant <ludovic@chabant.com>
parents:
933
diff
changeset
|
145 |
84fc72a17f7a
sources: Changes in related contents management.
Ludovic Chabant <ludovic@chabant.com>
parents:
933
diff
changeset
|
146 # Don't return a group for paths that are outside of our |
84fc72a17f7a
sources: Changes in related contents management.
Ludovic Chabant <ludovic@chabant.com>
parents:
933
diff
changeset
|
147 # endpoint directory. |
84fc72a17f7a
sources: Changes in related contents management.
Ludovic Chabant <ludovic@chabant.com>
parents:
933
diff
changeset
|
148 return None |
84fc72a17f7a
sources: Changes in related contents management.
Ludovic Chabant <ludovic@chabant.com>
parents:
933
diff
changeset
|
149 |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
150 if relationship == REL_LOGICAL_PARENT_ITEM: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
151 # If we want the logical parent item of a folder, we find a |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
152 # page file with the same name as the folder. |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
153 if not item.is_group: |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
154 raise ValueError() |
979
45ad976712ec
tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents:
977
diff
changeset
|
155 parent_glob = item.spec.rstrip('/\\') + '.*' |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
156 for n in glob.iglob(parent_glob): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
157 if os.path.isfile(n): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
158 metadata = self._createItemMetadata(n) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
159 return ContentItem(n, metadata) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
160 return None |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
161 |
1078
a6618fdab37e
sources: Fix some invalid relationship name.
Ludovic Chabant <ludovic@chabant.com>
parents:
1060
diff
changeset
|
162 if relationship == REL_LOGICAL_CHILD_GROUP: |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
163 # If we want the children items of an item, we look for |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
164 # a directory that has the same name as the item's file. |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
165 if item.is_group: |
1060
3678cddf99f9
sources: Improve error message when trying to get children pages on a group.
Ludovic Chabant <ludovic@chabant.com>
parents:
1042
diff
changeset
|
166 raise ValueError( |
3678cddf99f9
sources: Improve error message when trying to get children pages on a group.
Ludovic Chabant <ludovic@chabant.com>
parents:
1042
diff
changeset
|
167 "'%s' is a content group and doesn't have a logical " |
3678cddf99f9
sources: Improve error message when trying to get children pages on a group.
Ludovic Chabant <ludovic@chabant.com>
parents:
1042
diff
changeset
|
168 "child. Did you call `family.children` on a group? " |
3678cddf99f9
sources: Improve error message when trying to get children pages on a group.
Ludovic Chabant <ludovic@chabant.com>
parents:
1042
diff
changeset
|
169 "You need to check `is_group` first.") |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
170 dir_path, _ = os.path.splitext(item.spec) |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
171 if os.path.isdir(dir_path): |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
172 metadata = self._createGroupMetadata(dir_path) |
977
84fc72a17f7a
sources: Changes in related contents management.
Ludovic Chabant <ludovic@chabant.com>
parents:
933
diff
changeset
|
173 return ContentGroup(dir_path, metadata) |
854
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
174 return None |
08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
853
diff
changeset
|
175 |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
176 return None |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
177 |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
178 def getSupportedRouteParameters(self): |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
179 return [ |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
180 RouteParameter('path', RouteParameter.TYPE_PATH)] |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
181 |
853
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
182 |
1042
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
183 def _parse_patterns(patterns): |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
184 if not patterns: |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
185 return None |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
186 |
853
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
187 globs = [] |
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
188 regexes = [] |
1042
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
189 for pat in patterns: |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
190 if len(pat) > 2 and pat[0] == '/' and pat[-1] == '/': |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
191 regexes.append(re.compile(pat[1:-1])) |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
192 else: |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
193 globs.append(pat) |
853
f070a4fc033c
core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
194 return globs, regexes |
1042
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
195 |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
196 |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
197 def _matches_patterns(patterns, subj): |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
198 globs, regexes = patterns |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
199 if globs: |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
200 for g in globs: |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
201 if fnmatch.fnmatch(subj, g): |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
202 return True |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
203 if regexes: |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
204 for r in regexes: |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
205 if r.search(subj): |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
206 return True |
895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
Ludovic Chabant <ludovic@chabant.com>
parents:
989
diff
changeset
|
207 return False |