source: TI01-discovery/trunk/python/ws2/test_discovery.py @ 1574

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI01-discovery/trunk/python/ws2/test_discovery.py@1574
Revision 1574, 4.0 KB checked in by lawrence, 14 years ago (diff)

Test code that works with the latest Matt discovery wsdl

Line 
1# these imports are autogenerated by wsdl2py ...
2from DiscoveryService_services_types import *
3from DiscoveryService_services import *
4
5#normal imports
6import sys
7try: #python 2.5
8    import xml.etree.ElementTree as ET
9except:
10    import ElementTree as ET
11   
12from ETxmlView import loadET,et2text
13
14class ndgSearch:
15    ''' Proivdes a client to the NDG discovery services methods exposed by the Discovery.wsdl '''
16   
17    def __init__(self):
18        '''get an instance of the service'''
19        #how do we get a different backend provider?
20        loc=DiscoveryServiceLocator()
21        self.server=loc.getDiscovery()#tracefile=sys.stdout)
22        self.documents=None
23        self.hits=None
24        self.serverSessionID=None
25   
26    def search(self,term,start=1,howmany=30):
27        ''' Get a list of documents for a specific term using a free text search'''
28       
29        #create a request object
30        request=doFullTextSearchRequest()
31        request._term=term
32        request._start=start
33        request._howmany=howmany
34       
35        #do the search
36        response=self.server.doFullTextSearch(request)
37        if response._status:
38            self.serverSessionID=response._resultId
39            self.hits=response._hits
40            self.documents=response._documents._document
41        self.status=[response._statusMessage,]
42        return self.documents
43
44    def getDoc(self,document):
45        ''' Return a document from the backend database '''
46       
47        #create a request object
48        request=doPresentRequest()
49        request._document=document
50        searchResult=self.server.doPresent(request)
51        if searchResult._status:
52            result=searchResult._document
53        else:
54            result=searchResult._statusMessage
55        return result
56                   
57    def getAllDocs(self):           
58        ''' Parse the list of documents, get them, and load them into a list of tuple strings (name,content) '''
59        # we keep the name to make it easy to get the actual document out to avoid xpath issues ...
60       
61        docs=[]
62        self.status=[]
63       
64        #create a request object
65        request=doPresentRequest()
66        for doc in self.documents:
67            request._document=doc
68            result=self.server.doPresent(request)
69            if result._status:
70                docs.append((doc,result._document))
71            else:
72                self.status.append((doc,result._statusMessage))
73               
74        self.docs=docs
75        return self.docs
76           
77           
78    def __xmlerror(self,docmessage):
79        print docmessage
80        return ET.fromstring('<Error><Document>%s</Document><Message>%s</Message></Error>'%docmessage)
81           
82    def getDocElement(self,document):
83        ''' Takes a document path (maybe from a previous call to ndgSearch) and extracts that document
84        into an ElementTree instance '''
85        #we stick it straight into element tree because we need to use et to get the actual document
86        #we want, not the envelope xml elements
87       
88        doc=self.getDoc(document)
89        path=document.rstrip('.xml')
90        try:
91            r=loadET(doc)
92            #return r.find(path)
93            return r
94        except:
95            return self.__xmlerror((path,doc))
96           
97    def getAllDocsAsElements(self):
98        ''' Get all the documents and load them into a list of ET instances '''
99        result=[]
100        docs=self.getAllDocs()
101        for doc in docs: 
102            try:
103                r=loadET(doc[1])
104                path=doc[0].rstrip('.xml')
105                result.append(r)#result.append(r.find(path))
106            except:
107                result.append(self.__xmlerror((path,doc[1])))
108        return result
109           
110if __name__=="__main__":
111   
112    s=ndgSearch()
113    docs=s.search('temperature')
114    print s.status
115    for i in docs: print i
116   
117    #docs=s.getAllDocs()
118    #print docs[0]
119    #print s.status
120   
121    doc=s.documents[1]
122    r=s.getDocElement(doc)
123    print et2text(r)
Note: See TracBrowser for help on using the repository browser.