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

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

Sundry problems fixed with respect to namespaces etc

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        if self.tree is None: return None
27        helper=nsdumb(self.tree)
28        self.type='DIF'
29        self.entryID=helper.getText(self.tree,'Entry_ID')
30        self.abstract=helper.getText(self.tree,'Summary')
31        self.name=helper.getText(self.tree,'Entry_Title')
32        self.abbreviation=self.name[0:min(5,len(self.name))]
33        self.binding=ServiceBinding(n=self.abbreviation,
34                    entryID=self.entryID,serviceFile=serviceFile)
35        print self.entryID,self.binding.url
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       
49        match={'Category':self.categories,'Topic':self.topics,'Term':self.terms,
50                'Variable':self.variables,'Detailed_Variable':self.parameters}
51               
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            #values=helper.getText(self.tree,'Parameters/'+level,multiple=1)
58            #for value in values:
59            #    if value not in match[level]: match[level].append(value)
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')
72        self.curator=DIFcontact(self.tree)
73           
74        #Data Creators
75        self.creators=[]
76       
77        self.services=[]
78       
79        for item in helper.findall(self.tree,'Related_URL'):
80            n=ServiceBinding(self.name,serviceFile=serviceFile)
81            self.services.append(n.fromRelated(item,self.entryID,helper=helper))
82           
83    def toHTML(self,config):
84        if self.tree is not None:
85            renderer=renderEntity(config)
86            return renderer.render(self)
87        else:
88            return '<p>No Valid DIF</p>'
89           
90if __name__=="__main__":
91   
92    #f=file('../../../exampleD/spade.xml')
93    f=file('difeg.xml')
94    g=file('../../../exampleD/ucar.xml')
95    dif1xml=f.read()
96    dif2xml=g.read()
97    config=myConfig('browse.config')
98    D=DIF(dif1xml,serviceFile='serviceMap.config')
99    G=DIF(dif2xml,serviceFile='serviceMap.config')
100    y='''<?xml version="1.0" encoding="UTF-8"?>
101                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
102                <html xmlsns="http://www.w3.org/1999/xhtml" xml:lang="en">
103                        <head>
104                                <META http-equiv="Content-Type" content="text/xhtml; charset=iso-8859-1"/>
105                                <title>%s</title>
106                                <LINK media="all, screen" href="../layout/style.css" type="text/css" rel="stylesheet"/>
107                        </head> '''%D.name+D.toHTML(config)+G.toHTML(config)
108    f.close()
109    f=file('output.html','wb')
110    f.write(y)
111    print str(D.binding),D.binding.url
112   
113   
114       
115       
116       
117       
118       
Note: See TracBrowser for help on using the repository browser.