docstring.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import inspect
  2. from matplotlib import cbook
  3. class Substitution:
  4. """
  5. A decorator that performs %-substitution on an object's docstring.
  6. This decorator should be robust even if ``obj.__doc__`` is None (for
  7. example, if -OO was passed to the interpreter).
  8. Usage: construct a docstring.Substitution with a sequence or dictionary
  9. suitable for performing substitution; then decorate a suitable function
  10. with the constructed object, e.g.::
  11. sub_author_name = Substitution(author='Jason')
  12. @sub_author_name
  13. def some_function(x):
  14. "%(author)s wrote this function"
  15. # note that some_function.__doc__ is now "Jason wrote this function"
  16. One can also use positional arguments::
  17. sub_first_last_names = Substitution('Edgar Allen', 'Poe')
  18. @sub_first_last_names
  19. def some_function(x):
  20. "%s %s wrote the Raven"
  21. """
  22. def __init__(self, *args, **kwargs):
  23. if args and kwargs:
  24. raise TypeError("Only positional or keyword args are allowed")
  25. self.params = args or kwargs
  26. def __call__(self, func):
  27. if func.__doc__:
  28. func.__doc__ %= self.params
  29. return func
  30. def update(self, *args, **kwargs):
  31. """
  32. Update ``self.params`` (which must be a dict) with the supplied args.
  33. """
  34. self.params.update(*args, **kwargs)
  35. @classmethod
  36. def from_params(cls, params):
  37. """
  38. In the case where the params is a mutable sequence (list or
  39. dictionary) and it may change before this class is called, one may
  40. explicitly use a reference to the params rather than using *args or
  41. **kwargs which will copy the values and not reference them.
  42. """
  43. result = cls()
  44. result.params = params
  45. return result
  46. @cbook.deprecated("3.1")
  47. class Appender:
  48. r"""
  49. A function decorator that will append an addendum to the docstring
  50. of the target function.
  51. This decorator should be robust even if func.__doc__ is None
  52. (for example, if -OO was passed to the interpreter).
  53. Usage: construct a docstring.Appender with a string to be joined to
  54. the original docstring. An optional 'join' parameter may be supplied
  55. which will be used to join the docstring and addendum. e.g.
  56. add_copyright = Appender("Copyright (c) 2009", join='\n')
  57. @add_copyright
  58. def my_dog(has='fleas'):
  59. "This docstring will have a copyright below"
  60. pass
  61. """
  62. def __init__(self, addendum, join=''):
  63. self.addendum = addendum
  64. self.join = join
  65. def __call__(self, func):
  66. docitems = [func.__doc__, self.addendum]
  67. func.__doc__ = func.__doc__ and self.join.join(docitems)
  68. return func
  69. @cbook.deprecated("3.1", alternative="inspect.getdoc()")
  70. def dedent(func):
  71. "Dedent a docstring (if present)"
  72. func.__doc__ = func.__doc__ and cbook.dedent(func.__doc__)
  73. return func
  74. def copy(source):
  75. "Copy a docstring from another source function (if present)"
  76. def do_copy(target):
  77. if source.__doc__:
  78. target.__doc__ = source.__doc__
  79. return target
  80. return do_copy
  81. # Create a decorator that will house the various docstring snippets reused
  82. # throughout Matplotlib.
  83. interpd = Substitution()
  84. def dedent_interpd(func):
  85. """Dedent *func*'s docstring, then interpolate it with ``interpd``."""
  86. func.__doc__ = inspect.getdoc(func)
  87. return interpd(func)
  88. @cbook.deprecated("3.1", alternative="docstring.copy() and cbook.dedent()")
  89. def copy_dedent(source):
  90. """A decorator that will copy the docstring from the source and
  91. then dedent it"""
  92. # note the following is ugly because "Python is not a functional
  93. # language" - GVR. Perhaps one day, functools.compose will exist.
  94. # or perhaps not.
  95. # http://mail.python.org/pipermail/patches/2007-February/021687.html
  96. return lambda target: dedent(copy(source)(target))