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

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

mucking around with unicode issues for parsing DIF's ...

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.decode('utf-8','replace')
19        #self.xml=unicode(xml,'latin-1')
20        #self.xml=xml.encode('latin-1','replace')
21 
22        try:
23            self.elem=ET.fromstring(xml)
24        except:
25            # for some reason we can't parse the document ...
26            #print '<p>%s</p>'%xml[11385:11395]
27            #raise bnl
28            #import codecs
29            #f=codecs.open('unparseable.xml','a','utf-8')
30            #f.write(xml+'\n')
31            self.elem=None
32            return
33        self.renderMethod=renderMethod
34        self.type='DIF'
35        self.entryID=wrapGetText(self.elem,'Entry_ID')
36       
37        self.abstract=wrapGetText(self.elem,'Summary')
38        self.name=wrapGetText(self.elem,'Entry_Title')
39       
40        #Note that entity.constraints.html is about access control on the metadata,
41        #and so we don't populate this here ...
42       
43        #need entity.parameters, entity.bbox, entity.timeCoverage, entity.curator, entity.creators ...
44       
45        #load up all the information about parameters
46        self.categories=[]
47        self.topics=[]
48        self.terms=[]
49        self.variables=[]
50        self.parameters=[]
51        match={'Category':self.categories,'Topic':self.topics,'Term':self.terms,
52                'Variable':self.variables,'Detailed_Variable':self.parameters}
53        for level in ['Category','Topic','Term','Variable','Detailed_Variable']:
54            for item in self.elem.findall('Parameters/'+level):
55                value=item.text
56                if value not in match[level]: match[level].append(value)
57       
58        #load up information about spatial bounding box
59        self.bbox=Bounding(self.elem,entity='DIF')
60       
61        #load up information about temporal extent
62        self.timeCoverage=(
63            wrapGetText(self.elem,'Temporal_Coverage/Start_Date'),
64            wrapGetText(self.elem,'Temporal_Coverage/Stop_Date'),
65            wrapGetText(self.elem,'Data_Set_Progress') )
66           
67        #Data curator information
68        self.centre=DIFcontact(self.elem.find('Data_Center'),ctype='centre')
69        self.curator=DIFcontact(self.elem)
70           
71        #Data Creators
72        self.creators=[]
73
74        #should create a class for the service links (i.e. similar to data granules for stubB)
75        #that would include any security info ...
76       
77        self.services={}
78        for item in self.elem.findall('Related_URL'):
79            s=Service()
80            s.name=wrapGetText(item,'URL_Content_Type')
81           
82            s.description=wrapGetText(item,'Description')
83            s.url=wrapGetText(item,'URL')
84            self.services[s.name]=s
85           
86    def toHTML(self):
87        ''' Use the render method (optionally passed at initialisation, or the
88        default - renderEntity - to get an HTML version of the DIF '''
89        return self.renderMethod(self)
90       
91           
92if __name__=="__main__":
93   
94    f=file('../../exampleD/spade.xml')
95    g=file('../../exampleD/ucar.xml')
96    dif1xml=f.read()
97    dif2xml=g.read()
98    D=DIF(cleanup(dif1xml))
99    G=DIF(cleanup(dif2xml))
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()+G.toHTML()
108    f.close()
109    f=file('output.html','wb')
110    f.write(y)
111   
112   
113       
114       
115       
116       
117       
Note: See TracBrowser for help on using the repository browser.