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

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

Modifications to improve status of author information in DIF citation
group ...

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