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 |
bh |
201 |
import Thuban.Lib.fileutil |
19 |
bh |
6 |
|
20 |
bh |
201 |
def relative_filename(dir, filename): |
21 |
|
|
"""Return a filename relative to dir for the absolute file name absname. |
22 |
|
|
|
23 |
|
|
This is almost the same as the function in fileutil, except that dir |
24 |
|
|
can be an empty string in which case filename will be returned |
25 |
|
|
unchanged. |
26 |
|
|
""" |
27 |
|
|
if dir: |
28 |
|
|
return Thuban.Lib.fileutil.relative_filename(dir, filename) |
29 |
|
|
else: |
30 |
|
|
return filename |
31 |
|
|
|
32 |
bh |
6 |
def escape(data): |
33 |
|
|
"""Escape &, \", ', <, and > in a string of data. |
34 |
|
|
""" |
35 |
|
|
data = string.replace(data, "&", "&") |
36 |
|
|
data = string.replace(data, "<", "<") |
37 |
|
|
data = string.replace(data, ">", ">") |
38 |
|
|
data = string.replace(data, '"', """) |
39 |
|
|
data = string.replace(data, "'", "'") |
40 |
|
|
return data |
41 |
|
|
|
42 |
|
|
def save_session(session, filename): |
43 |
|
|
"""Save the session session to the file given by filename""" |
44 |
|
|
dir = os.path.dirname(filename) |
45 |
|
|
file = open(filename, 'w') |
46 |
|
|
write = file.write |
47 |
|
|
write('<?xml version="1.0" encoding="UTF-8"?>\n') |
48 |
|
|
write('<!DOCTYPE session SYSTEM "thuban.dtd">\n') |
49 |
|
|
write('<session title="%s">\n' % escape(session.title)) |
50 |
|
|
for map in session.Maps(): |
51 |
|
|
write('\t<map title="%s">\n' % escape(map.title)) |
52 |
|
|
if map.projection and len(map.projection.params) > 0: |
53 |
|
|
write('\t\t<projection>\n') |
54 |
|
|
for param in map.projection.params: |
55 |
|
|
write('\t\t\t<parameter value="%s"/>\n' % escape(param)) |
56 |
|
|
write('\t\t</projection>\n') |
57 |
|
|
|
58 |
|
|
for layer in map.Layers(): |
59 |
|
|
fill = layer.fill |
60 |
|
|
if fill is None: |
61 |
|
|
fill = "None" |
62 |
|
|
else: |
63 |
|
|
fill = fill.hex() |
64 |
|
|
stroke = layer.stroke |
65 |
|
|
if stroke is None: |
66 |
|
|
stroke = "None" |
67 |
|
|
else: |
68 |
|
|
stroke = stroke.hex() |
69 |
|
|
write(('\t\t<layer title="%s" filename="%s"' |
70 |
bh |
74 |
' fill="%s" stroke="%s" stroke_width="%d"/>\n') % |
71 |
bh |
6 |
(escape(layer.title), |
72 |
|
|
escape(relative_filename(dir, layer.filename)), |
73 |
bh |
74 |
fill, stroke, layer.stroke_width)) |
74 |
bh |
6 |
labels = map.LabelLayer().Labels() |
75 |
|
|
if labels: |
76 |
|
|
write('\t\t<labellayer>\n') |
77 |
|
|
for label in labels: |
78 |
|
|
write(('\t\t\t<label x="%g" y="%g" text="%s"' |
79 |
|
|
' halign="%s" valign="%s"/>\n') |
80 |
|
|
% (label.x, label.y, label.text, label.halign, |
81 |
|
|
label.valign)) |
82 |
|
|
write('\t\t</labellayer>\n') |
83 |
|
|
write('\t</map>\n') |
84 |
|
|
write('</session>\n') |
85 |
|
|
|
86 |
|
|
# after a successful save consider the session unmodified. |
87 |
|
|
session.UnsetModified() |