source: TI07-MOLES/trunk/PythonCode/browse/portal/cgi/browse/DiscoveryWS.py @ 1577

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

Browse now modified to use Matt's backend ...

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