[deprecated]?new DeprecatedProxy convenience class. Closes #88942

authorSylvain Th?nault <sylvain.thenault@logilab.fr>
changesetefc603c988b9
branchdefault
phasepublic
hiddenno
parent revision#644cdc2976d9 backport stable
child revision#888d9c08f6d9 backport stable
files modified by this revision
ChangeLog
deprecation.py
# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1331819666 -3600
# Thu Mar 15 14:54:26 2012 +0100
# Node ID efc603c988b9572dc596bf8c71de24fe8483c6c2
# Parent 644cdc2976d9d78dac61e6b29c14d8ca201d80ff
[deprecated] new DeprecatedProxy convenience class. Closes #88942

diff --git a/ChangeLog b/ChangeLog
@@ -3,10 +3,11 @@
1 
2  	-- #default
3      * new `registry` module containing a backport of CubicWeb selectable objects registry (closes #84654)
4      * testlib: DocTestCase fix builtins pollution after doctest execution.
5      * shellutil: add argument to ``ProgressBar.update`` to tune cursor progression (closes #88981)
6 +    * deprecated: new DeprecationWrapper class (closes #88942)
7 
8  	-- #stable
9      * texutils: apply_units raise ValueError if string isn'nt valid (closes #88808)
10      * pytest: do not enable extra warning other than DeprecationWarning.
11 
diff --git a/deprecation.py b/deprecation.py
@@ -108,5 +108,23 @@
12          m = load_module_from_name(modpath)
13          return getattr(m, objname)(*args, **kwargs)
14      return callnew
15 
16 
17 +
18 +class DeprecationWrapper(object):
19 +    """proxy to print a warning on access to any attribute of the wrapped object
20 +    """
21 +    def __init__(self, proxied, msg=None):
22 +        self._proxied = proxied
23 +        self._msg = msg
24 +
25 +    def __getattr__(self, attr):
26 +        warn(self._msg, DeprecationWarning, stacklevel=2)
27 +        return getattr(self._proxied, attr)
28 +
29 +    def __setattr__(self, attr, value):
30 +        if attr in ('_proxied', '_msg'):
31 +            self.__dict__[attr] = value
32 +        else:
33 +            warn(self._msg, DeprecationWarning, stacklevel=2)
34 +            setattr(self._proxied, attr, value)