deprutils.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2013, Mahmoud Hashemi
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are
  6. # met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. #
  11. # * Redistributions in binary form must reproduce the above
  12. # copyright notice, this list of conditions and the following
  13. # disclaimer in the documentation and/or other materials provided
  14. # with the distribution.
  15. #
  16. # * The names of the contributors may not be used to endorse or
  17. # promote products derived from this software without specific
  18. # prior written permission.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. """
  32. Note that DeprecationWarnings are ignored by default in Python
  33. 2.7/3.2+, so be sure to either un-ignore them in your code, or run
  34. Python with the -Wd flag.
  35. """
  36. import sys
  37. from warnings import warn
  38. ModuleType = type(sys)
  39. # todo: only warn once
  40. class DeprecatableModule(ModuleType):
  41. def __init__(self, module):
  42. name = module.__name__
  43. super(DeprecatableModule, self).__init__(name=name)
  44. self.__dict__.update(module.__dict__)
  45. def __getattribute__(self, name):
  46. get_attribute = super(DeprecatableModule, self).__getattribute__
  47. try:
  48. depros = get_attribute('_deprecated_members')
  49. except AttributeError:
  50. self._deprecated_members = depros = {}
  51. ret = get_attribute(name)
  52. message = depros.get(name)
  53. if message is not None:
  54. warn(message, DeprecationWarning, stacklevel=2)
  55. return ret
  56. def deprecate_module_member(mod_name, name, message):
  57. module = sys.modules[mod_name]
  58. if not isinstance(module, DeprecatableModule):
  59. sys.modules[mod_name] = module = DeprecatableModule(module)
  60. module._deprecated_members[name] = message
  61. return