source: TI07-MOLES/trunk/StubB/XSLT/browse/portal/cgi/browse/DIF.py @ 1164

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/StubB/XSLT/browse/portal/cgi/browse/DIF.py@1164
Revision 1164, 4.7 KB checked in by lawrence, 13 years ago (diff)

Putting browse code into a module to make it easier
to coexist with other cgi scripts etc ...

Line 
1# python class to support methods on a DIF ... to conform with
2# renderEntity etc ...
3#
4import ElementTree as ET
5from Utilities import *
6from geoUtilities import *
7from People import *
8from ServiceBinding import ServiceBinding
9from renderEntity import renderEntity
10from ETxmlView import loadET
11
12def DIFid2NDGid(string):
13    ''' takes a dif thing parses it and produces an ET ndg element id ...
14    and use this in dif ... '''
15    s=string.split(':')
16    try:
17        r='''<DIFid><schemeIdentifier>%s</schemeIdentifier>
18         <repositoryIdentifier>%s</repositoryIdentifier>
19         <localIdentifier>%s</localIdentifier></DIFid>'''%(s[1],s[0],s[2])
20        return ET.fromstring(r)
21    except:
22        r='''<DIFid><schemeIdentifier>DIF</schemeIdentifier>
23        <repositoryIdentifier>Unknown</repositoryIdentifier>
24        <localIdentifier>%s</localIdentifier></DIFid>'''%string
25        return ET.fromstring(r)
26class DIF:
27    ''' Supports the NASA GCMD DIF format for python operations,
28    note ... not a complete implementation, currently minimum to
29    show a reasonable piece of content '''
30    def __init__(self,xml,renderMethod=renderEntity,serviceFile='serviceMap.config'):
31        '''Initialise a python dif instance based on an xml document '''
32        self.metadataType='DIF'
33        try:
34            self.elem=loadET(xml)
35            self.xml=xml
36        except:
37            # for some reason we can't parse the document ...
38            raise
39            self.elem=None
40            return
41        self.renderMethod=renderMethod
42        self.type='DIF'
43        self.entryID=wrapGetText(self.elem,'Entry_ID')
44       
45       
46        self.abstract=wrapGetText(self.elem,'Summary')
47        self.name=wrapGetText(self.elem,'Entry_Title')
48        self.binding=ServiceBinding(n=self.name[0:min(5,len(self.name))],
49                    nativeID=DIFid2NDGid(self.entryID),serviceFile=serviceFile)
50        #Note that entity.constraints.html is about access control on the metadata,
51        #and so we don't populate this here ...
52       
53        #need entity.parameters, entity.bbox, entity.timeCoverage, entity.curator, entity.creators ...
54       
55        #load up all the information about parameters
56        self.categories=[]
57        self.topics=[]
58        self.terms=[]
59        self.variables=[]
60        self.parameters=[]
61        match={'Category':self.categories,'Topic':self.topics,'Term':self.terms,
62                'Variable':self.variables,'Detailed_Variable':self.parameters}
63        for level in ['Category','Topic','Term','Variable','Detailed_Variable']:
64            for item in self.elem.findall('Parameters/'+level):
65                value=item.text
66                if value not in match[level]: match[level].append(value)
67       
68        #load up information about spatial bounding box
69        self.bbox=Bounding(self.elem,entity='DIF')
70       
71        #load up information about temporal extent
72        self.timeCoverage=(
73            wrapGetText(self.elem,'Temporal_Coverage/Start_Date'),
74            wrapGetText(self.elem,'Temporal_Coverage/Stop_Date'),
75            wrapGetText(self.elem,'Data_Set_Progress') )
76           
77        #Data curator information
78        self.centre=DIFcontact(self.elem.find('Data_Center'),ctype='centre')
79        self.curator=DIFcontact(self.elem)
80           
81        #Data Creators
82        self.creators=[]
83       
84        self.services=[]
85        f=file('tmplog.log','a')
86        for item in self.elem.findall('Related_URL'):
87            n=ServiceBinding(self.name,serviceFile=serviceFile)
88            self.services.append(n.related(item,DIFid2NDGid(self.entryID)))
89            f.write(str(self.services[-1])+self.services[-1].icon())
90        f.close()
91    def toHTML(self):
92        ''' Use the render method (optionally passed at initialisation, or the
93        default - renderEntity - to get an HTML version of the DIF '''
94        return self.renderMethod.render(self)
95       
96           
97if __name__=="__main__":
98   
99    f=file('../../exampleD/spade.xml')
100    g=file('../../exampleD/ucar.xml')
101    dif1xml=f.read()
102    dif2xml=g.read()
103    D=DIF(dif1xml,serviceFile='serviceMap.config')
104    G=DIF(dif2xml,serviceFile='serviceMap.config')
105    y='''<?xml version="1.0" encoding="UTF-8"?>
106                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
107                <html xmlsns="http://www.w3.org/1999/xhtml" xml:lang="en">
108                        <head>
109                                <META http-equiv="Content-Type" content="text/xhtml; charset=iso-8859-1"/>
110                                <title>%s</title>
111                                <LINK media="all, screen" href="../layout/style.css" type="text/css" rel="stylesheet"/>
112                        </head> '''%D.name+D.toHTML()+G.toHTML()
113    f.close()
114    f=file('output.html','wb')
115    f.write(y)
116   
117   
118       
119       
120       
121       
122       
Note: See TracBrowser for help on using the repository browser.