-
Notifications
You must be signed in to change notification settings - Fork 1
/
halwa.py
752 lines (571 loc) · 25.2 KB
/
halwa.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
import json, calendar, shutil, sys, os, os.path, glob, shelve, time, pprint, imp, re
from markdown import markdown
from collections import OrderedDict, Counter, namedtuple, MutableMapping
from jinja2 import Environment, FileSystemLoader
if sys.version_info[0] == 2:
reload(sys)
sys.setdefaultencoding('utf8')
class Content(object):
def __init__(self, app, path):
self.app = app
self.path = path
def load(self):
pass
def update(self, data):
pass
def render(self):
pass
class StaticContent(Content):
def __init__(self, app, path, mappings=None, dependencies=None):
super(StaticContent, self).__init__(app, path)
status, val = self.app.cache.get_file(path)
if status != 'Cached':
self.app.cache.put_file(path, '')
def load(self):
return 'Cached'
def render(self):
path = self.app.get_output_path(self.path, static=True)
status = self.app.cache.need_update(path, [self.path])
if status != 'Ignore':
shutil.copy(self.path, path)
return [(status, path)]
class DynamicContent(Content):
def __init__(self, app, path, mappings=None, dependencies=None):
super(DynamicContent, self).__init__(app, path)
self.mappings = {}
if mappings is not None:
self.mappings = mappings
self.dependencies = dependencies
def load(self):
self.metadata = None
self.content = None
self.data = {}
self.content_line = 0
status, val = self.app.cache.get_file(self.path)
if status == 'Cached':
self.metadata = val['metadata']
self.content_line = val['content_line']
else:
stream = []
with open(self.app.get_path(self.path)) as f:
stream = f.readlines()
idx = 0
lis = [i for (i,x) in enumerate(stream) if x == '}\n']
if len(lis) > 0:
idx = lis[0]
self.content_line = idx+2
metadata = ''.join(stream[:idx+1])
self.metadata = json.loads(metadata, object_pairs_hook=OrderedDict)
self.app.cache.put_file(self.path, {'metadata': self.metadata, 'content_line': self.content_line})
self.set_url()
return status
def load_content(self):
stream = []
with open(self.app.get_path(self.path)) as f:
stream = f.readlines()[self.content_line:]
self.content = ''.join(stream)
def set_url(self):
try:
self.metadata['url'] = self.app.url_for(**self.metadata)
except Exception:
pass
def update(self, data):
for (k,v) in data.items():
key = self.mappings.get(k, k)
self.data[key] = v
self.data['url_for'] = self.app.url_for
self.data['len'] = len
self.data.update(self.metadata)
def render(self):
path = self.app.get_output_path(self.app.url_for(**self.data))
deps = [self.path]
if self.dependencies is not None:
deps.extend(self.dependencies)
status = self.app.cache.need_update(path, deps)
if status != 'Ignore':
if self.content is None:
self.load_content()
template = self._render()
with open(path, 'w') as f:
f.write(template.render(**self.data))
return [(status, path)]
class Page(DynamicContent):
def __init__(self, app, path, mappings=None, dependencies=None):
super(Page, self).__init__(app, path, mappings, dependencies)
def load(self):
return super(Page, self).load()
def _render(self):
return self.app.jinja_env.hamlish_from_string(self.content)
class MultiDynamicContent(DynamicContent):
def __init__(self, app, path, mappings=None, dependencies=None):
super(MultiDynamicContent, self).__init__(app, path, mappings, dependencies)
self.template = None
def load(self):
return super(MultiDynamicContent,self).load()
def render(self):
return super(MultiDynamicContent, self).render()
def _render(self):
return NotImplementedError()
def need_update(self):
updated = False
for dep in (self.dependencies + [self.path]):
if dep in self.app.cache.updated_content:
updated = True
return updated
class TagPage(MultiDynamicContent):
def __init__(self, app, path, mappings=None, dependencies=None):
super(TagPage, self).__init__(app, path, mappings, dependencies)
def load(self):
return super(TagPage,self).load()
def _render(self):
if self.template is None:
self.template = self.app.jinja_env.hamlish_from_string(self.content)
return self.template
def render(self):
if not self.need_update():
return []
rets = []
for tag in self.data['tags']:
self.data['tag'] = tag['tag']
self.data['tagname'] = tag['tag']
self.data['tagposts'] = tag['posts']
rets.extend(super(TagPage, self).render())
return rets
class ReadersCornerPage(MultiDynamicContent):
def __init__(self, app, path, mappings=None, dependencies=None):
super(ReadersCornerPage, self).__init__(app, path, mappings, dependencies)
def load(self):
return super(ReadersCornerPage,self).load()
def _render(self):
if self.template is None:
self.template = self.app.jinja_env.hamlish_from_string(self.content)
return self.template
def render(self):
if not self.need_update():
return []
rets = []
archives = self.data['readerscorner']
for (year, yeararchive) in archives.items():
for (month, montharchive) in yeararchive.items():
self.data['year'] = year
self.data['month'] = month
self.data['montharchive'] = montharchive
self.data['monthname'] = calendar.month_name[int(month)]
rets.extend(super(ReadersCornerPage, self).render())
return rets
class ReadersCornerJSONItem(MultiDynamicContent):
def __init__(self, app, path, mappings=None, dependencies=None):
super(ReadersCornerJSONItem, self).__init__(app, path, mappings, dependencies)
self.renderfn = namedtuple('Template', ['render'])
def load(self):
return super(ReadersCornerJSONItem,self).load()
def _render(self):
def __render(**kwargs):
return json.dumps(kwargs['entry']).replace('\\n', '<br>')
return self.renderfn(__render)
def render(self):
if not self.need_update():
return []
rets = []
archives = self.data['readerscorner']
for (year, yeararchive) in archives.items():
for (month, montharchive) in yeararchive.items():
for (day, dayarchive) in montharchive.items():
for item in dayarchive:
entry = {}
for key in ['link', 'name', 'caption', 'description', 'message']:
entry[key] = item.get(key, '')
self.data['entry'] = entry
self.data['id'] = item['id']
rets.extend(super(ReadersCornerJSONItem, self).render())
return rets
class BlogPost(DynamicContent):
def __init__(self, app, path, mappings=None, dependencies=None):
super(BlogPost, self).__init__(app, path, mappings, dependencies)
def load(self):
status = super(BlogPost, self).load()
year, month, day = self.metadata.get('date', '0000/00/00').split('/')
self.metadata['year'] = '%d' % int(year)
self.metadata['month'] = '%02d' % int(month)
self.metadata['day'] = '%02d' % int(day)
(head, tail) = os.path.split(self.path)
(root, ext) = os.path.splitext(tail)
self.metadata['slug'] = root
self.set_url()
return status
def update(self, data):
super(BlogPost, self).update(data)
def _render(self):
content = markdown(self.content)
btemplate = ''
with open(self.app.get_path(self.data['template'], 'templates')) as f:
btemplate = f.read()
self.data['content'] = self.app.jinja_env.from_string(content).render()
return self.app.jinja_env.hamlish_from_string(btemplate)
class Processor(object):
def __init__(self, app):
self.app = app
def process(self, content):
pass
class TagList(Processor):
def __init__(self, app, key='tags', datakey='tags', sortkey='date', route='tag', reverse=True):
super(TagList, self).__init__(app)
self.key = key
self.datakey = datakey
self.sortkey = sortkey
self.route = route
self.reverse = reverse
def process(self, content, data):
lis = []
posts = [c for c in content if type(c) == BlogPost]
for post in posts:
lis.extend(post.metadata.get(self.key, []))
tags = []
for (tag, count) in sorted(Counter(lis).items(), reverse=self.reverse, key=lambda k_v: (k_v[1], k_v[0])):
tagged = [p.metadata for p in posts if tag in p.metadata[self.datakey]]
tagged = sorted(tagged, reverse=self.reverse, key=lambda p: p[self.sortkey])
tags.append(OrderedDict([('tag',tag), ('count',count), ('posts', tagged), ('url', self.app.url_for(self.route, tag=tag))]))
data[self.key] = tags
return data
class BlogSidebar(Processor):
def __init__(self, app, routes='blogsidebarroutes', routekey='links', tags='tags', key='blogsidebar'):
super(BlogSidebar, self).__init__(app)
self.routes = routes
self.tags = tags
self.key = key
self.routekey = routekey
def process(self, content, data):
sidebar = {
self.routekey : OrderedDict((k,self.app.url_for(v)) for (k,v) in data[self.routes].items()),
self.tags : OrderedDict(('%s x %s' % (t['tag'], t['count']), t['url']) for t in data[self.tags])
}
data[self.key] = sidebar
return data
class PostList(Processor):
def __init__(self, app, count=5, key='posts', sortkey='date', uniquekey='slug', reverse=True, filters=None, exclude=''):
super(PostList, self).__init__(app)
self.count = count
self.key = key
self.sortkey = sortkey
self.uniquekey = uniquekey
self.reverse = reverse
self.filters = filters
self.exclude = exclude
def process(self, content, data):
posts = [c.metadata for c in content if type(c) == BlogPost]
if self.filters is not None:
for filter in self.filters:
posts = [p for p in posts if filter(p)]
ignore = [p[self.uniquekey] for p in data.get(self.exclude, {})]
posts = [p for p in posts if p[self.uniquekey] not in ignore]
recent = []
for post in sorted(posts, reverse=self.reverse, key=lambda p: p[self.sortkey])[:self.count]:
recent.append(post)
data[self.key] = recent
return data
class PostArchives(Processor):
def __init__(self, app, key='blogarchives', reverse=True):
super(PostArchives, self).__init__(app)
self.key = key
self.reverse = True
def process(self, content, data):
posts = [c for c in content if type(c) == BlogPost]
years = sorted(k for k in set(p.metadata['year'] for p in posts))
archives = OrderedDict()
for year in years:
yeararchive = OrderedDict()
yearposts = [p for p in posts if p.metadata['year'] == year]
months = sorted(k for k in set(p.metadata['month'] for p in yearposts))
for month in months:
monthposts = [p for p in yearposts if p.metadata['month'] == month]
monthposts = sorted(monthposts, reverse=self.reverse, key=lambda p: p.metadata['day'])
yeararchive[calendar.month_name[int(month)]] = [p.metadata for p in monthposts]
archives[year] = yeararchive
data[self.key] = archives
return data
class ReadersCorner(Processor):
def __init__(self, app, filename, filter=None, key='readerscorner', sidebarkey='readerscornersidebar', indexkey='readerscornerindex', route='readerscornerpge', homeroute='readerscornerhome', searchroute='readerscornersearch', stopwords=set(), reverse=True):
super(ReadersCorner, self).__init__(app)
self.filename = filename
self.filter = filter
self.key = key
self.indexkey = indexkey
self.sidebarkey = sidebarkey
self.route = route
self.homeroute = homeroute
self.searchroute = searchroute
self.stopwords = stopwords
self.reverse = True
def process(self, content, data):
status, val = self.app.cache.get_file(self.filename)
if status == 'Cached':
data[self.key] = val[self.key]
data[self.sidebarkey] = val[self.sidebarkey]
data[self.indexkey] = val[self.indexkey]
return data
source = []
with open(self.filename) as input:
source = json.load(input)
words = {}
eid = 0
patternapos = re.compile("'")
patternspace = re.compile('_|-')
patternalnum = re.compile(r'\W+')
etable = {}
entries = []
for entry in source:
if self.filter is not None:
entry = self.filter(entry)
if entry is None:
continue
tm = time.strptime(entry['created_time'], '%Y-%m-%dT%H:%M:%S+0000')
entry['year'] = tm.tm_year
entry['month'] = tm.tm_mon
entry['day'] = tm.tm_mday
entry['timestamp'] = time.strftime('%H:%M:%S', tm)
if entry.get('description', '') == 'null':
entry['description'] = None
seq = (entry.get(k, '') for k in ['description', 'message', 'link'])
text = ' '.join(s for s in seq if s is not None)
text = patternapos.sub('', text)
text = patternspace.sub(' ', text)
text = patternalnum.sub(' ', text)
text = [w for w in text.lower().strip().split() if len(w) > 2]
text = [w for w in text if w not in self.stopwords]
entry['text'] = text
for w in text:
if w in words:
words[w].add(eid)
else:
words[w] = set([eid])
entry['id'] = eid
etable[eid] = entry
eid = eid + 1
entries.append(entry)
_index = OrderedDict()
for word, results in sorted(words.items(), key = lambda wr: wr[0]):
_index[word] = sorted([r for r in results], key = lambda r: etable[r]['created_time'], reverse = self.reverse)
index = json.dumps(_index)
years = sorted((k for k in set(e['year'] for e in entries)), reverse=self.reverse)
archives = OrderedDict()
sidebar = OrderedDict()
sidebar['Main'] = OrderedDict()
sidebar['Main']['Home'] = self.app.url_for(self.homeroute)
sidebar['Main']['Search'] = self.app.url_for(self.searchroute)
for year in years:
yeararchive = OrderedDict()
sidebar[year] = OrderedDict()
yearentries = [e for e in entries if e['year'] == year]
months = sorted((k for k in set(e['month'] for e in yearentries)), reverse=self.reverse)
for month in months:
montharchive = OrderedDict()
monthentries = [e for e in yearentries if e['month'] == month]
days = sorted((k for k in set(e['day'] for e in monthentries)), reverse=self.reverse)
for day in days:
dayentries = [e for e in monthentries if e['day'] == day]
montharchive[day] = sorted(dayentries, reverse=self.reverse, key=lambda e: e['timestamp'])
yeararchive[month] = montharchive
linkstring = '%s (%s)' % (calendar.month_name[int(month)], sum(len(v) for k,v in montharchive.items()))
sidebar[year][linkstring] = self.app.url_for(self.route, year=year, month=month)
archives[year] = yeararchive
data[self.key] = archives
data[self.sidebarkey] = sidebar
data[self.indexkey] = index
self.app.cache.put_file(self.filename, {self.key : archives, self.sidebarkey : sidebar, self.indexkey : index})
return data
class RSSFeed(Processor):
def __init__(self, app, count=5, key='blogrss', title='title', link='link', description='description', sortkey='date', reverse=True):
super(RSSFeed, self).__init__(app)
self.key = key
self.title = title
self.link = link
self.description = description
self.count = count
self.sortkey = sortkey
self.reverse = True
def process(self, content, data):
posts = [c for c in content if type(c) == BlogPost]
map = {'title' : 'title', 'description' : 'excerpt', 'link' : 'url'}
items = []
for p in sorted(posts, reverse=self.reverse, key=lambda p: p.metadata[self.sortkey])[:self.count]:
items.append(dict((k,p.metadata.get(map[k])) for k in map.keys()))
feed = dict(title=self.title, link=self.link, description=self.description, items=items)
data[self.key] = feed
return data
class Sitemap(Processor):
def __init__(self, app, root='', key='sitemap'):
super(Sitemap, self).__init__(app)
self.key = key
self.root = root
def process(self, content, data):
urls = []
for item in (c for c in content if (type(c) != TagPage) and (type(c) != StaticContent) and (type(c) != ReadersCornerPage) and (type(c) != ReadersCornerJSONItem)):
url = self.root + self.app.get_output_path(self.app.url_for(**item.metadata)).replace(self.app.directories['output'], '')
urls.append(url)
data[self.key] = urls
return data
class CacheDict(MutableMapping):
def __init__(self, path, *args, **kwargs):
self.store = dict()
self.path = path
if os.path.exists(path):
self.store.update(json.loads(open(path).read()))
self.update(dict(*args, **kwargs))
def __getitem__(self, key):
return self.store[key]
def __setitem__(self, key, value):
self.store[key] = value
def __delitem__(self, key):
del self.store[key]
def __iter__(self):
return iter(self.store)
def __len__(self):
return len(self.store)
def close(self):
with open(self.path, 'w') as output:
output.write(json.dumps(self.store))
class Cache(object):
def __init__(self, path):
self.store = CacheDict(path)
self.updated_content = []
self.mtimes = {}
def get_file(self, path):
val = None
status = 'Read'
key = path
if key not in self.store:
return (status, val)
mtime = os.path.getmtime(path)
val = self.store[key]
if mtime < val['mtime']:
status = 'Cached'
val = val['value']
if status == 'Read':
self.updated_content.append(path)
return (status, val)
def put_file(self, path, value):
key = path
val = {'mtime': int(time.time()), 'value': value}
self.store[key] = val
self.mtimes[path] = val['mtime']
def put_content(self, name, value):
key = name
val = {'mtime': int(time.time()), 'value': value}
v = self.store.get(key, {'value': None})
if v['value'] != value:
self.store[key] = val
self.updated_content.append(name)
self.mtimes[name] = val['mtime']
def need_update(self, path, dependencies=None):
if not os.path.exists(path):
return 'Create'
mtime = os.path.getmtime(path)
deps = []
if dependencies is not None:
deps.extend(dependencies)
mtimes = [0]
for dep in deps:
if dep in self.mtimes:
mtimes.append(self.mtimes[dep])
else:
mtimes.append(self.store.get(dep, {'mtime': 0})['mtime'])
self.mtimes[dep] = mtimes[-1]
if mtime < max(mtimes):
return 'Modified'
for dep in dependencies:
if dep in self.updated_content:
return 'Modified'
return 'Ignore'
def shutdown(self):
self.store.close()
class Engine(object):
def __init__(self, directories, routes, sources, processors, data, verbose=False):
self.directories = directories
self.routes = routes
self.sources = sources
self.processors = processors
self.data = data
self.verbose = verbose
self.content = []
self.cache = Cache('cache')
self.jinja_env = Environment(extensions=['jinja2.ext.autoescape', 'jinja2.ext.with_', 'hamlish_jinja.HamlishExtension'])
self.jinja_env.loader = FileSystemLoader(self.directories['templates'])
self.jinja_env.hamlish_enable_div_shortcut = True
self.jinja_env.hamlish_mode = 'debug'
self.jinja_env.hamlish_file_extensions=('.haml','.xml')
for path in glob.glob(self.directories['templates'] + os.sep + '*'):
status, val = self.cache.get_file(path)
if status != 'Cached':
self.cache.put_file(path, '')
def get_path(self, path, resource=None):
if resource is not None:
return os.path.join(self.directories[resource], path)
return path
def get_output_path(self, path, static=False):
(root, ext) = os.path.splitext(path)
root = (self.directories['output'] + os.sep + root).replace('%s%s' % (os.sep, os.sep), os.sep)
if ext == '':
ext = os.sep + 'index.html'
if os.path.basename(path) != '':
root, tmp = os.path.split(root)
ext = tmp + ext
root = root + os.sep
if not os.path.exists(root):
os.makedirs(root)
return (root + ext).replace('%s%s' % (os.sep, os.sep), os.sep)
def url_for(self, route, **kwargs):
return self.routes[route].format(**kwargs)
def load_content(self):
print('Loading content ...')
count = 0
cached = 0
start = time.time()
for type, expr, mappings, dependencies in self.sources:
cons = getattr(sys.modules[__name__], type)
for path in [p for p in glob.glob(expr) if not os.path.isdir(p)]:
item = cons(self, path, mappings, dependencies)
status = item.load()
if status != 'Cached' or self.verbose:
print('[%s] %s' % (status, path))
else:
cached += 1
self.content.append(item)
count += 1
print('Loaded {} items ({} cached) in {:.2f}s.'.format(count, cached, time.time()-start))
def process_content(self):
print('Processing content ...')
start = time.time()
count = 0
for (type, kwargs) in self.processors:
if self.verbose:
print('Processing %s' % type)
processor = getattr(sys.modules[__name__], type)(self, **kwargs)
self.data.update(processor.process(self.content, self.data))
count += 1
for key,val in self.data.items():
self.cache.put_content(key, val)
print('Ran {} processors in {:.2f}s.'.format(count, time.time()-start))
def generate_output(self):
print('Generating output ...')
count = 0
cached = 0
start = time.time()
for content in self.content:
content.update(self.data)
for (status, path) in content.render():
if status != 'Ignore' or self.verbose:
print('[%s] %s' % (status, path))
else:
cached += 1
count += 1
print('Generated {} items ({} cached) in {:.2f}s.'.format(count, cached, time.time()-start))
def generate(self):
self.load_content()
self.process_content()
self.generate_output()
self.cache.shutdown()
if __name__ == '__main__':
filename = sys.argv[1]
settings = imp.load_source('settings', filename)
engine = Engine(settings.directories, settings.routes, settings.sources, settings.processors, settings.data)
engine.generate()