Changeset 963
- Timestamp:
- 19/05/06 13:19:32 (15 years ago)
- Location:
- TI07-MOLES/trunk/StubB/XSLT/browse/portal/cgi
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
TI07-MOLES/trunk/StubB/XSLT/browse/portal/cgi/DIF.py
r949 r963 16 16 '''Initialise a python dif instance based on an xml document ''' 17 17 self.metadataType='DIF' 18 self.xml=xml 18 19 self.elem=ET.fromstring(xml) 19 20 self.renderMethod=renderMethod -
TI07-MOLES/trunk/StubB/XSLT/browse/portal/cgi/Utilities.py
r959 r963 3 3 import os 4 4 import re 5 6 def cleanup(string):7 '''This is a hack to make sure that xml doesn't contain any naughty & characters alone'''8 #match ? but not (?! if it's followed by alphanumeric \w characters + and a semicolon ;9 return re.sub(r'&(?!\w+;)', '&', string)10 5 11 6 class myConfig: … … 32 27 class Response: 33 28 ''' Holds all the parts of the response to an HTML get or post ''' 34 def __init__(self ):29 def __init__(self,content='text/html'): 35 30 self.headers=[] 36 31 self.cookie=None 37 self.contentType= 'text/html'32 self.contentType=content 38 33 self.content='' 39 34 def write(self,out): … … 73 68 else: return rr[0] 74 69 75 def PrettyPrint(elem,indent='',html=0,space=' '): 76 '''Lightweight pretty printing of elementTree elements''' 77 def estrip(elem): 78 ''' Just want to get rid of unwanted whitespace ''' 79 if elem is None: 80 return '' 81 else: 82 return elem.strip() 83 strAttrib='' 84 for att in elem.attrib: 85 strAttrib+=' %s="%s"'%(att,elem.attrib[att]) 86 result='%s<%s%s>%s'%(indent,elem.tag,strAttrib,estrip(elem.text)) 87 children=len(elem) 88 if children: 89 for item in elem: 90 result+='\n'+PrettyPrint(item,indent=indent+space) 91 result+='\n%s%s</%s>'%(indent,estrip(item.tail),elem.tag) 92 else: 93 result+='</%s>'%(elem.tag) 94 return result 70 95 71 96 def PrettyPrintHTML(elem,matchList=[]): 97 '''Lightweight HTML pretty printing of elementTree elements + highlight 98 any words which occur in the element text (and tails) which occur in matchList, 99 and formatted using a css something like this: 100 === 101 DIV.xmlElem {PADDING-LEFT: 20px;} 102 .xmlAttrVal {COLOR:Red; } 103 .xmlAttrTyp {COLOR:Green; } 104 .xmlElemTag {COLOR:Blue; } 105 . highlight {BACKGROUND-COLOR:Yellow; } 106 === 107 ''' 108 def span(x,c): return '<span class="%s">%s</span>'%(c,x) 109 def div(x,c): return '<div class="%s">%s</div>'%(c,x) 110 def match(x): 111 if x is None: return '' 112 for w in matchList: x=re.sub(w,span(w,'highlight'),x) 113 return x 114 lt,gt='<b><</b>','<b>></b>' 115 strAttrib='' 116 for att in elem.attrib: 117 strAttrib+=' %s="%s"'%(span(att,'xmlAttrTyp'),span(elem.attrib[att],'xmlAttrVal')) 118 result='%s%s%s%s%s'%(lt,span(elem.tag,"xmlElemTag"),strAttrib,gt,match(elem.text)) 119 children=len(elem) 120 if children: 121 for item in elem: 122 result+=PrettyPrintHTML(item) 123 result+='%s%s/%s%s'%(match(item.tail),lt,span(elem.tag,'xmlElemTag'),gt) 124 else: 125 result+='%s/%s%s'%(lt,span(elem.tag,'xmlElemTag'),gt) 126 return div(result,'xmlElem') 72 127 73 128 74 if __name__=="__main__": -
TI07-MOLES/trunk/StubB/XSLT/browse/portal/cgi/browseCGI.py
r938 r963 105 105 return self.error('No valid URI') 106 106 107 self.ViewTextOnly,self.ViewXML=0,0 108 if self.FieldStorage.has_key('text'): 109 self.ViewTextOnly=1 110 elif self.FieldStorage.has_key('xml'): 111 self.ViewXML=1 112 107 113 self.session=BrowseSession(self.cookie,self.uri,self.config) 108 114 if self.config.logfile is not None: self.config.log(self.cookie) … … 129 135 access=1 130 136 if access: 131 content=self.b.toHTML() 137 if self.ViewTextOnly: 138 self.response.contentType='text/text' 139 content=et2text(self.b.tree) 140 elif self.ViewXML=1: 141 content=et2HTML(self.b.tree) 142 else: 143 content=self.b.toHTML() 132 144 else: 133 145 content=self.error('No access to Secure Metadata') 134 146 135 historyHTML='<p>' 136 for item in self.session.getHistory(): 137 historyHTML+='<a href="%s">%s</a><br/>'%item 138 historyHTML+='</p>' 139 self.response.content=renderPage( 147 if not self.ViewTextOnly: 148 historyHTML='<p>' 149 for item in self.session.getHistory(): 150 historyHTML+='<a href="%s">%s</a><br/>'%item 151 historyHTML+='</p>' 152 self.response.content=renderPage( 140 153 content,historyHTML,historyHTML,self.b.name,self.config) 141 return self.response 154 155 return self.response 142 156 143 157 def error(self,message): -
TI07-MOLES/trunk/StubB/XSLT/browse/portal/cgi/renderDiscoveryResponses.py
r949 r963 1 1 from DiscoveryWS import DiscoveryWS 2 2 from DIF import DIF 3 from Utilities import cleanup4 3 5 def render(difSet,summary=0,services=0,spatial=0,temporal=0,linkto='NDG_B_SERVICE'): 4 5 def render(difSet,summary=0,services=0,spatial=0,temporal=0, 6 linkto='NDG_B_SERVICE'): 6 7 '''Takes a set of xml DIFS from a discovery search and renders a list of responses as a table, 7 with layout depending on a set of keywords''' 8 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 name should go to a service binding to this url 14 ''' 15 # 8 16 # List of methods which deal with the actual row content ... 9 columns=['Dataset','Repository'] 10 17 # 11 18 def row(rowList,bgcolor='#EEEEEE'): 12 19 h='<tr bgcolor="%s">'%bgcolor … … 25 32 return '' 26 33 27 def serviceList(elem ):34 def serviceList(elem,linkDefault): 28 35 return '' 29 30 #next three lines not yet used in anger 36 37 # ################################## 38 # Actual html production follows 39 # ################################## 40 41 # depending on keyword options, provide column headings 42 # 43 columns=['Dataset','Repository'] 31 44 if spatial: columns.insert(-1,'Location') 32 45 if temporal: 33 46 #two columns for temporal but put a column header across both columns 34 header='''<table><tr><td colspan="2" >Temporal Coverage</td></tr><tr>35 <td >Start Date</td><td> End Date</td></tr></table>'''47 header='''<table><tr><td colspan="2" align="center">Temporal Coverage</td></tr><tr> 48 <td align="center">Start Date</td><td align="center"> End Date</td></tr></table>''' 36 49 columns.insert(-1,(header,2)) 37 50 if services: columns.insert(-1,'Services') 38 html='<table><tbody>'+row(columns,bgcolor="#FDFFCC") 51 html='<div class="ListOfResults"><table><tbody>'+row(columns,bgcolor="#FDFFCC") 52 53 # ok, now let's cycle through the hits, nb, look and feel should be 54 # factored out to css .. 39 55 40 56 i=1 … … 52 68 rlist.insert(-1,d.timeCoverage[0]) 53 69 rlist.insert(-1,d.timeCoverage[1]) 54 if services: rlist.insert(-1,serviceList(d ))70 if services: rlist.insert(-1,serviceList(d,linkto)) 55 71 56 72 html+=row(rlist,bgcolor=bgc) 57 73 58 html+='</tbody></table> '74 html+='</tbody></table></div>' 59 75 return html 60 76 61 77 if __name__=="__main__": 78 79 from ETxmlView import xmlCleanup 62 80 ws=DiscoveryWS() 63 81 ws.SearchFullText('acsoe') … … 65 83 difs=[] 66 84 for result in results: 67 difs.append( cleanup(result))85 difs.append(xmlCleanup(result)) 68 86 html=render(difs,summary=1,spatial=1,temporal=1) 69 87 f=file('output.html','wb')
Note: See TracChangeset
for help on using the changeset viewer.