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

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

This has the code to support a temporary patch for
the unicode and orphan html characters problems. Further
details are in ticket:311

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