http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10013
Revision: 10013 Author: xqt Date: 2012-03-12 14:06:07 +0000 (Mon, 12 Mar 2012) Log Message: ----------- strip trailing whitespace
Modified Paths: -------------- trunk/pywikiparser/ObjectTree/Element.py trunk/pywikiparser/ObjectTree/XMLParse.py trunk/pywikiparser/ObjectTree/__init__.py
Modified: trunk/pywikiparser/ObjectTree/Element.py =================================================================== --- trunk/pywikiparser/ObjectTree/Element.py 2012-03-12 14:04:28 UTC (rev 10012) +++ trunk/pywikiparser/ObjectTree/Element.py 2012-03-12 14:06:07 UTC (rev 10013) @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- +# -*- coding: utf-8 -*- """ Simple object tree system for python. This module contains the Element class @@ -13,7 +13,7 @@ class Element(list): """ Element in the element tree. Usage examples: - + >>> book = Element(u'book', {u'title': u'Wikitext Parsing', u'authors': u'valhallasw'}) >>> chapter = Element(u'chapter', {u'title': u'Wikitext'}) >>> section = chapter.appendElement(u'section', {u'title': u'Basic principles of wikitext'}) @@ -30,14 +30,14 @@ </chapter> </book> """ - + def __init__(self, element_name, element_attributes={}, contents=[]): self.name = element_name self.attributes = element_attributes self.parent = None for item in contents: self.append(item) - + def toxml(self, pretty=False, level=1, symbol=u'\t'): retval = u'<%s' % (self.name,) for (attribute, value) in self.attributes.iteritems(): @@ -53,7 +53,7 @@ else: retval += u'\n' + symbol*level + subelement.toxml(pretty, level+1, symbol) retval += u'\n' + symbol*(level-1) + '</%s>' % (self.name,) - else: + else: for subelement in self: if isinstance(subelement, unicode): retval += xmlify(subelement) @@ -64,10 +64,10 @@ retval += subelement.toxml(pretty, level+1, symbol) retval += u'</%s>' % (self.name,) return retval - + def __repr__(self): return u"<'%s' element: %r %s>" % (self.name, self.attributes, list.__repr__(self)) - + def append(self, arg): if isinstance(arg, basestring): if len(arg) == 0: #don't attach empty strings @@ -84,16 +84,16 @@ arg.parent = self else: raise TypeError(u'Argument is of %r; expected <type 'BaseElement'>.' % (type(arg),)) - + def extend(self, list): for item in list: self.append(item) - + def appendElement(self, *args, **kwargs): element = Element(*args, **kwargs) self.append(element) return element - + def xmlify(data): """ >>> xmlify(u'mooh&<>\'"') @@ -109,6 +109,6 @@ def _test(*args, **kwargs): import doctest doctest.testmod(*args, **kwargs) - + if __name__ == "__main__": - _test() \ No newline at end of file + _test()
Modified: trunk/pywikiparser/ObjectTree/XMLParse.py =================================================================== --- trunk/pywikiparser/ObjectTree/XMLParse.py 2012-03-12 14:04:28 UTC (rev 10012) +++ trunk/pywikiparser/ObjectTree/XMLParse.py 2012-03-12 14:06:07 UTC (rev 10013) @@ -19,10 +19,10 @@ def __init__(self): self.root = Element('root') self.currentNode = self.root - + def startElement(self, name, attrs): self.currentNode = self.currentNode.appendElement(name, dict(attrs.items())) - + def endElement(self, name): if self.currentNode.name == name: self.currentNode = self.currentNode.parent @@ -41,7 +41,7 @@ <'root' element: {} [u'Hello, ', <'bold' element: {} [u'this']>, u' is a test! ', <'link' element: {u'rel': u'blah'} []>]> >>> tree.toxml() u'<root>Hello, <bold>this</bold> is a test! <link rel="blah"/></root>' - + >>> parseText("<root><nonclosed></root>") Traceback (most recent call last): ... @@ -49,4 +49,4 @@ """ handler = XMLParser() xml.sax.parseString(data, handler) - return handler.root[0] \ No newline at end of file + return handler.root[0]
Modified: trunk/pywikiparser/ObjectTree/__init__.py =================================================================== --- trunk/pywikiparser/ObjectTree/__init__.py 2012-03-12 14:04:28 UTC (rev 10012) +++ trunk/pywikiparser/ObjectTree/__init__.py 2012-03-12 14:06:07 UTC (rev 10013) @@ -10,4 +10,4 @@ __all__ = ['Element', 'XMLParse']
from Element import Element -from XMLParse import parseText \ No newline at end of file +from XMLParse import parseText