From dc4961da934b6d3133eea41aff7e41dafee60c9a Mon Sep 17 00:00:00 2001 From: Francis Charette Migneault Date: Fri, 5 Apr 2024 22:10:46 -0400 Subject: [PATCH] workaournd fix of invalid pyramid route_prefix path (relates to https://github.com/Pylons/pyramid/issues/3758) --- weaver/app.py | 4 +--- weaver/wps_restapi/patches.py | 39 ++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/weaver/app.py b/weaver/app.py index 1f3545931..a7d015380 100644 --- a/weaver/app.py +++ b/weaver/app.py @@ -8,15 +8,13 @@ from typing import TYPE_CHECKING import yaml -from pyramid.config import Configurator -from pyramid.settings import asbool from weaver.config import WEAVER_DEFAULT_REQUEST_OPTIONS_CONFIG, get_weaver_config_file, get_weaver_configuration from weaver.database import get_db from weaver.processes.builtin import register_builtin_processes from weaver.processes.utils import register_cwl_processes_from_config, register_wps_processes_from_config from weaver.utils import parse_extra_options, setup_cache, setup_loggers -from weaver.wps_restapi.patches import patch_pyramid_view_no_auto_head_get_method +from weaver.wps_restapi.patches import Configurator, patch_pyramid_view_no_auto_head_get_method if TYPE_CHECKING: from typing import Any diff --git a/weaver/wps_restapi/patches.py b/weaver/wps_restapi/patches.py index 6edd738ff..7a4c500cf 100644 --- a/weaver/wps_restapi/patches.py +++ b/weaver/wps_restapi/patches.py @@ -1,16 +1,53 @@ """ Helpers to work around some default view configurations that are not desired. """ +import contextlib from typing import TYPE_CHECKING from cornice import Service as ServiceAutoGetHead from pyramid.predicates import RequestMethodPredicate +from pyramid.config import Configurator as PyramidConfigurator from pyramid.util import as_sorted_tuple if TYPE_CHECKING: from typing import Any, Tuple, Union - from pyramid.config import Configurator + +class Configurator(PyramidConfigurator): + @contextlib.contextmanager + def route_prefix_context(self, route_prefix): + """ + Copy of the original configurator, with tweak for leaving the leading ``/`` of the supplied ``route_prefix``. + + .. fixme:: + Workaround for https://github.com/Pylons/pyramid/issues/3758 + """ + original_route_prefix = self.route_prefix + + if route_prefix is None: + route_prefix = '' + + old_route_prefix = self.route_prefix + if old_route_prefix is None: + old_route_prefix = '' + + route_prefix = '{}/{}'.format( + old_route_prefix.rstrip('/'), route_prefix.lstrip('/') + ) + + route_prefix = route_prefix.rstrip('/') # FIXME: this is the only change 'strip' -> 'rstrip' + + if not route_prefix: + route_prefix = None + + self.begin() + try: + self.route_prefix = route_prefix + yield + + finally: + self.route_prefix = original_route_prefix + self.end() class NoAutoHeadList(list):