File:  [Repository] / zogiLib / xml_helpers.py
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Tue Oct 11 20:06:05 2005 UTC (18 years, 9 months ago) by dwinter
Branches: MAIN
CVS tags: v0_9, Root_v0_9, HEAD
funktionen fuer skalierte bilder hinzugefuegt

    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: def getUniqueElementText(node):
   48: 
   49:         try:
   50:                 return getText(node[0].childNodes)
   51:         except:
   52:                 return ""
   53: def getText(nodelist):
   54:     
   55:     rc = ""
   56:     for node in nodelist:
   57:     
   58:         if node.nodeType == node.TEXT_NODE:
   59:            rc = rc + node.data
   60:     return rc
   61: 
   62: def changeNodesInIndexMeta(path,node,subnodes,parent=None,nodeAttributes=None):
   63:     """node mit subnodes wird in index.meta in path eingetragen bzw. ausgetauscht"""
   64: 
   65:     if os.path.exists(path+"/index.meta"):
   66:         inFile=file(path+"/index.meta")
   67:         f=inFile.read()
   68:         inFile.close()
   69:     else:
   70:         f="""<resource type="ECHO" version="1.1"></resource>"""
   71:     
   72:     
   73:     dom=xml.dom.minidom.parseString(f)
   74:     root=dom.getElementsByTagName("resource")[0]
   75:     
   76:     if parent:
   77: 
   78:         try:
   79:             root=root.getElementsByTagName(parent)[0]
   80:         except:
   81:             #print "parent",parent
   82:             nodeNew=dom.createElement(parent)
   83:             root.appendChild(nodeNew)
   84:             root=root.getElementsByTagName(parent)[0]
   85:     
   86:     # check if node already exist
   87: 
   88: 
   89:     if node=="":
   90:         for subnode in subnodes.keys():
   91: 
   92:             nodeOld=root.getElementsByTagName(subnode)
   93:             
   94:             if nodeOld: # if yes -> delete it
   95:                 try:
   96:                     root.removeChild(nodeOld[0]).unlink()
   97:                 except:
   98:                     """nothing"""
   99: 
  100:             namenode=dom.createElement(re.sub(r' ','-',subnode))
  101:             namenodetext=dom.createTextNode(subnodes[subnode].decode("utf8"))
  102:             namenode.appendChild(namenodetext)
  103:             root.appendChild(namenode)
  104: 
  105:     else:
  106:         nodeOld=root.getElementsByTagName(node)
  107:         
  108:         if nodeOld: # if yes -> delete it
  109:             root.removeChild(nodeOld[0]).unlink()
  110: 
  111:         nodeNew=dom.createElement(node) # create new
  112: 
  113:         if nodeAttributes:
  114:             for attribute in nodeAttributes.keys():
  115:                 #print attribute,nodeAttributes[attribute]
  116:                 nodeNew.setAttribute(attribute,nodeAttributes[attribute])
  117: 
  118:         for subnode in subnodes.keys():
  119: 
  120:             namenode=dom.createElement(re.sub(r' ','-',subnode))
  121:             namenodetext=dom.createTextNode(subnodes[subnode].decode("utf8"))
  122:             namenode.appendChild(namenodetext)
  123:             nodeNew.appendChild(namenode)
  124: 
  125: 
  126:         root.appendChild(nodeNew)
  127: 
  128:     writefile=file(path+"/index.meta","w")
  129:     writefile.write(dom.toxml().encode('utf-8'))
  130:     writefile.close()
  131:     os.chmod(path+"/index.meta",0664)
  132: 
  133: 
  134:     
  135:     
  136:     

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