1 ## This file is part of Crapvine.
3 ## Copyright (C) 2007 Andrew Sayman <lorien420@myrealbox.com>
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.
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/>.
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']
35 self
.reading_description
= False
36 self
.current_description
= ''
38 self
.reading_usualplace
= False
39 self
.current_usualplace
= ''
42 self
.reading_creature
= ''
43 self
.creatures
['vampire'] = VampireLoader()
47 if self
.creatures
['vampire']:
48 return self
.creatures
['vampire'].vampires
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
)
56 if name
in self
.creatures_elements
:
57 self
.reading_creature
= name
58 if self
.creatures
[name
]:
59 self
.creatures
[name
].startElement(name
, attrs
)
62 if name
== 'grapevine':
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
)
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
)
85 if name
== 'grapevine':
88 elif name
== 'usualplace':
89 assert self
.reading_usualplace
90 self
.reading_usualplace
= False
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
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
)
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
):
116 def startCDATA(self
):
117 if self
.reading_creature
:
118 if self
.creatures
[self
.reading_creature
]:
119 self
.creatures
[self
.reading_creature
].startCDATA()
123 if self
.reading_creature
:
124 if self
.creatures
[self
.reading_creature
]:
125 self
.creatures
[self
.reading_creature
].endCDATA()
127 self
.in_cdata
= False
133 def comment(self
, text
):
137 def error(self
, exception
):
140 def fatalError(self
, exception
):
143 def warning(self
, exception
):