Commit 69ad124b by Michael DeHaan

Merge pull request #1298 from dagwieers/doc-optparse

Moving now from getopt to optparse
parents 5849ab31 2786149b
...@@ -25,7 +25,7 @@ import json ...@@ -25,7 +25,7 @@ import json
import ast import ast
from jinja2 import Environment, FileSystemLoader from jinja2 import Environment, FileSystemLoader
import re import re
import getopt import optparse
import time import time
import datetime import datetime
import subprocess import subprocess
...@@ -169,9 +169,9 @@ def get_docstring(filename, verbose=False): ...@@ -169,9 +169,9 @@ def get_docstring(filename, verbose=False):
return doc return doc
def return_data(text, args, outputname, module): def return_data(text, options, outputname, module):
if args.output_dir is not None: if options.output_dir is not None:
f = open(os.path.join(args.output_dir, outputname % module), 'w') f = open(os.path.join(options.output_dir, outputname % module), 'w')
f.write(text) f.write(text)
f.close() f.close()
else: else:
...@@ -179,91 +179,80 @@ def return_data(text, args, outputname, module): ...@@ -179,91 +179,80 @@ def return_data(text, args, outputname, module):
def main(): def main():
class Object(object):
pass
type_choices = ['html', 'latex', 'man', 'rst', 'json']
args = Object()
args.ansible_version = 'unknown'
args.module_dir = MODULEDIR
args.template_dir = 'hacking/templates'
args.type = 'latex'
args.module_list = []
args.verbose = False
args.output_dir = None
args.includes_file = None
args.do_boilerplate = False
try: p = optparse.OptionParser(
opts, arguments = getopt.getopt(sys.argv[1:], 'A:M:T:t:m:vo:I:GVh', version='%prog 1.0',
[ 'ansible-version=', 'module-dir=', 'template-dir=', 'type=', usage='usage: %prog [options] arg1 arg2',
'module=', 'verbose', 'output-dir=', 'includes-file=', description='Convert Ansible module DOCUMENTATION strings to other formats',
'generate', 'version', 'help', ]) )
except getopt.error, e:
print >>sys.stderr, 'ERROR: %s'% str(e) p.add_option("-A", "--ansible-version",
sys.exit(1) action="store",
dest="ansible_version",
for opt, arg in opts: default="unknown",
if opt in ('-A', '--ansible-version'): help="Ansible version number")
args.ansible_version = arg p.add_option("-M", "--module-dir",
elif opt in ('-M', '--module-dir'): action="store",
args.module_dir = arg dest="module_dir",
elif opt in ('-T', '--template-dir'): default=MODULEDIR,
args.template_dir = arg help="Ansible modules/ directory")
elif opt in ('-t', '--type'): p.add_option("-T", "--template-dir",
args.type = arg action="store",
if args.type not in type_choices: dest="template_dir",
print >>sys.stderr, 'ERROR: Type %s not in possible types %s.' % (args.type, type_choices) default="hacking/templates",
sys.exit(1) help="directory containing Jinja2 templates")
elif opt in ('-m', '--module'): p.add_option("-t", "--type",
args.module_list.append(arg) action='store',
elif opt in ('-v', '--verbose'): dest='type',
args.verbose = True choices=['html', 'latex', 'man', 'rst', 'json'],
elif opt in ('-o', '--output-dir'): default='latex',
args.output_dir = arg help="Output type")
elif opt in ('-I', '--includes-file'): p.add_option("-m", "--module",
args.includes_file = arg action='append',
elif opt in ('-G', '--generate'): default=[],
args.do_boilerplate = True dest='module_list',
elif opt in ('-V', '--version'): help="Add modules to process in module_dir")
print >>sys.stderr, '%(prog)s 1.0' p.add_option("-v", "--verbose",
elif opt in ('-h', '--help'): action='store_true',
print >>sys.stderr, '''Convert Ansible module DOCUMENTATION strings to other formats default=False,
help="Verbose")
-A, --ansible-version= Ansible version number p.add_option("-o", "--output-dir",
-M, --module-dir= Ansible modules/ directory action="store",
-T, --template-dir= Directory containing Jinja2 templates dest="output_dir",
-t, --type= Output type default=None,
-m, --module= Add modules to process in module_dir help="Output directory for module files")
-v, --verbose Verbose p.add_option("-I", "--includes-file",
-o, --output-dir= Output directory for module files action="store",
-I, --includes-file= Create a file containing list of processed modules dest="includes_file",
-G, --generate Generate boilerplate DOCUMENTATION to stdout default=None,
''' help="Create a file containing list of processed modules")
sys.exit(0) p.add_option("-G", "--generate",
else: action="store_true",
print >>sys.stderr, 'ERROR: Option %s unknown to getopt' % opt dest="do_boilerplate",
sys.exit(1) default=False,
help="generate boilerplate DOCUMENTATION to stdout")
# print "M: %s" % args.module_dir p.add_option('-V', action='version')
# print "t: %s" % args.type
# print "m: %s" % args.module_list (options, args) = p.parse_args()
# print "v: %s" % args.verbose
# print "M: %s" % options.module_dir
if args.do_boilerplate: # print "t: %s" % options.type
# print "m: %s" % options.module_list
# print "v: %s" % options.verbose
if options.do_boilerplate:
boilerplate() boilerplate()
sys.exit(0) sys.exit(0)
if not args.module_dir: if not options.module_dir:
print "Need module_dir" print "Need module_dir"
sys.exit(1) sys.exit(1)
if not args.template_dir: if not options.template_dir:
print "Need template_dir" print "Need template_dir"
sys.exit(1) sys.exit(1)
env = Environment(loader=FileSystemLoader(args.template_dir), env = Environment(loader=FileSystemLoader(options.template_dir),
variable_start_string="@{", variable_start_string="@{",
variable_end_string="}@", variable_end_string="}@",
trim_blocks=True, trim_blocks=True,
...@@ -271,25 +260,25 @@ def main(): ...@@ -271,25 +260,25 @@ def main():
env.globals['xline'] = rst_xline env.globals['xline'] = rst_xline
if args.type == 'latex': if options.type == 'latex':
env.filters['jpfunc'] = latex_ify env.filters['jpfunc'] = latex_ify
template = env.get_template('latex.j2') template = env.get_template('latex.j2')
outputname = "%s.tex" outputname = "%s.tex"
includecmt = "% generated code\n" includecmt = "% generated code\n"
includefmt = "\\input %s\n" includefmt = "\\input %s\n"
if args.type == 'html': if options.type == 'html':
env.filters['jpfunc'] = html_ify env.filters['jpfunc'] = html_ify
template = env.get_template('html.j2') template = env.get_template('html.j2')
outputname = "%s.html" outputname = "%s.html"
includecmt = "" includecmt = ""
includefmt = "" includefmt = ""
if args.type == 'man': if options.type == 'man':
env.filters['jpfunc'] = man_ify env.filters['jpfunc'] = man_ify
template = env.get_template('man.j2') template = env.get_template('man.j2')
outputname = "ansible.%s.3" outputname = "ansible.%s.3"
includecmt = "" includecmt = ""
includefmt = "" includefmt = ""
if args.type == 'rst': if options.type == 'rst':
env.filters['jpfunc'] = rst_ify env.filters['jpfunc'] = rst_ify
env.filters['html_ify'] = html_ify env.filters['html_ify'] = html_ify
env.filters['fmt'] = rst_fmt env.filters['fmt'] = rst_fmt
...@@ -298,28 +287,28 @@ def main(): ...@@ -298,28 +287,28 @@ def main():
outputname = "%s.rst" outputname = "%s.rst"
includecmt = ".. Generated by module_formatter\n" includecmt = ".. Generated by module_formatter\n"
includefmt = ".. include:: modules/%s.rst\n" includefmt = ".. include:: modules/%s.rst\n"
if args.type == 'json': if options.type == 'json':
env.filters['jpfunc'] = json_ify env.filters['jpfunc'] = json_ify
outputname = "%s.json" outputname = "%s.json"
includecmt = "" includecmt = ""
includefmt = "" includefmt = ""
if args.type == 'js': if options.type == 'js':
env.filters['jpfunc'] = js_ify env.filters['jpfunc'] = js_ify
template = env.get_template('js.j2') template = env.get_template('js.j2')
outputname = "%s.js" outputname = "%s.js"
if args.includes_file is not None and includefmt != "": if options.includes_file is not None and includefmt != "":
incfile = open(args.includes_file, "w") incfile = open(options.includes_file, "w")
incfile.write(includecmt) incfile.write(includecmt)
# Temporary variable required to genrate aggregated content in 'js' format. # Temporary variable required to genrate aggregated content in 'js' format.
js_data = [] js_data = []
for module in sorted(os.listdir(args.module_dir)): for module in sorted(os.listdir(options.module_dir)):
if len(args.module_list): if len(options.module_list):
if not module in args.module_list: if not module in options.module_list:
continue continue
fname = os.path.join(args.module_dir, module) fname = os.path.join(options.module_dir, module)
extra = os.path.join("inc", "%s.tex" % module) extra = os.path.join("inc", "%s.tex" % module)
if fname.endswith(".swp"): if fname.endswith(".swp"):
...@@ -327,7 +316,7 @@ def main(): ...@@ -327,7 +316,7 @@ def main():
print " processing module source ---> %s" % fname print " processing module source ---> %s" % fname
if args.type == 'js': if options.type == 'js':
if fname.endswith(".json"): if fname.endswith(".json"):
f = open(fname) f = open(fname)
j = json.load(f) j = json.load(f)
...@@ -335,7 +324,7 @@ def main(): ...@@ -335,7 +324,7 @@ def main():
js_data.append(j) js_data.append(j)
continue continue
doc = get_docstring(fname, verbose=args.verbose) doc = get_docstring(fname, verbose=options.verbose)
if doc is None and module not in BLACKLIST_MODULES: if doc is None and module not in BLACKLIST_MODULES:
sys.stderr.write("*** ERROR: CORE MODULE MISSING DOCUMENTATION: %s ***\n" % module) sys.stderr.write("*** ERROR: CORE MODULE MISSING DOCUMENTATION: %s ***\n" % module)
...@@ -346,34 +335,34 @@ def main(): ...@@ -346,34 +335,34 @@ def main():
doc['filename'] = fname doc['filename'] = fname
doc['docuri'] = doc['module'].replace('_', '-') doc['docuri'] = doc['module'].replace('_', '-')
doc['now_date'] = datetime.date.today().strftime('%Y-%m-%d') doc['now_date'] = datetime.date.today().strftime('%Y-%m-%d')
doc['ansible_version'] = args.ansible_version doc['ansible_version'] = options.ansible_version
if args.includes_file is not None and includefmt != "": if options.includes_file is not None and includefmt != "":
incfile.write(includefmt % module) incfile.write(includefmt % module)
if args.verbose: if options.verbose:
print json.dumps(doc, indent=4) print json.dumps(doc, indent=4)
if args.type == 'latex': if options.type == 'latex':
if os.path.exists(extra): if os.path.exists(extra):
f = open(extra) f = open(extra)
extradata = f.read() extradata = f.read()
f.close() f.close()
doc['extradata'] = extradata doc['extradata'] = extradata
if args.type == 'json': if options.type == 'json':
text = json.dumps(doc, indent=2) text = json.dumps(doc, indent=2)
else: else:
text = template.render(doc) text = template.render(doc)
return_data(text, args, outputname, module) return_data(text, options, outputname, module)
if args.type == 'js': if options.type == 'js':
docs = {} docs = {}
docs['json'] = json.dumps(js_data, indent=2) docs['json'] = json.dumps(js_data, indent=2)
text = template.render(docs) text = template.render(docs)
return_data(text, args, outputname, 'modules') return_data(text, options, outputname, 'modules')
#def boilerplate(): #def boilerplate():
# #
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment