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

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

Handling selection and browse history better ...

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