From 8e7b889bfea8cb6761c987f8f05c310b1bdc0927 Mon Sep 17 00:00:00 2001 From: Andreas Jaeger Date: Sat, 18 Jan 2014 17:15:02 +0100 Subject: [PATCH] Resolve pep8 comments Now all files pass the pep8 tests with hacking enabled. Change-Id: If804f1727464cea74ae48de1275470d69587efdb --- autogenerate-config-docs/autohelp.py | 13 +++-- autogenerate-config-docs/common.py | 42 +++++++++------- .../extract_swift_flags.py | 50 +++++++++++-------- autogenerate-config-docs/test/generator.py | 30 +++++------ tox.ini | 2 +- 5 files changed, 75 insertions(+), 62 deletions(-) diff --git a/autogenerate-config-docs/autohelp.py b/autogenerate-config-docs/autohelp.py index 6cd8cf0b..c4c0bb12 100755 --- a/autogenerate-config-docs/autohelp.py +++ b/autogenerate-config-docs/autohelp.py @@ -18,13 +18,12 @@ # # Must import this before argparse -import oslo.config +import oslo.config # NOQA import argparse -import os.path import sys -import common +import common # NOQA # this is for the internationalisation function in gettext import __builtin__ @@ -59,16 +58,16 @@ def main(): __import__(package_name) except ImportError as e: if args.verbose >= 1: - print str(e) - print "Failed to import: %s (%s)" % (package_name, e) + print(str(e)) + print("Failed to import: %s (%s)" % (package_name, e)) modules = common.import_modules(args.repo, package_name, verbose=args.verbose) options = common.OptionsCache(modules, verbose=args.verbose) if args.verbose > 0: - print "%s options imported from package %s." % (len(options), - str(package_name)) + print("%s options imported from package %s." % (len(options), + str(package_name))) if args.subcommand == 'create': common.create_flagmappings(package_name, options, verbose=args.verbose) diff --git a/autogenerate-config-docs/common.py b/autogenerate-config-docs/common.py index fc06aa11..382ba879 100644 --- a/autogenerate-config-docs/common.py +++ b/autogenerate-config-docs/common.py @@ -17,7 +17,8 @@ import importlib import os import sys -from xml.sax.saxutils import escape + +import xml.sax.saxutils import openstack.common.config.generator as generator @@ -36,15 +37,16 @@ def git_check(repo_path): repo = Repo(repo_path) assert repo.bare is False package_name = os.path.basename(repo.remotes.origin.url).rstrip('.git') - except: - print "\nThere is a problem verifying that the directory passed in" - print "is a valid git repository. Please try again.\n" + except Exception: + print("\nThere is a problem verifying that the directory passed in") + print("is a valid git repository. Please try again.\n") sys.exit(1) return package_name def import_modules(repo_location, package_name, verbose=0): - """ + """Import modules. + Loops through the repository, importing module by module to populate the configuration object (cfg.CONF) created from Oslo. """ @@ -71,7 +73,7 @@ def import_modules(repo_location, package_name, verbose=0): module = importlib.import_module(modname) modules[modname] = module if verbose >= 1: - print "imported %s" % modname + print("imported %s" % modname) except ImportError as e: """ work around modules that don't like being imported in @@ -79,7 +81,7 @@ def import_modules(repo_location, package_name, verbose=0): not affect the configuration options found at this stage """ if verbose >= 2: - print "Failed to import: %s (%s)" % (modname, e) + print("Failed to import: %s (%s)" % (modname, e)) continue return modules @@ -106,9 +108,9 @@ class OptionsCache(object): oldmod = self._opts_by_name[optname][0] if oldmod.startswith(modname + '/'): if verbose >= 2: - print (("Duplicate option name %s" + - " from %s and %s. Using %s.") % - (optname, modname, oldmod, oldmod)) + print(("Duplicate option name %s" + + " from %s and %s. Using %s.") % + (optname, modname, oldmod, oldmod)) elif modname.startswith(oldmod + '/'): self._opts_by_name[optname] = (modname, group, opt) if verbose >= 2: @@ -151,7 +153,8 @@ class OptionsCache(object): def write_docbook(package_name, options, verbose=0): - """ + """Write DocBook tables. + Prints a docbook-formatted table for every group of options. """ options_by_cat = {} @@ -203,7 +206,8 @@ def write_docbook(package_name, options, verbose=0): groups_file.write(' %s = %s\n' % (option.name, default)) groups_file.write(' (%s) %s\n' % - (type(option).__name__, escape(option.help))) + (type(option).__name__, + xml.sax.saxutils.escape(option.help))) groups_file.write(' \n') groups_file.write(' \n\ \n\ @@ -212,7 +216,8 @@ def write_docbook(package_name, options, verbose=0): def create_flagmappings(package_name, options, verbose=0): - """ + """Create a flagmappings file. + Create a flagmappings file. This will create a new file called $package_name.flagmappings with all the categories set to Unknown. """ @@ -222,7 +227,8 @@ def create_flagmappings(package_name, options, verbose=0): def update_flagmappings(package_name, options, verbose=0): - """ + """Update flagmappings file. + Update a flagmappings file, adding or removing entries as needed. This will create a new file $package_name.flagmappings.new with category information merged from the existing $package_name.flagmappings. @@ -260,10 +266,10 @@ def update_flagmappings(package_name, options, verbose=0): added_flags = (set([x[0] for x in updated_flags]) - set(original_flags.keys())) - print "\nRemoved Flags\n" + print("\nRemoved Flags\n") for line in sorted(removed_flags): - print line + print(line) - print "\nAdded Flags\n" + print("\nAdded Flags\n") for line in sorted(added_flags): - print line + print(line) diff --git a/autogenerate-config-docs/extract_swift_flags.py b/autogenerate-config-docs/extract_swift_flags.py index 3196da8c..1a09c3ea 100644 --- a/autogenerate-config-docs/extract_swift_flags.py +++ b/autogenerate-config-docs/extract_swift_flags.py @@ -17,7 +17,7 @@ import os import sys from xml.dom import minidom -from xml.sax.saxutils import escape +import xml.sax.saxutils #Swift configuration example files live in # swift/etc/*.conf-sample @@ -28,13 +28,15 @@ from xml.sax.saxutils import escape def parse_line(line): - """ - takes a line from a swift sample configuration file and attempts + """Parse a line. + + Takes a line from a swift sample configuration file and attempts to separate the lines with actual configuration option and default value from the rest. Returns None if the line doesn't appear to contain a valid configuration option = default value pair, and a pair of the config and its default if it does. """ + if '=' not in line: return None temp_line = line.strip('#').strip() @@ -49,9 +51,8 @@ def parse_line(line): def get_existing_options(optfiles): - """ - parses an existing XML table to compile a list of existing options - """ + """Parse an existing XML table to compile a list of existing options.""" + options = {} for optfile in optfiles: xmldoc = minidom.parse(optfile) @@ -71,11 +72,13 @@ def get_existing_options(optfiles): def extract_descriptions_from_devref(repo, options): - """ - loop through the devref RST files, looking for lines formatted + """Extract descriptions from devref RST files. + + Loop through the devref RST files, looking for lines formatted such that they might contain a description of a particular - option + option. """ + option_descs = {} rsts = glob.glob(repo + '/doc/source/*.rst') for rst in rsts: @@ -108,7 +111,7 @@ def extract_descriptions_from_devref(repo, options): def new_section_file(sample, current_section): section_filename = ('swift-' + - path.basename(sample).split('.conf')[0] + os.path.basename(sample).split('.conf')[0] + '-' + current_section.replace('[', ''). replace(']', '').replace(':', '-') @@ -122,7 +125,7 @@ def new_section_file(sample, current_section): \n\ \n\ \n\ \n\ @@ -137,12 +140,14 @@ def new_section_file(sample, current_section): def create_new_tables(repo, verbose): - """ - writes a set of docbook-formatted tables, one per section in swift + """Create new DocBook tables. + + Writes a set of DocBook-formatted tables, one per section in swift configuration files. Uses existing tables and swift devref as a source of truth in that order to determine helptext for options found in - sample config files + sample config files. """ + existing_tables = glob.glob('../../doc/common/tables/swift*xml') options = {} #use the existing tables to get a list of option names @@ -186,10 +191,12 @@ def create_new_tables(repo, verbose): else: option_desc = 'No help text available for this option' if verbose > 0: - print parsed_line[0] + "has no help text" - section_file.write('\n \n\ - \n' + ' \n' + ' ') if section_file is not None: @@ -200,9 +207,10 @@ def create_new_tables(repo, verbose): def main(repo, verbose=0): - """ - writes a set of docbook-formatted files, based on configuration sections - in swift sample configuration files + """Write DocBook formatted files. + + Writes a set of DocBook-formatted files, based on configuration sections + in swift sample configuration files. """ create_new_tables(repo, verbose) diff --git a/autogenerate-config-docs/test/generator.py b/autogenerate-config-docs/test/generator.py index ef761bac..937331cc 100755 --- a/autogenerate-config-docs/test/generator.py +++ b/autogenerate-config-docs/test/generator.py @@ -105,7 +105,7 @@ def generate(srcfiles): for group, opts in opts_by_group.items(): print_group_opts(group, opts) - print "# Total option count: %d" % OPTION_COUNT + print("# Total option count: %d" % OPTION_COUNT) def _import_module(mod_str): @@ -169,14 +169,14 @@ def _list_opts(obj): def print_group_opts(group, opts_by_module): - print "[%s]" % group + print("[%s]" % group) print global OPTION_COUNT for mod, opts in opts_by_module: OPTION_COUNT += len(opts) - print '#' - print '# Options defined in %s' % mod - print '#' + print('#') + print('# Options defined in %s' % mod) + print('#') print for opt in opts: _print_opt(opt) @@ -216,36 +216,36 @@ def _print_opt(opt): opt_type = None try: opt_type = OPTION_REGEX.search(str(type(opt))).group(0) - except (ValueError, AttributeError), err: + except (ValueError, AttributeError) as err: sys.stderr.write("%s\n" % str(err)) sys.exit(1) opt_help += ' (' + OPT_TYPES[opt_type] + ')' - print '#', "\n# ".join(textwrap.wrap(opt_help, WORDWRAP_WIDTH)) + print('#', "\n# ".join(textwrap.wrap(opt_help, WORDWRAP_WIDTH))) try: if opt_default is None: - print '#%s=' % opt_name + print('#%s=' % opt_name) elif opt_type == STROPT: assert(isinstance(opt_default, basestring)) - print '#%s=%s' % (opt_name, _sanitize_default(opt_default)) + print('#%s=%s' % (opt_name, _sanitize_default(opt_default))) elif opt_type == BOOLOPT: assert(isinstance(opt_default, bool)) - print '#%s=%s' % (opt_name, str(opt_default).lower()) + print('#%s=%s' % (opt_name, str(opt_default).lower())) elif opt_type == INTOPT: assert(isinstance(opt_default, int) and not isinstance(opt_default, bool)) - print '#%s=%s' % (opt_name, opt_default) + print('#%s=%s' % (opt_name, opt_default)) elif opt_type == FLOATOPT: assert(isinstance(opt_default, float)) - print '#%s=%s' % (opt_name, opt_default) + print('#%s=%s' % (opt_name, opt_default)) elif opt_type == LISTOPT: assert(isinstance(opt_default, list)) - print '#%s=%s' % (opt_name, ','.join(opt_default)) + print('#%s=%s' % (opt_name, ','.join(opt_default))) elif opt_type == MULTISTROPT: assert(isinstance(opt_default, list)) if not opt_default: opt_default = [''] for default in opt_default: - print '#%s=%s' % (opt_name, default) + print('#%s=%s' % (opt_name, default)) print except Exception: sys.stderr.write('Error in option "%s"\n' % opt_name) @@ -254,7 +254,7 @@ def _print_opt(opt): def main(): if len(sys.argv) < 2: - print "usage: %s [srcfile]...\n" % sys.argv[0] + print("usage: %s [srcfile]...\n" % sys.argv[0]) sys.exit(0) generate(sys.argv[1:]) diff --git a/tox.ini b/tox.ini index 01f18320..f81e5fcc 100644 --- a/tox.ini +++ b/tox.ini @@ -29,4 +29,4 @@ commands = {posargs} show-source = True ignore = E123,E125,H803 builtins = _ -exclude=.venv,.git,.tox,dist,doc,*openstack/common*,*lib/python*,*egg,build,autogenerate-config-docs +exclude=.venv,.git,.tox,dist,doc,*openstack/common*,*lib/python*,*egg,build
Description of configuration options for ' + current_section + ' in ' - + path.basename(sample) + + + os.path.basename(sample) + '
' + parsed_line[0] + '=' + - escape(str(parsed_line[1])) + + print(parsed_line[0] + "has no help text") + section_file.write('\n
' + + parsed_line[0] + '=' + + xml.sax.saxutils.escape( + str(parsed_line[1])) + '' + option_desc + '