1 | from DiscoveryWS import DiscoveryWS,DiscoveryState |
---|
2 | from DIF import DIF |
---|
3 | from htmlUtilities import * |
---|
4 | |
---|
5 | def renderDiscoverySet(difSet,state,selector=None,summary=0,services=0,spatial=0,temporal=0, |
---|
6 | linkto='NDG_B_SERVICE'): |
---|
7 | '''Takes a set of xml DIFS from a discovery search (difSet) and renders a |
---|
8 | list of responses as a table, with layout depending on a set of keywords: |
---|
9 | summary (boolean) - show the abstract, |
---|
10 | services (boolean) - show the service list, |
---|
11 | spatial (boolean) - show the bounding box, |
---|
12 | temporal (boolean) - show the date range, |
---|
13 | linkto (string) - clicking on the select widget should add the url for |
---|
14 | this service to the selected list |
---|
15 | Note that state is a DiscoveryState instance containing the sessionID from the search, |
---|
16 | and the offset, stride, and total number of results ...selector is a class for adding |
---|
17 | particular datasets to a selection dataset (and cookie). |
---|
18 | ''' |
---|
19 | |
---|
20 | # |
---|
21 | # List of methods which deal with the actual row content ... |
---|
22 | # |
---|
23 | |
---|
24 | def row(rowList,bgcolor='rbgWhite'): |
---|
25 | h='<tr class="%s">'%bgcolor |
---|
26 | for item in rowList: |
---|
27 | if type(item)==type((1,2)): |
---|
28 | h+='<td colspan="%s">%s</td>'%(item[1],item[0]) |
---|
29 | else:h+='<td>%s</td>'%item |
---|
30 | h+='</tr>' |
---|
31 | return h#span(h,bgcolor) |
---|
32 | |
---|
33 | def spatialBox(elem): |
---|
34 | return elem.bbox.toHTMLbox() |
---|
35 | |
---|
36 | def temporalRange(elem): |
---|
37 | return '' |
---|
38 | |
---|
39 | def serviceList(d): |
---|
40 | ll=span(' Links: ','ndgem') |
---|
41 | ndg=0 |
---|
42 | ll+=d.binding.icon() |
---|
43 | for item in d.services: |
---|
44 | ll+=', ' |
---|
45 | #s=0 |
---|
46 | if item.serviceType == 'NDG_A_SERVICE': |
---|
47 | ll+=item.icon() |
---|
48 | ndg+=1 |
---|
49 | elif item.serviceType == 'NDG_B_SERVICE': |
---|
50 | ndg+=1 |
---|
51 | ll+=item.icon() |
---|
52 | else: |
---|
53 | ll+=item.icon() |
---|
54 | |
---|
55 | if ndg<2: ndg=0 |
---|
56 | return ndg,ll |
---|
57 | |
---|
58 | |
---|
59 | # ################################## |
---|
60 | # Actual html production follows |
---|
61 | # ################################## |
---|
62 | |
---|
63 | # depending on keyword options, provide column headings |
---|
64 | # |
---|
65 | |
---|
66 | columns=['Dataset',] |
---|
67 | if spatial: columns.append('Location') |
---|
68 | if temporal: |
---|
69 | #two columns for temporal but put a column header across both columns |
---|
70 | header='''<table><tbody><tr><td colspan="2" align="center">Time Coverage</td></tr><tr> |
---|
71 | <td align="center">Start</td><td align="center"> End</td></tr></tbody></table>''' |
---|
72 | columns.append((header,2)) |
---|
73 | html='<div class="ListOfResults"><table><tbody>'+row(columns,bgcolor="rbgBeige") |
---|
74 | |
---|
75 | # ok, now let's cycle through the hits |
---|
76 | i=1 |
---|
77 | for item in difSet: |
---|
78 | d=item # we used to do the diffing here ... but we don't anymore |
---|
79 | bgc={1:'rbgWhite',-1:'rbgGrey'}[i] |
---|
80 | i=-1*i |
---|
81 | |
---|
82 | if d.elem is None: |
---|
83 | html+='<tr class="%s"><td colspan="%s"> Unparseable record </td></tr>'%(bgc,len(columns)) |
---|
84 | else: |
---|
85 | ndg,slist=serviceList(d) |
---|
86 | rlist=[abbreviate(d.name,60),] |
---|
87 | if summary: |
---|
88 | rlist[0]='%s: %s'%(span('Name','ndgem'),rlist[0]) |
---|
89 | rlist[0]+='<br/>%s: %s'%(span('Summary','ndgem'),abbreviate(d.abstract,200)) |
---|
90 | rlist[0]+=' '+hyperlink('(more)',d.binding.url) |
---|
91 | rlist[0]+='<br/>%s: %s.'%(span('Repository','ndgem'),d.centre.toHTML()) |
---|
92 | if not summary: |
---|
93 | rlist[0]+=' '+hyperlink('(more)',d.binding.url) |
---|
94 | if services:rlist[0]+=slist |
---|
95 | if ndg and selector !=None: |
---|
96 | rlist[0]+=', '+selector.target(d.entryID,name=d.abbreviation) |
---|
97 | if spatial: rlist.append(spatialBox(d)) |
---|
98 | if temporal: |
---|
99 | rlist.append(htmlTime(d.timeCoverage[0])) |
---|
100 | rlist.append(htmlTime(d.timeCoverage[1])) |
---|
101 | html+=row(rlist,bgcolor=bgc) |
---|
102 | |
---|
103 | html+='</tbody></table>' |
---|
104 | |
---|
105 | #now work out the links to step through a large result set |
---|
106 | html+='<p>Results %s to %s of %s'%(state.offset,min(state.offset+state.stride-1,state.hits),state.hits) |
---|
107 | if state.stride < state.hits and selector is not None: |
---|
108 | if state.offset+state.stride-1<state.hits: |
---|
109 | next1=state.offset+state.stride |
---|
110 | nextNum=min(next1+state.stride,state.hits)-next1 |
---|
111 | s1='Next %s'%nextNum |
---|
112 | nexturl='%s&start=%s&howmany=%s'%(selector.baseURL,next1,nextNum) |
---|
113 | html+=', %s'%hyperlink(s1,nexturl) |
---|
114 | if state.offset>1: |
---|
115 | last1=state.offset-state.stride |
---|
116 | lastNum=state.stride |
---|
117 | l1='Last %s'%lastNum |
---|
118 | lasturl='%s&start=%s&howmany=%s'%(selector.baseURL,last1,lastNum) |
---|
119 | html+=', %s'%hyperlink(l1,lasturl) |
---|
120 | else: |
---|
121 | html+='</p>' |
---|
122 | html+='</div>' |
---|
123 | return html |
---|
124 | |
---|
125 | if __name__=="__main__": |
---|
126 | |
---|
127 | ws=DiscoveryWS() |
---|
128 | hits=ws.SearchFullText('acsoe') |
---|
129 | results=ws.GetResults(offset=1,number=5) |
---|
130 | difs=[] |
---|
131 | querystring='dummyUrl' |
---|
132 | state=DiscoveryState(ws.sessID,querystring,hits,stride=5) |
---|
133 | #g=file('log.xml','w') |
---|
134 | for result in results: |
---|
135 | #g.write(xmlCleanup(result)+'\n') |
---|
136 | difs.append(DIF(result,serviceFile='serviceMap.config')) |
---|
137 | html=renderDiscoverySet(difs,state,summary=1,spatial=1,temporal=1) |
---|
138 | f=file('output.html','wb') |
---|
139 | f.write(html) |
---|
140 | ws.release() |
---|