docstring.py 3.9 KB

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