1 | # python class to support methods on a DIF ... to conform with |
---|
2 | # renderEntity etc ... |
---|
3 | # |
---|
4 | import ElementTree as ET |
---|
5 | from Utilities import * |
---|
6 | from geoUtilities import * |
---|
7 | from People import * |
---|
8 | from ServiceBinding import ServiceBinding |
---|
9 | from secure import AccessControl |
---|
10 | from renderEntity import renderEntity |
---|
11 | from ETxmlView import loadET |
---|
12 | |
---|
13 | class 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,serviceFile='serviceMap.config'): |
---|
18 | '''Initialise a python dif instance based on an xml document ''' |
---|
19 | self.metadataType='DIF' |
---|
20 | try: |
---|
21 | self.elem=loadET(xml) |
---|
22 | self.xml=xml |
---|
23 | except: |
---|
24 | # for some reason we can't parse the document ... |
---|
25 | raise |
---|
26 | self.elem=None |
---|
27 | return |
---|
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 | self.abbreviation=self.name[0:min(5,len(self.name))] |
---|
34 | self.binding=ServiceBinding(n=self.abbreviation, |
---|
35 | nativeID=DIFid2NDGid(self.entryID),serviceFile=serviceFile) |
---|
36 | #Note that entity.constraints.html is about access control on the metadata, |
---|
37 | #and so we don't populate this here ... |
---|
38 | self.constraints=AccessControl(None) |
---|
39 | |
---|
40 | #need entity.parameters, entity.bbox, entity.timeCoverage, entity.curator, entity.creators ... |
---|
41 | |
---|
42 | #load up all the information about parameters |
---|
43 | self.categories=[] |
---|
44 | self.topics=[] |
---|
45 | self.terms=[] |
---|
46 | self.variables=[] |
---|
47 | self.parameters=[] |
---|
48 | match={'Category':self.categories,'Topic':self.topics,'Term':self.terms, |
---|
49 | 'Variable':self.variables,'Detailed_Variable':self.parameters} |
---|
50 | for level in ['Category','Topic','Term','Variable','Detailed_Variable']: |
---|
51 | for item in self.elem.findall('Parameters/'+level): |
---|
52 | value=item.text |
---|
53 | if value not in match[level]: match[level].append(value) |
---|
54 | |
---|
55 | #load up information about spatial bounding box |
---|
56 | self.bbox=Bounding(self.elem,entity='DIF') |
---|
57 | |
---|
58 | #load up information about temporal extent |
---|
59 | self.timeCoverage=( |
---|
60 | wrapGetText(self.elem,'Temporal_Coverage/Start_Date'), |
---|
61 | wrapGetText(self.elem,'Temporal_Coverage/Stop_Date'), |
---|
62 | wrapGetText(self.elem,'Data_Set_Progress') ) |
---|
63 | |
---|
64 | #Data curator information |
---|
65 | self.centre=DIFcontact(self.elem.find('Data_Center'),ctype='centre') |
---|
66 | self.curator=DIFcontact(self.elem) |
---|
67 | |
---|
68 | #Data Creators |
---|
69 | self.creators=[] |
---|
70 | |
---|
71 | self.services=[] |
---|
72 | f=file('tmplog.log','a') |
---|
73 | for item in self.elem.findall('Related_URL'): |
---|
74 | n=ServiceBinding(self.name,serviceFile=serviceFile) |
---|
75 | self.services.append(n.related(item,DIFid2NDGid(self.entryID))) |
---|
76 | f.write(str(self.services[-1])+self.services[-1].icon()) |
---|
77 | f.close() |
---|
78 | def toHTML(self,config): |
---|
79 | if self.elem is not None: |
---|
80 | renderer=renderEntity(config) |
---|
81 | return renderer.render(self) |
---|
82 | else: |
---|
83 | return '<p>No Valid DIF</p>' |
---|
84 | |
---|
85 | if __name__=="__main__": |
---|
86 | |
---|
87 | f=file('../../../exampleD/spade.xml') |
---|
88 | g=file('../../../exampleD/ucar.xml') |
---|
89 | dif1xml=f.read() |
---|
90 | dif2xml=g.read() |
---|
91 | config=myConfig('browse.config') |
---|
92 | D=DIF(dif1xml,serviceFile='serviceMap.config') |
---|
93 | G=DIF(dif2xml,serviceFile='serviceMap.config') |
---|
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(config)+G.toHTML(config) |
---|
102 | f.close() |
---|
103 | f=file('output.html','wb') |
---|
104 | f.write(y) |
---|
105 | print str(D.binding),D.binding.url |
---|
106 | |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | |
---|
111 | |
---|
112 | |
---|