File:  [Repository] / OSAS / OSA_system / OSAS_helpers.py
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Wed Jan 7 15:29:01 2004 UTC (20 years, 6 months ago) by dwinter
Branches: MAIN
CVS tags: HEAD
bug fixed in archive

    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:                 try:
   49:                     root.removeChild(nodeOld[0]).unlink()
   50:                 except:
   51:                     """nothing"""
   52: 
   53:             namenode=dom.createElement(re.sub(r' ','-',subnode))
   54:             namenodetext=dom.createTextNode(subnodes[subnode].decode("utf8"))
   55:             namenode.appendChild(namenodetext)
   56:             root.appendChild(namenode)
   57: 
   58:     else:
   59:         nodeOld=root.getElementsByTagName(node)
   60:         
   61:         if nodeOld: # if yes -> delete it
   62:             root.removeChild(nodeOld[0]).unlink()
   63: 
   64:         nodeNew=dom.createElement(node) # create new
   65: 
   66:         if nodeAttributes:
   67:             for attribute in nodeAttributes.keys():
   68:                 #print attribute,nodeAttributes[attribute]
   69:                 nodeNew.setAttribute(attribute,nodeAttributes[attribute])
   70: 
   71:         for subnode in subnodes.keys():
   72: 
   73:             namenode=dom.createElement(re.sub(r' ','-',subnode))
   74:             namenodetext=dom.createTextNode(subnodes[subnode].decode("utf8"))
   75:             namenode.appendChild(namenodetext)
   76:             nodeNew.appendChild(namenode)
   77: 
   78: 
   79:         root.appendChild(nodeNew)
   80: 
   81:     writefile=file(path+"/index.meta","w")
   82:     writefile.write(dom.toxml().encode('utf-8'))
   83:     writefile.close()
   84:     os.chmod(path+"/index.meta",0664)
   85: 
   86: 
   87:     
   88:     
   89:     

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>