You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.7 KiB

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