1: """ some helpfull methods """
2: import xml.dom.minidom
3: import os.path
4: import re
5:
6: def getText(nodelist):
7:
8: rc = ""
9: for node in nodelist:
10:
11: if node.nodeType == node.TEXT_NODE:
12: rc = rc + node.data
13: return rc
14:
15: def changeNodesInIndexMeta(path,node,subnodes,parent=None,nodeAttributes=None):
16: """node mit subnodes wird in index.meta in path eingetragen bzw. ausgetauscht"""
17:
18: if os.path.exists(path+"/index.meta"):
19: inFile=file(path+"/index.meta")
20: f=inFile.read()
21: inFile.close()
22: else:
23: f="""<resource type="ECHO" version="1.1"></resource>"""
24:
25:
26: dom=xml.dom.minidom.parseString(f)
27: root=dom.getElementsByTagName("resource")[0]
28:
29: if parent:
30:
31: try:
32: root=root.getElementsByTagName(parent)[0]
33: except:
34: #print "parent",parent
35: nodeNew=dom.createElement(parent)
36: root.appendChild(nodeNew)
37: root=root.getElementsByTagName(parent)[0]
38:
39: # check if node already exist
40:
41:
42: if node=="":
43: for subnode in subnodes.keys():
44:
45: nodeOld=root.getElementsByTagName(subnode)
46:
47: if nodeOld: # if yes -> delete it
48: root.removeChild(nodeOld[0]).unlink()
49:
50:
51: namenode=dom.createElement(re.sub(r' ','-',subnode))
52: namenodetext=dom.createTextNode(subnodes[subnode].decode("utf8"))
53: namenode.appendChild(namenodetext)
54: root.appendChild(namenode)
55:
56: else:
57: nodeOld=root.getElementsByTagName(node)
58:
59: if nodeOld: # if yes -> delete it
60: root.removeChild(nodeOld[0]).unlink()
61:
62: nodeNew=dom.createElement(node) # create new
63:
64: if nodeAttributes:
65: for attribute in nodeAttributes.keys():
66: #print attribute,nodeAttributes[attribute]
67: nodeNew.setAttribute(attribute,nodeAttributes[attribute])
68:
69: for subnode in subnodes.keys():
70:
71: namenode=dom.createElement(re.sub(r' ','-',subnode))
72: namenodetext=dom.createTextNode(subnodes[subnode].decode("utf8"))
73: namenode.appendChild(namenodetext)
74: nodeNew.appendChild(namenode)
75:
76:
77: root.appendChild(nodeNew)
78:
79: writefile=file(path+"/index.meta","w")
80: writefile.write(dom.toxml().encode('utf-8'))
81: writefile.close()
82: os.chmod(path+"/index.meta",0664)
83:
84:
85:
86:
87:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>