From 5094719cb77b74a6dded039defdc6c675c5ecc35 Mon Sep 17 00:00:00 2001 From: Erik Johnson Date: Sun, 15 Sep 2013 00:00:49 -0500 Subject: [PATCH] Modify sys.doc to replace rst directives This commit modifies sys.doc so that the rst directives within it look better on the CLI when the docs are printed to the terminal. --- salt/modules/sysmod.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/salt/modules/sysmod.py b/salt/modules/sysmod.py index c5925711fa..8f91b95cc3 100644 --- a/salt/modules/sysmod.py +++ b/salt/modules/sysmod.py @@ -5,6 +5,7 @@ minion. # Import python libs import logging +import re # Import salt libs import salt.utils @@ -19,7 +20,28 @@ def __virtual__(): return 'sys' -def doc(*args, **kwargs): +def _strip_rst(docs): + ''' + Strip/replace reStructuredText directives in docstrings + ''' + for func, docstring in docs.iteritems(): + if not docstring: + continue + docstring_new = re.sub(' *.. code-block:: \S+\n{1,2}', + '', docstring) + docstring_new = re.sub('.. note::', + 'Note:', docstring_new) + docstring_new = re.sub('.. warning::', + 'Warning:', docstring_new) + docstring_new = re.sub('.. versionadded::', + 'New in version', docstring_new) + docstring_new = re.sub('.. versionchanged::', + 'Changed in version', docstring_new) + if docstring != docstring_new: + docs[func] = docstring_new + + +def doc(*args): ''' Return the docstrings for all modules. Optionally, specify a module or a function to narrow the selection. @@ -38,12 +60,11 @@ def doc(*args, **kwargs): salt '*' sys.doc sys.doc salt '*' sys.doc network.traceroute user.info ''' - ### NOTE: **kwargs is used here to prevent a traceback when garbage - ### arguments are tacked on to the end. docs = {} if not args: for fun in __salt__: docs[fun] = __salt__[fun].__doc__ + _strip_rst(docs) return docs for module in args: @@ -56,6 +77,7 @@ def doc(*args, **kwargs): for fun in __salt__: if fun == module or fun.startswith(target_mod): docs[fun] = __salt__[fun].__doc__ + _strip_rst(docs) return docs