source: TI07-MOLES/trunk/StubB/XSLT/browse/portal/cgi/DiscoveryWS.py @ 1143

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/StubB/XSLT/browse/portal/cgi/DiscoveryWS.py@1143
Revision 1143, 4.5 KB checked in by lawrence, 13 years ago (diff)

Sundry errors fixed to support browse under apache ...

Line 
1#/usr/bin/env python
2from ZSI import ServiceProxy
3from ZSI.wstools.WSDLTools import WSDLReader
4import sys
5import os
6import pdb
7
8# This version known to work (in part) with ZSI 1.6 patched for NDG ...
9
10DEBUG=0
11
12class DiscoveryState:
13    ''' This class holds the state associated with presenting multiple slices
14    of a large result set '''
15    def __init__(self,sessionID,queryURL,hits,stride=10):
16        ''' On instantiation, provide
17                the backend sessionID
18                the application URL that produced this query
19                the stride through the result set '''
20        self.queryURL=queryURL
21        self.sessionID=sessionID
22        self.hits=hits
23        self.offset=1
24        self.stride=10
25
26class DiscoveryWS:
27   
28    ''' This is the class which provides an interface to the NDG web discovery
29    web services '''
30   
31    def __init__(self,
32        wsdl='http://superglue.badc.rl.ac.uk/exist/services/Discovery?WSDL',
33        tracefile=None,
34        user=None,
35        passwd=None):
36        ''' Establish connection to wsdl proxy at self.srv with self.sessID '''
37
38        #try:
39        wr=WSDLReader()
40        self.srv=ServiceProxy(wsdl,use_wsdl=True,tracefile=tracefile)
41        if user is not None:
42            conResp=self.srv.connect(in0=user,in1=passwd)
43        else:
44            conResp=self.srv.connect()
45        self.sessID=conResp['connectReturn']
46        if DEBUG: print 'Connection Made to Session: ',self.sessID
47
48    def SearchFullText(self,query):
49        ''' Produce a result set from a full text query on the NDG database '''
50        #known to work.
51        result = self.srv.fullText(in0=self.sessID, in1=query)
52        self.result=result['fullTextReturn']
53        if DEBUG: self.__performance()
54        return self.result['hits']
55
56    def Xquery(self,query):
57        ''' Create a result set based on an xquery over the database '''
58        #this doesn't work properly ...
59        result=self.srv.xquery(in0=query,in1=self.sessID)
60
61    def SearchBoundingBox(self,bbox):
62        ''' Obtain a result set from a spatial search using a bounding box '''
63        print bbox
64        result = self.srv.spatialSearch(in0=self.sessID,
65               in1=bbox[0],in2=bbox[1],in3=bbox[2],in4=bbox[3])
66        print 'got a spatial result'
67        self.result=result['spatialSearchReturn']
68
69    def SearchTemporal(self,date1,date2):
70        ''' Obtain  a reset set from a temporal search using a standard
71        calendar '''
72        ok=self.__checkdates(date1)
73        ok=self.__checkdates(date2)
74        result=self.srv.temporalSearch(in0=self.sessID,in1=date1,in2=date2)
75        self.result=result['temporalSearchReturn']
76       
77    def GetResults(self,
78            number=1, # number of records to return
79            offset=1, # offset index within result set (begins at 1)
80            highlight=u'none',  # string to highlight in result set
81            xinclude=1,
82            indent=0):
83        ''' Return <number> of records from the result set beginning
84        at <offset> '''
85
86        sets=self.srv.retrieve(in0=self.sessID,
87                              in1=offset,
88                              in2=number,
89                              in3=indent,
90                              in4=xinclude,
91                              in5=highlight)
92
93        return sets['retrieveReturn']   
94       
95    def release(self):
96        self.srv.disconnect(in0=self.sessID)
97
98    def __performance(self):
99        print 'QueryTime='+str(self.result['queryTime'])
100        print 'Hits='+str(self.result['hits'])
101       
102    def __checkdates(self,date):
103        ''' Parse date for correctness wrt web service '''
104        return 1
105
106def log(results,name):
107   
108    f=file('output.'+name,'w')
109    for r in results:f.write(r+'\n')
110    f.close()
111   
112
113if __name__ == '__main__':
114
115    ws=DiscoveryWS()
116    ws.SearchFullText('acsoe')
117    results=ws.GetResults(number=2)
118    ws.release()
119    log(results,'fulltext')
120    print 'Successfully wrote full text file '
121   
122    # doesn't work
123    ss=DiscoveryWS()
124    ss.SearchBoundingBox([85.5,-49.5,166.5,-9.0])
125    results=ss.GetResults()
126    ss.release()
127    log(results,'spatial')
128    print 'got past spatial'
129   
130    #doesn't work
131    ts=DiscoveryWS()
132    ts.SearchTemporal('1999-01-01','2000-01-01')
133    results=ts.GetResults()
134    ts.release()
135    log(results,'temporal')
136    print 'got past temporal'
137
138    #doesn't work
139    xq=DiscoveryWS()
140    xquery=u"//dgMetadata/dgMetadataRecord/dgMetadataID/localIdentifier[. = 'dataent1']"
141    xq.Xquery(xquery)
142    print 'Successfully issued xquery'
143    xq.release()
144 
Note: See TracBrowser for help on using the repository browser.