diff --git a/pyramid_oereb/__init__.py b/pyramid_oereb/__init__.py index fa9af4ff4d..4ffa677f01 100644 --- a/pyramid_oereb/__init__.py +++ b/pyramid_oereb/__init__.py @@ -2,11 +2,14 @@ import logging +from pyramid.request import Request + from pyramid_oereb.core.adapter import DatabaseAdapter from pyramid_oereb.core.config import Config from pyramid.config import Configurator + log = logging.getLogger(__name__) route_prefix = None # initially instantiate database adapter for global session handling @@ -34,6 +37,7 @@ def includeme(config): Args: config (Configurator): The pyramid apps config object """ + from pyramid_oereb.core.processor import create_processor, Processor global route_prefix @@ -57,6 +61,21 @@ def includeme(config): settings.update({ 'pyramid_oereb': Config.get_config() }) + processor = None + try: + processor = create_processor() + except Exception as e: + log.error(f'Initialisation of processor failed with an error: {e}') + exit(1) + + def get_processor(request: Request) -> Processor: + return processor + + config.add_request_method( + get_processor, + 'pyramid_oereb_processor', + reify=True + ) config.add_renderer('pyramid_oereb_extract_json', 'pyramid_oereb.core.renderer.extract.json_.Renderer') config.add_renderer('pyramid_oereb_extract_xml', 'pyramid_oereb.core.renderer.extract.xml_.Renderer') diff --git a/pyramid_oereb/core/views/webservice.py b/pyramid_oereb/core/views/webservice.py index f60e3381a0..d12b19f793 100644 --- a/pyramid_oereb/core/views/webservice.py +++ b/pyramid_oereb/core/views/webservice.py @@ -16,7 +16,6 @@ from pyramid_oereb import Config from pyreproj import Reprojector -from pyramid_oereb.core.processor import create_processor from pyramid_oereb.core.readers.address import AddressReader from pyramid_oereb.core.renderer import Base as Renderer from timeit import default_timer as timer @@ -195,7 +194,7 @@ def _get_egrid_coord(self, params): Config.get('srid'), self.__parse_gnss__(gnss).wkt ) - processor = create_processor(real_estate_only=True) + processor = self._request.pyramid_oereb_processor return processor.real_estate_reader.read(params, **{'geometry': geom_wkt}) else: raise HTTPBadRequest('EN or GNSS must be defined.') @@ -214,7 +213,7 @@ def _get_egrid_ident(self, params): identdn = self._params.get('IDENTDN') number = self._params.get('NUMBER') if identdn and number: - processor = create_processor(real_estate_only=True) + processor = self._request.pyramid_oereb_processor return processor.real_estate_reader.read( params, **{ @@ -251,7 +250,7 @@ def _get_egrid_address(self, params): srid=Config.get('srid'), wkt=addresses[0].geom.wkt ) - processor = create_processor(real_estate_only=True) + processor = self._request.pyramid_oereb_processor return processor.real_estate_reader.read(params, **{'geometry': geometry}) else: raise HTTPBadRequest('POSTALCODE, LOCALISATION and NUMBER must be defined.') @@ -267,7 +266,7 @@ def get_extract_by_id(self): log.debug("get_extract_by_id() start") try: params = self.__validate_extract_params__() - processor = create_processor() + processor = self._request.pyramid_oereb_processor # read the real estate from configured source by the passed parameters real_estate_reader = processor.real_estate_reader if params.egrid: