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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/python/ndgUtils/ndgRetrieve.py@3502
Revision 3502, 3.5 KB checked in by cbyrom, 13 years ago (diff)

Add missing space in error message.

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