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
23 from vampire
import Vampire
24 from trait
import Trait
, TraitList
25 from experience
import Experience
, ExperienceEntry
27 def normalize_whitespace(text
):
28 "Remove redundant whitespace from a string"
29 return ' '.join(text
.split())
31 class VampireLoader(ContentHandler
):
34 self
.current_vampire
= None
38 self
.current_traitlist
= None
40 self
.reading_biography
= False
41 self
.current_biography
= ''
43 self
.reading_notes
= False
44 self
.current_notes
= ''
46 self
.current_experience
= None
48 def add_vampire(self
, vamp
):
49 self
.vampires
[vamp
.name
] = vamp
51 def startElement(self
, name
, attrs
):
53 if not attrs
.has_key('name'):
56 v
.read_attributes(attrs
)
57 self
.current_vampire
= v
59 elif name
== 'experience':
60 if self
.current_experience
:
61 raise 'Experience encountered while still reading traitlist'
63 exp
.read_attributes(attrs
)
64 self
.current_experience
= exp
65 if self
.current_vampire
:
66 self
.current_vampire
.add_experience(exp
)
69 if not self
.current_experience
:
70 raise 'Entry without bounding Experience'
71 ent
= ExperienceEntry()
72 ent
.read_attributes(attrs
)
73 self
.current_experience
.add_entry(ent
, False)
78 elif name
== 'biography':
79 self
.reading_biography
= True
82 self
.reading_notes
= True
84 elif name
== 'traitlist':
85 if self
.current_traitlist
:
86 raise 'TraitList encountered while still reading traitlist'
88 tl
.read_attributes(attrs
)
89 self
.current_traitlist
= tl
90 if self
.current_vampire
:
91 self
.current_vampire
.add_traitlist(tl
)
94 if not self
.current_traitlist
:
95 raise 'Trait without bounding traitlist'
97 t
.read_attributes(attrs
)
98 self
.current_traitlist
.add_trait(t
)
100 def endElement(self
, name
):
101 if name
== 'vampire':
102 assert self
.current_vampire
103 self
.add_vampire(self
.current_vampire
)
104 self
.current_vampire
= None
106 elif name
== 'experience':
107 assert self
.current_experience
108 self
.current_experience
= None
110 elif name
== 'traitlist':
111 assert self
.current_traitlist
112 self
.current_traitlist
= None
114 elif name
== 'biography':
115 assert self
.reading_biography
116 self
.reading_biography
= False
117 if self
.current_vampire
:
118 self
.current_vampire
['biography'] = unescape(self
.current_biography
)
119 print self
.current_biography
120 self
.current_biography
= ''
122 elif name
== 'notes':
123 assert self
.reading_notes
124 self
.reading_notes
= False
125 if self
.current_vampire
:
126 self
.current_vampire
['notes'] = unescape(self
.current_notes
)
127 print self
.current_notes
128 self
.current_notes
= ''
130 def characters(self
, ch
):
131 if self
.reading_biography
and self
.in_cdata
:
132 self
.current_biography
+= ch
133 elif self
.reading_notes
and self
.in_cdata
:
134 self
.current_notes
+= ch
136 def ignorableWhitespace(self
, space
):
139 def startCDATA(self
):
142 self
.in_cdata
= False
148 def comment(self
, text
):
151 def error(self
, exception
):
154 def fatalError(self
, exception
):
157 def warning(self
, exception
):