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

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

Further modifications to support xml versions of displayed records in
discovery.

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