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