source: exist/trunk/python/ndgUtils/ndgRetrieve.py @ 3125

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/python/ndgUtils/ndgRetrieve.py@3125
Revision 3125, 3.1 KB checked in by lawrence, 12 years ago (diff)

ndgUtils taking shape with test cases etc

Line 
1import DocumentRetrieve, ndgObject, xmlHandler2
2import cgi,time
3
4def ndgRetrieve(uri,config,logger=0,output='',remote=0):
5   
6    ''' Given an ndgObject, uri, retrieve it, with security in place.
7            If logger is non-zero it should be a python log class
8            If output is not '' it should be desired output schema (normally
9        to be used for discovery documents in a variety of outputs or
10        to indicate that an original document is required.
11            If remote is non zero, then attempt to get the document
12        from a remote site via a (secured) restful http call'''
13   
14    def getws(config,uri,remote):
15        ''' Get a ws connection to the local exist database '''
16        # The logic here is that
17        #    this code is running on server, which supports repositories sr
18        #    this particular object needs the repository for uri.repository
19        #    which had better be the same as sr, and then we use the
20        #    actual exist repository er.
21        #    I'm sure this can be cleaned up further :-)
22        try:
23            r=config.get('NDG_B_SERVICE',uri.repository)
24            sr=config.get('DEFAULT','repository')
25            er=config.get('NDG_EXIST',uri.repository)
26        except:
27            raise ValueError,'Config file not available or is incomplete'
28        if r<> sr:
29            if not remote:
30                return 0,'The uri [%s] is not available on [%s]'%(uri,server)
31            else:
32                ps=config.get('DEFAULT','proxyServer')
33                ws=DocumentRetrieve.ndgHTTP(r,proxyServer=ps)
34        else:
35            pwf=config.get('NDG_EXIST','passwordFile')
36            ws=DocumentRetrieve.DocumentRetrieve(er,pwfile=pwf)
37        return 1,ws
38   
39    try:
40        server=config.get('DEFAULT','server')
41    except:
42        raise ValueError('Config file not available or does not include "server" entry!')
43    status,ws=getws(config,uri,remote)
44 
45    if not status: return status,ws
46           
47    if uri.schema=='NDG-A0':
48        target='/db/ndg_A_metadata'
49    elif uri.schema=='NumSim':
50        target='/db/NumSim'
51    else:
52        target='/db/ndg_B_metadata'
53   
54    #do the actual retrieve:
55    e=None
56    try:
57        time1=time.time()
58        r=ws.get(uri.repository,uri.schema,uri.localID,targetCollection=target)
59        time2=time.time()-time1
60        logString='Document retrieve [%s] took [%s]'%(uri,time2)
61    except Exception,e:
62        e=str(e)
63        r=e
64        logString='Document retrieve [%s] failed [%s]'%(uri,e)
65   
66    if logger: logger.info(logString)
67
68    # did we get a sensible answer?
69    if isinstance(r,int):
70        return 0,'<p> There are %s identifiers matching your request! </p>'%r
71    if e is not None:
72        return 0,'Document retrieve[%s] failed [%s]'%(uri,cgi.escape(e))
73   
74    try:
75        x=xmlHandler2.xmlHandler(r,string=1)
76        return 1,x
77    except Exception,e:
78        logString='Document parse for [%s] failed [%s]'%(uri,e)
79        if logger: logger.info(logString)
80        return 0,'''<p>Unable to parse documment [%s]<br/>
81                    Reason was [%s]</p>'''%(uri,cgi.escape(str(e)))
82   
Note: See TracBrowser for help on using the repository browser.