home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / lib / site-packages / gtk-2.0 / gtk / deprecation.py < prev    next >
Encoding:
Python Source  |  2007-11-01  |  3.0 KB  |  84 lines

  1. # -*- Mode: Python; py-indent-offset: 4 -*-
  2. # pygtk - Python bindings for the GTK toolkit.
  3. # Copyright (C) 2004-2006  Johan Dahlin
  4. #
  5. #   gtk/deprecation.py: deprecation helpers for gtk
  6. #
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Lesser General Public
  9. # License as published by the Free Software Foundation; either
  10. # version 2.1 of the License, or (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. # Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public
  18. # License along with this library; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  20. # USA
  21.  
  22. import os
  23. import sys
  24. import warnings
  25.  
  26. from gtk._gtk import DeprecationWarning
  27.  
  28. def _is_pydoc():
  29.     if sys.argv:
  30.         name = os.path.basename(sys.argv[0])
  31.         if 'pydoc' in name:
  32.             return True
  33.  
  34.     return False
  35.  
  36. class _Deprecated:
  37.     def __init__(self, module, funcname, oldname, modulename=''):
  38.         self.module = module
  39.         self.funcname = funcname
  40.         self.oldname = oldname
  41.         if modulename:
  42.             self.modulename = modulename
  43.         else:
  44.             self.modulename = 'gtk'
  45.  
  46.     def __repr__(self):
  47.         return '<deprecated function %s>' % (self.oldname)
  48.  
  49.     def __call__(self, *args, **kwargs):
  50.         if type(self.module) == str:
  51.             module = __import__(self.module, {}, {}, ' ')
  52.         else:
  53.             module = self.module
  54.         func = getattr(module, self.funcname)
  55.         if not _is_pydoc():
  56.             message = 'gtk.%s is deprecated, use %s.%s instead' % (
  57.                 self.oldname, self.modulename, func.__name__)
  58.             # DeprecationWarning is imported from _gtk, so it's not the same
  59.             # as the one found in exceptions.
  60.             warnings.warn(message, DeprecationWarning, 2)
  61.         try:
  62.             return func(*args, **kwargs)
  63.         except TypeError, e:
  64.             raise TypeError(str(e).replace(func.__name__, self.oldname))
  65.  
  66. class _DeprecatedConstant:
  67.     def __init__(self, value, name, suggestion):
  68.         self._v = value
  69.         self._name = name
  70.         self._suggestion = suggestion
  71.  
  72.     def _deprecated(self, value):
  73.         if not _is_pydoc():
  74.             message = '%s is deprecated, use %s instead' % (self._name,
  75.                                                             self._suggestion)
  76.             warnings.warn(message, DeprecationWarning, 3)
  77.         return value
  78.  
  79.     __nonzero__ = lambda self: self._deprecated(self._v == True)
  80.     __int__     = lambda self: self._deprecated(int(self._v))
  81.     __str__     = lambda self: self._deprecated(str(self._v))
  82.     __repr__    = lambda self: self._deprecated(repr(self._v))
  83.     __cmp__     = lambda self, other: self._deprecated(cmp(self._v, other))
  84.