/teca-env18/lib/python2.7/site-packages/matplotlib/cbook/deprecation.py

https://github.com/dpritsos/WEGA · Python · 222 lines · 111 code · 17 blank · 94 comment · 5 complexity · 9fef9705495fcaf3c99f59e539953a20 MD5 · raw file

  1. import functools
  2. import textwrap
  3. import warnings
  4. class MatplotlibDeprecationWarning(UserWarning):
  5. """
  6. A class for issuing deprecation warnings for Matplotlib users.
  7. In light of the fact that Python builtin DeprecationWarnings are ignored
  8. by default as of Python 2.7 (see link below), this class was put in to
  9. allow for the signaling of deprecation, but via UserWarnings which are not
  10. ignored by default.
  11. https://docs.python.org/dev/whatsnew/2.7.html#the-future-for-python-2-x
  12. """
  13. pass
  14. mplDeprecation = MatplotlibDeprecationWarning
  15. def _generate_deprecation_message(since, message='', name='',
  16. alternative='', pending=False,
  17. obj_type='attribute',
  18. addendum=''):
  19. if not message:
  20. if pending:
  21. message = (
  22. 'The %(name)s %(obj_type)s will be deprecated in a '
  23. 'future version.')
  24. else:
  25. message = (
  26. 'The %(name)s %(obj_type)s was deprecated in version '
  27. '%(since)s.')
  28. altmessage = ''
  29. if alternative:
  30. altmessage = ' Use %s instead.' % alternative
  31. message = ((message % {
  32. 'func': name,
  33. 'name': name,
  34. 'alternative': alternative,
  35. 'obj_type': obj_type,
  36. 'since': since}) +
  37. altmessage)
  38. if addendum:
  39. message += addendum
  40. return message
  41. def warn_deprecated(
  42. since, message='', name='', alternative='', pending=False,
  43. obj_type='attribute', addendum=''):
  44. """
  45. Used to display deprecation warning in a standard way.
  46. Parameters
  47. ----------
  48. since : str
  49. The release at which this API became deprecated.
  50. message : str, optional
  51. Override the default deprecation message. The format
  52. specifier `%(name)s` may be used for the name of the function,
  53. and `%(alternative)s` may be used in the deprecation message
  54. to insert the name of an alternative to the deprecated
  55. function. `%(obj_type)s` may be used to insert a friendly name
  56. for the type of object being deprecated.
  57. name : str, optional
  58. The name of the deprecated object.
  59. alternative : str, optional
  60. An alternative function that the user may use in place of the
  61. deprecated function. The deprecation warning will tell the user
  62. about this alternative if provided.
  63. pending : bool, optional
  64. If True, uses a PendingDeprecationWarning instead of a
  65. DeprecationWarning.
  66. obj_type : str, optional
  67. The object type being deprecated.
  68. addendum : str, optional
  69. Additional text appended directly to the final message.
  70. Examples
  71. --------
  72. Basic example::
  73. # To warn of the deprecation of "matplotlib.name_of_module"
  74. warn_deprecated('1.4.0', name='matplotlib.name_of_module',
  75. obj_type='module')
  76. """
  77. message = _generate_deprecation_message(
  78. since, message, name, alternative, pending, obj_type)
  79. warnings.warn(message, mplDeprecation, stacklevel=1)
  80. def deprecated(since, message='', name='', alternative='', pending=False,
  81. obj_type=None, addendum=''):
  82. """
  83. Decorator to mark a function or a class as deprecated.
  84. Parameters
  85. ----------
  86. since : str
  87. The release at which this API became deprecated. This is
  88. required.
  89. message : str, optional
  90. Override the default deprecation message. The format
  91. specifier `%(name)s` may be used for the name of the object,
  92. and `%(alternative)s` may be used in the deprecation message
  93. to insert the name of an alternative to the deprecated
  94. object. `%(obj_type)s` may be used to insert a friendly name
  95. for the type of object being deprecated.
  96. name : str, optional
  97. The name of the deprecated object; if not provided the name
  98. is automatically determined from the passed in object,
  99. though this is useful in the case of renamed functions, where
  100. the new function is just assigned to the name of the
  101. deprecated function. For example::
  102. def new_function():
  103. ...
  104. oldFunction = new_function
  105. alternative : str, optional
  106. An alternative object that the user may use in place of the
  107. deprecated object. The deprecation warning will tell the user
  108. about this alternative if provided.
  109. pending : bool, optional
  110. If True, uses a PendingDeprecationWarning instead of a
  111. DeprecationWarning.
  112. addendum : str, optional
  113. Additional text appended directly to the final message.
  114. Examples
  115. --------
  116. Basic example::
  117. @deprecated('1.4.0')
  118. def the_function_to_deprecate():
  119. pass
  120. """
  121. def deprecate(obj, message=message, name=name, alternative=alternative,
  122. pending=pending, addendum=addendum):
  123. if not name:
  124. name = obj.__name__
  125. if isinstance(obj, type):
  126. obj_type = "class"
  127. old_doc = obj.__doc__
  128. func = obj.__init__
  129. def finalize(wrapper, new_doc):
  130. try:
  131. obj.__doc__ = new_doc
  132. except (AttributeError, TypeError):
  133. # cls.__doc__ is not writeable on Py2.
  134. # TypeError occurs on PyPy
  135. pass
  136. obj.__init__ = wrapper
  137. return obj
  138. else:
  139. obj_type = "function"
  140. if isinstance(obj, classmethod):
  141. func = obj.__func__
  142. old_doc = func.__doc__
  143. def finalize(wrapper, new_doc):
  144. wrapper = functools.wraps(func)(wrapper)
  145. wrapper.__doc__ = new_doc
  146. return classmethod(wrapper)
  147. else:
  148. func = obj
  149. old_doc = func.__doc__
  150. def finalize(wrapper, new_doc):
  151. wrapper = functools.wraps(func)(wrapper)
  152. wrapper.__doc__ = new_doc
  153. return wrapper
  154. message = _generate_deprecation_message(
  155. since, message, name, alternative, pending,
  156. obj_type, addendum)
  157. def wrapper(*args, **kwargs):
  158. warnings.warn(message, mplDeprecation, stacklevel=2)
  159. return func(*args, **kwargs)
  160. old_doc = textwrap.dedent(old_doc or '').strip('\n')
  161. message = message.strip()
  162. new_doc = (('\n.. deprecated:: %(since)s'
  163. '\n %(message)s\n\n' %
  164. {'since': since, 'message': message}) + old_doc)
  165. if not old_doc:
  166. # This is to prevent a spurious 'unexected unindent' warning from
  167. # docutils when the original docstring was blank.
  168. new_doc += r'\ '
  169. return finalize(wrapper, new_doc)
  170. return deprecate