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

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

More browse/discovery rendering modifications, utilising
Kev's new DIF returning routine ...

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
12
13def DIFid2NDGid(string):
14    ''' takes a dif thing parses it and produces an ET ndg element id ...
15    and use this in dif ... '''
16    s=string.split(':')
17    try:
18        r='''<DIFid><schemeIdentifier>%s</schemeIdentifier>
19         <repositoryIdentifier>%s</repositoryIdentifier>
20         <localIdentifier>%s</localIdentifier></DIFid>'''%(s[1],s[0],s[2])
21        return ET.fromstring(r)
22    except:
23        r='''<DIFid><schemeIdentifier>DIF</schemeIdentifier>
24        <repositoryIdentifier>Unknown</repositoryIdentifier>
25        <localIdentifier>%s</localIdentifier></DIFid>'''%string
26        return ET.fromstring(r)
27class DIF:
28    ''' Supports the NASA GCMD DIF format for python operations,
29    note ... not a complete implementation, currently minimum to
30    show a reasonable piece of content '''
31    def __init__(self,xml,serviceFile='serviceMap.config'):
32        '''Initialise a python dif instance based on an xml document '''
33        self.metadataType='DIF'
34        try:
35            self.elem=loadET(xml)
36            self.xml=xml
37        except:
38            # for some reason we can't parse the document ...
39            raise
40            self.elem=None
41            return
42        self.type='DIF'
43        self.entryID=wrapGetText(self.elem,'Entry_ID')
44       
45        self.abstract=wrapGetText(self.elem,'Summary')
46        self.name=wrapGetText(self.elem,'Entry_Title')
47        self.abbreviation=self.name[0:min(5,len(self.name))]
48        self.binding=ServiceBinding(n=self.abbreviation,
49                    nativeID=DIFid2NDGid(self.entryID),serviceFile=serviceFile)
50        #Note that entity.constraints.html is about access control on the metadata,
51        #and so we don't populate this here ...
52        self.constraints=AccessControl(None)
53       
54        #need entity.parameters, entity.bbox, entity.timeCoverage, entity.curator, entity.creators ...
55       
56        #load up all the information about parameters
57        self.categories=[]
58        self.topics=[]
59        self.terms=[]
60        self.variables=[]
61        self.parameters=[]
62        match={'Category':self.categories,'Topic':self.topics,'Term':self.terms,
63                'Variable':self.variables,'Detailed_Variable':self.parameters}
64        for level in ['Category','Topic','Term','Variable','Detailed_Variable']:
65            for item in self.elem.findall('Parameters/'+level):
66                value=item.text
67                if value not in match[level]: match[level].append(value)
68       
69        #load up information about spatial bounding box
70        self.bbox=Bounding(self.elem,entity='DIF')
71       
72        #load up information about temporal extent
73        self.timeCoverage=(
74            wrapGetText(self.elem,'Temporal_Coverage/Start_Date'),
75            wrapGetText(self.elem,'Temporal_Coverage/Stop_Date'),
76            wrapGetText(self.elem,'Data_Set_Progress') )
77           
78        #Data curator information
79        self.centre=DIFcontact(self.elem.find('Data_Center'),ctype='centre')
80        self.curator=DIFcontact(self.elem)
81           
82        #Data Creators
83        self.creators=[]
84       
85        self.services=[]
86        f=file('tmplog.log','a')
87        for item in self.elem.findall('Related_URL'):
88            n=ServiceBinding(self.name,serviceFile=serviceFile)
89            self.services.append(n.related(item,DIFid2NDGid(self.entryID)))
90            f.write(str(self.services[-1])+self.services[-1].icon())
91        f.close()
92    def toHTML(self,config):
93        if self.elem is not None:
94            renderer=renderEntity(config)
95            return renderer.render(self)
96        else:
97            return '<p>No Valid DIF</p>'
98           
99if __name__=="__main__":
100   
101    f=file('../../exampleD/spade.xml')
102    g=file('../../exampleD/ucar.xml')
103    dif1xml=f.read()
104    dif2xml=g.read()
105    D=DIF(dif1xml,serviceFile='serviceMap.config')
106    G=DIF(dif2xml,serviceFile='serviceMap.config')
107    y='''<?xml version="1.0" encoding="UTF-8"?>
108                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
109                <html xmlsns="http://www.w3.org/1999/xhtml" xml:lang="en">
110                        <head>
111                                <META http-equiv="Content-Type" content="text/xhtml; charset=iso-8859-1"/>
112                                <title>%s</title>
113                                <LINK media="all, screen" href="../layout/style.css" type="text/css" rel="stylesheet"/>
114                        </head> '''%D.name+D.toHTML()+G.toHTML()
115    f.close()
116    f=file('output.html','wb')
117    f.write(y)
118   
119   
120       
121       
122       
123       
124       
Note: See TracBrowser for help on using the repository browser.