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