From 7b0cbd6a9979ed91d935f0dc8748ef1974f5505f Mon Sep 17 00:00:00 2001 From: amercader Date: Fri, 12 Aug 2016 11:03:53 +0100 Subject: [PATCH] [#3196] Remove context related functions from maintain module `deprecate_context_item` was a bit of a hack, messing with the underlying object internal methods and hasn't really been used in ages. It was hard to make work with the current common context object, so it makes sense to get rid of it. `defer_context_item` wasn't used anywhere --- ckan/controllers/group.py | 7 +---- ckan/controllers/home.py | 8 ----- ckan/controllers/package.py | 8 +---- ckan/lib/base.py | 5 ---- ckan/lib/maintain.py | 60 ------------------------------------- ckan/lib/plugins.py | 4 --- 6 files changed, 2 insertions(+), 90 deletions(-) diff --git a/ckan/controllers/group.py b/ckan/controllers/group.py index 61c4e8d20bf..5aec211a2ee 100644 --- a/ckan/controllers/group.py +++ b/ckan/controllers/group.py @@ -8,7 +8,6 @@ import ckan.lib.base as base import ckan.lib.helpers as h -import ckan.lib.maintain as maintain import ckan.lib.navl.dictization_functions as dict_fns import ckan.logic as logic import ckan.lib.search as search @@ -348,13 +347,10 @@ def pager_url(q=None, page=None): ) c.group_dict['package_count'] = query['count'] - c.facets = query['facets'] - maintain.deprecate_context_item('facets', - 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] c.search_facets_limits = {} - for facet in c.facets.keys(): + for facet in c.search_facets.keys(): limit = int(request.params.get('_%s_limit' % facet, config.get('search.facets.default', 10))) c.search_facets_limits[facet] = limit @@ -365,7 +361,6 @@ def pager_url(q=None, page=None): except search.SearchError, se: log.error('Group search error: %r', se.args) c.query_error = True - c.facets = {} c.page = h.Page(collection=[]) self._setup_template_variables(context, {'id': id}, diff --git a/ckan/controllers/home.py b/ckan/controllers/home.py index 4becf7cef97..2a438886082 100644 --- a/ckan/controllers/home.py +++ b/ckan/controllers/home.py @@ -4,7 +4,6 @@ import sqlalchemy.exc import ckan.logic as logic -import ckan.lib.maintain as maintain import ckan.lib.search as search import ckan.lib.base as base import ckan.model as model @@ -58,13 +57,6 @@ def index(self): c.package_count = query['count'] c.datasets = query['results'] - c.facets = query['facets'] - maintain.deprecate_context_item( - 'facets', - 'Use `c.search_facets` instead.') - - c.search_facets = query['search_facets'] - c.facet_titles = { 'organization': _('Organizations'), 'groups': _('Groups'), diff --git a/ckan/controllers/package.py b/ckan/controllers/package.py index 098f4b3fb1e..ec4bbd8ef95 100644 --- a/ckan/controllers/package.py +++ b/ckan/controllers/package.py @@ -12,8 +12,8 @@ import ckan.logic as logic import ckan.lib.base as base -import ckan.lib.maintain as maintain import ckan.lib.i18n as i18n +import ckan.lib.maintain as maintain import ckan.lib.navl.dictization_functions as dict_fns import ckan.lib.helpers as h import ckan.model as model @@ -277,7 +277,6 @@ def pager_url(q=None, page=None): item_count=query['count'], items_per_page=limit ) - c.facets = query['facets'] c.search_facets = query['search_facets'] c.page.items = query['results'] except SearchQueryError, se: @@ -293,7 +292,6 @@ def pager_url(q=None, page=None): # SOLR log.error('Dataset search error: %r', se.args) c.query_error = True - c.facets = {} c.search_facets = {} c.page = h.Page(collection=[]) c.search_facets_limits = {} @@ -307,10 +305,6 @@ def pager_url(q=None, page=None): parameter_name='_%s_limit' % facet)) c.search_facets_limits[facet] = limit - maintain.deprecate_context_item( - 'facets', - 'Use `c.search_facets` instead.') - self._setup_template_variables(context, {}, package_type=package_type) diff --git a/ckan/lib/base.py b/ckan/lib/base.py index 164a1cc3b4b..2442a69b9cd 100644 --- a/ckan/lib/base.py +++ b/ckan/lib/base.py @@ -25,7 +25,6 @@ import ckan.lib.app_globals as app_globals import ckan.plugins as p import ckan.model as model -import ckan.lib.maintain as maintain # These imports are for legacy usages and will be removed soon these should # be imported directly from ckan.common for internal ckan code and via the @@ -199,10 +198,6 @@ def __before__(self, action, **params): i18n.handle_request(request, c) - maintain.deprecate_context_item( - 'new_activities', - 'Use `h.new_activities` instead.') - def _identify_user(self): '''Try to identify the user If the user is identified then: diff --git a/ckan/lib/maintain.py b/ckan/lib/maintain.py index fc8ac1ed5a7..94ceeeefb47 100644 --- a/ckan/lib/maintain.py +++ b/ckan/lib/maintain.py @@ -1,13 +1,11 @@ # encoding: utf-8 ''' This module contains code that helps in maintaining the Ckan codebase. ''' - import inspect import time import logging import re -from ckan.common import c log = logging.getLogger(__name__) @@ -39,64 +37,6 @@ def wrapped(*args, **kw): return wrapped return decorator -def deprecate_context_item(item_name, message=''): - ''' Deprecate a named item in the global context object. - - It logs a warning when the item is accessed. If a mesage is passed, it is - also logged. This can be useful to indicate for example that a different - function should be used instead. - - No warnings are given when an attempt to change or delete the named item - from the context object. - - Example usage: - - >>> c.facets = "Foobar" - >>> deprecate_context_item('facets', 'Use `c.search_facets` instead') - >>> print c.facets - 2012-07-12 13:27:06,294 WARNI [ckan.lib.maintain] c.facets has been deprecated [...] - Foobar - - This function works by attaching a property to the underlying - `pylons.util.AttribSafeContextObj` object which provides the storage of the - context object. ie - it adds a class-level attribute to the - `pylons.util.AttribSafeContextObj` at runtime. - ''' - - # we need to store the origional __getattr__ and replace with our own one - if not hasattr(c.__class__, '__old_getattr__'): - def custom__getattr__(self, name): - # get the origional __getattr__ so we can access things - __old_getattr__ = self.__class__.__dict__['__old_getattr__'] - # see if we have a __deprecated_properties__ for this name and - # if so log a warning - try: - deprecated = __old_getattr__(self, '__deprecated_properties__') - if name in deprecated: - log.warn(deprecated[name]) - except AttributeError: - pass - # return the requested value - return __old_getattr__(self, name) - - # get store the old __getattr__ method and then replace it - __old_getattr__ = getattr(c.__class__, '__getattr__') - setattr(c.__class__, '__old_getattr__', __old_getattr__) - setattr(c.__class__, '__getattr__', custom__getattr__) - - # if c.__deprecated_properties__ is not set it returns '' - if not c.__deprecated_properties__: - c.__deprecated_properties__ = {} - c.__deprecated_properties__[item_name] = message - - -def defer_context_item(item_name, function): - ''' Allows a function to be passed that will be appended to c as a property - so that it is only called if actually used. ''' - - assert hasattr(function, '__call__'), 'must pass a function' - setattr(c, item_name, property(function)) - def timer(params): ''' Decorator function for basic performance testing. It logs the time diff --git a/ckan/lib/plugins.py b/ckan/lib/plugins.py index d7257ffca02..94c0b5cc511 100644 --- a/ckan/lib/plugins.py +++ b/ckan/lib/plugins.py @@ -6,7 +6,6 @@ from ckan.common import c from ckan.lib import base -import ckan.lib.maintain as maintain from ckan import logic import logic.schema from ckan import plugins @@ -283,9 +282,6 @@ def setup_template_variables(self, context, data_dict): c.groups_available = authz_fn(context, data_dict) c.licenses = [('', '')] + base.model.Package.get_license_options() - # CS: bad_spelling ignore 2 lines - c.licences = c.licenses - maintain.deprecate_context_item('licences', 'Use `c.licenses` instead') c.is_sysadmin = ckan.authz.is_sysadmin(c.user) if context.get('revision_id') or context.get('revision_date'):