src/pyams_utils/decorator.py
changeset 1 3f89629b9e54
child 95 f6cc362670a6
equal deleted inserted replaced
0:16d47bd81d84 1:3f89629b9e54
       
     1 #
       
     2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 
       
    16 # import standard library
       
    17 import functools
       
    18 import warnings
       
    19 
       
    20 # import interfaces
       
    21 
       
    22 # import packages
       
    23 
       
    24 
       
    25 def deprecated(*msg):
       
    26     """This is a decorator which can be used to mark functions
       
    27     as deprecated. It will result in a warning being emitted
       
    28     when the function is used.
       
    29     """
       
    30 
       
    31     def decorator(func):
       
    32 
       
    33         @functools.wraps(func)
       
    34         def new_func(*args, **kwargs):
       
    35             warnings.warn_explicit("Function %s is deprecated. %s" % (func.__name__, message),
       
    36                                    category=DeprecationWarning,
       
    37                                    filename=func.func_code.co_filename,
       
    38                                    lineno=func.func_code.co_firstlineno + 1)
       
    39             return func(*args, **kwargs)
       
    40         return new_func
       
    41 
       
    42     if len(msg) == 1 and callable(msg[0]):
       
    43         message = u''
       
    44         return decorator(msg[0])
       
    45     else:
       
    46         message = msg[0]
       
    47         return decorator