XmlFormatter.py :  » XML » 4Suite » 4Suite-XML-1.0.2 » Ft » Lib » DistExt » Formatters » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » XML » 4Suite 
4Suite » 4Suite XML 1.0.2 » Ft » Lib » DistExt » Formatters » XmlFormatter.py
import pydoc, cStringIO
from repr import Repr

class XmlRepr(pydoc.TextRepr):
    """
    Class for safely making a XML representation of a Python object.
    """

    _escape_table = [('&', '&'),
                     ('<', '&lt;'),
                     ('>', '&gt;'),
                     ('"', '&quot;'),
                     ]

    def escape(self, text):
        for char, repl in self._escape_table:
            text = text.replace(char, repl)
        return text

    def repr(self, object):
        return self.escape(pydoc.TextRepr.repr(self, object))

    # Make sure all types are covered
    repr_unicode = repr_str = pydoc.TextRepr.repr_string
    

class XmlFormatter(XmlRepr):

    indent = '  '
    document_type = None

    def __init__(self, dist_command):
        XmlRepr.__init__(self)
        self.dist_command = dist_command

        # These are reset each time through document()
        self.module = None
        self.write = None
        self.indent_level = 0
        return

    def warn(self, msg):
        return self.dist_command.warn(msg)

    # -- xml writing functions -----------------------------------------------

    def start_element(self, tagname, attributes={}):
        self.write(self.indent * self.indent_level)
        self.write('<%s' % tagname)
        for name, value in attributes.items():
            self.write(' %s="%s"' % (name, self.escape(str(value))))
        self.write('>\n')
        self.indent_level += 1
        return

    def end_element(self, tagname):
        self.indent_level -= 1
        self.write(self.indent * self.indent_level)
        self.write('</%s>\n' % tagname)
        return

    def write_element(self, tagname, attributes={}, content=''):
        self.write(self.indent * self.indent_level)
        self.write('<%s' % tagname)
        for name, value in attributes.items():
            self.write(' %s="%s"' % (name, self.escape(str(value))))
        if content:
            self.write('>%s</%s>\n' % (content, tagname))
        else:
            self.write('/>\n')
        return

    # -- documenting functions -----------------------------------------------

    def section(self, title, list, format):
        self.start_element(title)
        for name, object in list:
            format(object, name)
        self.end_element(title)
        return

    def format(self, object, stream, encoding=None):
        """
        Print documentation for a Python object to a stream.
        """
        self.module = object
        self.write = stream.write
        self.indent_level = 0
        
        self.write('<?xml version="1.0"')
        if encoding:
            self.write(' encoding="%s"' % encoding)
        self.write('?>\n')
        
        # Document the object
        if self.document_type and not isinstance(object, self.document_type):
            expected = self.document_type.__name__
            compared = object is None and 'None' or type(object).__name__
            raise TypeError("argument must be %s, not %s" % (expected,
                                                             compared))
        self.document(object)
        return

    def document(self, object):
        raise NotImplementedError('subclass %s must override' % self.__class__)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.