Skip to content

Commit

Permalink
[#1841] make plugins.toolkit more like a normal module
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Aug 14, 2014
1 parent 878be66 commit 08a1fb3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
8 changes: 8 additions & 0 deletions ckan/exceptions.py
Expand Up @@ -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
5 changes: 1 addition & 4 deletions 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
35 changes: 16 additions & 19 deletions 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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 =\
Expand All @@ -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('.')]

Expand Down Expand Up @@ -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:
Expand All @@ -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()

0 comments on commit 08a1fb3

Please sign in to comment.