E4XML/XMLWriterBase.py

Sat, 02 Jan 2010 15:11:35 +0000

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 02 Jan 2010 15:11:35 +0000
changeset 12
1d8dd9706f46
parent 0
de9c2efb9d02
child 13
1af94a91f439
permissions
-rw-r--r--

First commit after changing to Python 3.1.

# -*- coding: utf-8 -*-

# Copyright (c) 2004 - 2009 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a base class for all of eric4s XML writers.
"""

import os
import pickle

class XMLWriterBase(object):
    """
    Class implementing a base class for all of eric4s XML writers.
    """
    def __init__(self, file):
        """
        Constructor
        
        @param file open file (like) object for writing
        """
        self.pf = file
        
        self.basics = {
            type(None) : self._write_none,
            type(1)    : self._write_int,
            type(1.1)  : self._write_float,
            type(1+1j) : self._write_complex,
            type(True) : self._write_bool,
            type("")   : self._write_string,
##            UnicodeType : self._write_unicode,  # should be bytes
            type((1,)) : self._write_tuple,
            type([])   : self._write_list,
            type({})   : self._write_dictionary,
            # TODO: add set
        }
        
        self.NEWPARA = chr(0x2029)
        self.NEWLINE = chr(0x2028)
        
    def _write(self, s, newline = True):
        """
        Protected method used to do the real write operation.
        
        @param s string to be written to the XML file
        @param newline flag indicating a linebreak
        """
##        self.pf.write("%s%s" % (s.encode('utf-8'), 
        self.pf.write("%s%s" % (s, 
            newline and os.linesep or ""))
        
    def writeXML(self):
        """
        Public method to write the XML to the file.
        """
        # write the XML header
        self._write('<?xml version="1.0" encoding="UTF-8"?>')
    
    def escape(self, data, attribute=False):
        """
        Function to escape &, <, and > in a string of data.
        
        @param data data to be escaped (string)
        @param attribute flag indicating escaping is done for an attribute
        @return the escaped data (string)
        """
    
        # must do ampersand first
        data = data.replace("&", "&amp;")
        data = data.replace(">", "&gt;")
        data = data.replace("<", "&lt;")
        if attribute:
            data = data.replace('"', "&quot;")
        return data
    
    def encodedNewLines(self, text):
        """
        Public method to encode newlines and paragraph breaks.
        
        @param text text to encode (string or QString)
        """
        return text.replace("\n\n", self.NEWPARA).replace("\n", self.NEWLINE)
    
    def _writeBasics(self, pyobject, indent = 0):
        """
        Protected method to dump an object of a basic Python type.
        
        @param pyobject object to be dumped
        @param indent indentation level for prettier output (integer)
        """
        writeMethod = self.basics.get(type(pyobject)) or self._write_unimplemented
        writeMethod(pyobject, indent)

    ############################################################################
    ## The various writer methods for basic types
    ############################################################################

    def _write_none(self, value, indent):
        """
        Protected method to dump a NoneType object.
        
        @param value value to be dumped (None) (ignored)
        @param indent indentation level for prettier output (integer)
        """
        self._write('%s<none />' % ("  " * indent))
        
    def _write_int(self, value, indent):
        """
        Protected method to dump an IntType object.
        
        @param value value to be dumped (integer)
        @param indent indentation level for prettier output (integer)
        """
        self._write('%s<int>%s</int>' % ("  " * indent, value))
        
    def _write_long(self, value, indent):
        """
        Protected method to dump a LongType object.
        
        @param value value to be dumped (long)
        @param indent indentation level for prettier output (integer)
        """
        self._write('%s<long>%s</long>' % ("  " * indent, value))
        
    def _write_bool(self, value, indent):
        """
        Protected method to dump a BooleanType object.
        
        @param value value to be dumped (boolean)
        @param indent indentation level for prettier output (integer)
        """
        self._write('%s<bool>%s</bool>' % ("  " * indent, value))
        
    def _write_float(self, value, indent):
        """
        Protected method to dump a FloatType object.
        
        @param value value to be dumped (float)
        @param indent indentation level for prettier output (integer)
        """
        self._write('%s<float>%s</float>' % ("  " * indent, value))
        
    def _write_complex(self, value, indent):
        """
        Protected method to dump a ComplexType object.
        
        @param value value to be dumped (complex)
        @param indent indentation level for prettier output (integer)
        """
        self._write('%s<complex>%s %s</complex>' % \
            ("  " * indent, value.real, value.imag))
        
    def _write_string(self, value, indent):
        """
        Protected method to dump a StringType object.
        
        @param value value to be dumped (string)
        @param indent indentation level for prettier output (integer)
        """
        self._write('%s<string>%s</string>' % ("  " * indent, self.escape(value)))
        
    # TODO: add method for writing bytes
    def _write_unicode(self, value, indent):
        """
        Protected method to dump an UnicodeType object.
        
        @param value value to be dumped (unicode)
        @param indent indentation level for prettier output (integer)
        """
        self._write('%s<unicode>%s</unicode>' % ("  " * indent, self.escape(value)))
        
    def _write_tuple(self, value, indent):
        """
        Protected method to dump a TupleType object.
        
        @param value value to be dumped (tuple)
        @param indent indentation level for prettier output (integer)
        """
        self._write('%s<tuple>' % ("  " * indent))
        nindent = indent + 1
        for elem in value:
            self._writeBasics(elem, nindent)
        self._write('%s</tuple>' % ("  " * indent))
        
    def _write_list(self, value, indent):
        """
        Protected method to dump a ListType object.
        
        @param value value to be dumped (list)
        @param indent indentation level for prettier output (integer)
        """
        self._write('%s<list>' % ("  " * indent))
        nindent = indent + 1
        for elem in value:
            self._writeBasics(elem, nindent)
        self._write('%s</list>' % ("  " * indent))
        
    def _write_dictionary(self, value, indent):
        """
        Protected method to dump a DictType object.
        
        @param value value to be dumped (dictionary)
        @param indent indentation level for prettier output (integer)
        """
        self._write('%s<dict>' % ("  " * indent))
        nindent1 = indent + 1
        nindent2 = indent + 2
        keys = sorted(list(value.keys()))
        for key in keys:
            self._write('%s<key>' % ("  " * nindent1))
            self._writeBasics(key, nindent2)
            self._write('%s</key>' % ("  " * nindent1))
            self._write('%s<value>' % ("  " * nindent1))
            self._writeBasics(value[key], nindent2)
            self._write('%s</value>' % ("  " * nindent1))
        self._write('%s</dict>' % ("  " * indent))
        
    def _write_unimplemented(self, value, indent):
        """
        Protected method to dump a type, that has no special method.
        
        @param value value to be dumped (any pickleable object)
        @param indent indentation level for prettier output (integer)
        """
        self._write('%s<pickle method="pickle" encoding="base64">%s</pickle>' % \
            ("  " * indent, pickle.dumps(value).encode('base64')))

eric ide

mercurial