Files
M. Anthony Aiello 8737b1d748 Feature/style checks (#45)
Support pre-commit checks used in AdaCore/e3-core

These are a good example of best style practices for python projects. Adding the associated badges to the README (even though it needs to be rewritten) so the commit is complete and consistent.

Documentation of how to set up and run the checks is in CONTRIBUTING.md
2020-06-23 09:54:41 -04:00

56 lines
1.5 KiB
Python

"""
Logging configuration for install scripts.
This module also contains text-wrapping functions.
"""
from __future__ import annotations
import logging
import textwrap
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from argparse import Namespace
STREAM_FMT = "%(levelname)-8s %(message)s"
FILE_FMT = "%(asctime)s: %(name)-24s: %(levelname)-8s %(message)s"
def configure_logging(args: Namespace) -> None:
"""
Configure the log based on parsed command-line arguments.
To be used with `support.arguments.add_logging_group`.
"""
if args.verbose == 1:
level = logging.INFO
elif args.verbose == 2:
level = logging.DEBUG
else:
level = args.loglevel
logging.getLogger("").setLevel(logging.DEBUG)
streamHandler = logging.StreamHandler()
streamHandler.setFormatter(logging.Formatter(STREAM_FMT))
streamHandler.setLevel(level)
logging.getLogger("").addHandler(streamHandler)
if args.log_file:
fileHandler = logging.FileHandler(args.log_file)
fileHandler.setFormatter(logging.Formatter(FILE_FMT))
fileHandler.setLevel(min(level, logging.DEBUG))
logging.getLogger("").addHandler(fileHandler)
def wrap(s: str) -> str:
"""Dedent and wrap a string to 79 characters."""
return textwrap.fill(textwrap.dedent(s), width=79)
def log_wrap(s: str) -> str:
"""Dedent and wrap a string to 70 characters with a 9-space hanging indent."""
return textwrap.fill(textwrap.dedent(s), width=70, subsequent_indent=" " * 9)