accrual: Use cliutil for better logging and error reporting.

This commit is contained in:
Brett Smith 2020-05-29 22:02:47 -04:00
parent 3fbd05d553
commit e07a47ec8f
2 changed files with 15 additions and 17 deletions

View file

@ -91,15 +91,18 @@ import rt
from beancount.parser import printer as bc_printer from beancount.parser import printer as bc_printer
from . import core from . import core
from .. import cliutil
from .. import config as configmod from .. import config as configmod
from .. import data from .. import data
from .. import filters from .. import filters
from .. import rtutil from .. import rtutil
PROGNAME = 'accrual-report'
PostGroups = Mapping[Optional[MetaValue], core.RelatedPostings] PostGroups = Mapping[Optional[MetaValue], core.RelatedPostings]
RTObject = Mapping[str, str] RTObject = Mapping[str, str]
_logger = logging.getLogger('conservancy_beancount.reports.accrual') logger = logging.getLogger('conservancy_beancount.reports.accrual')
class Account(NamedTuple): class Account(NamedTuple):
name: str name: str
@ -134,7 +137,7 @@ class AccrualAccount(enum.Enum):
class BaseReport: class BaseReport:
def __init__(self, out_file: TextIO) -> None: def __init__(self, out_file: TextIO) -> None:
self.out_file = out_file self.out_file = out_file
self.logger = _logger.getChild(type(self).__name__) self.logger = logger.getChild(type(self).__name__)
def _since_last_nonzero(self, posts: core.RelatedPostings) -> core.RelatedPostings: def _since_last_nonzero(self, posts: core.RelatedPostings) -> core.RelatedPostings:
retval = core.RelatedPostings() retval = core.RelatedPostings()
@ -349,7 +352,8 @@ def filter_search(postings: Iterable[data.Posting],
return postings return postings
def parse_arguments(arglist: Optional[Sequence[str]]=None) -> argparse.Namespace: def parse_arguments(arglist: Optional[Sequence[str]]=None) -> argparse.Namespace:
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser(prog=PROGNAME)
cliutil.add_version_argument(parser)
parser.add_argument( parser.add_argument(
'--report-type', '-t', '--report-type', '-t',
metavar='NAME', metavar='NAME',
@ -368,6 +372,7 @@ You can either specify a fiscal year, or a negative offset from the current
fiscal year, to start loading entries from. The default is -1 (start from the fiscal year, to start loading entries from. The default is -1 (start from the
previous fiscal year). previous fiscal year).
""") """)
cliutil.add_loglevel_argument(parser)
parser.add_argument( parser.add_argument(
'search', 'search',
nargs=argparse.ZERO_OR_MORE, nargs=argparse.ZERO_OR_MORE,
@ -381,25 +386,17 @@ metadata to match. A single ticket number is a shortcut for
args.search_terms = [SearchTerm.parse(s) for s in args.search] args.search_terms = [SearchTerm.parse(s) for s in args.search]
return args return args
def setup_logger(logger: logging.Logger, loglevel: int, stream: TextIO=sys.stderr) -> None:
formatter = logging.Formatter('%(name)s: %(levelname)s: %(message)s')
handler = logging.StreamHandler(stream)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(loglevel)
def main(arglist: Optional[Sequence[str]]=None, def main(arglist: Optional[Sequence[str]]=None,
stdout: TextIO=sys.stdout, stdout: TextIO=sys.stdout,
stderr: TextIO=sys.stderr, stderr: TextIO=sys.stderr,
config: Optional[configmod.Config]=None, config: Optional[configmod.Config]=None,
logger: Optional[logging.Logger]=None,
) -> int: ) -> int:
if logger is None: if cliutil.is_main_script():
global _logger global logger
_logger = logger = logging.getLogger('accrual-report') logger = logging.getLogger(PROGNAME)
setup_logger(_logger, logging.INFO, stderr) sys.excepthook = cliutil.ExceptHook(logger)
returncode = 0
args = parse_arguments(arglist) args = parse_arguments(arglist)
cliutil.setup_logger(logger, args.loglevel, stderr)
if config is None: if config is None:
config = configmod.Config() config = configmod.Config()
config.load_file() config.load_file()
@ -417,6 +414,7 @@ def main(arglist: Optional[Sequence[str]]=None,
groups = core.RelatedPostings.group_by_meta(postings, 'invoice') groups = core.RelatedPostings.group_by_meta(postings, 'invoice')
groups = AccrualAccount.filter_paid_accruals(groups) or groups groups = AccrualAccount.filter_paid_accruals(groups) or groups
meta_errors = consistency_check(groups) meta_errors = consistency_check(groups)
returncode = 0
for error in load_errors: for error in load_errors:
bc_printer.print_error(error, file=stderr) bc_printer.print_error(error, file=stderr)
returncode |= ReturnFlag.LOAD_ERRORS returncode |= ReturnFlag.LOAD_ERRORS

View file

@ -5,7 +5,7 @@ from setuptools import setup
setup( setup(
name='conservancy_beancount', name='conservancy_beancount',
description="Plugin, library, and reports for reading Conservancy's books", description="Plugin, library, and reports for reading Conservancy's books",
version='1.0.7', version='1.0.8',
author='Software Freedom Conservancy', author='Software Freedom Conservancy',
author_email='info@sfconservancy.org', author_email='info@sfconservancy.org',
license='GNU AGPLv3+', license='GNU AGPLv3+',