-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.py
184 lines (150 loc) · 5.42 KB
/
code.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
# First attempt at the git backed blog!
import web
import git
import settings
import feed.atom
from jinja import Environment, FunctionLoader
urls = (
'/', 'index',
'/(.*)\.atom', 'atomize',
'/(.*)/log/?', 'log',
'/(.*)', 'page',
)
# Template environment
def load_template(templ):
(res, err) = git.show(settings.templates + '/' + templ)
return res
tenv = Environment(loader=FunctionLoader(load_template))
class index:
def GET(self):
print dirify('/', git.ls()[0])
def dirify(start, list):
result = '<table>\n'
dir = []
if start != '/':
start = '/'+start+'/'
for entry in list:
e = entry.split()
e[3] = '<a href="'+start+e[3]+'">'+e[3]+'</a>'
result += '\t<tr>\n'
for i in e:
result += '\t\t<td>' + i + '</td>\n'
result += '\t</tr>\n'
result += '</table>\n'
return result
# Get jinja comments that define the template dict to send.
# something like:
# {# template : sometemplate.html #}
# {# title : This is some entry #}
# {# metadata : 23847123987129387 #}
def get_dict(str):
fin = {}
lines = str.splitlines()
strfin = []
for line in lines:
if line.find("{#") > -1:
(vari, colo, valu) = line.replace('{# ', ''
).replace(' #}', ''
).partition(' : ')
fin[vari] = valu
else:
strfin.append(line)
fin['content'] = '\n'.join(strfin)
if 'template' not in fin:
fin['template'] = 'default.html'
templ = fin['template']
del fin['template']
return (templ, fin)
class page:
def GET(self, file):
file = file.rstrip('/')
(out, ret) = git.type(file)
if ret == 128:
web.webapi.notfound()
elif out == 'blob':
(fout, fret) = git.show(file)
(templ, dict) = get_dict(fout)
templ = tenv.get_template(templ)
print templ.render(dict)
elif out == 'tree':
print dirify(file, git.ls(file)[0])
else:
web.webapi.notfound()
# Make an atom feed out of a directory in git.
class atomize:
def GET(self, dir):
dir = dir.rstrip('/').lstrip('/')
(out, ret) = git.type(dir)
if ret == 128:
web.webapi.notfound()
elif out == 'blob':
print self.file_feed(dir)
elif out == 'tree':
print self.dir_feed(dir)
else:
web.webapi.notfound()
def dir_feed(self, dir):
xmldoc, dfeed = feed.atom.new_xmldoc_feed()
dfeed.title = settings.blog_name + " " + dir
dfeed.id = settings.blog_url + '/' + dir # << url for feed
dfeed.updated = self.atom_date(dir)
links = feed.atom.Link(settings.blog_url) # << url to home.
dfeed.links.append(links)
selflink = feed.atom.Link(settings.blog_url + '/' + dir + '.atom')
selflink.attrs['rel'] = "self"
dfeed.links.append(selflink)
for a in self.atom_authors(dir):
author = feed.atom.Author(a)
dfeed.authors.append(author)
(eout, eerr) = git.ls(file=dir, name=True)
for e in eout:
if dir != '':
e = dir + '/' + e
entry = feed.atom.Entry()
(entryc, ecerr) = git.show(e)
entry.content = "<![CDATA[\n" + entryc + "\n]]>"
entry.title = entryc.splitlines()[0]
entry.id = settings.blog_url + '/' + e
entry.updated = self.atom_date(e.lstrip('/'))
dfeed.entries.append(entry)
return str(xmldoc)
def file_feed(self, file):
xmldoc, dfeed = feed.atom.new_xmldoc_feed()
dfeed.title = settings.blog_name + " " + file
dfeed.id = settings.blog_url + '/' + file
dfeed.updated = self.atom_date(file)
links = feed.atom.Link(settings.blog_url)
dfeed.links.append(links)
selflink = feed.atom.Link(settings.blog_url + '/' + file + '.atom')
selflink.attrs['rel'] = "self"
dfeed.links.append(selflink)
for a in self.atom_authors(file):
author = feed.atom.Author(a)
dfeed.authors.append(author)
(blobs, berr) = git.log(file=file, num=10, format="%H")
for b in blobs.splitlines():
entry = feed.atom.Entry()
(entryc, err) = git.log(file=file, num=1, extopts="--stat -p -M -C --full-index", rev=b)
entry.content = "<![CDATA[\n" + entryc + "\n]]>"
entry.title = "%s : %s" % (file, b)
entry.id = settings.blog_url + '/' + file
entry.updated = self.atom_date(file, rev=b)
dfeed.entries.append(entry)
return str(xmldoc)
def atom_authors(self, arr):
(arr, err) = git.log(file=arr, format="%an")
res = []
for a in arr.splitlines():
if a not in res:
res.append(a)
return res
def atom_date(self, file, rev=None):
if rev == None:
(arr, err) = git.log(file=file, num=1, format="%ai")
else:
(arr, err) = git.log(file=file, num=1, format="%ai", rev=rev)
arr = arr.splitlines()[0].split()
return arr[0] + 'T' + arr[1] + arr[2]
web.webapi.internalerror = web.debugerror
if __name__ == "__main__":
web.run(urls, globals(), web.reloader)