source: TI07-MOLES/trunk/PythonCode/wsgi/ndgRetrieve.py @ 2046

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/PythonCode/wsgi/ndgRetrieve.py@2046
Revision 2046, 5.8 KB checked in by lawrence, 13 years ago (diff)

Mainly test code to understand browse deployment environment (plus tiny
bug fix in browse docuemnt retrieval).

Line 
1from Utilities import myConfig
2from paste.request import parse_querystring
3from renderEntity import renderEntity
4import xmlHandler
5from DC import DC
6from DIF import DIF
7from stubB import stubB
8
9# any class with a get method can be used here, and then only
10# the instantiation within ndgRetrieve would need
11# modification to use it ...
12
13from DocumentRetrieve import DocumentRetrieve
14
15def retrieveFactory(global_config,**local_conf):
16   
17    ''' This factory is intended to be used by paste-deploy to return
18    the ndgRetrieve wsgi application, configured to use the configDir
19    to find local configuration information '''
20   
21    if 'configDir' in local_conf.keys():
22        configDir=local_conf[configDir]
23    else:
24        try:
25            configDir=global_config['configDir']
26        except:
27            configDir=None
28    ndg=ndgRetrieve(configDir)
29    return ndg
30
31class showDoc:
32    ''' Given an xml document (xml), of format ('DIF','DC','NDG-B1'), return
33    either the document rendered as "pretty" xml for printing, or as html
34    rendered by an appropriate application'''
35   
36    def __init__(self,config):
37        self.config=config
38        self.renderer=renderEntity(self.config)
39       
40    def get(self,xml,format,otype='html'):
41        if otype=='html':
42            if format=='DIF':
43                r=DIF(xml,self.config)
44            elif format=='NDG-B1':
45                r=stubB(xml,self.config)
46            elif format=='DC':
47                r=DC(xml)
48            r=self.renderer.render(r)
49        elif otype=='print':
50            x=xmlHandler.xmlHandler(xml,string=1)
51            r=x.html
52        elif otype=='xml':
53            return xml
54        else:
55            raise TypeError('Invalid output type [%s]'%otype)
56        return r
57
58class ndgRetrieve:
59   
60    ''' This is the ndgRetrieve application which simply allows one to enter a uri
61    of a document held in a LOCAL exist repository and return one of
62       - the raw document (type=xml),
63       - an html version fully rendered (type=html, which is the default), or
64       - a pretty printed version suitable for printing (type=print)
65    Note that this WSGI application SHOULD be wrapped by an ndgGatekeeper MIDDLEWARE application and
66    not exposed directly to users (except in testing of course :-) '''
67   
68    def __init__(self,configDir):
69       
70        ''' Instantiate the wsgi document retrieval application '''
71        self.configDir=configDir
72        self.config=myConfig(configDir+'ndgDiscovery.config')
73        self.shower=showDoc(self.config)
74        # nb ... we don't instantiate the retrieval web service, since
75        # we expect to potentially have multiple web services pointing
76        # at multiple databases (eg. BADC + NEODC + NDG ) ...
77        self.retrieveWS=DocumentRetrieve
78        self.WScursors={}
79   
80    def __call__(self,environ,start_response):
81       
82        ''' This is the function which implements the document retrieval '''
83       
84        inputs=dict(parse_querystring(environ))
85        print inputs
86       
87        ctype='text/html'   # default content type (since the default
88                            # may be an error return ...) the actual default
89                            # data type for return is xml.
90        if 'uri' not in inputs.keys():
91            r=self.__prompt()
92            raise ValueError,r
93        else:
94            uri=inputs['uri'].strip()
95            ok=1
96            bits=uri.split(':')
97            bits2=uri.split('__')
98            if len(bits)==3:
99                repository,schema,localID=bits
100            elif len(bits2)==3:
101                repository,schema,localID=bits2
102            else:
103                bits=uri.split('/')
104                if len(bits)==2:
105                    schema='NDG-B0'
106                    repository,localID=bits
107                else: ok=0
108            if not ok:
109                r='URI [%s] is not a valid NDG style URI'%uri
110            else:
111                if 'repository' in inputs.keys(): 
112                    db=inputs['repository']
113                else:
114                    db=repository
115                if db in self.WScursors.keys():
116                    ws=self.WScursors[db]
117                else:
118                    ws=DocumentRetrieve(db)
119                    self.WScursors[db]=ws
120                try:
121                    if 'format' not in inputs.keys():
122                        format='NDG-B0'
123                    else:
124                        format=inputs['format']
125                    r=ws.get(repository,schema,localID,format=format)
126                    if isinstance(r,int):
127                        r='<p> There are %s identifiers matching your request! </p>'%r
128                        ok=0
129                except Exception,e:
130                    r='<p> Unable to retrieve [%s], reason was [%s]</p>'%(uri,e)
131                    ok=0
132            if ok:
133                outputType=inputs['type']
134                if outputType!='xml': 
135                    r=self.shower.get(r,format,otype=outputType)
136                else:
137                    ctype='text/xml'
138            start_response('200 OK', [('Content-Type', ctype)])
139        return [r]   
140               
141   
142   
143    def __prompt(self):
144        ''' If the URI is not an argument to the request, prompt the user for a URI '''
145        h='''
146        <form method='get' action=''>
147        <p>
148          Enter a Document Identifier to View: 
149            <input type='text' name='uri'/><br/>
150            (uri format: e.g. badc.nerc.ac.uk:DIF:xyz39) <br/>
151            (Querying <input type='text' name='repository' value='gepidae.esc.rl.ac.uk'/>)<br/>
152            Return: <input type="radio" name="type" value="xml"/> XML Raw
153                <input type="radio" name="type" value="html"/> Formatted
154                <input type="radio" name="type" value="print"/> XML Printable
155          <input type='submit'/>
156        </p>
157        </form>
158        '''
159        return h
Note: See TracBrowser for help on using the repository browser.