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

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

Browse now modified to use Matt's backend ...

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
75        #services
76        self.services=[]
77        for item in helper.findall(self.tree,'Related_URL'):
78            n=ServiceBinding(self.name,serviceFile=serviceFile)
79            self.services.append(n.fromRelated(item,self.entryID,helper=helper))
80         
81        if self.debug:
82           f=open('difs.log','a')
83           f.write('%s##\n%s\n##################################\n'%(self.entryID,self.xml))
84           f.close()
85           
86           
87    def toHTML(self,config):
88        if self.tree is not None:
89            renderer=renderEntity(config)
90            return renderer.render(self)
91        else:
92            return '<p>No Valid DIF</p>'
93           
94if __name__=="__main__":
95   
96    #f=file('../../../exampleD/spade.xml')
97    f=file('difeg.xml')
98    g=file('../../../exampleD/ucar.xml')
99    dif1xml=f.read()
100    dif2xml=g.read()
101    config=myConfig('browse.config')
102    D=DIF(dif1xml,serviceFile='serviceMap.config')
103    G=DIF(dif2xml,serviceFile='serviceMap.config')
104    y='''<?xml version="1.0" encoding="UTF-8"?>
105                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
106                <html xmlsns="http://www.w3.org/1999/xhtml" xml:lang="en">
107                        <head>
108                                <META http-equiv="Content-Type" content="text/xhtml; charset=iso-8859-1"/>
109                                <title>%s</title>
110                                <LINK media="all, screen" href="../layout/style.css" type="text/css" rel="stylesheet"/>
111                        </head> '''%D.name+D.toHTML(config)+G.toHTML(config)
112    f.close()
113    f=file('output.html','wb')
114    f.write(y)
115    print str(D.binding),D.binding.url
116   
117   
118       
119       
120       
121       
122       
Note: See TracBrowser for help on using the repository browser.