Moving now from getopt to optparse

Let me know if we can move to another library instead ;-)
This commit is contained in:
Dag Wieers 2012-10-11 18:11:33 +02:00
parent d6b3f40cb7
commit 2786149bdc

View file

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