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

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

More discovery (from browse) related improvments

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 Service
9from renderEntity import renderEntity
10
11class DIF:
12    ''' Supports the NASA GCMD DIF format for python operations,
13    note ... not a complete implementation, currently minimum to
14    show a reasonable piece of content '''
15    def __init__(self,xml,renderMethod=renderEntity):
16        '''Initialise a python dif instance based on an xml document '''
17        self.metadataType='DIF'
18        self.xml=xml
19        try:
20            self.elem=ET.fromstring(xml)
21        except:
22            # for some reason we can't parse the document ...
23            self.elem=None
24            return
25        self.renderMethod=renderMethod
26        self.type='DIF'
27        self.entryID=wrapGetText(self.elem,'Entry_ID')
28       
29        self.abstract=wrapGetText(self.elem,'Summary')
30        self.name=wrapGetText(self.elem,'Entry_Title')
31       
32        #Note that entity.constraints.html is about access control on the metadata,
33        #and so we don't populate this here ...
34       
35        #need entity.parameters, entity.bbox, entity.timeCoverage, entity.curator, entity.creators ...
36       
37        #load up all the information about parameters
38        self.categories=[]
39        self.topics=[]
40        self.terms=[]
41        self.variables=[]
42        self.parameters=[]
43        match={'Category':self.categories,'Topic':self.topics,'Term':self.terms,
44                'Variable':self.variables,'Detailed_Variable':self.parameters}
45        for level in ['Category','Topic','Term','Variable','Detailed_Variable']:
46            for item in self.elem.findall('Parameters/'+level):
47                value=item.text
48                if value not in match[level]: match[level].append(value)
49       
50        #load up information about spatial bounding box
51        self.bbox=Bounding(self.elem,entity='DIF')
52       
53        #load up information about temporal extent
54        self.timeCoverage=(
55            wrapGetText(self.elem,'Temporal_Coverage/Start_Date'),
56            wrapGetText(self.elem,'Temporal_Coverage/Stop_Date'),
57            wrapGetText(self.elem,'Data_Set_Progress') )
58           
59        #Data curator information
60        self.centre=DIFcontact(self.elem.find('Data_Center'),ctype='centre')
61        self.curator=DIFcontact(self.elem)
62           
63        #Data Creators
64        self.creators=[]
65
66        #should create a class for the service links (i.e. similar to data granules for stubB)
67        #that would include any security info ...
68       
69        self.services={}
70        for item in self.elem.findall('Related_URL'):
71            s=Service()
72            s.name=wrapGetText(item,'URL_Content_Type')
73           
74            s.description=wrapGetText(item,'Description')
75            s.url=wrapGetText(item,'URL')
76            self.services[s.name]=s
77           
78    def toHTML(self):
79        ''' Use the render method (optionally passed at initialisation, or the
80        default - renderEntity - to get an HTML version of the DIF '''
81        return self.renderMethod(self)
82       
83           
84if __name__=="__main__":
85   
86    f=file('../../exampleD/spade.xml')
87    g=file('../../exampleD/ucar.xml')
88    dif1xml=f.read()
89    dif2xml=g.read()
90    D=DIF(cleanup(dif1xml))
91    G=DIF(cleanup(dif2xml))
92    y='''<?xml version="1.0" encoding="UTF-8"?>
93                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
94                <html xmlsns="http://www.w3.org/1999/xhtml" xml:lang="en">
95                        <head>
96                                <META http-equiv="Content-Type" content="text/xhtml; charset=iso-8859-1"/>
97                                <title>%s</title>
98                                <LINK media="all, screen" href="../layout/style.css" type="text/css" rel="stylesheet"/>
99                        </head> '''%D.name+D.toHTML()+G.toHTML()
100    f.close()
101    f=file('output.html','wb')
102    f.write(y)
103   
104   
105       
106       
107       
108       
109       
Note: See TracBrowser for help on using the repository browser.