142 |
# all dispatchers should be used for the 0.8 and 0.9 namespaces too |
# all dispatchers should be used for the 0.8 and 0.9 namespaces too |
143 |
for xmlns in ("http://thuban.intevation.org/dtds/thuban-0.8.dtd", |
for xmlns in ("http://thuban.intevation.org/dtds/thuban-0.8.dtd", |
144 |
"http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd", |
"http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd", |
145 |
"http://thuban.intevation.org/dtds/thuban-0.9.dtd"): |
"http://thuban.intevation.org/dtds/thuban-0.9.dtd", |
146 |
|
"http://thuban.intevation.org/dtds/thuban-1.0-dev.dtd", |
147 |
|
"http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd", |
148 |
|
"http://thuban.intevation.org/dtds/thuban-1.0.0.dtd"): |
149 |
for key, value in dispatchers.items(): |
for key, value in dispatchers.items(): |
150 |
dispatchers[(xmlns, key)] = value |
dispatchers[(xmlns, key)] = value |
151 |
|
|
152 |
XMLReader.AddDispatchers(self, dispatchers) |
XMLReader.AddDispatchers(self, dispatchers) |
153 |
|
|
154 |
|
def Destroy(self): |
155 |
|
"""Clear all instance variables to cut cyclic references. |
156 |
|
|
157 |
|
The GC would have collected the loader eventually but it can |
158 |
|
happen that it doesn't run at all until Thuban is closed (2.3 |
159 |
|
but not 2.2 tries a bit harder and forces a collection when the |
160 |
|
interpreter terminates) |
161 |
|
""" |
162 |
|
self.__dict__.clear() |
163 |
|
|
164 |
def start_session(self, name, qname, attrs): |
def start_session(self, name, qname, attrs): |
165 |
self.theSession = Session(self.encode(attrs.get((None, 'title'), |
self.theSession = Session(self.encode(attrs.get((None, 'title'), |
166 |
None))) |
None))) |
188 |
If the attribute has a default value and it is not present in |
If the attribute has a default value and it is not present in |
189 |
attrs, use that default value as the value in the returned dict. |
attrs, use that default value as the value in the returned dict. |
190 |
|
|
191 |
If a conversion is specified, convert the value before putting |
The value is converted before putting it into the returned dict. |
192 |
it into the returned dict. The following conversions are |
The following conversions are available: |
|
available: |
|
193 |
|
|
194 |
'filename' -- The attribute is a filename. |
'filename' -- The attribute is a filename. |
195 |
|
|
211 |
|
|
212 |
'ascii' -- The attribute is converted to a bytestring with |
'ascii' -- The attribute is converted to a bytestring with |
213 |
ascii encoding. |
ascii encoding. |
214 |
|
|
215 |
|
a callable -- The attribute value is passed to the callable |
216 |
|
and the return value is used as the converted |
217 |
|
value |
218 |
|
|
219 |
|
If no conversion is specified for an attribute it is converted |
220 |
|
with self.encode. |
221 |
""" |
""" |
222 |
normalized = {} |
normalized = {} |
223 |
|
|
248 |
value)) |
value)) |
249 |
elif d.conversion == "ascii": |
elif d.conversion == "ascii": |
250 |
value = value.encode("ascii") |
value = value.encode("ascii") |
251 |
|
elif d.conversion: |
252 |
|
# Assume it's a callable |
253 |
|
value = d.conversion(value) |
254 |
else: |
else: |
255 |
if d.conversion: |
value = self.encode(value) |
|
raise ValueError("Unknown attribute conversion %r" |
|
|
% d.conversion) |
|
256 |
|
|
257 |
normalized[d.name] = value |
normalized[d.name] = value |
258 |
return normalized |
return normalized |
384 |
self.aMap = None |
self.aMap = None |
385 |
|
|
386 |
def start_projection(self, name, qname, attrs): |
def start_projection(self, name, qname, attrs): |
387 |
self.ProjectionName = self.encode(attrs.get((None, 'name'), None)) |
attrs = self.check_attrs(name, attrs, |
388 |
self.ProjectionParams = [ ] |
[AttrDesc("name", conversion=self.encode), |
389 |
|
AttrDesc("epsg", default=None, |
390 |
|
conversion=self.encode)]) |
391 |
|
self.projection_name = attrs["name"] |
392 |
|
self.projection_epsg = attrs["epsg"] |
393 |
|
self.projection_params = [ ] |
394 |
|
|
395 |
def end_projection(self, name, qname): |
def end_projection(self, name, qname): |
396 |
if self.aLayer is not None: |
if self.aLayer is not None: |
401 |
assert False, "projection tag out of context" |
assert False, "projection tag out of context" |
402 |
pass |
pass |
403 |
|
|
404 |
obj.SetProjection( |
obj.SetProjection(Projection(self.projection_params, |
405 |
Projection(self.ProjectionParams, self.ProjectionName)) |
self.projection_name, |
406 |
|
epsg = self.projection_epsg)) |
407 |
|
|
408 |
def start_parameter(self, name, qname, attrs): |
def start_parameter(self, name, qname, attrs): |
409 |
s = attrs.get((None, 'value')) |
s = attrs.get((None, 'value')) |
410 |
s = str(s) # we can't handle unicode in proj |
s = str(s) # we can't handle unicode in proj |
411 |
self.ProjectionParams.append(s) |
self.projection_params.append(s) |
412 |
|
|
413 |
def start_layer(self, name, qname, attrs, layer_class = Layer): |
def start_layer(self, name, qname, attrs, layer_class = Layer): |
414 |
"""Start a layer |
"""Start a layer |
452 |
self.aLayer = None |
self.aLayer = None |
453 |
|
|
454 |
def start_classification(self, name, qname, attrs): |
def start_classification(self, name, qname, attrs): |
455 |
field = attrs.get((None, 'field'), None) |
attrs = self.check_attrs(name, attrs, |
456 |
|
[AttrDesc("field", True), |
457 |
|
AttrDesc("field_type", True)]) |
458 |
|
field = attrs["field"] |
459 |
|
fieldType = attrs["field_type"] |
460 |
|
|
|
fieldType = attrs.get((None, 'field_type'), None) |
|
461 |
dbFieldType = self.aLayer.GetFieldType(field) |
dbFieldType = self.aLayer.GetFieldType(field) |
462 |
|
|
463 |
if fieldType != dbFieldType: |
if fieldType != dbFieldType: |
547 |
self.aLayer = self.aMap.LabelLayer() |
self.aLayer = self.aMap.LabelLayer() |
548 |
|
|
549 |
def start_label(self, name, qname, attrs): |
def start_label(self, name, qname, attrs): |
550 |
x = float(attrs[(None, 'x')]) |
attrs = self.check_attrs(name, attrs, |
551 |
y = float(attrs[(None, 'y')]) |
[AttrDesc("x", True, conversion = float), |
552 |
text = self.encode(attrs[(None, 'text')]) |
AttrDesc("y", True, conversion = float), |
553 |
halign = attrs[(None, 'halign')] |
AttrDesc("text", True), |
554 |
valign = attrs[(None, 'valign')] |
AttrDesc("halign", True, |
555 |
|
conversion = "ascii"), |
556 |
|
AttrDesc("valign", True, |
557 |
|
conversion = "ascii")]) |
558 |
|
x = attrs['x'] |
559 |
|
y = attrs['y'] |
560 |
|
text = attrs['text'] |
561 |
|
halign = attrs['halign'] |
562 |
|
valign = attrs['valign'] |
563 |
|
if halign not in ("left", "center", "right"): |
564 |
|
raise LoadError("Unsupported halign value %r" % halign) |
565 |
|
if valign not in ("top", "center", "bottom"): |
566 |
|
raise LoadError("Unsupported valign value %r" % valign) |
567 |
self.aLayer.AddLabel(x, y, text, halign = halign, valign = valign) |
self.aLayer.AddLabel(x, y, text, halign = halign, valign = valign) |
568 |
|
|
569 |
def characters(self, chars): |
def characters(self, chars): |
590 |
# Newly loaded session aren't modified |
# Newly loaded session aren't modified |
591 |
session.UnsetModified() |
session.UnsetModified() |
592 |
|
|
593 |
|
handler.Destroy() |
594 |
|
|
595 |
return session |
return session |
596 |
|
|