Skip to content

Commit

Permalink
Proper fix of deprecated_context_item
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Aug 1, 2012
1 parent 3ee836d commit fdfe6a0
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions ckan/lib/maintain.py
Expand Up @@ -62,11 +62,28 @@ def deprecate_context_item(item_name, message=''):
# prevent a circular import
from ckan.lib.base import c

def get_item(self):
log.warning('c.%s has been deprecated. %s', item_name, message)
return getattr(c.__ckan_deprecated__, item_name)

c.__ckan_deprecated__ = c.__ckan_deprecated__ or {}
c.__ckan_deprecated__[item_name] = getattr(c, item_name)
setattr(c, item_name, property(get_item))
class Fake(object):
def __init__(self, obj):
self._obj = obj
def __getattribute__(self,name):
obj = object.__getattribute__(self, '_obj')
if name == '_obj':
return obj
return getattr(obj, name)


# store the value in a fake object
setattr(c, item_name, Fake(getattr(c, item_name)))

# we need to store the origional __getattr__ and replace with our own one
if not hasattr(c.__class__, '__old_getattr__'):
def fake_attr(self, name):
obj = self.__class__.__dict__['__old_getattr__'](self, name)
if isinstance(obj, Fake):
return obj._obj
else:
return obj
get_attr = getattr(c.__class__, '__getattr__')
setattr(c.__class__, '__old_getattr__', get_attr)
setattr(c.__class__, '__getattr__', fake_attr)

0 comments on commit fdfe6a0

Please sign in to comment.