* first attempt to implement xmlext with piksemel

* a failed attempt, of course :) check out xmlext tests
This commit is contained in:
Eray Özkural
2005-11-09 18:52:39 +00:00
parent 6335479135
commit d3e0c1ee13
5 changed files with 64 additions and 44 deletions
+28 -8
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-08, 18:14:10 -->
<!-- Saved: 2005-11-09, 19:48:27 -->
<!-- Copyright (C) 2005 PiSi Development Team, -->
<Project version="3.8">
<ProgLanguage mixed="0">Python</ProgLanguage>
@@ -174,10 +174,6 @@
<Dir>pisi</Dir>
<Name>build.py</Name>
</Source>
<Source>
<Dir>pisi</Dir>
<Name>xmlfile.py</Name>
</Source>
<Source>
<Dir>pisi</Dir>
<Name>packagedb.py</Name>
@@ -376,6 +372,30 @@
<Dir>pisi</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>
<Name>xmlfilemdom.py</Name>
</Source>
<Source>
<Dir>pisi</Dir>
<Name>autoxml.py</Name>
</Source>
</Sources>
<Forms>
</Forms>
@@ -418,9 +438,9 @@
</Vcs>
<FiletypeAssociations>
<FiletypeAssociation pattern="*.ui.h" type="FORMS" />
<FiletypeAssociation pattern="*.ui" type="FORMS" />
<FiletypeAssociation pattern="*.idl" type="INTERFACES" />
<FiletypeAssociation pattern="*.py" type="SOURCES" />
<FiletypeAssociation pattern="*.ptl" type="SOURCES" />
<FiletypeAssociation pattern="*.idl" type="INTERFACES" />
<FiletypeAssociation pattern="*.ui" type="FORMS" />
<FiletypeAssociation pattern="*.py" type="SOURCES" />
</FiletypeAssociations>
</Project>
+5 -1
View File
@@ -11,4 +11,8 @@
#
# Authors: Eray Ozkural <eray@uludag.org.tr>
from xmlextmdom import *
try:
from xmlextpiks import *
except:
print 'xmlext: piksemel implementation cannot be loaded, falling back to minidom'
from xmlextmdom import *
+12 -16
View File
@@ -27,7 +27,7 @@ __trans = gettext.translation('pisi', fallback=True)
_ = __trans.ugettext
import pisi
import piksemel as iks
from piksemel import *
class XmlError(pisi.Error):
"named this way because the class if mostly used with an import *"
@@ -35,31 +35,28 @@ class XmlError(pisi.Error):
def getNodeAttribute(node, attrname):
"""get named attribute from DOM node"""
if not node.hasAttribute(attrname):
return None
return node.getAttribute(attrname)
def getChildElts(node):
"""get only child elements"""
return filter(lambda x:x.nodeType == x.ELEMENT_NODE, node.childNodes)
return [x for x in parent.childNodes if x.type == TAG ]
def getTagByName(parent, childName):
return [x for x in parent.childNodes
if x.nodeType == x.ELEMENT_NODE and x.tagName == childName]
return [x for x in parent if x.type == TAG and x.name == childName]
def getNodeText(node, tagpath = ""):
"""get the first child and expect it to be text!"""
if tagpath!="":
node = getNode(node, tagpath)
try:
child = node.childNodes[0]
child = node.firstChild()
except IndexError:
return None
except AttributeError: # no node by that name
return None
if child.nodeType == child.TEXT_NODE:
if child.nodeType == CDATA: #FIXME: ??? TEXT type?
# in any case, strip whitespaces...
return child.data.strip()
return child.getTagData().strip()
else:
raise XmlError(_("getNodeText: Expected text node, got something else!"))
@@ -79,8 +76,8 @@ def getNode(node, tagpath):
# iterative code to search for the path
for tag in tags:
for child in node.childNodes:
if child.nodeType == node.ELEMENT_NODE and child.tagName == tag:
for child in node:
if child.type == TAG and child.name == tag:
return child
return None
@@ -112,16 +109,15 @@ def createTagPath(node, tags):
no matter what"""
if len(tags)==0:
return node
dom = node.ownerDocument
for tag in tags:
node = node.appendChild(dom.createElement(tag))
node = node.appendTag(Node(tag))
return node
def addTagPath(node, tags, newnode=None):
"""add newnode at the end of a tag chain, smart one"""
node = createTagPath(node, tags)
if newnode: # node to add specified
node.appendChild(newnode)
node.appendTag(newnode)
return node
def addNode(node, tagpath, newnode = None, branch=True):
@@ -160,10 +156,10 @@ def addNode(node, tagpath, newnode = None, branch=True):
return node
def newNode(node, tag):
return node.ownerDocument.createElement(tag)
return Node(tag)
def newTextNode(node, text):
return node.ownerDocument.createTextNode(text)
return Node(text)
def addText(node, tagPath, text, branch = True):
newnode = newTextNode(node, text)
+6 -1
View File
@@ -11,4 +11,9 @@
#
# Authors: Eray Ozkural <eray@uludag.org.tr>
from xmlfilemdom import *
try:
from xmlextpiks import *
from xmlfilepiks import *
except:
print 'xmlfile: piksemel implementation cannot be loaded, falling back to minidom'
from xmlfilemdom import *
+13 -18
View File
@@ -10,16 +10,12 @@
import unittest
import os
import xml.dom.minidom as mdom
from xml.parsers.expat import ExpatError
import types
import pisi
import pisi.api
from pisi import xmlfile
import pisi.context as ctx
import pisi.util as util
from pisi.xmlext import *
from pisi.xml import xmlext
import testcase
@@ -29,22 +25,21 @@ class XmlExtTestCase(testcase.TestCase):
testcase.TestCase.setUp(self, database=False)
def testGet(self):
self.a = mdom.parse('tests/a.xml')
self.doc = self.a.documentElement
self.assertEqual(getNodeText(self.doc, 'Number'), '868')
self.assertEqual(getNodeText(self.doc, 'OtherInfo/BirthDate'), '18071976')
codeswith = getAllNodes(self.doc, 'OtherInfo/CodesWith/Person')
self.doc = xmlext.parse('tests/a.xml')
#self.doc = self.a.documentElement
self.assertEqual(xmlext.getNodeText(self.doc, 'Number'), '868')
self.assertEqual(xmlext.getNodeText(self.doc, 'OtherInfo/BirthDate'), '18071976')
codeswith = xmlext.getAllNodes(self.doc, 'OtherInfo/CodesWith/Person')
self.assertEqual(len(codeswith), 4)
self.assertEqual(getNodeText(codeswith[2]), 'Caglar')
self.assertEqual(xmlext.getNodeText(codeswith[2]), 'Caglar')
def testAdd(self):
impl = mdom.getDOMImplementation()
a = impl.createDocument(None, 'team', None)
node = a.documentElement
addText(node, 'team/coder', 'zibidi1')
addText(node, 'team/coder', 'zibidi2')
addText(node, 'team/coder', 'zibidi3')
reada = getAllNodes(node, 'team/coder')
node = xmlext.newDocument('team')
#node = a.documentElement
xmlext.addText(node, 'team/coder', 'zibidi1')
xmlext.addText(node, 'team/coder', 'zibidi2')
xmlext.addText(node, 'team/coder', 'zibidi3')
reada = xmlext.getAllNodes(node, 'team/coder')
self.assertEqual(len(reada), 3)
pass