From 6d33a03a67fb0969d2d1ce748b7947b8e7a69e55 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Fri, 16 Sep 2011 12:30:32 -0400 Subject: [PATCH] Add the PYSAWSIM_LOG_LEVEL environmental variable. --- pysawsim/__init__.py | 21 ++++++++++++++++++--- pysawsim/parameter_error.py | 2 ++ pysawsim/parameter_scan.py | 2 ++ pysawsim/sawsim.py | 5 ++++- pysawsim/sawsim_histogram.py | 5 ++++- 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/pysawsim/__init__.py b/pysawsim/__init__.py index 9cb3a97..19cfcba 100644 --- a/pysawsim/__init__.py +++ b/pysawsim/__init__.py @@ -47,7 +47,8 @@ Just go crazy with doctests and unittests; nose_ will find them. import logging import logging.handlers -import sys +import sys as _sys +import os as _os _multiprocess_shared_ = True @@ -59,10 +60,24 @@ This module cannot be split because _log setup is not re-entrant. __version__ = '0.10' # match sawsim version +PYSAWSIM_LOG_LEVEL_MSG = """ +You can control the log verbosity with the `PYSAWSIM_LOG_LEVEL` +environmental variable. Set it to one of the level names in Python's +`logging` module (e.g. `DEBUG`). +""" + def log(): return logging.getLogger('pysawsim') -def add_stderr_log_handler(level=logging.DEBUG): +def add_stderr_log_handler(level=None): + if level == None: + level_string = _os.environ.get('PYSAWSIM_LOG_LEVEL', 'WARNING') + try: + level = getattr(logging, level_string) + except AttributeError: + _sys.stderr.write( + 'unrecognized PYSAWSIM_LOG_LEVEL: %s\n' % level_string) + raise _log = log() _log.setLevel(level) console = logging.StreamHandler() @@ -70,4 +85,4 @@ def add_stderr_log_handler(level=logging.DEBUG): console.setFormatter(formatter) _log.addHandler(console) -add_stderr_log_handler(logging.WARNING) +add_stderr_log_handler() diff --git a/pysawsim/parameter_error.py b/pysawsim/parameter_error.py index f56ba26..44031f2 100644 --- a/pysawsim/parameter_error.py +++ b/pysawsim/parameter_error.py @@ -22,6 +22,7 @@ import numpy +from . import PYSAWSIM_LOG_LEVEL_MSG as _PYSAWSIM_LOG_LEVEL_MSG from .histogram import Histogram from .parameter_scan import ( EXAMPLE_HISTOGRAM_FILE_CONTENTS, HistogramMatcher, @@ -122,6 +123,7 @@ def main(argv=None): '`` should mark the left-hand side of the bin, and', 'all bins should be of equal width (so we know where the last', 'one ends).', + _PYSAWSIM_LOG_LEVEL_MSG, ]) parser = OptionParser(usage, epilog=epilog) parser.format_epilog = lambda formatter: epilog+'\n' diff --git a/pysawsim/parameter_scan.py b/pysawsim/parameter_scan.py index 439fd0b..0f30fb6 100644 --- a/pysawsim/parameter_scan.py +++ b/pysawsim/parameter_scan.py @@ -31,6 +31,7 @@ import numpy import pylab from . import log +from . import PYSAWSIM_LOG_LEVEL_MSG as _PYSAWSIM_LOG_LEVEL_MSG from .histogram import Histogram from .sawsim_histogram import sawsim_histogram from .sawsim import SawsimRunner @@ -381,6 +382,7 @@ def main(argv=None): '`` should mark the left-hand side of the bin, and', 'all bins should be of equal width (so we know where the last', 'one ends).', + PYSAWSIM_LOG_LEVEL_MSG, ]) parser = OptionParser(usage, epilog=epilog) parser.format_epilog = lambda formatter: epilog+'\n' diff --git a/pysawsim/sawsim.py b/pysawsim/sawsim.py index 0f85869..da726c7 100644 --- a/pysawsim/sawsim.py +++ b/pysawsim/sawsim.py @@ -37,6 +37,7 @@ import shutil from uuid import uuid4 from . import __version__ +from . import PYSAWSIM_LOG_LEVEL_MSG as _PYSAWSIM_LOG_LEVEL_MSG from .manager import MANAGERS, get_manager, InvokeJob @@ -302,9 +303,11 @@ def main(argv=None): epilog = '\n'.join([ 'Python wrapper around `sawsim`. Distribute `N` runs using', 'one of the possible job "managers". Also supports caching', - 'results to speed future runs.' + 'results to speed future runs.', + _PYSAWSIM_LOG_LEVEL_MSG, ]) parser = OptionParser(usage, epilog=epilog) + parser.format_epilog = lambda formatter: epilog+'\n' for option in sr.optparse_options: parser.add_option(option) diff --git a/pysawsim/sawsim_histogram.py b/pysawsim/sawsim_histogram.py index 14b94ed..d349316 100644 --- a/pysawsim/sawsim_histogram.py +++ b/pysawsim/sawsim_histogram.py @@ -19,6 +19,7 @@ import numpy +from . import PYSAWSIM_LOG_LEVEL_MSG as _PYSAWSIM_LOG_LEVEL_MSG from .histogram import Histogram from .manager import MANAGERS, get_manager from .sawsim import SawsimRunner @@ -67,8 +68,10 @@ def main(argv=None): epilog = '\n'.join([ 'Generate an unfolding force histogram from a series of `sawsim`', 'runs.', - ]) + _PYSAWSIM_LOG_LEVEL_MSG, + ]) parser = OptionParser(usage, epilog=epilog) + parser.format_epilog = lambda formatter: epilog+'\n' for option in sr.optparse_options: parser.add_option(option) parser.add_option('-w', '--bin-width', dest='bin_width', -- 2.26.2