From 472663aa4648e58f2c2b6b99604b68118b11c5fd Mon Sep 17 00:00:00 2001 From: amercader Date: Fri, 22 Nov 2019 12:15:37 +0100 Subject: [PATCH] [#4801] Selective loading in plugins toolkit This took a while to get right because exceptions raised during the toolkit initialization fail silently and the sympton would be the an import error later on when trying to import something from the toolkit: from ckan.plugins.toolkit import get_action ImportError: cannot import name 'get_action' TODO: Sort out what gets exposed to extensions in terms of CLI commands --- ckan/plugins/toolkit.py | 42 +++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/ckan/plugins/toolkit.py b/ckan/plugins/toolkit.py index c2874421e2b..04bc9212401 100644 --- a/ckan/plugins/toolkit.py +++ b/ckan/plugins/toolkit.py @@ -130,14 +130,14 @@ def __init__(self): def _initialize(self): ''' get the required functions/objects, store them for later access and check that they match the contents dict. ''' - + import six import ckan - import ckan.lib.base as base import ckan.logic as logic + + import ckan.lib.base as base import ckan.logic.validators as logic_validators import ckan.lib.navl.dictization_functions as dictization_functions import ckan.lib.helpers as h - import ckan.lib.cli as cli import ckan.lib.plugins as lib_plugins import ckan.common as common from ckan.exceptions import ( @@ -147,8 +147,10 @@ def _initialize(self): from ckan.lib.jobs import enqueue as enqueue_job import ckan.common as converters - import pylons - import webhelpers.html.tags + if six.PY2: + import ckan.lib.cli as cli + import pylons + import webhelpers.html.tags # Allow class access to these modules self.__class__.ckan = ckan @@ -213,6 +215,7 @@ def _initialize(self): ''' t['render'] = base.render + t['abort'] = base.abort t['asbool'] = converters.asbool self.docstring_overrides['asbool'] = '''Convert a string (e.g. 1, true, True) from the config file into a boolean. @@ -234,7 +237,6 @@ def _initialize(self): For example: ``bar = toolkit.aslist(config.get('ckan.foo.bar', []))`` ''' - t['literal'] = webhelpers.html.tags.literal t['get_action'] = logic.get_action t['chained_action'] = logic.chained_action @@ -251,22 +253,10 @@ def _initialize(self): t['UnknownValidator'] = logic.UnknownValidator t['Invalid'] = logic_validators.Invalid - t['CkanCommand'] = cli.CkanCommand - t['load_config'] = cli.load_config t['DefaultDatasetForm'] = lib_plugins.DefaultDatasetForm t['DefaultGroupForm'] = lib_plugins.DefaultGroupForm t['DefaultOrganizationForm'] = lib_plugins.DefaultOrganizationForm - t['response'] = pylons.response - self.docstring_overrides['response'] = '''The Pylons response object. - -Pylons uses this object to generate the HTTP response it returns to the web -browser. It has attributes like the HTTP status code, the response headers, -content type, cookies, etc. - -''' - t['BaseController'] = base.BaseController - t['abort'] = base.abort t['redirect_to'] = h.redirect_to t['url_for'] = h.url_for t['get_or_bust'] = logic.get_or_bust @@ -290,6 +280,22 @@ def _initialize(self): t['HelperError'] = HelperError t['enqueue_job'] = enqueue_job + if six.PY2: + + t['literal'] = webhelpers.html.tags.literal + t['response'] = pylons.response + self.docstring_overrides['response'] = '''The Pylons response object. + +Pylons uses this object to generate the HTTP response it returns to the web +browser. It has attributes like the HTTP status code, the response headers, +content type, cookies, etc. + +''' + t['BaseController'] = base.BaseController + # TODO: Sort these out + t['CkanCommand'] = cli.CkanCommand + t['load_config'] = cli.load_config + # check contents list correct errors = set(t).symmetric_difference(set(self.contents)) if errors: