progfiguration.cli.util

Utilities for command-line programs

Attributes

progfiguration_log_levels

Log levels that our command-line programs can configure

Functions

progfiguration_error_handler(→ int)

Special error handler

idb_excepthook(type, value, tb)

Call an interactive debugger in post-mortem mode

syslog_excepthook(type, value, tb)

Send an unhandled exception to syslog

configure_logging(→ None)

Configure logging for the command-line programs

CommaSeparatedStrList(→ List[str])

Convert a string with commas into a list of strings

CommaSeparatedDict(→ dict[str, str])

Convert a string with commas into a dict

Module Contents

progfiguration.cli.util.progfiguration_log_levels = ['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG', 'NOTSET', 'NONE']

Log levels that our command-line programs can configure Taken from the logging module, see <https://docs.python.org/3/library/logging.html#logging-levels>. Includes NONE, which is a special value that means “don’t configure logging at all”.

progfiguration.cli.util.progfiguration_error_handler(func: collections.abc.Callable[[List[str]], int], *arguments: List[str]) int

Special error handler Wrap a main() function in this to properly handle broken pipes. The EPIPE signal is sent if you run e.g. script.py | head. Wrapping the main function with this one exits cleanly if that happens. See <https://docs.python.org/3/library/signal.html#note-on-sigpipe>. Params: * func: The main function to wrap, which should take a list of arguments and return an exit code. * arguments: A list of arguments to pass to func, probably from sys.argv[1:]. Returns: * The exit code from func.

progfiguration.cli.util.idb_excepthook(type, value, tb)

Call an interactive debugger in post-mortem mode If you do sys.excepthook = idb_excepthook, then an interactive debugger will be spawned at an unhandled exception

progfiguration.cli.util.syslog_excepthook(type, value, tb)

Send an unhandled exception to syslog If you do sys.excepthook = syslog_excepthook, then unhandled exceptions will be sent to syslog before the program exits.

progfiguration.cli.util.configure_logging(log_stderr: str, log_syslog: str = 'NONE') None

Configure logging for the command-line programs A convenience function that sets up logging based on the command-line arguments. Should only be called once. Params: * log_stderr: The log level for stderr, one of progfiguration_log_levels. * log_syslog: The log level for syslog, one of progfiguration_log_levels.

progfiguration.cli.util.CommaSeparatedStrList(cssl: str) List[str]

Convert a string with commas into a list of strings

Useful as a type= argument to argparse.add_argument()

progfiguration.cli.util.CommaSeparatedDict(csd: str) dict[str, str]

Convert a string with commas into a dict

Useful as a type= argument to argparse.add_argument()