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

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

I assume this is a typo. variable "server" is never defined.

Line 
1import DocumentRetrieve
2from ndgSearch import ndgSearch
3import cgi,time
4
5def ndgRetrieve(uri,config,logger=0,output='',remote=0,discovery=0):
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            if r<> sr:
39                if not remote:
40                    return 0,'The uri [%s] is not available on [%s]'%(uri,sr)
41                else:
42                    try:
43                        ps=config.get('DEFAULT','proxyServer')
44                    except:
45                        raise ValueError,'Config file not available or has no [proxyServer]'
46                    ws=DocumentRetrieve.ndgHTTP(r,proxyServer=ps)
47            else:
48                try:
49                    er=config.get('NDG_EXIST',uri.repository)
50                except:
51                    raise ValueError,'Config file not available or has no Exist repository'
52                pwf=config.get('NDG_EXIST','passwordFile')
53                ws=DocumentRetrieve.DocumentRetrieve(er,pwfile=pwf)
54        return 1,ws
55   
56    status,ws=getws(config,uri,remote)
57 
58    if not status: return status,ws
59           
60    if uri.schema=='NDG-A0':
61        target='/db/ndg_A_metadata'
62    elif uri.schema=='NumSim':
63        target='/db/NumSim'
64    else:
65        target='/db/ndg_B_metadata'
66   
67    #do the actual retrieve:
68    e=None
69    try:
70        time1=time.time()
71        r=ws.get(uri.repository,uri.schema,uri.localID,targetCollection=target)
72        time2=time.time()-time1
73        logString='Document retrieve [%s] took [%s]'%(uri,time2)
74    except Exception,e:
75        e=str(e)
76        r=e
77        logString='Document retrieve [%s] failed [%s]'%(uri,e)
78   
79    if logger: logger.info(logString)
80
81    # did we get a sensible answer?
82    if isinstance(r,int):
83        return 0,'<p> There are %s identifiers matching your request! </p>'%r
84    if e is not None:
85        return 0,'Document retrieve[%s] failed [%s]'%(uri,cgi.escape(e))
86   
87    # we used to return an xmlHandler instance, but we don't do that any more ...
88    return 1,r
89   
Note: See TracBrowser for help on using the repository browser.