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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2642 - (hide annotations)
Fri Jul 1 20:49:04 2005 UTC (19 years, 8 months ago) by bh
Original Path: trunk/thuban/Thuban/Model/xmlwriter.py
File MIME type: text/x-python
File size: 4166 byte(s)
First step towards unicode.  With this roughly we're at step 1
string_representation.txt

* Doc/technotes/string_representation.txt: New.  Document how
strings are represented in Thuban and how to get to a Unicode
Thuban.

* Thuban/__init__.py (set_internal_encoding)
(unicode_from_internal, internal_from_unicode): New. The first few
functions for the internal string representation

* Thuban/UI/about.py (unicodeToLocale): Removed.  Use
internal_from_unicode instead.

* Thuban/UI/__init__.py (install_wx_translation): Determine the
encoding to use for the internal string representation.  Also,
change the translation function to return strings in internal
representation even on unicode builds of wxPython

* Thuban/Model/load.py (SessionLoader.check_attrs): Decode
filenames too.
(SessionLoader.start_clrange): Use check_attrs to decode and check
the attributes.

* Thuban/Model/xmlreader.py (XMLReader.encode): Use
internal_from_unicode to convert unicode strings.

* Thuban/Model/xmlwriter.py (XMLWriter.encode): Use
unicode_from_internal when applicable

* test/runtests.py (main): New command line option:
internal-encoding to specify the internal string encoding to use
in the tests.

* test/support.py (initthuban): Set the internal encoding to
latin-1

* test/test_load.py (TestSingleLayer.test, TestClassification.test)
(TestLabelLayer.test): Use the internal string representation when
dealing with non-ascii characters

* test/test_load_1_0.py (TestSingleLayer.test)
(TestClassification.test, TestLabelLayer.test): Use the internal
string representation when dealing with non-ascii characters

* test/test_load_0_9.py (TestSingleLayer.test)
(TestClassification.test): Use the internal string representation
when dealing with non-ascii characters

* test/test_load_0_8.py (TestUnicodeStrings.test): Use the
internal string representation when dealing with non-ascii
characters

* test/test_save.py (XMLWriterTest.testEncode)
(SaveSessionTest.testClassifiedLayer): Use the internal string
representation when dealing with non-ascii characters where
applicable

1 bh 2642 # Copyright (c) 2003, 2005 by Intevation GmbH
2 jonathan 1171 # Authors:
3     # Jonathan Coles <[email protected]>
4     #
5     # This program is free software under the GPL (>=v2)
6     # Read the file COPYING coming with Thuban for details.
7    
8     """
9     Functions to save an XML file
10     """
11    
12     __version__ = "$Revision$"
13    
14 jan 1886 import os
15 jonathan 1171 from types import UnicodeType
16    
17 bh 2642 from Thuban import unicode_from_internal
18    
19 jonathan 1171 #
20     # one level of indention
21     #
22     TAB = " "
23    
24     def escape(data):
25     """Escape &, \", ', <, and > in a string of data.
26     """
27 jan 1886 data = data.replace("&", "&amp;")
28     data = data.replace("<", "&lt;")
29     data = data.replace(">", "&gt;")
30     data = data.replace('"', "&quot;")
31     data = data.replace("'", "&apos;")
32 jonathan 1171 return data
33    
34     class XMLWriter:
35     """Abstract XMLWriter.
36    
37     Should be overridden to provide specific object saving functionality.
38     """
39    
40     def __init__(self):
41     self.filename = None
42     pass
43    
44     def write(self, file_or_filename):
45     """Write the session to a file.
46    
47     The argument may be either a file object or a filename. If it's
48     a filename, the file will be opened for writing. Files of
49     shapefiles will be stored as filenames relative to the directory
50     the file is stored in (as given by os.path.dirname(filename)) if
51     they have a common parent directory other than the root
52     directory.
53    
54     If the argument is a file object (which is determined by the
55     presence of a write method) all filenames will be absolute
56     filenames.
57     """
58    
59     # keep track of how many levels of indentation to write
60     self.indent_level = 0
61     # track whether an element is currently open. see open_element().
62     self.element_open = 0
63    
64     if hasattr(file_or_filename, "write"):
65     # it's a file object
66     self.file = file_or_filename
67     self.dir = ""
68     else:
69     self.filename = file_or_filename
70     self.dir = os.path.dirname(self.filename)
71     self.file = open(self.filename, 'w')
72    
73     def close(self):
74     assert self.indent_level == 0
75     if self.filename is not None:
76     self.file.close()
77    
78     def write_header(self, doctype, system):
79     """Write the XML header"""
80     self.file.write('<?xml version="1.0" encoding="UTF-8"?>\n')
81     self.file.write('<!DOCTYPE %s SYSTEM "%s">\n' % (doctype, system))
82    
83     def open_element(self, element, attrs = {}):
84    
85     #
86     # we note when an element is opened so that if two open_element()
87     # calls are made successively we can end the currently open
88     # tag and will later write a proper close tag. otherwise,
89     # if a close_element() call is made directly after an open_element()
90     # call we will close the tag with a />
91     #
92     if self.element_open == 1:
93     self.file.write(">\n")
94    
95     self.element_open = 1
96    
97     # Helper function to write an element open tag with attributes
98     self.file.write("%s<%s" % (TAB*self.indent_level, element))
99     self.__write_attribs(attrs)
100    
101     self.indent_level += 1
102    
103     def close_element(self, element):
104     self.indent_level -= 1
105     assert self.indent_level >= 0
106    
107     # see open_element() for an explanation
108     if self.element_open == 1:
109     self.element_open = 0
110     self.file.write("/>\n")
111     else:
112     self.file.write("%s</%s>\n" % (TAB*self.indent_level, element))
113    
114     def write_element(self, element, attrs = {}):
115     """write an element that won't need a closing tag"""
116     self.open_element(element, attrs)
117     self.close_element(element)
118    
119     def __write_attribs(self, attrs):
120     for name, value in attrs.items():
121     self.file.write(' %s="%s"' % (self.encode(name),
122     self.encode(value)))
123    
124 bh 2642 def encode(self, s):
125     """Return an XML-escaped and UTF-8 encoded copy of the string s.
126 jonathan 1171
127 bh 2642 The parameter must be a string in Thuban's internal string
128     representation or a unicode object.
129     """
130     if not isinstance(s, unicode):
131     s = unicode_from_internal(s)
132     return escape(s).encode("utf8")

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26