* improve piksemel implementation a bit

* add newDocument and parse functions to xmlext (kind of for piks compat.)
* fix: getNode function was b0rked, xmlext test passes now
* switch back to mdom impl. because piks. impl. doesn't work yet
This commit is contained in:
Eray Özkural
2005-11-09 21:06:47 +00:00
parent d978e50bf2
commit e9bd8c943c
7 changed files with 82 additions and 45 deletions
+31 -19
View File
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Project SYSTEM "Project-3.8.dtd">
<!-- Project file for project pisi -->
<!-- Saved: 2005-11-09, 19:48:27 -->
<!-- Saved: 2005-11-09, 22:55:46 -->
<!-- Copyright (C) 2005 PiSi Development Team, -->
<Project version="3.8">
<ProgLanguage mixed="0">Python</ProgLanguage>
@@ -370,32 +370,44 @@
</Source>
<Source>
<Dir>pisi</Dir>
<Dir>xml</Dir>
<Name>xmlextmdom.py</Name>
</Source>
<Source>
<Dir>pisi</Dir>
<Name>xmlext.py</Name>
</Source>
<Source>
<Dir>pisi</Dir>
<Name>xmlextpiks.py</Name>
</Source>
<Source>
<Dir>pisi</Dir>
<Name>xmlfile.py</Name>
</Source>
<Source>
<Dir>pisi</Dir>
<Name>xmlfilepiks.py</Name>
</Source>
<Source>
<Dir>pisi</Dir>
<Dir>xml</Dir>
<Name>xmlfilemdom.py</Name>
</Source>
<Source>
<Dir>pisi</Dir>
<Dir>xml</Dir>
<Name>xmlextpiks.py</Name>
</Source>
<Source>
<Dir>pisi</Dir>
<Dir>xml</Dir>
<Name>xmlfilepiks.py</Name>
</Source>
<Source>
<Dir>pisi</Dir>
<Dir>xml</Dir>
<Name>xmlext.py</Name>
</Source>
<Source>
<Dir>pisi</Dir>
<Dir>xml</Dir>
<Name>autoxml.py</Name>
</Source>
<Source>
<Dir>pisi</Dir>
<Dir>xml</Dir>
<Name>xmlfile.py</Name>
</Source>
<Source>
<Dir>pisi</Dir>
<Dir>xml</Dir>
<Name>__init__.py</Name>
</Source>
</Sources>
<Forms>
</Forms>
@@ -438,9 +450,9 @@
</Vcs>
<FiletypeAssociations>
<FiletypeAssociation pattern="*.ui.h" type="FORMS" />
<FiletypeAssociation pattern="*.ptl" type="SOURCES" />
<FiletypeAssociation pattern="*.idl" type="INTERFACES" />
<FiletypeAssociation pattern="*.ui" type="FORMS" />
<FiletypeAssociation pattern="*.idl" type="INTERFACES" />
<FiletypeAssociation pattern="*.ptl" type="SOURCES" />
<FiletypeAssociation pattern="*.py" type="SOURCES" />
</FiletypeAssociations>
</Project>
+2 -1
View File
@@ -12,7 +12,8 @@
# Authors: Eray Ozkural <eray@uludag.org.tr>
try:
from xmlextpiks import *
#from xmlextpiks import *
zimbabwe
except:
print 'xmlext: piksemel implementation cannot be loaded, falling back to minidom'
from xmlextmdom import *
+35 -13
View File
@@ -15,11 +15,9 @@
"""
xmlext is a helper module for accessing XML files using
xml.dom.minidom.
XmlFile class further abstracts a dom object using the
high-level dom functions provided in xmlext module (and sorely lacking
in xml.dom :( )
xml.dom.minidom . It is a convenient wrapper for some
DOM functions, and provides path based get/add functions
as in KDE API.
function names are mixedCase for compatibility with minidom,
an 'old library'
@@ -32,6 +30,7 @@ __trans = gettext.translation('pisi', fallback=True)
_ = __trans.ugettext
import xml.dom.minidom as mdom
from xml.parsers.expat import ExpatError
import pisi
@@ -39,6 +38,29 @@ class XmlError(pisi.Error):
"named this way because the class if mostly used with an import *"
pass
# Document wrappers
def newDocument(tag):
impl = mdom.getDOMImplementation()
dom = impl.createDocument(None, tag, None)
return dom.documentElement
def parse(filename):
try:
dom = mdom.parse(filename)
return dom.documentElement
except ExpatError, inst:
raise Error(_("File '%s' has invalid XML: %s\n") % (fileName,
str(inst)))
def newNode(node, tag):
return node.ownerDocument.createElement(tag)
def newTextNode(node, text):
return node.ownerDocument.createTextNode(text)
# Node related wrappers
def getNodeAttribute(node, attrname):
"""get named attribute from DOM node"""
if not node.hasAttribute(attrname):
@@ -85,10 +107,16 @@ def getNode(node, tagpath):
# iterative code to search for the path
for tag in tags:
currentNode = None
for child in node.childNodes:
if child.nodeType == node.ELEMENT_NODE and child.tagName == tag:
return child
return None
currentNode = child
break
if not currentNode:
return None
else:
node = currentNode
return currentNode
def getAllNodes(node, tagPath):
"""retrieve all nodes that match a given tag path."""
@@ -165,12 +193,6 @@ def addNode(node, tagpath, newnode = None, branch=True):
return node
def newNode(node, tag):
return node.ownerDocument.createElement(tag)
def newTextNode(node, text):
return node.ownerDocument.createTextNode(text)
def addText(node, tagPath, text, branch = True):
newnode = newTextNode(node, text)
addNode(node, tagPath, newnode, branch)
+7 -5
View File
@@ -154,12 +154,14 @@ def addNode(node, tagpath, newnode = None, branch=True):
return addTagPath(node, tags, newnode)
return node
# cannot be implemented with piksemel
#def newNode(node, tag):
# return Node(tag)
def newNode(node, tag):
return Node(tag)
def newTextNode(node, text):
return Node(text)
# cannot be implemented with piksemel :(
#def newTextNode(node, text):
# return Node(text)
def addText(node, tagPath, text, branch = True):
newnode = newTextNode(node, text)
+3 -2
View File
@@ -12,8 +12,9 @@
# Authors: Eray Ozkural <eray@uludag.org.tr>
try:
from xmlextpiks import *
from xmlfilepiks import *
#from xmlextpiks import *
#from xmlfilepiks import *
horagata
except:
print 'xmlfile: piksemel implementation cannot be loaded, falling back to minidom'
from xmlfilemdom import *
+4 -3
View File
@@ -28,7 +28,8 @@ import gettext
__trans = gettext.translation('pisi', fallback=True)
_ = __trans.ugettext
import xml.dom.minidom as mdom
#import xml.dom.minidom as mdom
from xml.parsers.expat import ExpatError
import codecs
import pisi
@@ -61,8 +62,8 @@ class XmlFile(object):
try:
self.dom = mdom.parse(fileName)
except ExpatError, inst:
raise XmlError(_("File '%s' has invalid XML: %s\n") % (fileName,
str(inst)))
raise Error(_("File '%s' has invalid XML: %s\n") % (fileName,
str(inst)))
def writexml(self, fileName):
f = codecs.open(fileName,'w', "utf-8")
-2
View File
@@ -42,6 +42,4 @@ class XmlExtTestCase(testcase.TestCase):
reada = xmlext.getAllNodes(node, 'team/coder')
self.assertEqual(len(reada), 3)
pass
suite = unittest.makeSuite(XmlExtTestCase)