Skip to content

Commit

Permalink
added metatypes support, repr, and trait_names for introspection in i…
Browse files Browse the repository at this point in the history
…pdb/ipython
  • Loading branch information
caetanus committed Dec 3, 2012
1 parent 7a40f12 commit 95d3205
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion simplexml/simplexml.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,29 @@
except ImportError:
from StringIO import StringIO

class FactoryNode(object):
_classes = {}

@classmethod
def create_class(cls, node, parent):
key = (node, parent)
if not cls._classes.has_key(key):
cls._classes[key] = type(node.tag, (parent,), {})
return cls._classes[key]

@classmethod
def create_instance(cls, node, parent):
return cls.create_class(node, parent)(node)



class XMLTree(object):
def __init__(self, node):
self.nodes = {}
self.node = node
for n in node:
if len(n.getchildren()):
xmlnode = XMLTree(n)
xmlnode = FactoryNode.create_instance(n, XMLTree)
else:
xmlnode = XMLNode(n)
if n.tag in self.nodes:
Expand All @@ -25,6 +40,12 @@ def __init__(self, node):
else:
self.nodes[n.tag] = xmlnode

def trait_names(self):
"""
used to work with tabs in ipython and ipdb
"""
return self.nodes

def __unicode__(self):
return unicode(dict((k, str(v)) for k, v in self.nodes.iteritems()))

Expand All @@ -43,6 +64,13 @@ def __getitem__(self, key):
def __len__(self):
return len(self.nodes)

def __repr__(self):
return unicode(self.node).encode('utf-8')

@property
def tag_name(self):
return unicode(self.node.tag).encode('utf-8')


class XMLNode(object):
def __init__(self, node):
Expand Down

0 comments on commit 95d3205

Please sign in to comment.