source: TI07-MOLES/trunk/PythonCode/browse/portal/cgi/browse/DIF.py @ 1615

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

Modifications to discovery (and browse) to support ndgDoc as an interim
measure ...

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