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

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

More discovery (from browse) related improvments

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=1
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        except Exception, e:
48            raise Exception(str(e)+os.linesep)
49
50    def SearchFullText(self,query):
51        ''' Produce a result set from a full text query on the NDG database '''
52        #known to work.
53        result = self.srv.fullText(in0=self.sessID, in1=query)
54        self.result=result['fullTextReturn']
55        if DEBUG: self.__performance()
56        return self.result['hits']
57
58    def Xquery(self,query):
59        ''' Create a result set based on an xquery over the database '''
60        #this doesn't work properly ...
61        result=self.srv.xquery(in0=query,in1=self.sessID)
62
63    def SearchBoundingBox(self,bbox):
64        ''' Obtain a result set from a spatial search using a bounding box '''
65        print bbox
66        result = self.srv.spatialSearch(in0=self.sessID,
67               in1=bbox[0],in2=bbox[1],in3=bbox[2],in4=bbox[3])
68        print 'got a spatial result'
69        self.result=result['spatialSearchReturn']
70
71    def SearchTemporal(self,date1,date2):
72        ''' Obtain  a reset set from a temporal search using a standard
73        calendar '''
74        ok=self.__checkdates(date1)
75        ok=self.__checkdates(date2)
76        result=self.srv.temporalSearch(in0=self.sessID,in1=date1,in2=date2)
77        self.result=result['temporalSearchReturn']
78       
79    def GetResults(self,
80            number=1, # number of records to return
81            offset=1, # offset index within result set (begins at 1)
82            highlight=u'none',  # string to highlight in result set
83            xinclude=1,
84            indent=0):
85        ''' Return <number> of records from the result set beginning
86        at <offset> '''
87
88        sets=self.srv.retrieve(in0=self.sessID,
89                              in1=offset,
90                              in2=number,
91                              in3=indent,
92                              in4=xinclude,
93                              in5=highlight)
94
95        return sets['retrieveReturn']   
96       
97    def release(self):
98        self.srv.disconnect(in0=self.sessID)
99
100    def __performance(self):
101        print 'QueryTime='+str(self.result['queryTime'])
102        print 'Hits='+str(self.result['hits'])
103       
104    def __checkdates(self,date):
105        ''' Parse date for correctness wrt web service '''
106        return 1
107
108def log(results,name):
109   
110    f=file('output.'+name,'w')
111    for r in results:f.write(r+'\n')
112    f.close()
113   
114
115if __name__ == '__main__':
116
117    ws=DiscoveryWS()
118    ws.SearchFullText('acsoe')
119    results=ws.GetResults(number=2)
120    ws.release()
121    log(results,'fulltext')
122    print 'Successfully wrote full text file '
123   
124    # doesn't work
125    ss=DiscoveryWS()
126    ss.SearchBoundingBox([85.5,-49.5,166.5,-9.0])
127    results=ss.GetResults()
128    ss.release()
129    log(results,'spatial')
130    print 'got past spatial'
131   
132    #doesn't work
133    ts=DiscoveryWS()
134    ts.SearchTemporal('1999-01-01','2000-01-01')
135    results=ts.GetResults()
136    ts.release()
137    log(results,'temporal')
138    print 'got past temporal'
139
140    #doesn't work
141    xq=DiscoveryWS()
142    xquery=u"//dgMetadata/dgMetadataRecord/dgMetadataID/localIdentifier[. = 'dataent1']"
143    xq.Xquery(xquery)
144    print 'Successfully issued xquery'
145    xq.release()
146 
Note: See TracBrowser for help on using the repository browser.