source: TI07-MOLES/trunk/PythonCode/browse/portal/cgi/browse/ndgSearch.py @ 1615

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

Modifications to discovery (and browse) to support ndgDoc as an interim
measure ...

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   
27    def search(self,term,start=1,howmany=20,scope=None):
28        ''' Get a list of documents for a specific term using a free text search'''
29       
30        #create a request object
31        request=doFullTextSearchRequest()
32        request._term=term
33        request._start=start
34        request._howMany=howmany
35       
36        if scope is not None:
37            request._scope=scope
38        #do the search
39        response=self.server.doFullTextSearch(request)
40        if response._status:
41            self.serverSessionID=response._resultId
42            self.hits=response._hits
43            self.documents=response._documents._document
44        self.status=[response._statusMessage,]
45        return self.documents
46
47    def getDoc(self,document):
48        ''' Return a document from the backend database '''
49       
50        #create a request object
51        request=doPresentRequest()
52        request._document=document
53        request._format='ndgDoc'
54        searchResult=self.server.doPresent(request)
55        if searchResult._status:
56            result=searchResult._document
57        else:
58            result=searchResult._statusMessage
59        return result
60                   
61    def getAllDocs(self):           
62        ''' Parse the list of documents, get them, and load them into a list of tuple strings (name,content) '''
63        # we keep the name to make it easy to get the actual document out to avoid xpath issues ...
64       
65        docs=[]
66        self.status=[]
67       
68        if self.documents is None: return []
69       
70        #create a request object
71        request=doPresentRequest()
72        for doc in self.documents:
73            request._document=doc
74            request._format='ndgDoc'
75            result=self.server.doPresent(request)
76            if result._status:
77                docs.append((doc,result._document))
78            else:
79                self.status.append((doc,result._statusMessage))
80               
81        self.docs=docs
82        return self.docs
83           
84           
85    def __xmlerror(self,docmessage):
86        print docmessage
87        return ET.fromstring('<Error><Document>%s</Document><Message>%s</Message></Error>'%docmessage)
88           
89    def getDocElement(self,document):
90        ''' Takes a document path (maybe from a previous call to ndgSearch) and extracts that document
91        into an ElementTree instance '''
92        #we stick it straight into element tree because we need to use et to get the actual document
93        #we want, not the envelope xml elements
94       
95        doc=self.getDoc(document)
96        path=document.rstrip('.xml')
97        try:
98            r=loadET(doc)
99            #return r.find(path)
100            return r
101        except:
102            return self.__xmlerror((path,doc))
103           
104    def getAllDocsAsElements(self):
105        ''' Get all the documents and load them into a list of ET instances '''
106        result=[]
107        docs=self.getAllDocs()
108        for doc in docs: 
109            try:
110                r=loadET(doc[1])
111                path=doc[0].rstrip('.xml')
112                result.append(r)#result.append(r.find(path))
113            except:
114                result.append(self.__xmlerror((path,doc[1])))
115        return result
116           
117if __name__=="__main__":
118   
119    args=sys.argv
120    if len(args)>1:
121        term=args[1]
122    else:
123        term='temperature'
124   
125    s=ndgSearch()
126    howmany=10
127    docs=s.search(term,start=1,howmany=howmany)
128    print s.status
129    print 'Asked for ',howmany,' documents, and got:'
130    j=0
131    for i in docs: 
132        j+=1
133        print j,':',i
134       
135   
136    #docs=s.getAllDocs()
137    #print docs[0]
138    #print s.status
139   
140    doc=s.documents[1]
141    r=s.getDocElement(doc)
142    #print et2text(r)
Note: See TracBrowser for help on using the repository browser.