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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 932 by jonathan, Tue May 20 15:23:33 2003 UTC revision 1160 by jonathan, Thu Jun 12 12:40:43 2003 UTC
# Line 14  Functions to save a session to a file Line 14  Functions to save a session to a file
14  __version__ = "$Revision$"  __version__ = "$Revision$"
15    
16  import os  import os
 import string  
   
 from types import UnicodeType  
17    
18  import Thuban.Lib.fileutil  import Thuban.Lib.fileutil
19    
# Line 26  from Thuban.Model.layer import Layer, Ra Line 23  from Thuban.Model.layer import Layer, Ra
23  from Thuban.Model.classification import \  from Thuban.Model.classification import \
24      ClassGroupDefault, ClassGroupSingleton, ClassGroupRange, ClassGroupMap      ClassGroupDefault, ClassGroupSingleton, ClassGroupRange, ClassGroupMap
25    
26  #  from Thuban.Model.xmlwriter import XMLWriter
 # one level of indention  
 #  
 TAB = "    "  
27    
28  def relative_filename(dir, filename):  def relative_filename(dir, filename):
29      """Return a filename relative to dir for the absolute file name absname.      """Return a filename relative to dir for the absolute file name absname.
# Line 43  def relative_filename(dir, filename): Line 37  def relative_filename(dir, filename):
37      else:      else:
38          return filename          return filename
39    
 def escape(data):  
     """Escape &, \", ', <, and > in a string of data.  
     """  
     data = string.replace(data, "&", "&amp;")  
     data = string.replace(data, "<", "&lt;")  
     data = string.replace(data, ">", "&gt;")  
     data = string.replace(data, '"', "&quot;")  
     data = string.replace(data, "'", "&apos;")  
     return data  
   
 class XMLWriter:  
     """Abstract XMLWriter.  
   
     Should be overridden to provide specific object saving functionality.  
     """  
   
     def __init__(self):  
         self.filename = None  
         pass  
   
     def write(self, file_or_filename):  
         """Write the session to a file.  
   
         The argument may be either a file object or a filename. If it's  
         a filename, the file will be opened for writing. Files of  
         shapefiles will be stored as filenames relative to the directory  
         the file is stored in (as given by os.path.dirname(filename)) if  
         they have a common parent directory other than the root  
         directory.  
   
         If the argument is a file object (which is determined by the  
         presence of a write method) all filenames will be absolute  
         filenames.  
         """  
   
         # keep track of how many levels of indentation to write  
         self.indent_level = 0  
         # track whether an element is currently open. see open_element().  
         self.element_open = 0  
   
         if hasattr(file_or_filename, "write"):  
             # it's a file object  
             self.file = file_or_filename  
             self.dir = ""  
         else:  
             self.filename = file_or_filename  
             self.dir = os.path.dirname(self.filename)  
             self.file = open(self.filename, 'w')  
   
     def close(self):  
         assert self.indent_level == 0  
         if self.filename is not None:  
             self.file.close()  
   
     def write_header(self, doctype, system):  
         """Write the XML header"""  
         self.file.write('<?xml version="1.0" encoding="UTF-8"?>\n')  
         self.file.write('<!DOCTYPE %s SYSTEM "%s">\n' % (doctype, system))  
   
     def open_element(self, element, attrs = {}):  
   
         #  
         # we note when an element is opened so that if two open_element()  
         # calls are made successively we can end the currently open  
         # tag and will later write a proper close tag. otherwise,  
         # if a close_element() call is made directly after an open_element()  
         # call we will close the tag with a />  
         #  
         if self.element_open == 1:  
             self.file.write(">\n")  
   
         self.element_open = 1  
   
         # Helper function to write an element open tag with attributes  
         self.file.write("%s<%s" % (TAB*self.indent_level, element))  
         self.__write_attribs(attrs)  
   
         self.indent_level += 1  
   
     def close_element(self, element):  
         self.indent_level -= 1  
         assert self.indent_level >= 0  
   
         # see open_element() for an explanation  
         if self.element_open == 1:  
             self.element_open = 0  
             self.file.write("/>\n")  
         else:  
             self.file.write("%s</%s>\n" % (TAB*self.indent_level, element))  
   
     def write_element(self, element, attrs = {}):  
         """write an element that won't need a closing tag"""  
         self.open_element(element, attrs)  
         self.close_element(element)  
   
     def __write_attribs(self, attrs):  
         for name, value in attrs.items():  
             self.file.write(' %s="%s"' % (self.encode(name),  
                                           self.encode(value)))  
   
     def encode(self, str):  
         """Return an XML-escaped and UTF-8 encoded copy of the string str."""  
   
         esc = escape(str)  
   
         if isinstance(esc, UnicodeType):  
             return esc.encode("utf8")  
         else:  
             return unicode(escape(str),'latin1').encode("utf8")  
   
40  class SessionSaver(XMLWriter):  class SessionSaver(XMLWriter):
41    
42      """Class to serialize a session into an XML file.      """Class to serialize a session into an XML file.

Legend:
Removed from v.932  
changed lines
  Added in v.1160

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26