Skip to content

Commit

Permalink
[#3196] Remove context related functions from maintain module
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
amercader committed Aug 12, 2016
1 parent c5cbcca commit 7b0cbd6
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 90 deletions.
7 changes: 1 addition & 6 deletions ckan/controllers/group.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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},
Expand Down
8 changes: 0 additions & 8 deletions ckan/controllers/home.py
Expand Up @@ -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
Expand Down Expand Up @@ -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'),
Expand Down
8 changes: 1 addition & 7 deletions ckan/controllers/package.py
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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 = {}
Expand All @@ -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)

Expand Down
5 changes: 0 additions & 5 deletions ckan/lib/base.py
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
60 changes: 0 additions & 60 deletions 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__)

Expand Down Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions ckan/lib/plugins.py
Expand Up @@ -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
Expand Down Expand Up @@ -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'):
Expand Down

0 comments on commit 7b0cbd6

Please sign in to comment.