src/pyams_utils/decorator.py
branchdev-tf
changeset 408 cf2304af0fab
parent 380 c062ab4db6cd
child 419 05ff71a02b2d
equal deleted inserted replaced
407:0037199881fb 408:cf2304af0fab
    14 
    14 
    15 This module only provides a single decorator, which can be used to mark a function as
    15 This module only provides a single decorator, which can be used to mark a function as
    16 deprecated.
    16 deprecated.
    17 """
    17 """
    18 
    18 
    19 __docformat__ = 'restructuredtext'
       
    20 
       
    21 import functools
    19 import functools
    22 import warnings
    20 import warnings
       
    21 
       
    22 __docformat__ = 'restructuredtext'
    23 
    23 
    24 
    24 
    25 def deprecated(*msg):
    25 def deprecated(*msg):
    26     """This is a decorator which can be used to mark functions as deprecated.
    26     """This is a decorator which can be used to mark functions as deprecated.
    27 
    27 
    28     It will result in a warning being emitted when the function is used.
    28     It will result in a warning being emitted when the function is used.
       
    29 
       
    30     >>> from pyams_utils.context import capture_stderr
       
    31     >>> from pyams_utils.decorator import deprecated
       
    32 
       
    33     >>> @deprecated
       
    34     ... def my_function(value):
       
    35     ...     return value
       
    36 
       
    37     >>> with capture_stderr(my_function, 1) as err:
       
    38     ...     print(err.split('\\n')[0])
       
    39     <doctest ... DeprecationWarning: Function my_function is deprecated.
       
    40 
       
    41     >>> @deprecated('Deprecation message')
       
    42     ... def my_function_2(value):
       
    43     ...     return value
       
    44 
       
    45     >>> with capture_stderr(my_function_2, 2) as err:
       
    46     ...     print(err.split('\\n')[0])
       
    47     <doctest ... DeprecationWarning: Function my_function_2 is deprecated. Deprecation message
    29     """
    48     """
    30 
    49 
    31     def decorator(func):
    50     def decorator(func):
       
    51         """Actual decorator"""
    32 
    52 
    33         @functools.wraps(func)
    53         @functools.wraps(func)
    34         def new_func(*args, **kwargs):
    54         def new_func(*args, **kwargs):
       
    55             """Wrapped decorator function"""
    35             warnings.warn_explicit("Function %s is deprecated. %s" % (func.__name__, message),
    56             warnings.warn_explicit("Function %s is deprecated. %s" % (func.__name__, message),
    36                                    category=DeprecationWarning,
    57                                    category=DeprecationWarning,
    37                                    filename=func.__code__.co_filename,
    58                                    filename=func.__code__.co_filename,
    38                                    lineno=func.__code__.co_firstlineno + 1)
    59                                    lineno=func.__code__.co_firstlineno + 1)
    39             return func(*args, **kwargs)
    60             return func(*args, **kwargs)