Small program to test initial bin reading work
[crapvine.git] / chronicle_loader.py
blob1f23130097aa3920b1ddbc0e20599004d3935f11
1 ## This file is part of Crapvine.
2 ##
3 ## Copyright (C) 2007 Andrew Sayman <lorien420@myrealbox.com>
4 ##
5 ## Crapvine is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or
8 ## (at your option) any later version.
9 ##
10 ## Crapvine is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ## GNU General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # XML
19 from xml.sax import ContentHandler
20 from xml.sax.saxutils import unescape, escape
22 from vampire_loader import VampireLoader
23 from chronicle import Chronicle
25 def normalize_whitespace(text):
26 "Remove redundant whitespace from a string"
27 return ' '.join(text.split())
29 class ChronicleLoader(ContentHandler):
30 creatures_elements = ['vampire', 'mortal']
31 def __init__(self):
32 self.chronicle = None
33 self.in_cdata = False
35 self.reading_description = False
36 self.current_description = ''
38 self.reading_usualplace = False
39 self.current_usualplace = ''
41 self.creatures = {}
42 self.reading_creature = ''
43 self.creatures['vampire'] = VampireLoader()
45 @property
46 def vampires(self):
47 if self.creatures['vampire']:
48 return self.creatures['vampire'].vampires
49 return None
51 def startElement(self, name, attrs):
52 if self.reading_creature:
53 if self.creatures[self.reading_creature]:
54 self.creatures[self.reading_creature].startElement(name, attrs)
55 return
56 if name in self.creatures_elements:
57 self.reading_creature = name
58 if self.creatures[name]:
59 self.creatures[name].startElement(name, attrs)
60 return
62 if name == 'grapevine':
63 chron = Chronicle()
64 chron.read_attributes(attrs)
65 self.chronicle = chron
67 elif name == 'usualplace':
68 self.reading_usualplace = True
70 elif name == 'description':
71 self.reading_description = True
73 def endElement(self, name):
74 if self.reading_creature:
75 if self.creatures[self.reading_creature]:
76 self.creatures[self.reading_creature].endElement(name)
77 return
78 if name in self.creatures_elements:
79 assert self.reading_creature
80 self.reading_creature = ''
81 if self.creatures[name]:
82 self.creatures[name].endElement(name)
83 return
85 if name == 'grapevine':
86 assert self.chronicle
88 elif name == 'usualplace':
89 assert self.reading_usualplace
90 self.reading_usualplace = False
91 if self.chronicle:
92 self.chronicle['usualplace'] = unescape(self.current_usualplace)
93 self.current_usualplace = ''
95 elif name == 'description':
96 assert self.reading_description
97 self.reading_description = False
98 if self.chronicle:
99 self.chronicle['description'] = unescape(self.current_description)
100 self.current_description = ''
102 def characters(self, ch):
103 if self.reading_creature:
104 if self.creatures[self.reading_creature]:
105 self.creatures[self.reading_creature].characters(ch)
106 return
108 if self.reading_usualplace and self.in_cdata:
109 self.current_usualplace += ch
110 if self.reading_description and self.in_cdata:
111 self.current_description += ch
113 def ignorableWhitespace(self, space):
114 pass
116 def startCDATA(self):
117 if self.reading_creature:
118 if self.creatures[self.reading_creature]:
119 self.creatures[self.reading_creature].startCDATA()
120 return
121 self.in_cdata = True
122 def endCDATA(self):
123 if self.reading_creature:
124 if self.creatures[self.reading_creature]:
125 self.creatures[self.reading_creature].endCDATA()
126 return
127 self.in_cdata = False
129 def startDTD(self):
130 pass
131 def endDTD(self):
132 pass
133 def comment(self, text):
134 pass
137 def error(self, exception):
138 print 'Error'
139 raise exception
140 def fatalError(self, exception):
141 print 'Fatal Error'
142 raise exception
143 def warning(self, exception):
144 print 'Warning'
145 raise exception