/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/Model/save.py
ViewVC logotype

Annotation of /branches/WIP-pyshapelib-bramz/Thuban/Model/save.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 201 - (hide annotations)
Mon Jul 8 10:50:53 2002 UTC (22 years, 8 months ago) by bh
Original Path: trunk/thuban/Thuban/Model/save.py
File MIME type: text/x-python
File size: 2973 byte(s)
	* Thuban/Model/save.py (relative_filename): Wrapper around
	Thuban.Lib.fileutil.relative_filename that accepts an empty dir
	argument. save_session now automatically uses this version,
	solving a problem when saving to a relative filename.

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, "&", "&amp;")
36     data = string.replace(data, "<", "&lt;")
37     data = string.replace(data, ">", "&gt;")
38     data = string.replace(data, '"', "&quot;")
39     data = string.replace(data, "'", "&apos;")
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()

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26