/
__init__.py
320 lines (276 loc) · 10.5 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# encoding: utf8
from collections import defaultdict
from datetime import date, datetime
from importlib.machinery import SourceFileLoader
from operator import itemgetter
import os
from shutil import copyfile, rmtree
import sys
import warnings
from jinja2 import Environment, FileSystemLoader
from sectile import Sectile
import toml
from .lib import relative_list_of_files_in_directory
from .sectileloader import SectileLoader
from .source import (
JsonSourceFile,
MarkdownSourceFile,
SourceFile,
TomlSourceFile,
)
from .sourcelist import SourceList
from .version import __version__ # noqa: F401
sys.dont_write_bytecode = True
class Flourish(object):
def __init__(
self,
source_dir='source',
templates_dir='templates',
output_dir='output',
sass_dir='sass',
fragments_dir=None,
):
self.source_dir = source_dir
self.templates_dir = templates_dir
self.fragments_dir = fragments_dir
self.output_dir = output_dir
self.sass_dir = sass_dir
self._assets = {}
self._cache = {}
self._source_files = []
self._source_path = None
self._paths = {}
# using sectile fragments overrides standard filesystem templates
if self.fragments_dir:
self.using_sectile = True
self.jinja = Environment(
loader=SectileLoader(self.fragments_dir),
keep_trailing_newline=True,
)
else:
self.using_sectile = False
self.jinja = Environment(
loader=FileSystemLoader(self.templates_dir),
keep_trailing_newline=True,
)
self.jinja.globals['path'] = self.template_resolve_path
self.jinja.globals['lookup'] = self.template_get
if not os.path.isdir(self.source_dir):
raise AttributeError(
'source_dir "%s" must exist' % self.source_dir)
self.site_config = self._read_site_config()
self._rescan_sources()
generate = SourceFileLoader(
'generate', '%s/generate.py' % self.source_dir
).load_module()
try:
self.set_global_context(getattr(generate, 'GLOBAL_CONTEXT'))
except AttributeError:
# global context is optional
pass
try:
self.add_template_filters(getattr(generate, 'TEMPLATE_FILTERS'))
except AttributeError:
# filters are optional
pass
try:
for path in generate.PATHS:
self.add_path(path)
except NameError:
raise AttributeError(
'There are no paths configured in generate.py')
@property
def sources(self):
return SourceList(self._source_files)
def get(self, slug):
""" Get a single source document by slug. """
try:
return self._cache[slug]
except KeyError:
raise SourceFile.DoesNotExist
def template_get(self, slug):
try:
return self.get(slug)
except SourceFile.DoesNotExist:
warnings.warn('Cannot get "%s" as it does not exist' % slug)
return None
def add_path(self, path):
path.setup(self)
if path.name == 'source':
self._source_path = path
self._paths.update({path.name: path})
def resolve_path(self, name, **kwargs):
return self._paths[name].resolve(**kwargs)
def template_resolve_path(self, name, **kwargs):
try:
return self.resolve_path(name, **kwargs)
except KeyError:
warnings.warn(
'Cannot resolve path "%s", it has missing arguments' % name
)
return ''
def get_handler_for_path(self, path):
matches = []
for key in self._paths:
_dicts = self._paths[key].can_generate(path)
if _dicts:
for _dict in _dicts:
matches.append((key, _dict))
return matches
def all_valid_filters_for_path(self, name):
if name in self._paths:
return self._paths[name].all_valid_filters()
else:
raise SourceFile.DoesNotExist
def generate_site(self, report=False):
if os.path.exists(self.output_dir):
rmtree(self.output_dir)
os.makedirs(self.output_dir)
self.generate_all_paths(report=report)
self.copy_assets(report=report)
if report:
print('')
def generate_all_paths(self, report=False):
for path in self._paths:
self._paths[path].generate(report)
def generate_path(self, path, report=False):
handlers = self.get_handler_for_path(path)
for key, args in handlers:
path = self._paths[key]
path.generate(report, tokens=[args])
def path_recipe(self, path):
handlers = self.get_handler_for_path(path)
for key, args in handlers:
path = self._paths[key]
recipe = path.get_recipe(tokens=[args])
if recipe:
return recipe
return None
def set_global_context(self, global_context):
self.global_context = global_context
def add_template_filters(self, filters):
for key, value in filters.items():
self.jinja.filters[key] = value
def copy_assets(self, report=False):
for _file in self._assets:
_source = '%s/%s' % (self.source_dir, _file)
_destination = '%s/%s' % (self.output_dir, _file)
_directory = os.path.dirname(_destination)
if not os.path.isdir(_directory):
os.makedirs(_directory)
copyfile(_source, _destination)
if report:
print('->', _destination)
def _read_site_config(self):
_config_file = '%s/_site.toml' % self.source_dir
with open(_config_file) as _file:
_config = toml.loads(_file.read())
for key in ('author', 'title', 'base_url'):
if key not in _config:
raise RuntimeError(
'"%s" is a required entry in _site.toml' % key)
return _config
def _rescan_sources(self):
""" Find source documents and register them. """
_seen = {}
for _file in relative_list_of_files_in_directory(self.source_dir):
if _file == '_site.toml':
continue
if _file.startswith('generate.py'):
continue
slug, _ = os.path.splitext(_file)
timestamp = os.stat(os.path.join(self.source_dir, _file)).st_mtime
try:
source = self.get(slug)
assert timestamp == source.timestamp
_seen[slug] = 1
except: # noqa: E722
# FIXME check for slug-ishness and otherwise ignore
# (this could simplify _site.toml by being just another
# ignored filename?)
is_attachment_file = (
_file.endswith(('.markdown', '.html')) and
len(_file.split('.')) == 3
)
if _file.endswith('.toml'):
self._cache[slug] = TomlSourceFile(self, _file)
elif (
_file.endswith('.markdown')
and len(_file.split('.')) == 2
):
self._cache[slug] = MarkdownSourceFile(self, _file)
elif _file.endswith('.json'):
self._cache[slug] = JsonSourceFile(self, _file)
elif not is_attachment_file:
self._assets[_file] = True
_seen[slug] = 1
# remove anything no longer there
# FIXME will also need output removing
_removed = {}
for source in self._cache:
if source not in _seen:
_removed[source] = 1
for source in _removed:
del self._cache[source]
self._source_files = self._cache.values()
@property
def publication_dates(self):
def recursively_default_dict():
return defaultdict(recursively_default_dict)
_captured = recursively_default_dict()
for _source in self.sources.all():
try:
_date = getattr(_source, 'published')
_captured[_date.year][_date.month][_date.day] = 1
except AttributeError:
pass
_dates = []
for _year in sorted(_captured):
_months = []
for _month in sorted(_captured[_year]):
_days = []
for _day in sorted(_captured[_year][_month]):
_days.append(date(_year, _month, _day))
_months.append({
'month': date(_year, _month, 1),
'days': _days
})
_dates.append({'year': date(_year, 1, 1), 'months': _months})
return _dates
def get_valid_filters_for_tokens(self, tokens, objects=None):
if objects is None:
objects = self.sources
_first_token = tokens[0]
_values = set()
for _source in objects:
_is_date_filter = (
_first_token in ['year', 'month', 'day'] and
'published' in _source and
type(_source['published'] == datetime)
)
if _is_date_filter:
_value = getattr(_source['published'], _first_token)
_values.add('%02d' % _value)
elif _first_token in _source:
if type(_source[_first_token]) == list:
for _value in _source[_first_token]:
_values.add(_value)
else:
_values.add(_source[_first_token])
_filters = []
for _value in _values:
_dict = {_first_token: _value}
if len(tokens) == 1:
_filters.append(_dict)
else:
_sub_objects = objects.filter(**_dict)
_sub_tokens = self.get_valid_filters_for_tokens(
tokens[1:], _sub_objects)
for _sub_token in _sub_tokens:
_update = _dict.copy()
_update.update(_sub_token)
_filters.append(_update)
# the sort is only so the tests can compare results easily
return sorted(_filters, key=itemgetter(_first_token))
def __repr__(self):
return '<flourish.Flourish object (source=%s)>' % self.source_dir