Edit an experience entry by double-clicking on the tree
[crapvine.git] / vampire_loader.py
blob98637f5e7c30806eac419e5a537f55b257a7bd6a
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 # Vampire Support
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):
32 def __init__(self):
33 self.vampires = {}
34 self.current_vampire = None
36 self.in_cdata = False
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):
52 if name == 'vampire':
53 if not attrs.has_key('name'):
54 return
55 v = Vampire()
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'
62 exp = Experience()
63 exp.read_attributes(attrs)
64 self.current_experience = exp
65 if self.current_vampire:
66 self.current_vampire.add_experience(exp)
68 elif name == 'entry':
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)
75 elif name == 'entry':
76 pass
78 elif name == 'biography':
79 self.reading_biography = True
81 elif name == 'notes':
82 self.reading_notes = True
84 elif name == 'traitlist':
85 if self.current_traitlist:
86 raise 'TraitList encountered while still reading traitlist'
87 tl = TraitList()
88 tl.read_attributes(attrs)
89 self.current_traitlist = tl
90 if self.current_vampire:
91 self.current_vampire.add_traitlist(tl)
93 elif name == 'trait':
94 if not self.current_traitlist:
95 raise 'Trait without bounding traitlist'
96 t = Trait()
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):
137 pass
139 def startCDATA(self):
140 self.in_cdata = True
141 def endCDATA(self):
142 self.in_cdata = False
144 def startDTD(self):
145 pass
146 def endDTD(self):
147 pass
148 def comment(self, text):
149 pass
151 def error(self, exception):
152 print 'Error'
153 raise exception
154 def fatalError(self, exception):
155 print 'Fatal Error'
156 raise exception
157 def warning(self, exception):
158 print 'Warning'
159 raise exception