# python class to support methods on a DIF ... to conform with # renderEntity etc ... # import ElementTree as ET from Utilities import * from geoUtilities import * from People import * from ServiceBinding import ServiceBinding from renderEntity import renderEntity from ETxmlView import loadET def DIFid2NDGid(string): ''' takes a dif thing parses it and produces an ET ndg element id ... and use this in dif ... ''' s=string.split(':') try: r='''%s %s %s'''%(s[1],s[0],s[2]) return ET.fromstring(r) except: r='''DIF Unknown %s'''%string return ET.fromstring(r) class DIF: ''' Supports the NASA GCMD DIF format for python operations, note ... not a complete implementation, currently minimum to show a reasonable piece of content ''' def __init__(self,xml,renderMethod=renderEntity,serviceFile='serviceMap.config'): '''Initialise a python dif instance based on an xml document ''' self.metadataType='DIF' try: self.elem=loadET(xml) self.xml=xml except: # for some reason we can't parse the document ... raise self.elem=None return self.renderMethod=renderMethod self.type='DIF' self.entryID=wrapGetText(self.elem,'Entry_ID') self.abstract=wrapGetText(self.elem,'Summary') self.name=wrapGetText(self.elem,'Entry_Title') self.binding=ServiceBinding(n=self.name[0:min(5,len(self.name))], nativeID=DIFid2NDGid(self.entryID),serviceFile=serviceFile) #Note that entity.constraints.html is about access control on the metadata, #and so we don't populate this here ... #need entity.parameters, entity.bbox, entity.timeCoverage, entity.curator, entity.creators ... #load up all the information about parameters self.categories=[] self.topics=[] self.terms=[] self.variables=[] self.parameters=[] match={'Category':self.categories,'Topic':self.topics,'Term':self.terms, 'Variable':self.variables,'Detailed_Variable':self.parameters} for level in ['Category','Topic','Term','Variable','Detailed_Variable']: for item in self.elem.findall('Parameters/'+level): value=item.text if value not in match[level]: match[level].append(value) #load up information about spatial bounding box self.bbox=Bounding(self.elem,entity='DIF') #load up information about temporal extent self.timeCoverage=( wrapGetText(self.elem,'Temporal_Coverage/Start_Date'), wrapGetText(self.elem,'Temporal_Coverage/Stop_Date'), wrapGetText(self.elem,'Data_Set_Progress') ) #Data curator information self.centre=DIFcontact(self.elem.find('Data_Center'),ctype='centre') self.curator=DIFcontact(self.elem) #Data Creators self.creators=[] self.services=[] f=file('tmplog.log','a') for item in self.elem.findall('Related_URL'): n=ServiceBinding(self.name,serviceFile=serviceFile) self.services.append(n.related(item,DIFid2NDGid(self.entryID))) f.write(str(self.services[-1])+self.services[-1].icon()) f.close() def toHTML(self): ''' Use the render method (optionally passed at initialisation, or the default - renderEntity - to get an HTML version of the DIF ''' return self.renderMethod.render(self) if __name__=="__main__": f=file('../../exampleD/spade.xml') g=file('../../exampleD/ucar.xml') dif1xml=f.read() dif2xml=g.read() D=DIF(dif1xml,serviceFile='serviceMap.config') G=DIF(dif2xml,serviceFile='serviceMap.config') y=''' %s '''%D.name+D.toHTML()+G.toHTML() f.close() f=file('output.html','wb') f.write(y)