Author: dmeyer
Date: Sun Mar 18 14:01:52 2007
New Revision: 9347
Added:
trunk/core/src/fxdparser2.py
Log:
add a new fxdparser based on minidom
Added: trunk/core/src/fxdparser2.py
==============================================================================
--- (empty file)
+++ trunk/core/src/fxdparser2.py Sun Mar 18 14:01:52 2007
@@ -0,0 +1,102 @@
+import codecs
+from xml.dom import minidom
+
+def _write_data(writer, data):
+ "Writes datachars to writer."
+ data = data.replace("&", "&").replace("<", "<")
+ data = data.replace("\"", """).replace(">", ">")
+ writer.write(data)
+
+class Wrapper(object):
+ def __init__(self, node):
+ self._node = node
+
+ def __getattr__(self, attr):
+ if attr == 'name':
+ return self._node.nodeName
+ if attr == 'getattr':
+ return self._node.getAttribute
+ if attr == 'children':
+ return self
+ return getattr(self._node, attr)
+
+ def __iter__(self):
+ for n in self._node.childNodes:
+ if isinstance(n, minidom.Element):
+ yield Wrapper(n)
+
+ def _get_content(self):
+ if len(self._node.childNodes):
+ return self._node.childNodes[0].data
+ return u''
+
+ def _set_content(self, value):
+ if not isinstance(value, (unicode, str)):
+ value = str(value)
+ node = self._node
+ while node.parentNode:
+ node = node.parentNode
+ node = node.createTextNode(value)
+ self._node.appendChild(node)
+
+ content = property(_get_content, _set_content, None, 'cdata content')
+
+ def add_child(self, name, content=None, **kwargs):
+ node = self._node
+ while node.parentNode:
+ node = node.parentNode
+ node = node.createElement(name)
+ self._node.appendChild(node)
+ node = Wrapper(node)
+ if content is not None:
+ node.content = content
+ for key, value in kwargs.items():
+ node.setAttribute(key, str(value))
+ return node
+
+ def _writexml(self, writer, indent="", addindent="", newl=""):
+ # indent = current indentation
+ # addindent = indentation to add to higher levels
+ # newl = newline string
+ writer.write(indent+"<" + self.tagName)
+
+ attrs = self.attributes
+ a_names = attrs.keys()
+ a_names.sort()
+
+ for a_name in a_names:
+ writer.write(" %s=\"" % a_name)
+ _write_data(writer, attrs[a_name].value)
+ writer.write("\"")
+ if len(self.childNodes) == 1 and \
+ self.childNodes[0].nodeType == self._node.TEXT_NODE:
+ writer.write(">")
+ writer.write(self.childNodes[0].data)
+ writer.write("</%s>%s" % (self.tagName,newl))
+ elif self.childNodes:
+ writer.write(">%s"%(newl))
+ for node in self.children:
+ node._writexml(writer,indent+addindent,addindent,newl)
+ writer.write("%s</%s>%s" % (indent,self.tagName,newl))
+ else:
+ writer.write("/>%s"%(newl))
+
+ def save(self, filename):
+ node = self._node
+ while node.parentNode:
+ node = node.parentNode
+ f = codecs.open(filename, 'w', 'utf8')
+ f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
+ self._writexml(f, '', ' ', '\n')
+
+
+def Document(filename=None):
+ if filename:
+ tree = minidom.parse(filename).firstChild
+ if tree.nodeName != 'freevo':
+ raise RuntimeError('%s is not fxd file' % filename)
+ return Wrapper(tree)
+ doc = minidom.Document()
+ tree = doc.createElement('freevo')
+ doc.appendChild(tree)
+ return Wrapper(tree)
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog