-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.py
60 lines (47 loc) · 1.67 KB
/
serve.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
import lxml.etree as etree
import sys
from twisted.internet import reactor
from twisted.web import resource, server, static
import dump
class Articles(resource.Resource):
def __init__(self, dump):
resource.Resource.__init__(self)
self.dump = dump
def getChild(self, name, req):
try:
node = self.dump.get_by_title(name)
except KeyError:
try:
name = name[0].capitalize() + name[1:].replace('_', ' ')
node = self.dump.get_by_title(name)
except KeyError:
return resource.NoResource()
return self.makeChild(name, node)
class ArticlesRaw(Articles):
def makeChild(self, name, node):
s = node.find('revision/text').text
return static.Data(s.encode('utf8'), 'text/plain')
class ArticlesXML(Articles):
def makeChild(self, name, node):
s = etree.tostring(node)
return static.Data(s, 'text/xml')
class ArticlesFormatted(Articles):
def getChild(self, name, req):
return static.File('skeleton.html')
class Site(server.Site):
def __init__(self, dump):
root = resource.Resource()
server.Site.__init__(self, root)
root.putChild('xml', ArticlesXML(dump))
root.putChild('raw', ArticlesRaw(dump))
root.putChild('a', ArticlesFormatted(dump))
root.putChild('jquery.js', static.File('jquery.js'))
root.putChild('hyper.js', static.File('hyper.js'))
root.putChild('hyper.css', static.File('hyper.css'))
def main():
(dump_path,) = sys.argv[1:]
site = Site(dump.Dump(dump_path))
reactor.listenTCP(8080, site)
reactor.run()
if __name__ == '__main__':
main()