1 |
# Copyright (c) 2001, 2002 by Intevation GmbH |
# Copyright (c) 2001, 2002, 2003 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Jan-Oliver Wagner <[email protected]> |
# Jan-Oliver Wagner <[email protected]> |
4 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
5 |
|
# Jonathan Coles <[email protected]> |
6 |
# |
# |
7 |
# This program is free software under the GPL (>=v2) |
# This program is free software under the GPL (>=v2) |
8 |
# Read the file COPYING coming with Thuban for details. |
# Read the file COPYING coming with Thuban for details. |
13 |
|
|
14 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
15 |
|
|
16 |
|
# fix for people using python2.1 |
17 |
|
from __future__ import nested_scopes |
18 |
|
|
19 |
import os |
import os |
20 |
import string |
import string |
21 |
|
|
23 |
|
|
24 |
from Thuban.Model.color import Color |
from Thuban.Model.color import Color |
25 |
|
|
26 |
|
from Thuban.Model.classification import * |
27 |
|
|
28 |
# |
# |
29 |
# one level of indention |
# one level of indention |
30 |
# |
# |
52 |
data = string.replace(data, "'", "'") |
data = string.replace(data, "'", "'") |
53 |
return data |
return data |
54 |
|
|
55 |
class Saver: |
class XMLWriter: |
56 |
|
"""Abstract XMLWriter. |
|
"""Class to serialize a session into an XML file. |
|
57 |
|
|
58 |
Applications built on top of Thuban may derive from this class and |
Should be overridden to provide specific object saving functionality. |
|
override or extend the methods to save additinal information. This |
|
|
additional information should take the form of additional attributes |
|
|
or elements whose names are prefixed with a namespace. To define a |
|
|
namespace derived classes should extend the write_session method to |
|
|
pass the namespaces to the default implementation. |
|
59 |
""" |
""" |
60 |
|
|
61 |
|
def __init__(self): |
62 |
def __init__(self, session): |
self.filename = None |
63 |
self.session = session |
pass |
64 |
|
|
65 |
def write(self, file_or_filename): |
def write(self, file_or_filename): |
66 |
"""Write the session to a file. |
"""Write the session to a file. |
73 |
directory. |
directory. |
74 |
|
|
75 |
If the argument is a file object (which is determined by the |
If the argument is a file object (which is determined by the |
76 |
presence of a write method) all filenames will be absolut |
presence of a write method) all filenames will be absolute |
77 |
filenames. |
filenames. |
78 |
""" |
""" |
79 |
|
|
80 |
self.indent_level = 0 |
# keep track of how many levels of indentation to write |
81 |
|
self.indent_level = 0 |
82 |
|
# track whether an element is currently open. see open_element(). |
83 |
|
self.element_open = 0 |
84 |
|
|
85 |
if hasattr(file_or_filename, "write"): |
if hasattr(file_or_filename, "write"): |
86 |
# it's a file object |
# it's a file object |
87 |
self.file = file_or_filename |
self.file = file_or_filename |
88 |
self.dir = "" |
self.dir = "" |
89 |
else: |
else: |
90 |
filename = file_or_filename |
self.filename = file_or_filename |
91 |
self.dir = os.path.dirname(filename) |
self.dir = os.path.dirname(self.filename) |
92 |
self.file = open(filename, 'w') |
self.file = open(self.filename, 'w') |
93 |
self.write_header() |
|
94 |
self.write_session(self.session) |
def close(self): |
95 |
|
assert self.indent_level == 0 |
96 |
|
if self.filename is not None: |
97 |
|
self.file.close() |
98 |
|
|
99 |
if self.indent_level != 0: |
def write_header(self, doctype, system): |
100 |
raise ValueError("indent_level still positive!") |
"""Write the XML header""" |
101 |
|
self.file.write('<?xml version="1.0" encoding="UTF-8"?>\n') |
102 |
|
self.file.write('<!DOCTYPE %s SYSTEM "%s">\n' % (doctype, system)) |
103 |
|
|
|
def write_attribs(self, attrs): |
|
|
for name, value in attrs.items(): |
|
|
if isinstance(value, Color): |
|
|
value = value.hex() |
|
|
else: |
|
|
value = str(value) |
|
|
self.file.write(' %s="%s"' % (escape(name), escape(value))) |
|
|
|
|
104 |
def open_element(self, element, attrs = {}): |
def open_element(self, element, attrs = {}): |
105 |
|
|
106 |
|
# |
107 |
|
# we note when an element is opened so that if two open_element() |
108 |
|
# calls are made successively we can end the currently open |
109 |
|
# tag and will later write a proper close tag. otherwise, |
110 |
|
# if a close_element() call is made directly after an open_element() |
111 |
|
# call we will close the tag with a /> |
112 |
|
# |
113 |
|
if self.element_open == 1: |
114 |
|
self.file.write(">\n") |
115 |
|
|
116 |
|
self.element_open = 1 |
117 |
|
|
118 |
# Helper function to write an element open tag with attributes |
# Helper function to write an element open tag with attributes |
119 |
self.file.write("%s<%s" % (TAB*self.indent_level, element)) |
self.file.write("%s<%s" % (TAB*self.indent_level, element)) |
120 |
self.write_attribs(attrs) |
self.__write_attribs(attrs) |
|
self.file.write(">\n") |
|
121 |
|
|
122 |
self.indent_level += 1 |
self.indent_level += 1 |
123 |
|
|
124 |
def close_element(self, element): |
def close_element(self, element): |
125 |
self.indent_level -= 1 |
self.indent_level -= 1 |
126 |
if self.indent_level < 0: |
assert self.indent_level >= 0 |
127 |
raise ValueError("close_element called too many times!") |
|
128 |
self.file.write("%s</%s>\n" % (TAB*self.indent_level, element)) |
# see open_element() for an explanation |
129 |
|
if self.element_open == 1: |
130 |
|
self.element_open = 0 |
131 |
|
self.file.write("/>\n") |
132 |
|
else: |
133 |
|
self.file.write("%s</%s>\n" % (TAB*self.indent_level, element)) |
134 |
|
|
135 |
def write_element(self, element, attrs = {}): |
def write_element(self, element, attrs = {}): |
136 |
# Helper function to write an element open tag with attributes |
"""write an element that won't need a closing tag""" |
137 |
self.file.write("%s<%s" % (TAB*self.indent_level, element)) |
self.open_element(element, attrs) |
138 |
self.write_attribs(attrs) |
self.close_element(element) |
|
self.file.write("/>\n") |
|
139 |
|
|
140 |
def write_header(self): |
def __write_attribs(self, attrs): |
141 |
"""Write the XML header""" |
for name, value in attrs.items(): |
142 |
self.file.write('<?xml version="1.0" encoding="UTF-8"?>\n') |
self.file.write(' %s="%s"' % (escape(name), escape(value))) |
143 |
self.file.write('<!DOCTYPE session SYSTEM "thuban.dtd">\n') |
|
144 |
|
class SessionSaver(XMLWriter): |
145 |
|
|
146 |
|
"""Class to serialize a session into an XML file. |
147 |
|
|
148 |
|
Applications built on top of Thuban may derive from this class and |
149 |
|
override or extend the methods to save additional information. This |
150 |
|
additional information should take the form of additional attributes |
151 |
|
or elements whose names are prefixed with a namespace. To define a |
152 |
|
namespace derived classes should extend the write_session method to |
153 |
|
pass the namespaces to the default implementation. |
154 |
|
""" |
155 |
|
|
156 |
|
|
157 |
|
def __init__(self, session): |
158 |
|
XMLWriter.__init__(self) |
159 |
|
self.session = session |
160 |
|
|
161 |
|
def write(self, file_or_filename): |
162 |
|
XMLWriter.write(self, file_or_filename) |
163 |
|
|
164 |
|
self.write_header("session", "thuban.dtd") |
165 |
|
self.write_session(self.session) |
166 |
|
self.close() |
167 |
|
|
168 |
def write_session(self, session, attrs = None, namespaces = ()): |
def write_session(self, session, attrs = None, namespaces = ()): |
169 |
"""Write the session and its contents |
"""Write the session and its contents |
199 |
element, call write_layer for each layer contained in the map |
element, call write_layer for each layer contained in the map |
200 |
and finally call write_label_layer to write the label layer. |
and finally call write_label_layer to write the label layer. |
201 |
""" |
""" |
|
write = self.file.write |
|
202 |
self.open_element('map title="%s"' % escape(map.title)) |
self.open_element('map title="%s"' % escape(map.title)) |
203 |
self.write_projection(map.projection) |
self.write_projection(map.projection) |
204 |
for layer in map.Layers(): |
for layer in map.Layers(): |
222 |
given, should be a mapping from attribute names to attribute |
given, should be a mapping from attribute names to attribute |
223 |
values. The values should not be XML-escaped yet. |
values. The values should not be XML-escaped yet. |
224 |
""" |
""" |
225 |
|
lc = layer.GetClassification() |
226 |
|
|
227 |
if attrs is None: |
if attrs is None: |
228 |
attrs = {} |
attrs = {} |
229 |
attrs["title"] = layer.title |
|
230 |
attrs["filename"] = relative_filename(self.dir, layer.filename) |
attrs["title"] = layer.title |
231 |
attrs["stroke_width"] = str(layer.stroke_width) |
attrs["filename"] = relative_filename(self.dir, layer.filename) |
232 |
fill = layer.fill |
attrs["stroke"] = lc.GetDefaultLineColor().hex() |
233 |
if fill is None: |
attrs["stroke_width"] = str(lc.GetDefaultLineWidth()) |
234 |
attrs["fill"] = "None" |
attrs["fill"] = lc.GetDefaultFill().hex() |
|
else: |
|
|
attrs["fill"] = fill.hex() |
|
|
stroke = layer.stroke |
|
|
if stroke is None: |
|
|
attrs["stroke"] = "None" |
|
|
else: |
|
|
attrs["stroke"] = stroke.hex() |
|
235 |
|
|
236 |
self.open_element("layer", attrs) |
self.open_element("layer", attrs) |
237 |
|
|
238 |
|
proj = layer.GetProjection() |
239 |
|
if proj is not None: |
240 |
|
self.write_projection(proj) |
241 |
|
|
242 |
self.write_classification(layer) |
self.write_classification(layer) |
243 |
|
|
244 |
self.close_element("layer") |
self.close_element("layer") |
245 |
|
|
246 |
def write_classification(self, layer, attrs = None): |
def write_classification(self, layer, attrs = None): |
247 |
if attrs is None: |
if attrs is None: |
248 |
attrs = {} |
attrs = {} |
249 |
|
|
250 |
lc = layer.classification |
lc = layer.GetClassification() |
251 |
|
|
252 |
field = lc.field |
field = lc.GetField() |
253 |
|
|
254 |
# |
# |
255 |
# there isn't a classification of anything |
# there isn't a classification of anything |
258 |
if field is None: return |
if field is None: return |
259 |
|
|
260 |
attrs["field"] = field |
attrs["field"] = field |
261 |
|
attrs["field_type"] = str(lc.GetFieldType()) |
262 |
self.open_element("classification", attrs) |
self.open_element("classification", attrs) |
263 |
|
|
264 |
if lc.NullData is not None: |
|
265 |
self.open_element("clnull") |
types = [[lambda p: 'clnull label="%s"' % p.GetLabel(), |
266 |
self.write_element("cldata", lc.NullData) |
lambda p: 'clnull'], |
267 |
self.close_element("clnull") |
[lambda p: 'clpoint label="%s" value="%s"' % |
268 |
|
(p.GetLabel(), str(p.GetValue())), |
269 |
if lc.points != {}: |
lambda p: 'clpoint'], |
270 |
for value, data in lc.points.items(): |
[lambda p: 'clrange label="%s" min="%s" max="%s"' % |
271 |
self.open_element('clpoint value="%s"' % (escape(str(value)))) |
(p.GetLabel(), |
272 |
self.write_element("cldata", data) |
str(p.GetMin()), (str(p.GetMax()))), |
273 |
self.close_element('clpoint') |
lambda p: 'clrange']] |
274 |
|
|
275 |
if lc.ranges != []: |
def write_class_group(group): |
276 |
for p in lc.ranges: |
type = -1 |
277 |
self.open_element('clrange min="%s" max="%s"' |
if isinstance(group, ClassGroupDefault): type = 0 |
278 |
% (escape(str(p[0])), escape(str(p[1])))) |
elif isinstance(group, ClassGroupSingleton): type = 1 |
279 |
self.write_element("cldata", p[2]) |
elif isinstance(group, ClassGroupRange): type = 2 |
280 |
self.close_element('clrange') |
elif isinstance(group, ClassGroupMap): type = 3 |
281 |
|
assert type >= 0 |
282 |
|
|
283 |
|
if type <= 2: |
284 |
|
data = group.GetProperties() |
285 |
|
dict = {'stroke' : data.GetLineColor().hex(), |
286 |
|
'stroke_width': str(data.GetLineWidth()), |
287 |
|
'fill' : data.GetFill().hex()} |
288 |
|
|
289 |
|
self.open_element(types[type][0](group)) |
290 |
|
self.write_element("cldata", dict) |
291 |
|
self.close_element(types[type][1](group)) |
292 |
|
else: pass # XXX: we need to handle maps |
293 |
|
|
294 |
|
for i in lc: |
295 |
|
write_class_group(i) |
296 |
|
|
297 |
self.close_element("classification") |
self.close_element("classification") |
298 |
|
|
318 |
|
|
319 |
The optional argument saver_class is the class to use to serialize |
The optional argument saver_class is the class to use to serialize |
320 |
the session. By default or if it's None, the saver class will be |
the session. By default or if it's None, the saver class will be |
321 |
Saver. |
SessionSaver. |
322 |
|
|
323 |
If writing the session is successful call the session's |
If writing the session is successful call the session's |
324 |
UnsetModified method |
UnsetModified method |
325 |
""" |
""" |
326 |
if saver_class is None: |
if saver_class is None: |
327 |
saver_class = Saver |
saver_class = SessionSaver |
328 |
saver = saver_class(session) |
saver = saver_class(session) |
329 |
saver.write(file) |
saver.write(file) |
330 |
|
|