From 08a1fb34e6e173846c9260d1278397d6e5cb8840 Mon Sep 17 00:00:00 2001 From: Ian Ward Date: Thu, 14 Aug 2014 16:04:21 -0400 Subject: [PATCH] [#1841] make plugins.toolkit more like a normal module --- ckan/exceptions.py | 8 ++++++++ ckan/plugins/__init__.py | 5 +---- ckan/plugins/toolkit.py | 35 ++++++++++++++++------------------- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/ckan/exceptions.py b/ckan/exceptions.py index 40bea2e8c29..1fc407029f6 100644 --- a/ckan/exceptions.py +++ b/ckan/exceptions.py @@ -6,3 +6,11 @@ class EmptyRevisionException(CkanException): class CkanUrlException(Exception): pass + +class CkanVersionException(Exception): + '''Exception raised by + :py:func:`~ckan.plugins.toolkit.requires_ckan_version` if the required CKAN + version is not available. + + ''' + pass diff --git a/ckan/plugins/__init__.py b/ckan/plugins/__init__.py index 719a24a4c12..1a84eee628e 100644 --- a/ckan/plugins/__init__.py +++ b/ckan/plugins/__init__.py @@ -1,7 +1,4 @@ from ckan.plugins.core import * from ckan.plugins.interfaces import * -# Expose the toolkit object without doing an import * -import toolkit as _toolkit -toolkit = _toolkit.toolkit -del _toolkit +import toolkit diff --git a/ckan/plugins/toolkit.py b/ckan/plugins/toolkit.py index e436c82a582..90c95aef592 100644 --- a/ckan/plugins/toolkit.py +++ b/ckan/plugins/toolkit.py @@ -1,20 +1,4 @@ -import inspect -import os -import re - -import paste.deploy.converters as converters -import webhelpers.html.tags - -__all__ = ['toolkit'] - - -class CkanVersionException(Exception): - '''Exception raised by - :py:func:`~ckan.plugins.toolkit.requires_ckan_version` if the required CKAN - version is not available. - - ''' - pass +import sys class _Toolkit(object): @@ -102,7 +86,12 @@ def _initialize(self): import ckan.lib.plugins as lib_plugins import ckan.common as common import ckan.lib.datapreview as datapreview + from ckan.exceptions import CkanVersionException + + from paste.deploy import converters import pylons + import webhelpers.html.tags + # Allow class access to these modules self.__class__.ckan = ckan @@ -251,6 +240,9 @@ def _add_public_directory(cls, config, relative_path): @classmethod def _add_served_directory(cls, config, relative_path, config_var): ''' Add extra public/template directories to config. ''' + import inspect + import os + assert config_var in ('extra_template_paths', 'extra_public_paths') # we want the filename that of the function caller but they will # have used one of the available helper functions @@ -275,6 +267,9 @@ def _add_resource(cls, path, name): See :doc:`/theming/index` for more details. ''' + import inspect + import os + # we want the filename that of the function caller but they will # have used one of the available helper functions frame, filename, line_number, function_name, lines, index =\ @@ -289,6 +284,7 @@ def _add_resource(cls, path, name): def _version_str_2_list(cls, v_str): ''' convert a version string into a list of ints eg 1.6.1b --> [1, 6, 1] ''' + import re v_str = re.sub(r'[^0-9.]', '', v_str) return [int(part) for part in v_str.split('.')] @@ -347,6 +343,7 @@ def _requires_ckan_version(cls, min_version, max_version=None): :type max_version: string ''' + from ckan.exceptions import CkanVersionException if not cls._check_ckan_version(min_version=min_version, max_version=max_version): if not max_version: @@ -373,5 +370,5 @@ def __dir__(self): return sorted(self._toolkit.keys()) -toolkit = _Toolkit() -del _Toolkit +# https://mail.python.org/pipermail/python-ideas/2012-May/014969.html +sys.modules[__name__] = _Toolkit()