source: TI01-discovery/trunk/python/DiscoveryWS.py @ 951

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI01-discovery/trunk/python/DiscoveryWS.py@951
Revision 951, 4.4 KB checked in by pjkersha, 13 years ago (diff)

Added explicit types in DiscoveryWStypes to try to get working - no apparent difference :/

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