E5XML/XMLWriterBase.py

Fri, 10 Sep 2010 08:54:33 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 10 Sep 2010 08:54:33 +0200
changeset 580
45c38566b001
parent 412
569efd7f2716
permissions
-rw-r--r--

Implemented the plug-in repository reader.

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

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

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

import pickle
import base64

class XMLWriterBase(object):
    """
    Class implementing a base class for all of eric5s 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,
            int        : self._write_int,
            float      : self._write_float,
            complex    : self._write_complex,
            bool       : self._write_bool,
            str        : self._write_string,
            bytes      : self._write_bytes, 
            bytearray  : self._write_bytearray, 
            tuple      : self._write_tuple,
            list       : self._write_list,
            dict       : self._write_dictionary,
            set        : self._write_set, 
            frozenset  : self._write_frozenset, 
        }
        
        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("{0}{1}".format(s, 
            newline and "\n" 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)
        """
        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('{0}<none />'.format("  " * indent))
        
    def _write_int(self, value, indent):
        """
        Protected method to dump an int object.
        
        @param value value to be dumped (integer)
        @param indent indentation level for prettier output (integer)
        """
        self._write('{0}<int>{1}</int>'.format("  " * indent, value))
        
    def _write_bool(self, value, indent):
        """
        Protected method to dump a bool object.
        
        @param value value to be dumped (boolean)
        @param indent indentation level for prettier output (integer)
        """
        self._write('{0}<bool>{1}</bool>'.format("  " * indent, value))
        
    def _write_float(self, value, indent):
        """
        Protected method to dump a float object.
        
        @param value value to be dumped (float)
        @param indent indentation level for prettier output (integer)
        """
        self._write('{0}<float>{1}</float>'.format("  " * indent, value))
        
    def _write_complex(self, value, indent):
        """
        Protected method to dump a complex object.
        
        @param value value to be dumped (complex)
        @param indent indentation level for prettier output (integer)
        """
        self._write('{0}<complex>{1} {2}</complex>'.format(
            "  " * indent, value.real, value.imag))
        
    def _write_string(self, value, indent):
        """
        Protected method to dump a str object.
        
        @param value value to be dumped (string)
        @param indent indentation level for prettier output (integer)
        """
        self._write('{0}<string>{1}</string>'.format("  " * indent, self.escape(value)))
        
    def _write_bytes(self, value, indent):
        """
        Protected method to dump a bytes object.
        
        @param value value to be dumped (bytes)
        @param indent indentation level for prettier output (integer)
        """
        self._write('{0}<bytes>{1}</bytes>'.format(
            " " * indent, ",".join(["{0:d}".format(b) for b in value])))
        
    def _write_bytearray(self, value, indent):
        """
        Protected method to dump a bytearray object.
        
        @param value value to be dumped (bytearray)
        @param indent indentation level for prettier output (integer)
        """
        self._write('{0}<bytearray>{1}</bytearray>'.format(
            " " * indent, ",".join(["{0:d}".format(b) for b in value])))
        
    def _write_tuple(self, value, indent):
        """
        Protected method to dump a tuple object.
        
        @param value value to be dumped (tuple)
        @param indent indentation level for prettier output (integer)
        """
        self._write('{0}<tuple>'.format("  " * indent))
        nindent = indent + 1
        for elem in value:
            self._writeBasics(elem, nindent)
        self._write('{0}</tuple>'.format("  " * indent))
        
    def _write_list(self, value, indent):
        """
        Protected method to dump a list object.
        
        @param value value to be dumped (list)
        @param indent indentation level for prettier output (integer)
        """
        self._write('{0}<list>'.format("  " * indent))
        nindent = indent + 1
        for elem in value:
            self._writeBasics(elem, nindent)
        self._write('{0}</list>'.format("  " * indent))
        
    def _write_dictionary(self, value, indent):
        """
        Protected method to dump a dict object.
        
        @param value value to be dumped (dictionary)
        @param indent indentation level for prettier output (integer)
        """
        self._write('{0}<dict>'.format("  " * indent))
        nindent1 = indent + 1
        nindent2 = indent + 2
        keys = sorted(list(value.keys()))
        for key in keys:
            self._write('{0}<key>'.format("  " * nindent1))
            self._writeBasics(key, nindent2)
            self._write('{0}</key>'.format("  " * nindent1))
            self._write('{0}<value>'.format("  " * nindent1))
            self._writeBasics(value[key], nindent2)
            self._write('{0}</value>'.format("  " * nindent1))
        self._write('{0}</dict>'.format("  " * indent))
        
    def _write_set(self, value, indent):
        """
        Protected method to dump a set object.
        
        @param value value to be dumped (set)
        @param indent indentation level for prettier output (integer)
        """
        self._write('{0}<set>'.format("  " * indent))
        nindent = indent + 1
        for elem in value:
            self._writeBasics(elem, nindent)
        self._write('{0}</set>'.format("  " * indent))
        
    def _write_frozenset(self, value, indent):
        """
        Protected method to dump a frozenset object.
        
        @param value value to be dumped (frozenset)
        @param indent indentation level for prettier output (integer)
        """
        self._write('{0}<frozenset>'.format("  " * indent))
        nindent = indent + 1
        for elem in value:
            self._writeBasics(elem, nindent)
        self._write('{0}</frozenset>'.format("  " * 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('{0}<pickle method="pickle" encoding="base64">{1}</pickle>'.format(
            "  " * indent, str(base64.b64encode(pickle.dumps(value)), "ASCII")))

eric ide

mercurial