docs: PEP8 compliance (#24681)

- Make PEP8 compliant
This commit is contained in:
Dag Wieers 2017-05-30 19:08:25 +02:00 committed by John R Barker
parent 44121352fe
commit 47738eb1dd
5 changed files with 131 additions and 151 deletions

View file

@ -16,22 +16,21 @@
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import print_function
__metaclass__ = type
import os
import glob
import sys
import yaml
import re
import optparse
import datetime
import cgi
import datetime
import glob
import optparse
import os
import re
import sys
import warnings
from collections import defaultdict
import yaml
from collections import defaultdict
from jinja2 import Environment, FileSystemLoader
from six import iteritems
@ -47,23 +46,23 @@ from ansible.utils import plugin_docs
TO_OLD_TO_BE_NOTABLE = 1.3
# Get parent directory of the directory this script lives in
MODULEDIR=os.path.abspath(os.path.join(
MODULEDIR = os.path.abspath(os.path.join(
os.path.dirname(os.path.realpath(__file__)), os.pardir, 'lib', 'ansible', 'modules'
))
# The name of the DOCUMENTATION template
EXAMPLE_YAML=os.path.abspath(os.path.join(
EXAMPLE_YAML = os.path.abspath(os.path.join(
os.path.dirname(os.path.realpath(__file__)), os.pardir, 'examples', 'DOCUMENTATION.yml'
))
_ITALIC = re.compile(r"I\(([^)]+)\)")
_BOLD = re.compile(r"B\(([^)]+)\)")
_BOLD = re.compile(r"B\(([^)]+)\)")
_MODULE = re.compile(r"M\(([^)]+)\)")
_URL = re.compile(r"U\(([^)]+)\)")
_CONST = re.compile(r"C\(([^)]+)\)")
_URL = re.compile(r"U\(([^)]+)\)")
_CONST = re.compile(r"C\(([^)]+)\)")
DEPRECATED = b" (D)"
#####################################################################################
def rst_ify(text):
''' convert symbols like I(this is in italics) to valid restructured text '''
@ -79,7 +78,6 @@ def rst_ify(text):
return t
#####################################################################################
def html_ify(text):
''' convert symbols like I(this is in italics) to valid HTML '''
@ -94,36 +92,30 @@ def html_ify(text):
return t
#####################################################################################
def rst_fmt(text, fmt):
''' helper for Jinja2 to do format strings '''
return fmt % (text)
#####################################################################################
def rst_xline(width, char="="):
''' return a restructured text line of a given length '''
return char * width
#####################################################################################
def write_data(text, options, outputname, module):
''' dumps module output to a file or the screen, as requested '''
if options.output_dir is not None:
fname = os.path.join(options.output_dir, outputname % module)
fname = fname.replace(".py","")
fname = fname.replace(".py", "")
f = open(fname, 'wb')
f.write(to_bytes(text))
f.close()
else:
print(text)
#####################################################################################
def list_modules(module_dir, depth=0):
''' returns a hash of categories, each category being a hash of module names to file paths '''
@ -163,7 +155,7 @@ def list_modules(module_dir, depth=0):
continue
if module.startswith("_") and os.path.islink(module_path):
source = os.path.splitext(os.path.basename(os.path.realpath(module_path)))[0]
module = module.replace("_","",1)
module = module.replace("_", "", 1)
aliases[source].add(module)
continue
@ -176,7 +168,6 @@ def list_modules(module_dir, depth=0):
return module_info, categories, aliases
#####################################################################################
def generate_parser():
''' generate an optparse parser '''
@ -197,15 +188,13 @@ def generate_parser():
p.add_option('-V', action='version', help='Show version number and exit')
return p
#####################################################################################
def jinja2_environment(template_dir, typ):
env = Environment(loader=FileSystemLoader(template_dir),
variable_start_string="@{",
variable_end_string="}@",
trim_blocks=True,
)
variable_start_string="@{",
variable_end_string="}@",
trim_blocks=True)
env.globals['xline'] = rst_xline
if typ == 'rst':
@ -220,7 +209,7 @@ def jinja2_environment(template_dir, typ):
return env, template, outputname
#####################################################################################
def too_old(added):
if not added:
return False
@ -233,6 +222,7 @@ def too_old(added):
return False
return (added_float < TO_OLD_TO_BE_NOTABLE)
def process_module(module, options, env, template, outputname, module_map, aliases):
fname = module_map[module]
@ -249,7 +239,7 @@ def process_module(module, options, env, template, outputname, module_map, alias
if os.path.islink(fname):
return # ignore, its an alias
deprecated = True
module = module.replace("_","",1)
module = module.replace("_", "", 1)
print("rendering: %s" % module)
@ -271,7 +261,7 @@ def process_module(module, options, env, template, outputname, module_map, alias
all_keys = []
if not 'version_added' in doc:
if 'version_added' not in doc:
sys.exit("*** ERROR: missing version_added in: %s ***\n" % module)
added = 0
@ -285,39 +275,39 @@ def process_module(module, options, env, template, outputname, module_map, alias
del doc['version_added']
if 'options' in doc and doc['options']:
for (k,v) in iteritems(doc['options']):
for (k, v) in iteritems(doc['options']):
# don't show version added information if it's too old to be called out
if 'version_added' in doc['options'][k] and too_old(doc['options'][k]['version_added']):
del doc['options'][k]['version_added']
if not 'description' in doc['options'][k]:
if 'description' not in doc['options'][k]:
raise AnsibleError("Missing required description for option %s in %s " % (k, module))
required_value = doc['options'][k].get('required', False)
if not isinstance(required_value, bool):
raise AnsibleError("Invalid required value '%s' for option '%s' in '%s' (must be truthy)" % (required_value, k, module))
if not isinstance(doc['options'][k]['description'],list):
if not isinstance(doc['options'][k]['description'], list):
doc['options'][k]['description'] = [doc['options'][k]['description']]
all_keys.append(k)
all_keys = sorted(all_keys)
doc['option_keys'] = all_keys
doc['filename'] = fname
doc['docuri'] = doc['module'].replace('_', '-')
doc['now_date'] = datetime.date.today().strftime('%Y-%m-%d')
doc['ansible_version'] = options.ansible_version
doc['plainexamples'] = examples #plain text
doc['metadata'] = metadata
doc['option_keys'] = all_keys
doc['filename'] = fname
doc['docuri'] = doc['module'].replace('_', '-')
doc['now_date'] = datetime.date.today().strftime('%Y-%m-%d')
doc['ansible_version'] = options.ansible_version
doc['plainexamples'] = examples # plain text
doc['metadata'] = metadata
if returndocs:
try:
doc['returndocs'] = yaml.safe_load(returndocs)
doc['returndocs'] = yaml.safe_load(returndocs)
except:
print("could not load yaml: %s" % returndocs)
raise
else:
doc['returndocs'] = None
doc['returndocs'] = None
# here is where we build the table of contents...
@ -328,7 +318,6 @@ def process_module(module, options, env, template, outputname, module_map, alias
write_data(text, options, outputname, module)
return doc['short_description']
#####################################################################################
def print_modules(module, category_file, deprecated, options, env, template, outputname, module_map, aliases):
modstring = module
@ -340,9 +329,10 @@ def print_modules(module, category_file, deprecated, options, env, template, out
category_file.write(b" %s - %s <%s_module>\n" % (to_bytes(modstring), to_bytes(rst_ify(module_map[module][1])), to_bytes(modname)))
def process_category(category, categories, options, env, template, outputname):
### FIXME:
# FIXME:
# We no longer conceptually deal with a mapping of category names to
# modules to file paths. Instead we want several different records:
# (1) Mapping of module names to file paths (what's presently used
@ -367,7 +357,7 @@ def process_category(category, categories, options, env, template, outputname):
# start a new category file
category = category.replace("_"," ")
category = category.replace("_", " ")
category = category.title()
modules = []
@ -406,12 +396,12 @@ def process_category(category, categories, options, env, template, outputname):
sections.sort()
for section in sections:
category_file.write(b"\n%s\n%s\n\n" % (to_bytes(section.replace("_"," ").title()), b'-' * len(section)))
category_file.write(b"\n%s\n%s\n\n" % (to_bytes(section.replace("_", " ").title()), b'-' * len(section)))
category_file.write(b".. toctree:: :maxdepth: 1\n\n")
section_modules = list(module_map[section].keys())
section_modules.sort(key=lambda k: k[1:] if k.startswith('_') else k)
#for module in module_map[section]:
# for module in module_map[section]:
for module in (m for m in section_modules if m in module_info):
print_modules(module, category_file, deprecated, options, env, template, outputname, module_info, aliases)
@ -424,7 +414,6 @@ def process_category(category, categories, options, env, template, outputname):
# TODO: end a new category file
#####################################################################################
def validate_options(options):
''' validate option parser options '''
@ -436,7 +425,6 @@ def validate_options(options):
if not options.template_dir:
sys.exit("--template-dir must be specified")
#####################################################################################
def main():
@ -465,9 +453,7 @@ def main():
for category in category_names:
category_list_file.write(b" list_of_%s_modules\n" % to_bytes(category))
#
# Import all the docs into memory
#
module_map = mod_info.copy()
for modname in module_map:
@ -477,11 +463,10 @@ def main():
else:
categories['all'][modname] = (categories['all'][modname], result)
#
# Render all the docs to rst via category pages
#
for category in category_names:
process_category(category, categories, options, env, template, outputname)
if __name__ == '__main__':
main()