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

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

Sundry modifications to browse to improve paging through
result sets and remove dependence on Marta's backend for
fulltext searching.

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
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.tree=loadET(xml)
22            self.xml=xml
23        except:
24            # for some reason we can't parse the document ...
25            self.tree=None
26           
27        if self.tree is None: return None
28        helper=nsdumb(self.tree)
29        self.type='DIF'
30        self.entryID=helper.getText(self.tree,'Entry_ID')
31        self.abstract=helper.getText(self.tree,'Summary')
32        self.name=helper.getText(self.tree,'Entry_Title')
33        self.abbreviation=self.name[0:min(5,len(self.name))]
34        self.binding=ServiceBinding(n=self.abbreviation,
35                    entryID=self.entryID,serviceFile=serviceFile)
36       
37        #Note that entity.constraints.html is about access control on the metadata,
38        #and so we don't populate this here ...
39        self.constraints=AccessControl(None)
40       
41        #need entity.parameters, entity.bbox, entity.timeCoverage, entity.curator, entity.creators ...
42       
43        #load up all the information about parameters
44        self.categories=[]
45        self.topics=[]
46        self.terms=[]
47        self.variables=[]
48        self.parameters=[]
49       
50        match={'Category':self.categories,'Topic':self.topics,'Term':self.terms,
51                'Variable':self.variables,'Detailed_Variable':self.parameters}
52               
53        for parameter in helper.findall(self.tree,'Parameters'):
54            name=''
55            for level in ['Category','Topic','Term','Variable','Detailed_Variable']:
56                name+=helper.getText(parameter,level)+'/'
57            self.parameters.append(name.rstrip('/'))
58            #values=helper.getText(self.tree,'Parameters/'+level,multiple=1)
59            #for value in values:
60            #    if value not in match[level]: match[level].append(value)
61       
62        #load up information about spatial bounding box
63        self.bbox=Bounding(self.tree,entity='DIF')
64       
65        #load up information about temporal extent
66        self.timeCoverage=(
67            helper.getText(self.tree,'Temporal_Coverage/Start_Date'),
68            helper.getText(self.tree,'Temporal_Coverage/Stop_Date'),
69            helper.getText(self.tree,'Data_Set_Progress') )
70           
71        #Data curator information
72        self.centre=DIFcontact(helper.find(self.tree,'Data_Center'),ctype='centre',helper=helper)
73        self.curator=DIFcontact(self.tree)
74        f=open('difs.log','a')
75        f.write(self.entryID+'##'+self.centre.toHTML()+'\n'+self.xml+'\n##################################\n')
76        f.close()
77           
78        #Data Creators
79        self.creators=[]
80       
81        self.services=[]
82       
83        for item in helper.findall(self.tree,'Related_URL'):
84            n=ServiceBinding(self.name,serviceFile=serviceFile)
85            self.services.append(n.fromRelated(item,self.entryID,helper=helper))
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.