source: TI05-delivery/ows_framework/trunk/ows_server/ows_server/lib/base.py @ 2577

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_server/ows_server/lib/base.py@2577
Revision 2577, 4.3 KB checked in by spascoe, 12 years ago (diff)

More consistent method naming. Using lowerCamelCase for methods but
underscore_separated names for internal variables, functions and modules.

Line 
1from pylons import Response, c, g, cache, request, session
2from pylons.controllers import WSGIController
3from pylons.decorators import jsonify, validate
4from pylons.templating import render, render_response
5from pylons.helpers import abort, redirect_to, etag_cache
6from pylons.i18n import N_, _, ungettext
7import ows_server.models as model
8import ows_server.lib.helpers as h
9
10from ows_common import exceptions as OWS_E
11from ows_common.operations_metadata import OperationsMetadata, Operation, RequestMethod
12from ows_common.get_capabilities import ServiceMetadata
13import ows_common.xml
14from elementtree import ElementTree as ET
15
16class BaseController(WSGIController):
17    def __call__(self, environ, start_response):
18        # Insert any code to be run per request here. The Routes match
19        # is under environ['pylons.routes_dict'] should you want to check
20        # the action or route vars here
21        return WSGIController.__call__(self, environ, start_response)
22   
23class OwsController(BaseController):
24    def __call__(self, environ, start_response):
25
26        # All OWS parameter names are case insensitive.
27        req = request._current_obj()
28        self.ows_params = {}
29        for k in req.params:
30            self.ows_params[k.lower()] = req.params[k]       
31
32        try:
33            self._fixOwsAction(environ)
34            return super(OwsController, self).__call__(environ, start_response)
35        except OWS_E.OwsError, e:
36            return render_response('exception_report', report=e.report,
37                                   format='xml')
38           
39 
40    def _fixOwsAction(self, environ):
41        # Override the Routes action from the request query parameter
42        try:
43            action = self.ows_params['request']
44        except KeyError:
45            raise OWS_E.MissingParameterValue('REQUEST parameter not specified', 'REQUEST')
46
47        # Check action is a method in self
48        if not getattr(self, action):
49            raise OWS_E.InvalidParameterValue('request=%s not supported' % action, 'REQUEST')
50
51        # override routes action with request
52        environ['pylons.routes_dict']['action'] = action
53        del self.ows_params['request']
54
55    def _loadCapabilities(self):
56        """
57        creates an ows_common.get_capabilities.ServiceMetadata object
58        by consulting the paste configuration and annotations in the
59        controller definition.
60
61        """
62
63        # Deduce ows_endpoint from routes
64        ows_endpoint = h.url_for(controller=request.environ['pylons.routes_dict']['controller'])
65
66        # Get the server-level configuration data from an XML file
67        config = request.environ['paste.config']
68        sm_tree = ET.parse(config['ows_common_config'])
69        sm = ows_common.xml.service_metadata(sm_tree.getroot())
70
71        # Extract service-level parameters and constraint
72        parameters = getattr(self, '_ows_parameters', {})
73        constraints = getattr(self, '_ows_constraints', {})
74        versions = getattr(self, '_ows_versions', [])
75
76        # Extract operation-level parameters and constraints
77        od = {}
78        for attr in dir(self):
79            op = getattr(self, attr)
80            if hasattr(op, '_ows_name'):
81                p = getattr(op, '_ows_parameters', {})
82                c = getattr(op, '_ows_constraints', {})
83                od[op._ows_name] = Operation(get=RequestMethod(href=ows_endpoint),
84                                             post=None,
85                                             parameters=p,
86                                             constraints=c)
87       
88        sm.operationsMetadata = OperationsMetadata(od, constraints, parameters)
89        sm.serviceIdentification.serviceTypeVersions = versions
90       
91        return sm
92
93    def _renderCapabilities(self, template='ows/get_capabilities'):
94        """
95        The standard way of returning a Capabilities document.
96
97        Each subclass should implement self._load_capabilities() and call
98        this method to return a response object.
99
100        """
101        c.service_metadata = self._loadCapabilities()
102
103        r = render_response(template, format='xml')
104        r.headers['content-type'] = 'text/xml'
105        return r
106
107
108# Include the '_' function in the public names
109__all__ = [__name for __name in locals().keys() if not __name.startswith('_') \
110           or __name == '_']
Note: See TracBrowser for help on using the repository browser.