""" some helpfull methods """
import xml.dom.minidom
import os.path
import re
def getText(nodelist):
rc = ""
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc = rc + node.data
return rc
def changeNodesInIndexMeta(path,node,subnodes,parent=None,nodeAttributes=None):
"""node mit subnodes wird in index.meta in path eingetragen bzw. ausgetauscht"""
if os.path.exists(path+"/index.meta"):
inFile=file(path+"/index.meta")
f=inFile.read()
inFile.close()
else:
f="""<resource type="ECHO" version="1.1"></resource>"""
dom=xml.dom.minidom.parseString(f)
root=dom.getElementsByTagName("resource")[0]
if parent:
try:
root=root.getElementsByTagName(parent)[0]
except:
#print "parent",parent
nodeNew=dom.createElement(parent)
root.appendChild(nodeNew)
root=root.getElementsByTagName(parent)[0]
# check if node already exist
if node=="":
for subnode in subnodes.keys():
nodeOld=root.getElementsByTagName(subnode)
if nodeOld: # if yes -> delete it
root.removeChild(nodeOld[0]).unlink()
namenode=dom.createElement(re.sub(r' ','-',subnode))
namenodetext=dom.createTextNode(subnodes[subnode].decode("utf8"))
namenode.appendChild(namenodetext)
root.appendChild(namenode)
else:
nodeOld=root.getElementsByTagName(node)
if nodeOld: # if yes -> delete it
root.removeChild(nodeOld[0]).unlink()
nodeNew=dom.createElement(node) # create new
if nodeAttributes:
for attribute in nodeAttributes.keys():
#print attribute,nodeAttributes[attribute]
nodeNew.setAttribute(attribute,nodeAttributes[attribute])
for subnode in subnodes.keys():
namenode=dom.createElement(re.sub(r' ','-',subnode))
namenodetext=dom.createTextNode(subnodes[subnode].decode("utf8"))
namenode.appendChild(namenodetext)
nodeNew.appendChild(namenode)
root.appendChild(nodeNew)
writefile=file(path+"/index.meta","w")
writefile.write(dom.toxml().encode('utf-8'))
writefile.close()
os.chmod(path+"/index.meta",0664)
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>