-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsite.py
55 lines (53 loc) · 1.98 KB
/
website.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
from xml.sax.handler import ContentHandler
from xml.sax import parse
import os
class Handler(ContentHandler):
def callback(self, prefix, name, attrs = None):
method = getattr(self, prefix + name, None)
if callable(method):
args = ()
else:
method = getattr(self, prefix + "default", None)
args = name,
if prefix == "start_": args += attrs,
if callable(method): method(*args)
def startElement(self, name, attrs):
self.callback('start_', name, attrs)
def endElement(self, name):
self.callback('end_', name, None)
class XMLHandler(Handler):
passthrough = False
def __init__(self, directory):
self.directory = [directory]
self.ensure_directory()
def ensure_directory(self):
path = os.path.join(*self.directory)
if not os.path.isdir(path): os.makedirs(path)
def start_page(self, attrs):
self.passthrough = True
filename = os.path.join(*self.directory + [attrs['name'] + '.html'])
self.out = open(filename, 'w')
self.out.write("<html>\n<head><title>%s</title></head>" % attrs['title'])
self.out.write("<body>\n")
def end_page(self):
self.passthrough = False
self.out.write("\n</body>\n</html>\n")
self.out.close()
def start_directory(self, attrs):
self.directory.append(attrs['name'])
self.ensure_directory()
def end_directory(self):
self.directory.pop()
def characters(self, string):
if self.passthrough and string.strip():
self.out.write("%s\n" % string)
def start_default(self, name, attrs):
if self.passthrough:
self.out.write("<%s" % name)
for key, val in attrs.items():
self.out.write(" %s = %s" % (key, val))
self.out.write(">\n")
def end_default(self, name):
if self.passthrough:
self.out.write("</%s>\n" % name)
parse("website.xml", XMLHandler("test"))