
# HG changeset patch # User Zachary T Welch zach@mandolincreekfarm.com # Fake Node ID 3c190b762714618ac4489532b6bdae2d760162b2
Fix pylint issues in logger.py (#159)
In addition to style/documentation clean ups, this fixes a call to an undefined routine (set_log_verbosity) that was renamed (set_log_level).
diff --git a/chirp/logger.py b/chirp/logger.py index 6e841bc..dc4884a 100644 --- a/chirp/logger.py +++ b/chirp/logger.py @@ -25,11 +25,11 @@ import os import sys import logging import argparse -import platform -from chirp import CHIRP_VERSION +from chirp import CHIRP_VERSION, platform
def version_string(): + """Return a string containing the versions of CHIRP, OS, and Python.""" args = (CHIRP_VERSION, platform.get_platform().os_version_string(), sys.version.split()[0]) @@ -37,12 +37,15 @@ def version_string():
class VersionAction(argparse.Action): + """Defines a class for printing the version string.""" def __call__(self, parser, namespace, value, option_string=None): + """Print the version string""" print version_string() sys.exit(1)
def add_version_argument(parser): + """Add the --version argument to the given argument parser.""" parser.add_argument("--version", action=VersionAction, nargs=0, help="Print version and exit")
@@ -56,6 +59,20 @@ log_level_names = {"critical": logging.CRITICAL,
class Logger(object): + """Singleton for managing CHIRP logging + + Set CHIRP_DEBUG in the environment for early console debugging. + It can be a number or a name; otherwise, the logging level is + set to 'debug' in order to maintain backward compatibility. + + Likewise, set CHIRP_LOG in the environment to the name of a log + file. CHIRP_LOG_LEVEL controls the logging level. + + If we're on Win32 or MacOS, we don't use the console; instead, + we create 'debug.log', redirect all output there, and set the + console logging handler level to DEBUG. To test this on Linux, + set CHIRP_DEBUG_LOG in the environment. + """
log_format = '[%(asctime)s] %(name)s - %(levelname)s: %(message)s'
@@ -66,9 +83,6 @@ class Logger(object):
self.LOG = logging.getLogger(__name__)
- # Set CHIRP_DEBUG in environment for early console debugging. - # It can be a number or a name; otherwise, level is set to 'debug' - # in order to maintain backward compatibility. CHIRP_DEBUG = os.getenv("CHIRP_DEBUG") self.early_level = logging.WARNING if CHIRP_DEBUG: @@ -80,10 +94,6 @@ class Logger(object): except KeyError: self.early_level = logging.DEBUG
- # If we're on Win32 or MacOS, we don't use the console; instead, - # we create 'debug.log', redirect all output there, and set the - # console logging handler level to DEBUG. To test this on Linux, - # set CHIRP_DEBUG_LOG in the environment. console_stream = None console_format = '%(levelname)s: %(message)s' if hasattr(sys, "frozen") or not os.isatty(0) \ @@ -102,14 +112,13 @@ class Logger(object): self.console.setFormatter(logging.Formatter(console_format)) self.logger.addHandler(self.console)
- # Set CHIRP_LOG in environment to the name of log file. logname = os.getenv("CHIRP_LOG") self.logfile = None if logname is not None: self.create_log_file(logname) level = os.getenv("CHIRP_LOG_LEVEL") if level is not None: - self.set_log_verbosity(level) + self.set_log_level(level) else: self.set_log_level(logging.DEBUG)
@@ -117,10 +126,11 @@ class Logger(object): self.LOG.debug(version_string())
def create_log_file(self, name): + """Create the log file specified by name.""" if self.logfile is None: self.logname = name # always truncate the log file - with file(name, "w") as fh: + with file(name, "w") as _: pass self.logfile = logging.FileHandler(name) format_str = self.log_format @@ -130,6 +140,7 @@ class Logger(object): self.logger.error("already logging to " + self.logname)
def set_verbosity(self, level): + """Set the logging level of the console""" self.LOG.debug("verbosity=%d", level) if level > logging.CRITICAL: level = logging.CRITICAL @@ -137,12 +148,14 @@ class Logger(object): self.console.setLevel(level)
def set_log_level(self, level): + """Set the logging level of the log file""" self.LOG.debug("log level=%d", level) if level > logging.CRITICAL: level = logging.CRITICAL self.logfile.setLevel(level)
def set_log_level_by_name(self, level): + """Set the logging level of the log file using a name""" self.set_log_level(log_level_names[level])
instance = None @@ -156,6 +169,7 @@ def is_visible(level):
def add_arguments(parser): + """Adds logging arguments to the given argument parser.""" parser.add_argument("-q", "--quiet", action="count", default=0, help="Decrease verbosity") parser.add_argument("-v", "--verbose", action="count", default=0, @@ -168,6 +182,14 @@ def add_arguments(parser):
def handle_options(options): + """Handles the logging command line options. + + If --verbose or --quiet were given, calculate the new logging level + for the console. + + If --log-file was given, create the log file. If --log-level + was given, adjust the log file level. + """ logger = Logger.instance
if options.verbose or options.quiet: diff --git a/tools/cpep8.lintful b/tools/cpep8.lintful index c3a2ebe..02ac62e 100644 --- a/tools/cpep8.lintful +++ b/tools/cpep8.lintful @@ -93,7 +93,6 @@ ./chirp/elib_intl.py ./chirp/errors.py ./chirp/import_logic.py -./chirp/logger.py ./chirp/memmap.py ./chirp/platform.py ./chirp/pyPEG.py