Annotation of zogiLib/xml_helpers.py, revision 1.1

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

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