From eac71fc1109f2edc6b71e62a6cff38d762bebe63 Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Tue, 25 Sep 2012 11:49:15 -0400 Subject: expanded pylint coverage --- src/lib/Bcfg2/Options.py | 102 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 72 insertions(+), 30 deletions(-) (limited to 'src/lib/Bcfg2/Options.py') diff --git a/src/lib/Bcfg2/Options.py b/src/lib/Bcfg2/Options.py index ff7c3ce70..a5436dbd0 100644 --- a/src/lib/Bcfg2/Options.py +++ b/src/lib/Bcfg2/Options.py @@ -8,12 +8,12 @@ import re import shlex import sys import Bcfg2.Client.Tools -# Compatibility imports from Bcfg2.Compat import ConfigParser from Bcfg2.version import __version__ class OptionFailure(Exception): + """ raised when malformed Option objects are instantiated """ pass DEFAULT_CONFIG_LOCATION = '/etc/bcfg2.conf' @@ -21,6 +21,9 @@ DEFAULT_INSTALL_PREFIX = '/usr' class DefaultConfigParser(ConfigParser.ConfigParser): + """ A config parser that can be used to query options with default + values in the event that the option is not found """ + def __init__(self, *args, **kwargs): """Make configuration options case sensitive""" ConfigParser.ConfigParser.__init__(self, *args, **kwargs) @@ -59,7 +62,11 @@ class DefaultConfigParser(ConfigParser.ConfigParser): class Option(object): - def __init__(self, desc, default, cmd=False, odesc=False, + """ a single option, which might be read from the command line, + environment, or config file """ + + # pylint: disable=C0103,R0913 + def __init__(self, desc, default, cmd=None, odesc=False, env=False, cf=False, cook=False, long_arg=False, deprecated_cf=None): self.desc = desc @@ -69,7 +76,7 @@ class Option(object): if not self.long: if cmd and (cmd[0] != '-' or len(cmd) != 2): raise OptionFailure("Poorly formed command %s" % cmd) - elif cmd and (not cmd.startswith('--')): + elif cmd and not cmd.startswith('--'): raise OptionFailure("Poorly formed command %s" % cmd) self.odesc = odesc self.env = env @@ -79,8 +86,13 @@ class Option(object): if not odesc and not cook and isinstance(self.default, bool): self.boolean = True self.cook = cook + self.value = None + # pylint: enable=C0103,R0913 def get_cooked_value(self, value): + """ get the value of this option after performing any option + munging specified in the 'cook' keyword argument to the + constructor """ if self.boolean: return True if self.cook: @@ -112,6 +124,7 @@ class Option(object): return "".join(rv) def buildHelpMessage(self): + """ build the help message for this option """ vals = [] if not self.cmd: return '' @@ -121,11 +134,13 @@ class Option(object): else: vals.append("%s %s" % (self.cmd, self.odesc)) else: - vals.append(self.cmd) + vals.append(self.cmd) vals.append(self.desc) return " %-28s %s\n" % tuple(vals) def buildGetopt(self): + """ build a string suitable for describing this short option + to getopt """ gstr = '' if self.long: return gstr @@ -136,12 +151,18 @@ class Option(object): return gstr def buildLongGetopt(self): + """ build a string suitable for describing this long option to + getopt """ if self.odesc: return self.cmd[2:] + '=' else: return self.cmd[2:] def parse(self, opts, rawopts, configparser=None): + """ parse a single option. try parsing the data out of opts + (the results of getopt), rawopts (the raw option string), the + environment, and finally the config parser. either opts or + rawopts should be provided, but not both """ if self.cmd and opts: # Processing getopted data optinfo = [opt[1] for opt in opts if opt[0] == self.cmd] @@ -170,7 +191,8 @@ class Option(object): pass if self.deprecated_cf: try: - self.value = self.get_cooked_value(configparser.get(*self.deprecated_cf)) + self.value = self.get_cooked_value( + configparser.get(*self.deprecated_cf)) print("Warning: [%s] %s is deprecated, use [%s] %s instead" % (self.deprecated_cf[0], self.deprecated_cf[1], self.cf[0], self.cf[1])) @@ -184,9 +206,13 @@ class Option(object): class OptionSet(dict): + """ a set of Option objects that interfaces with getopt and + DefaultConfigParser to populate a dict of