From 9221fb8db0cd5e6d9bf300f364aff9ce23b224a1 Mon Sep 17 00:00:00 2001 From: Vincent Le Gallic Date: Tue, 16 Apr 2013 04:07:01 +0200 Subject: [PATCH] =?UTF-8?q?[deprecated]=20On=20peut=20fournir=20la=20fonct?= =?UTF-8?q?ion=20=C3=A0=20utiliser=20en=20remplacement=20de=20celle=20d?= =?UTF-8?q?=C3=A9pr=C3=A9ci=C3=A9e.=20(ou=20un=20random=20message)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gestion/gen_confs/switchs.py | 2 +- lib/deprecated.py | 34 ++++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/gestion/gen_confs/switchs.py b/gestion/gen_confs/switchs.py index 3cf47251..b963536c 100755 --- a/gestion/gen_confs/switchs.py +++ b/gestion/gen_confs/switchs.py @@ -211,7 +211,7 @@ exit for switch in self.switch : self.configure_switch(switch) - @deprecated + @deprecated("Tous les switchs possèdent une authentification radius.") def configure_chbre(self,chbre) : """ Recontigure la chambre fournie chambre. Déprécié. Tous les switchs possèdent une authentification radius.""" diff --git a/lib/deprecated.py b/lib/deprecated.py index 36870887..2aca5197 100644 --- a/lib/deprecated.py +++ b/lib/deprecated.py @@ -4,18 +4,28 @@ import warnings import functools -def deprecated(func): +def deprecated(replace=None): '''This is a decorator which can be used to mark functions as deprecated. It will result in a warning being emitted when the function is used.''' - - @functools.wraps(func) - def new_func(*args, **kwargs): - warnings.warn_explicit( - "Call to deprecated function {}.".format(func.__name__), - category=DeprecationWarning, - filename=func.func_code.co_filename, - lineno=func.func_code.co_firstlineno + 1 - ) - return func(*args, **kwargs) - return new_func + + if replace == None: + instead = "" + elif isinstance(replace, str) or isinstance(replace, unicode): + instead = " " + replace + else: + instead = " Use %s instead." % (replace.__name__,) + + def real_decorator(func): + """Nested because a decorator with a parameter has to be coded this way""" + @functools.wraps(func) + def new_func(*args, **kwargs): + warnings.warn_explicit( + "Call to deprecated function %s.%s" % (func.__name__, instead), + category=DeprecationWarning, + filename=func.func_code.co_filename, + lineno=func.func_code.co_firstlineno + 1 + ) + return func(*args, **kwargs) + return new_func + return real_decorator