source: TI01-discovery/trunk/schema/numsim/renderNumSim.py @ 887

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI01-discovery/trunk/schema/numsim/renderNumSim.py@887
Revision 887, 4.8 KB checked in by lawrence, 13 years ago (diff)

Candidate NumSimV007 (includes schema split to support
model+deployment, an example instance, a completed standalone
html viewer, and oxygen generated documentation)

Line 
1import ElementTree as ET
2#
3# Start with methods for handling the various types we've defined and what they should look like
4#  See useful ideas at http://effbot.org/zone/element-iterparse.htm#plist
5
6class WrapGetText:
7        '''Return the character text tab from any subelement (returns None for
8        missing attributes/subelements unless they begin with __).'''
9        #From http://online.effbot.org/2003_07_01_archive.htm#element-tricks
10        def __init__(self, element):
11            self._element = element
12        def __getattr__(self, tag):
13            if tag.startswith("__"): raise AttributeError(tag)
14            return self._element.findtext(tag)
15
16def _theader(title,span=1):
17        ''' Produces a table header for table block titles '''
18        h='''
19        <table cellspacing="0" cellpadding="3" border="0">
20        <tbody>
21        <tr><td class="lineHead" colspan="%s"><span class="headingO">%s</span></td></tr>'''%(str(span),title)
22        return h
23
24def _wrapAttrib(e,item):
25        ''' Wraps an attribute request appropriately '''
26        a=e.attrib.get(item)
27        h=''
28        if a is not None: h=a
29        return h
30       
31def _hyperlink(e):
32        ''' Takes an element object which has an NS_link attribute
33        set and returns a hyperlink'''
34        href='{http://www.w3.org/1999/xlink}href'
35        title='{http://www.w3.org/1999/xlink}title'
36        try:
37                s='<a href="%s">%s</a>'%(_wrapAttrib(e,href),_wrapAttrib(e,title))
38        except:
39                s=''
40        return s
41       
42def Condition(e):
43        ''' Common elements for initial and boundary condition elements '''
44        if e is None: return ''
45        html='<div id="condition">'
46        ctype=_wrapAttrib(e,'NS_Type')
47        if ctype == 'Unknown': ctype=''
48        if ctype !='': ctype=': '+ctype
49        ptype=_wrapAttrib(e,'NS_Perturbed') #for initial condition
50        if ptype != '': ptype=' ('+ptype+')'
51        html+='<p class="emphatic">%s</p>'% (e.tag+ctype+ptype)
52        html+=Description(e.find('NS_Description'))
53        html+=References(e.find('NS_References'))
54        html+=Parameters(e.find('NS_ControlledParameters'))
55        html+=RelatedTo(e.find('NS_BoundaryDataset'))
56        html+='</div>'
57        return html
58       
59def Parameters(e):
60        if e is None: return ''
61        html='<ul>'
62        for item in e.findall('NS_Value'):
63                html+='<li>%s</li>'%item.text
64        html+='</ul>'
65        return html
66
67def Description(element):
68        '''Takes a description element and formats it appropriately'''
69        # note that there may be between zero and many related URIs
70        if element is None: return ''
71        related=element.findall('NS_RelatedURI')
72        s='<p>'+element.text
73        if len(related)>0: 
74                links=True
75                s+='<ul>'
76                for uri in related:
77                        s+='<li>'+RelatedTo(uri)+'</li>'
78                s+='</ul>'
79        s+='</p>'
80        return s
81       
82def Model(e,div=1,title=None):
83        ''' Parse a model element '''
84        html=''
85        if e is None: return html
86        if title is not None:
87                html+='<h4>%s</h4>'%title
88        if div: html+='<DIV id="SubBlock">'
89        html+=_theader(e.find('NS_Name').text)
90        html+='<tr><td>%s</td></tr>'%Description(e.find('NS_Description'))
91        html+='<tr><td>%s</td></tr>'%References(e.find('NS_References'))
92        html+='<tr><td>%s</td></tr>'%RelatedTo(e.find('NS_RelatedModel'))
93        for item in e.findall('NS_Component'):
94                html+='<tr><td>%s</td></tr>'%Model(item)
95        html+='</tbody></table>'
96        if div: html+='</DIV>'
97        return html
98       
99def References(e):
100        ''' Takes a reference list and formats it (currently doesn't support ISO option)'''
101        if e is None: return ''
102        s='<DIV id="RefList"><p>References:<ul>'
103        for item in e.findall('NS_Reference'):
104                s+='<li>%s</li>'%item.text
105        s+='</ul></p></DIV>'
106        return s
107       
108def RelatedTo(e):
109        '''Return html for a relationship'''
110        if e is None: return ''
111        r=e.find('NS_Relationship')
112        s='<p><span class="emphatic">%s, %s</span>'%(e.tag[3:],_hyperlink(e))
113        if r is not None:s+=': '+r.text
114        s+='</p>'
115        return s
116       
117def Simulated(element):
118       
119        html='<div id="Simulated"><h2>Simulated Data</h2><h3>CodeBase</h3>'
120        html+=Description(element.find('NS_CodeBase/NS_Description'))
121        html+=Model(element.find('NS_CodeBase/NS_Model'),div=0)
122       
123        html+='<h3>Experiment</h3>'
124        html+=Description(element.find('NS_Experiment/NS_Description'))
125        html+=_theader('Constraints')
126        for i in element.findall('NS_Experiment/NS_BoundaryCondition'):html+='<tr><td>%s</td></tr>'%Condition(i)
127        for i in element.findall('NS_Experiment/NS_InitialCondition'):html+='<tr><td>%s</td></tr>'%Condition(i)
128        html+='</tbody></table>'
129        if element.find('NS_Experiment/NS_Ensemble'): 
130                html+='<p> Ensemble Detail html not yet available'
131        html+=RelatedTo(element.find('NS_Experiment/NS_ParentEnsemble'))
132       
133       
134       
135        html+='</div>' #close simulated div
136
137        return html
138       
139if __name__=="__main__":
140        x=ET.parse('HADCM3 PUM 4.5 Beowulf.xml').getroot()
141        html=Simulated(x)
142        print '''<?xml version="1.0" encoding="UTF-8"?>
143                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
144                <html xmlsns="http://www.w3.org/1999/xhtml" xml:lang="en">
145                        <head>
146                                <META http-equiv="Content-Type" content="text/xhtml; charset=iso-8859-1"/>
147                                <title>Browse: %s</title>
148                                <LINK media="all, screen" href="numsim.css" type="text/css" rel="stylesheet"/>
149                        </head>'''%'NS Test'
150        print html
151        print '</html>'
Note: See TracBrowser for help on using the repository browser.