1 |
bh |
74 |
# Copyright (c) 2001, 2002 by Intevation GmbH |
2 |
bh |
6 |
# Authors: |
3 |
|
|
# Jan-Oliver Wagner <[email protected]> |
4 |
|
|
# Bernhard Herzog <[email protected]> |
5 |
|
|
# |
6 |
|
|
# This program is free software under the GPL (>=v2) |
7 |
|
|
# Read the file COPYING coming with Thuban for details. |
8 |
|
|
|
9 |
|
|
""" |
10 |
|
|
Functions to save a session to a file |
11 |
|
|
""" |
12 |
|
|
|
13 |
|
|
__version__ = "$Revision$" |
14 |
|
|
|
15 |
|
|
import os |
16 |
|
|
import string |
17 |
|
|
|
18 |
|
|
from Thuban.Lib.fileutil import relative_filename |
19 |
|
|
|
20 |
|
|
def escape(data): |
21 |
|
|
"""Escape &, \", ', <, and > in a string of data. |
22 |
|
|
""" |
23 |
|
|
data = string.replace(data, "&", "&") |
24 |
|
|
data = string.replace(data, "<", "<") |
25 |
|
|
data = string.replace(data, ">", ">") |
26 |
|
|
data = string.replace(data, '"', """) |
27 |
|
|
data = string.replace(data, "'", "'") |
28 |
|
|
return data |
29 |
|
|
|
30 |
|
|
def save_session(session, filename): |
31 |
|
|
"""Save the session session to the file given by filename""" |
32 |
|
|
dir = os.path.dirname(filename) |
33 |
|
|
file = open(filename, 'w') |
34 |
|
|
write = file.write |
35 |
|
|
write('<?xml version="1.0" encoding="UTF-8"?>\n') |
36 |
|
|
write('<!DOCTYPE session SYSTEM "thuban.dtd">\n') |
37 |
|
|
write('<session title="%s">\n' % escape(session.title)) |
38 |
|
|
for map in session.Maps(): |
39 |
|
|
write('\t<map title="%s">\n' % escape(map.title)) |
40 |
|
|
if map.projection and len(map.projection.params) > 0: |
41 |
|
|
write('\t\t<projection>\n') |
42 |
|
|
for param in map.projection.params: |
43 |
|
|
write('\t\t\t<parameter value="%s"/>\n' % escape(param)) |
44 |
|
|
write('\t\t</projection>\n') |
45 |
|
|
|
46 |
|
|
for layer in map.Layers(): |
47 |
|
|
fill = layer.fill |
48 |
|
|
if fill is None: |
49 |
|
|
fill = "None" |
50 |
|
|
else: |
51 |
|
|
fill = fill.hex() |
52 |
|
|
stroke = layer.stroke |
53 |
|
|
if stroke is None: |
54 |
|
|
stroke = "None" |
55 |
|
|
else: |
56 |
|
|
stroke = stroke.hex() |
57 |
|
|
write(('\t\t<layer title="%s" filename="%s"' |
58 |
bh |
74 |
' fill="%s" stroke="%s" stroke_width="%d"/>\n') % |
59 |
bh |
6 |
(escape(layer.title), |
60 |
|
|
escape(relative_filename(dir, layer.filename)), |
61 |
bh |
74 |
fill, stroke, layer.stroke_width)) |
62 |
bh |
6 |
labels = map.LabelLayer().Labels() |
63 |
|
|
if labels: |
64 |
|
|
write('\t\t<labellayer>\n') |
65 |
|
|
for label in labels: |
66 |
|
|
write(('\t\t\t<label x="%g" y="%g" text="%s"' |
67 |
|
|
' halign="%s" valign="%s"/>\n') |
68 |
|
|
% (label.x, label.y, label.text, label.halign, |
69 |
|
|
label.valign)) |
70 |
|
|
write('\t\t</labellayer>\n') |
71 |
|
|
write('\t</map>\n') |
72 |
|
|
write('</session>\n') |
73 |
|
|
|
74 |
|
|
# after a successful save consider the session unmodified. |
75 |
|
|
session.UnsetModified() |