Annotation of OSAS/OSA_system/OSAS_helpers.py, revision 1.6

1.1       dwinter     1: """ some helpfull methods """
                      2: import xml.dom.minidom
                      3: import os.path
                      4: import re
1.4       dwinter     5: import urllib
                      6: 
                      7: 
                      8: def readArchimedesXML(folder):
                      9:    """gib URL aus """
                     10:         try:
                     11:             XML=urllib.urlopen("http://archimedes.mpiwg-berlin.mpg.de/cgi-bin/toc/toc.cgi?step=xmlcorpusmanifest").read()
                     12:             #print XML
                     13:             dom=xml.dom.minidom.parseString(XML)
                     14:             items=dom.getElementsByTagName('item')
                     15:             dict={}
                     16: 
                     17:             for item in items:
                     18:                     #print item.attributes['dir'].value
                     19:                     try:       
                     20:                             dict[item.attributes['dir'].value]=item.attributes['xml'].value
                     21:                             #print item.attributes['dir'].value,item.attributes['text'].value
                     22:                     except:
                     23:                             """nothing"""
                     24: 
                     25:             if dict.has_key(folder):
                     26:                 return dict[folder]
                     27:             else:
                     28:        return ""
                     29:    except:
                     30:             return ""
                     31: 
                     32:    
1.1       dwinter    33: 
                     34: def getText(nodelist):
                     35:     
                     36:     rc = ""
                     37:     for node in nodelist:
                     38:     
                     39:        if node.nodeType == node.TEXT_NODE:
                     40:            rc = rc + node.data
                     41:     return rc
                     42: 
                     43: def changeNodesInIndexMeta(path,node,subnodes,parent=None,nodeAttributes=None):
                     44:     """node mit subnodes wird in index.meta in path eingetragen bzw. ausgetauscht"""
                     45: 
                     46:     if os.path.exists(path+"/index.meta"):
                     47:         inFile=file(path+"/index.meta")
                     48:         f=inFile.read()
                     49:         inFile.close()
                     50:     else:
1.2       dwinter    51:         f="""<resource type="ECHO" version="1.1"></resource>"""
1.1       dwinter    52:     
                     53:     
                     54:     dom=xml.dom.minidom.parseString(f)
                     55:     root=dom.getElementsByTagName("resource")[0]
                     56:     
                     57:     if parent:
                     58: 
                     59:         try:
                     60:             root=root.getElementsByTagName(parent)[0]
                     61:         except:
                     62:             #print "parent",parent
                     63:             nodeNew=dom.createElement(parent)
                     64:             root.appendChild(nodeNew)
                     65:             root=root.getElementsByTagName(parent)[0]
                     66:     
                     67:     # check if node already exist
                     68: 
                     69: 
                     70:     if node=="":
                     71:         for subnode in subnodes.keys():
                     72: 
1.2       dwinter    73:             nodeOld=root.getElementsByTagName(subnode)
1.3       dwinter    74:             
1.2       dwinter    75:             if nodeOld: # if yes -> delete it
1.3       dwinter    76:                 try:
                     77:                     root.removeChild(nodeOld[0]).unlink()
                     78:                 except:
                     79:                     """nothing"""
1.2       dwinter    80: 
1.1       dwinter    81:             namenode=dom.createElement(re.sub(r' ','-',subnode))
1.5       dwinter    82:             namenodetext=dom.createTextNode(unicode(subnodes[subnode],"utf8"))
1.1       dwinter    83:             namenode.appendChild(namenodetext)
                     84:             root.appendChild(namenode)
                     85: 
                     86:     else:
1.2       dwinter    87:         nodeOld=root.getElementsByTagName(node)
                     88:         
                     89:         if nodeOld: # if yes -> delete it
                     90:             root.removeChild(nodeOld[0]).unlink()
                     91: 
1.1       dwinter    92:         nodeNew=dom.createElement(node) # create new
                     93: 
                     94:         if nodeAttributes:
                     95:             for attribute in nodeAttributes.keys():
                     96:                 #print attribute,nodeAttributes[attribute]
                     97:                 nodeNew.setAttribute(attribute,nodeAttributes[attribute])
                     98: 
                     99:         for subnode in subnodes.keys():
                    100: 
                    101:             namenode=dom.createElement(re.sub(r' ','-',subnode))
1.5       dwinter   102:             namenodetext=dom.createTextNode(unicode(subnodes[subnode],"utf8"))
1.1       dwinter   103:             namenode.appendChild(namenodetext)
                    104:             nodeNew.appendChild(namenode)
                    105: 
                    106: 
                    107:         root.appendChild(nodeNew)
                    108: 
                    109:     writefile=file(path+"/index.meta","w")
                    110:     writefile.write(dom.toxml().encode('utf-8'))
                    111:     writefile.close()
1.2       dwinter   112:     os.chmod(path+"/index.meta",0664)
1.1       dwinter   113: 
                    114: 
                    115:     
                    116:     
                    117:     

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