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

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

More discovery/browse convergence, starting support
for linking to services ...

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