Skip to content

Commit

Permalink
extend plugin.toolkit functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Apr 19, 2012
1 parent f7834db commit ec1d211
Showing 1 changed file with 67 additions and 8 deletions.
75 changes: 67 additions & 8 deletions ckan/plugins/toolkit.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
## This file is intended to make functions consistently available to
## plugins whilst giving developers the ability move code around or
## change underlying frameworks etc. It should not be used internaly
## within ckan only by extensions. Functions should only be removed from
## this file after reasonable depreciation notice has been given.
## This file is intended to make functions/objects consistently
## available to plugins whilst giving developers the ability move code
## around or change underlying frameworks etc. It should not be used
## internaly within ckan only by extensions. Functions should only be
## removed from this file after reasonable depreciation notice has
## been given.

import inspect
import os
import re

import pylons
import paste.deploy.converters as converters
import webhelpers.html.tags

import lib.base as base
import ckan
import ckan.lib.base as base
import ckan.logic as logic
import ckan.lib.cli as cli



__all__ = [
## Imported functions ##
## Imported functions/objects ##
'_', # i18n translation
'c', # template context
'request', # http request object
'render', # template render function
Expand All @@ -25,12 +32,22 @@
'asint', # converts an object to an integer
'aslist', # converts an object to a list
'literal', # stop tags in a string being escaped
'get_action', # get logic action function
'check_access', # check logic function authorisation
'NotFound', # action not found exception
'NotAuthorized', # action not authourized exception
'ValidationError', # model update validation error
'CkanCommand', # class for providing cli interfaces

## Functions fully defined here ##
## Fully defined in this file ##
'add_template_directory',
'add_public_directory',
'requires_ckan_version',
'check_ckan_version',
'CkanVersionException',
]

_ = pylons.i18n._
c = pylons.c
request = pylons.request
render = base.render
Expand All @@ -40,6 +57,13 @@
aslist = converters.aslist
literal = webhelpers.html.tags.literal

get_action = logic.get_action
check_access = logic.check_access
NotFound = logic.NotFound
NotAuthorized = logic.NotAuthorized
ValidationError = logic.ValidationError

CkanCommand = cli.CkanCommand

# wrappers
def render_snippet(template, data=None):
Expand Down Expand Up @@ -73,3 +97,38 @@ def _add_served_directory(config, relative_path, config_var):
config[config_var] += ',' + absolute_path
else:
config[config_var] = absolute_path

class CkanVersionException(Exception):
''' Exception raised if required ckan version is not available. '''
pass


def _version_str_2_list(v_str):
''' conver a version string into a list of ints
eg 1.6.1b --> [1, 6, 1] '''
v_str = re.sub(r'[^0-9.]', '', v_str)
return [int(part) for part in v_str.split('.')]

def check_ckan_version(min_version=None, max_version=None):
''' Check that the ckan version is correct for the plugin. '''
current = _version_str_2_list(ckan.__version__)

if min_version:
min_required = _version_str_2_list(min_version)
if current < min_required:
return False
if max_version:
max_required = _version_str_2_list(max_version)
if current > max_required:
return False
return True

def requires_ckan_version(min_version, max_version=None):
''' Check that the ckan version is correct for the plugin. '''
if not check_ckan_version(min_version=min_version, max_version=max_version):
if not max_version:
error = 'Requires ckan version %s or higher' % min_version
else:
error = 'Requires ckan version between %s and %s' % \
(min_version, max_version)
raise CkanVersionException(error)

0 comments on commit ec1d211

Please sign in to comment.