~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to fai/arch/deprecation.py

  • Committer: Robert Collins
  • Date: 2005-09-13 10:46:27 UTC
  • mto: (147.2.6) (364.1.3 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 324.
  • Revision ID: robertc@robertcollins.net-20050913104627-51f938950a907475
handle inaccessible sibling archives somewhat - note version-0 is still not handled

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# arch-tag: 46e3b3ff-aebc-46ab-9bd3-c7cb1dff34e5
 
2
# Copyright (C) 2003-2004 David Allouche <david@allouche.net>
 
3
#
 
4
#    This program is free software; you can redistribute it and/or modify
 
5
#    it under the terms of the GNU General Public License as published by
 
6
#    the Free Software Foundation; either version 2 of the License, or
 
7
#    (at your option) any later version.
 
8
#
 
9
#    This program is distributed in the hope that it will be useful,
 
10
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
#    GNU General Public License for more details.
 
13
#
 
14
#    You should have received a copy of the GNU General Public License
 
15
#    along with this program; if not, write to the Free Software
 
16
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 
 
18
__all__ = [ 'deprecated_callable', 'deprecated_usage' ]
 
19
 
 
20
import warnings
 
21
import inspect
 
22
 
 
23
def callable_name(obj):
 
24
    if inspect.isfunction(obj):
 
25
        return '.'.join((obj.__module__, obj.__name__))
 
26
    if inspect.ismethod(obj):
 
27
        kls = obj.im_class
 
28
        return '.'.join((kls.__module__, kls.__name__, obj.__name__))
 
29
    if isinstance(obj, tuple):
 
30
        # user-defined properties have no name, so they have to specified by a
 
31
        # (class, attribute) tuple.
 
32
        assert len(obj) == 2
 
33
        assert isinstance(obj[0], type)
 
34
        assert isinstance(obj[1], str)
 
35
        assert hasattr(*obj)
 
36
        return '.'.join((obj[0].__module__, obj[0].__name__, obj[1]))
 
37
    return obj.__name__
 
38
 
 
39
def callable_type(obj):
 
40
    if isinstance(obj, tuple):
 
41
        return type(getattr(*obj)).__name__
 
42
    return type(obj).__name__
 
43
 
 
44
def deprecated_callable(old, new=None, because=None):
 
45
    assert 1 == (new is not None) + (because is not None)
 
46
    what = callable_type(old)
 
47
    header = "%s %s is deprecated." % (what, callable_name(old))
 
48
    if new is not None:
 
49
        warnings.warn("%s Use %s %s instead." %
 
50
                      (header, callable_type(new), callable_name(new)),
 
51
                      DeprecationWarning, stacklevel=3)
 
52
    elif because is not None:
 
53
        warnings.warn("%s %s" % (header, because),
 
54
                      DeprecationWarning, stacklevel=3)
 
55
 
 
56
def deprecated_usage(callable, msg):
 
57
    warnings.warn("Deprecated usage of %s. %s"
 
58
                  % (callable_name(callable), msg),
 
59
                  DeprecationWarning, stacklevel=3)