source: TI07-MOLES/trunk/PythonCode/wsgi/DIF.py @ 1955

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/PythonCode/wsgi/DIF.py@1955
Revision 1955, 5.5 KB checked in by lawrence, 13 years ago (diff)

More improvements to wsgi discovery, some unit tests, improved
namespace handling in DIF.py ...

Line 
1# python class to support methods on a DIF ... to conform with
2# renderEntity etc ...
3#
4from xml.etree import ElementTree as ET
5from Utilities import *
6from geoUtilities import *
7from People import *
8from ServiceBinding import ServiceBinding
9from AccessControl 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,config,et=0):
18        '''Initialise a python dif instance based on an xml document (expected
19        to be an input string if et=0, otherwise an ElementTree instance) '''
20        self.metadataType='DIF'
21        self.debug=1
22        self.config=config
23        if et:
24            self.tree=xml
25            self.xml=ET.tostring(xml)
26        else:
27            try:
28                self.tree=loadET(xml)
29                self.xml=xml
30            except:
31                # for some reason we can't parse the document ...
32                self.tree=None
33                self.xml=None
34           
35        if self.tree is None: return None
36        helper=nsdumb(self.tree)
37        self.type='DIF'
38        if helper.strip(self.tree.tag)!=self.type: self.tree=helper.find(self.tree,self.type)
39        if self.tree is None: raise TypeError, self.xml
40       
41        self.entryID=helper.getText(self.tree,'Entry_ID')
42        self.abstract=helper.getText(self.tree,'Summary')
43        self.name=helper.getText(self.tree,'Entry_Title')
44        self.abbreviation=self.name[0:min(5,len(self.name))]
45        self.binding=ServiceBinding(config,n=self.abbreviation,
46                    entryID=self.entryID)
47        #Note that entity.constraints.html is about access control on the metadata,
48        #and so we don't populate this here ...
49        self.constraints=AccessControl(None)
50       
51        #need entity.parameters, entity.bbox, entity.timeCoverage, entity.curator, entity.creators
52
53        self.parameters=[]
54        for parameter in helper.findall(self.tree,'Parameters'):
55            name=''
56            for level in ['Category','Topic','Term','Variable','Detailed_Variable']:
57                name+=helper.getText(parameter,level)+'/'
58            self.parameters.append(name.rstrip('/'))
59           
60
61        #load up information about spatial bounding box
62        self.bbox=Bounding(self.tree,entity='DIF',getter=helper.getText)
63       
64        #load up information about temporal extent
65        tc=(
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        self.timeCoverage=TimeCoverage(tc)
70           
71        #Data curator information
72        self.centre=DIFcontact(helper.find(self.tree,'Data_Center'),ctype='centre',helper=helper)
73        self.curator=DIFcontact(self.tree)
74
75        #Data Creators
76        self.creators=[]
77        # use author here because a full dif entry for creator wont necessarily exist in citation ...
78        self.getAuthors(helper)
79        self.date=dateParse(helper.getText(self.tree,'Data_Set_Citation/Dataset_Release_Date'),'YYYY')
80        if self.date=='': self.date='XXXX'
81        self.title=helper.getText(self.tree,'Data_Set_Citation/Dataset_Title')
82        self.briefCitation=None
83        if (self.authors!='' and self.date!='' and self.title!=''):
84            self.briefCitation='%s (%s): %s'%(self.authors,self.date,self.title)
85
86        #services
87        self.services=[]
88        for item in helper.findall(self.tree,'Related_URL'):
89            n=ServiceBinding(config,self.name)
90            self.services.append(n.fromRelated(item,self.entryID,helper=helper))
91         
92        if self.debug:
93           f=open('difs.log','a')
94           f.write('%s##\n%s\n##################################\n'%(self.entryID,self.xml))
95           f.close()
96           
97           
98    def toHTML(self):
99        if self.tree is not None:
100            renderer=renderEntity(self.config)
101            return renderer.render(self)
102        else:
103            return '<p>No Valid DIF</p>'
104
105    def getAuthors(self,helper):
106        ''' Attempt to get dataset authorship information from a DIF '''
107        # first try for an author entry in a citation
108        self.authors=helper.getText(self.tree,'Data_Set_Citation/Dataset_Creator')
109        if self.authors!='': return
110        # ok, now let's look for investigator(s) in the personnel section
111        a=''
112        people=helper.findall(self.tree,'Personnel')
113        for p in people:
114            role=helper.getText(p,'Role')
115            if role=='Investigator':
116                a+='%s %s,'%(helper.getText(p,'First_Name'),helper.getText(p,'Last_Name'))
117        if a!='':a=a[0:-1]
118        self.authors=a
119        print a
120        return
121       
122
123import unittest
124
125class TestCase(unittest.TestCase):
126    """
127    """
128
129    inputFile = 'examples/neodc.eg1.dif'
130    configFile='examples/example.config'
131   
132    def setUp(self):
133        ''' Load example config and DIF files for testing '''
134        f=file(self.inputFile,'r')
135        xml=f.read()
136        config=myConfig(self.configFile)
137        self.dif=DIF(xml,config)
138       
139
140    def testEntryID(self):
141        ''' Testing the DIF object can be loaded an entry ID extracted '''
142        print 'Entry ID [%s]'%self.dif.entryID
143       
144    def testrenderDIF(self):
145        ''' Testing the conversion to html '''
146        print self.dif.timeCoverage
147        html=self.dif.toHTML()
148        g=file('difOutput.html','w')
149        g.write(html)
150
151if __name__=="__main__":
152    unittest.main()
153
154       
155       
Note: See TracBrowser for help on using the repository browser.