bernhard: thuban/Extensions/svgexport svgmapwriter.py,1.4,1.5

cvs@intevation.de cvs at intevation.de
Mon Sep 27 11:56:15 CEST 2004


Author: bernhard

Update of /thubanrepository/thuban/Extensions/svgexport
In directory doto:/tmp/cvs-serv1600/Extensions/svgexport

Modified Files:
	svgmapwriter.py 
Log Message:
More fixes to svgexport to make used ids unique and
conforming to XML's Name production. 

* Extensions/svgexport/test/test_svgmapwriter.py: Added new tests
test_xml_id_constraints(), test_make_ide_nosetbaseid() and
test_make_id_nonintegersetid().  Switched SetID and SetBaseID.
Added Bernhard R. as author.
* Extensions/svgexport/svgmapwriter.py (make_id): Using "_" as
concatenation char now (makes test_make_ide_nosetbaseid() valid).
Also transform second id part with "%d" and catch the TypeError
to raise SVGMapWriterError (making test_make_id_nonintegersetid() ok).
Corrected typo inBernhard's author line.
(SetBaseID): Return the transformed base id. Transform characters
which are not alnum() or in ".-_" to binascii.b2a_hex(). Added
import binascii. If to be set string starts with "xml" or so, add "t".
(draw_shape_layer_incrementally): use the returned value of SetBaseID
for used_baseids checks.


Index: svgmapwriter.py
===================================================================
RCS file: /thubanrepository/thuban/Extensions/svgexport/svgmapwriter.py,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- svgmapwriter.py	25 Sep 2004 18:29:24 -0000	1.4
+++ svgmapwriter.py	27 Sep 2004 09:56:13 -0000	1.5
@@ -1,7 +1,7 @@
 # Copyright (c) 2001, 2002, 2003, 2004 by Intevation GmbH
 # Authors:
 #     Markus Rechtien <markus at intevation.de>
-#     Bernhard Reiter <bernhard at intevatin.de>
+#     Bernhard Reiter <bernhard at intevation.de>
 #
 # This program is free software under the GPL (>=v2)
 # Read the file COPYING coming with Thuban for details.
@@ -26,6 +26,8 @@
 from string import join
 # We need to determine some object types
 from types import ListType
+# for SetBaseID
+import binascii 
 
 from Thuban import _
 # VirtualDC extends XMLWriter
@@ -263,12 +265,12 @@
                 self.low_level_renderer(layer)
         tool_cache = {}
         
-        if layer.title in self.used_baseids:
+        new_baseid=dc.SetBaseID(layer.title)
+        if new_baseid in self.used_baseids:
             raise SVGMapWriterError(_("Clash of layer names!\n")+ \
                 _("Two layers probably have the same name, try renaming one."))
         # prefix of a shape id to be unique
-        dc.SetBaseID(layer.title)
-        self.used_baseids.append(layer.title)
+        self.used_baseids.append(new_baseid)
         # Titel of current layer to the groups meta informations
         dc.BeginGroup(meta={'Layer':layer.Title(), })
         # Delete all MetaData
@@ -489,25 +491,50 @@
         except ValueError:
             return (12,len(text) * 10)
     
-    def SetID(self, id):
-        """Set second part of ID stored by the svg elements.
+
+    def SetBaseID(self, id):
+        """Set first part of ID stored by the svg elements. Return it.
 
         Will be used in make_id() as first part of an XML attribute "id".
-        Read comments at SetBaseID() and make_id().
+        The second part is set by SetID().
+        Check comments at make_id().
 
-        Common practice is to only set this to integer numbers.
+        We might add an abritrary "t" for thuban if the parameter
+        starts with XML, which is not allowed in XML 1.0.
+
+        We need to ensure that all characters are okay as XML id attribute.
+        As there seem no easy way in Python (today 20040925) to check
+        for compliance with XML 1.0 character classes like NameChar,
+        we use Python's string method isalnum() as approximation.
+        (See http://mail.python.org/pipermail/xml-sig/2002-January/006981.html
+        and xmlgenchar.py. To be better we would need to hold our own
+        huge table of allowed unicode characters.
+        FIXME if python comes with a better funcation for XML 1.0 NameChar)
+
+        Characters that are not in our approx of NameChar get transformed
+        get escaped to their hex value. 
         """
-        self.id = id
+        # an ID Name shall not start with xml.
+        if id[0:3].lower() == "xml":
+            id = "t" + id
 
-    def SetBaseID(self, id):
-        """Set first part of ID stored by the svg elements.
+        self.baseid = ""
+        for c in id:
+            if c.isalnum() or c in ".-_":
+                self.baseid += c
+            else:
+                self.baseid += binascii.b2a_hex(c)
+        return self.baseid
+
+    def SetID(self, id):
+        """Set second part of ID stored by the svg elements.
 
         Will be used in make_id() as first part of an XML attribute "id".
-        The second part is set by SetID().
-        Check comments at make_id().
+        Only set this to positive integer numbers.
+        Read comments at SetBaseID() and make_id().
         """
-        self.baseid = id
-        
+        self.id = id
+
     def SetFont(self, font):
         """Set the fontproperties to use with text elements."""
         if font is not None:
@@ -597,14 +624,20 @@
         For this it completely depends 
         on what has been set by SetBaseID() and SetID().
         Only call this function if you have called them w unique values before
-        (or SetID(0) to get an empty result)
+        (or negative x in SetID(x) to get an empty result)
+        Will raise SVGMapWriterError if SetID value cannot be converted to %d.
 
         A check of uniqueness in this function might be time consuming,
         because it would require to hold and search through a complete table.
         """
         if self.id < 0:
             return ''
-        return 'id="%s-%s"' % (self.baseid, self.id)
+        try:
+            id= 'id="%s_%d"' % (self.baseid, self.id)
+        except TypeError, inst:
+            raise SVGMapWriterError(_("Internal make_id() failure: ") \
+                        + repr(inst))
+        return id
 
     def DrawEllipse(self, x, y, dx, dy):
         """Draw an ellipse."""





More information about the Thuban-devel mailing list

This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)