-
Notifications
You must be signed in to change notification settings - Fork 1
/
Gyre.py
349 lines (305 loc) · 12 KB
/
Gyre.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
#
# Gyre.py - Core of CMS
# Copyright (C) 2004 - 2009 Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
import sys
import os
import string
import time
import re
import traceback
import yaml
import cgi
import cgitb; cgitb.enable()
class Absent:
def __init__(self, name):
self.name = name
def __nonzero__(self):
return False
def __str__(self):
return ''
def __repr__(self):
return 'Absent(%s)' % (self.name,)
class Entity:
def __init__(self, _parent=None, _props=None, **kw):
self.__dict__['_parent'] = _parent
self.__dict__['_props'] = _props or {}
self.__dict__['_props'].update(kw)
def __getattr__(self, name):
if name.startswith('_'): raise AttributeError, name
if self._props.has_key(name):
v = self._props[name]
return Entity(_props = v) if isinstance(v, dict) else v
if self._parent is None: return Absent(name)
return getattr(self._parent, name)
def __setattr__(self, key, val):
self._props[key] = val
def DICT(self):
if self._parent is None: x = {}
elif isinstance(self._parent, Entity): x = self._parent.DICT()
elif isinstance(self._parent, dict): x = dict(self._parent)
else: x = dict(self._parent.__dict__)
x.update(self._props)
return x
class Store:
def __init__(self):
self.stories = {}
self.templates = {}
self.sources = []
self.indices = []
self.preprocessors = []
def _load_templates(self):
self.templates = {}
for templatedir in config.templatedirs:
for filename in os.listdir(templatedir):
if filename.startswith('.'): continue
filepath = os.path.join(templatedir, filename)
f = open(filepath)
self.templates[filename] = f.read()
f.close()
def load(self):
self._load_templates()
for source in self.sources: source.updateStore()
def save(self):
pass
def addSource(self, source):
self.sources.append(source)
def addIndex(self, index):
self.indices.append(index)
def addPreprocessor(self, processor):
self.preprocessors.append(processor)
def getTemplate(self, view, variant, flavour):
for possibility in view:
filename = '%s_%s.%s' % (possibility, variant, flavour)
if self.templates.has_key(filename): return self.templates[filename]
generic = 'generic_%s.%s' % (variant, flavour)
if self.templates.has_key(generic): return self.templates[generic]
raise KeyError, ('No such template', (view, variant, flavour))
def getStoryIds(self):
return self.stories.keys()
def getStoryCount(self):
return len(self.stories)
def update(self, story):
for processor in self.preprocessors: story = processor(story)
self.stories[story.id] = story
for index in self.indices: index.update(story)
def getStory(self, id, fallback = None):
if self.stories.has_key(id): return self.stories[id]
if fallback is None: raise KeyError, ('No such story', id)
return fallback
class CategoryIndex:
def __init__(self):
self.root = ({}, [])
config.store.addIndex(self)
def update(self, story):
(idx, stories) = self.root
for cat in story.category:
if not idx.has_key(cat):
idx[cat] = ({}, [])
(idx, stories) = idx[cat]
stories.append(story.id)
def allCategories(self):
acc = [[]]
def collect(prefix, idx):
for (cat, (newidx, storyids)) in idx.items():
newprefix = prefix + [cat]
acc.append(newprefix)
collect(newprefix, newidx)
collect([], self.root[0])
return acc
def lookup(self, category, includeSubcategories = True):
acc = set()
(idx, stories) = self.root
for cat in category:
if idx.has_key(cat):
(idx, stories) = idx[cat]
else:
(idx, stories) = ({}, [])
break
acc.update(stories)
def accumulate_tree(acc, outer_idx):
for (idx, stories) in outer_idx.values():
accumulate_tree(acc, idx)
acc.update(stories)
if includeSubcategories: accumulate_tree(acc, idx)
return acc
class TagIndex:
def __init__(self, propertyname = 'tags'):
self.tags = {}
self.propertyname = propertyname
config.store.addIndex(self)
def update(self, story):
tags = getattr(story, self.propertyname)
if tags:
for tag in tags:
if not self.tags.has_key(tag): self.tags[tag] = set()
self.tags[tag].add(story.id)
def lookup(self, tag):
return self.tags.get(tag, set())
exprre = re.compile('<\?((\?[^>]|[^?])+)\?>')
def template_s(tmpl, env):
tmpl = unicode(tmpl)
if isinstance(env, Entity): env = env.DICT()
acc = []
while 1:
m = exprre.search(tmpl)
if not m:
acc.append(tmpl)
break
acc.append(tmpl[:m.start()])
try:
acc.append(unicode(eval(m.group(1), sys.modules, env)))
except:
acc.append(cgi.escape('<Exception:\r\n' +
string.join(traceback.format_exception(*sys.exc_info()), '') +
'\r\n>'))
tmpl = tmpl[m.end():]
return string.join(acc, '')
def template(view, variant, flavour, env):
return template_s(config.store.getTemplate(view, variant, flavour), env)
storystack = {} # used to break (certain) infinite recursions
def renderStory(flavour, variant, story, context, avoidLoops = True):
if avoidLoops:
storystack_key = (variant, story.id)
if storystack_key in storystack:
return ''
storystack[storystack_key] = 1
try:
return template(story.view, variant, flavour,
Entity(config.renderenvt,
variant = variant,
story = story,
context = context))
finally:
if avoidLoops:
del storystack[storystack_key]
def renderStories(flavour, variant, stories, context):
fragments = []
stories = [config.store.getStory(storyid) for storyid in stories]
stories.sort(key = lambda s: s.mtime, reverse = True)
for story in stories: fragments.append(renderStory(flavour, variant, story, context))
return string.join(fragments, '')
def set_variable(entity, variablename, val):
setattr(entity, variablename, val)
return ''
def installPlugin(modname, *args, **kwargs):
mod = __import__('plugins.' + modname)
if hasattr(mod, modname):
mod = getattr(mod, modname)
mod.install(config, *args, **kwargs)
config = Entity()
config.version = '0.0.2'
config.language = 'en'
config.templatedirs = ['templates']
config.file_extension = 'txt'
config.store = Store()
config.categoryIndex = CategoryIndex()
config.snapshot_flavours = ['html', 'rss', 'atom']
config.verbose_snapshot = 1
config.legacy_story_links = 0
if os.environ.has_key('SCRIPT_NAME'):
config.base_url = os.path.dirname(os.environ['SCRIPT_NAME'])
else:
config.base_url = 'file://' + os.getcwd()
config.protostory = Entity()
config.protostory.view = ['story']
config.defaultstory = Entity(config.protostory, view = ['categoryindex'])
config.protocontext = Entity()
config.protocontext.flavour = 'html'
config.renderenvt = Entity()
config.renderenvt.config = config
config.renderenvt.Entity = Entity
config.renderenvt.template = template
config.renderenvt.template_s = template_s
config.renderenvt.renderStory = renderStory
config.renderenvt.renderStories = renderStories
config.renderenvt.timefmt = lambda fmt, t: time.strftime(fmt, time.localtime(t))
config.renderenvt.timefmt_utc = lambda fmt, t: time.strftime(fmt, time.gmtime(t))
config.renderenvt.time3339 = lambda t: time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(t))
config.renderenvt.set_variable = set_variable
def cgi_render(story, context):
sys.stdout.write(renderStory(context.flavour, 'response_headers', story, context).encode('ascii'))
sys.stdout.write('\r\n')
sys.stdout.write(renderStory(context.flavour, 'response_body', story, context).encode('utf-8'))
def cgi_main():
config.renderenvt.url = config.script_url
config.store.load()
category = []
for elt in string.split(os.environ.get('PATH_INFO', ''), '/'):
if not elt or elt.startswith('.') or elt.find('\0') != -1 or elt.find('\\') != -1:
continue
category.append(elt)
if config.legacy_story_links and category and category[0] == '_STORY_': category.pop(0)
context = Entity(config.protocontext, mode = 'script', category = category)
for (k, v) in cgi.parse_qs(os.environ.get('QUERY_STRING', '')).items():
setattr(context, k, yaml.safe_load(v[0]))
story = None
if category:
pos = category[-1].rfind('.')
if pos != -1:
context.flavour = category[-1][pos + 1:]
category[-1] = category[-1][:pos]
try:
story = config.store.getStory(string.join(category, '/'))
except KeyError:
pass
category.pop()
if not story:
story = config.defaultstory
cgi_render(story, context)
config.store.save()
def snapshotRender(path, flavour, story, context):
try:
os.makedirs(os.path.dirname(path), 0755)
except:
pass
document = renderStory(flavour, 'response_body', story, context)
if document:
if config.verbose_snapshot:
print 'Writing %s...' % path
f = open(path, 'w', 0644)
f.write(document.encode('utf-8'))
f.close()
def snapshot_main():
config.renderenvt.url = config.snapshot_url
config.store.load()
for flavour in config.snapshot_flavours:
context = Entity(config.protocontext,
flavour = flavour,
mode = 'snapshot')
for category in config.categoryIndex.allCategories():
if context.num_entries:
for skip in range(0, config.store.getStoryCount(), context.num_entries):
pfx = ('index-skip' + str(skip)) if skip else 'index'
path = os.path.join(*([config.snapshot_dir] + category + [pfx + '.' + flavour]))
snapshotRender(path, flavour, config.defaultstory, Entity(context,
category = category,
skip = skip))
else:
snapshotRender(flavour, config.defaultstory, Entity(context, category = category))
for storyid in config.store.getStoryIds():
path = os.path.join(config.snapshot_dir, storyid + '.' + flavour)
story = config.store.getStory(storyid)
snapshotRender(path, flavour, story, Entity(context, category = story.category))
config.store.save()
def sitemap_main():
config.renderenvt.url = config.script_url
config.store.load()
context = Entity(config.protocontext, mode = 'sitemap', category = [])
story = Entity(view = ['sitemap'])
cgi_render(story, context)
config.store.save()