CLI: ansible-doc rebased

This commit is contained in:
Jan-Piet Mens 2012-12-03 09:01:04 +01:00
parent dbf5a57a5a
commit 77e060250c

View file

@ -32,9 +32,12 @@ from ansible import utils
from ansible import errors from ansible import errors
from ansible.utils import module_docs from ansible.utils import module_docs
import ansible.constants as C import ansible.constants as C
from ansible.utils import version
MODULEDIR = C.DEFAULT_MODULE_PATH MODULEDIR = C.DEFAULT_MODULE_PATH
BLACKLIST_EXTS = ('.swp', '.bak', '~', '.rpm')
_ITALIC = re.compile(r"I\(([^)]+)\)") _ITALIC = re.compile(r"I\(([^)]+)\)")
_BOLD = re.compile(r"B\(([^)]+)\)") _BOLD = re.compile(r"B\(([^)]+)\)")
_MODULE = re.compile(r"M\(([^)]+)\)") _MODULE = re.compile(r"M\(([^)]+)\)")
@ -60,7 +63,8 @@ def print_man(doc):
print "%s\n" % textwrap.fill(tty_ify(desc), initial_indent=" ", subsequent_indent=" ") print "%s\n" % textwrap.fill(tty_ify(desc), initial_indent=" ", subsequent_indent=" ")
print "Options (= is mandatory):\n" if 'option_keys' in doc and len(doc['option_keys']) > 0:
print "Options (= is mandatory):\n"
for o in doc['option_keys']: for o in doc['option_keys']:
opt = doc['options'][o] opt = doc['options'][o]
@ -74,7 +78,7 @@ def print_man(doc):
desc = "".join(opt['description']) desc = "".join(opt['description'])
if 'choices' in opt: if 'choices' in opt:
choices = ", ".join(opt['choices']) choices = ", ".join(str(i) for i in opt['choices'])
desc = desc + " (Choices: " + choices + ")" desc = desc + " (Choices: " + choices + ")"
print "%s\n" % textwrap.fill(tty_ify(desc), initial_indent=opt_indent, print "%s\n" % textwrap.fill(tty_ify(desc), initial_indent=opt_indent,
subsequent_indent=opt_indent) subsequent_indent=opt_indent)
@ -98,12 +102,12 @@ def print_snippet(doc):
opt = doc['options'][o] opt = doc['options'][o]
desc = tty_ify("".join(opt['description'])) desc = tty_ify("".join(opt['description']))
s = o + "=" s = o + "="
print " %-20s # %s" % (s, desc[0:60]) print " %-20s # %s" % (s, desc)
def main(): def main():
p = optparse.OptionParser( p = optparse.OptionParser(
version='%prog 1.0', version=version("%prog"),
usage='usage: %prog [options] [module...]', usage='usage: %prog [options] [module...]',
description='Show Ansible module documentation', description='Show Ansible module documentation',
) )
@ -128,7 +132,8 @@ def main():
(options, args) = p.parse_args() (options, args) = p.parse_args()
if options.module_path is not None: if options.module_path is not None:
utils.plugins.vars_loader.add_directory(options.module_path) for i in options.module_path.split(os.pathsep):
utils.plugins.module_finder.add_directory(i)
if options.list_dir: if options.list_dir:
# list all modules # list all modules
@ -138,16 +143,18 @@ def main():
# os.system("ls -C %s" % (path)) # os.system("ls -C %s" % (path))
if os.path.isdir(path): if os.path.isdir(path):
for module in os.listdir(path): for module in os.listdir(path):
if any(module.endswith(x) for x in BLACKLIST_EXTS):
continue
module_list.append(module) module_list.append(module)
for module in sorted(module_list): for module in sorted(set(module_list)):
if module in module_docs.BLACKLIST_MODULES: if module in module_docs.BLACKLIST_MODULES:
continue continue
filename = utils.plugins.module_finder.find_plugin(module) filename = utils.plugins.module_finder.find_plugin(module)
try: try:
doc = utils.module_docs.get_docstring(filename) doc = module_docs.get_docstring(filename)
desc = tty_ify(doc.get('short_description', '?')) desc = tty_ify(doc.get('short_description', '?'))
if len(desc) > 55: if len(desc) > 55:
desc = desc + '...' desc = desc + '...'
@ -158,8 +165,6 @@ def main():
sys.exit() sys.exit()
module_list = []
if len(args) == 0: if len(args) == 0:
p.print_help() p.print_help()
@ -171,11 +176,11 @@ def main():
utils.plugins.module_finder.print_paths())) utils.plugins.module_finder.print_paths()))
continue continue
if filename.endswith(".swp"): if any(filename.endswith(x) for x in BLACKLIST_EXTS):
continue continue
try: try:
doc = utils.module_docs.get_docstring(filename) doc = module_docs.get_docstring(filename)
except: except:
sys.stderr.write("ERROR: module %s missing documentation\n" % module) sys.stderr.write("ERROR: module %s missing documentation\n" % module)
continue continue