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