accrual: Use cliutil for better logging and error reporting.
This commit is contained in:
parent
3fbd05d553
commit
e07a47ec8f
2 changed files with 15 additions and 17 deletions
|
@ -91,15 +91,18 @@ import rt
|
|||
from beancount.parser import printer as bc_printer
|
||||
|
||||
from . import core
|
||||
from .. import cliutil
|
||||
from .. import config as configmod
|
||||
from .. import data
|
||||
from .. import filters
|
||||
from .. import rtutil
|
||||
|
||||
PROGNAME = 'accrual-report'
|
||||
|
||||
PostGroups = Mapping[Optional[MetaValue], core.RelatedPostings]
|
||||
RTObject = Mapping[str, str]
|
||||
|
||||
_logger = logging.getLogger('conservancy_beancount.reports.accrual')
|
||||
logger = logging.getLogger('conservancy_beancount.reports.accrual')
|
||||
|
||||
class Account(NamedTuple):
|
||||
name: str
|
||||
|
@ -134,7 +137,7 @@ class AccrualAccount(enum.Enum):
|
|||
class BaseReport:
|
||||
def __init__(self, out_file: TextIO) -> None:
|
||||
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:
|
||||
retval = core.RelatedPostings()
|
||||
|
@ -349,7 +352,8 @@ def filter_search(postings: Iterable[data.Posting],
|
|||
return postings
|
||||
|
||||
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(
|
||||
'--report-type', '-t',
|
||||
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
|
||||
previous fiscal year).
|
||||
""")
|
||||
cliutil.add_loglevel_argument(parser)
|
||||
parser.add_argument(
|
||||
'search',
|
||||
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]
|
||||
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,
|
||||
stdout: TextIO=sys.stdout,
|
||||
stderr: TextIO=sys.stderr,
|
||||
config: Optional[configmod.Config]=None,
|
||||
logger: Optional[logging.Logger]=None,
|
||||
) -> int:
|
||||
if logger is None:
|
||||
global _logger
|
||||
_logger = logger = logging.getLogger('accrual-report')
|
||||
setup_logger(_logger, logging.INFO, stderr)
|
||||
returncode = 0
|
||||
if cliutil.is_main_script():
|
||||
global logger
|
||||
logger = logging.getLogger(PROGNAME)
|
||||
sys.excepthook = cliutil.ExceptHook(logger)
|
||||
args = parse_arguments(arglist)
|
||||
cliutil.setup_logger(logger, args.loglevel, stderr)
|
||||
if config is None:
|
||||
config = configmod.Config()
|
||||
config.load_file()
|
||||
|
@ -417,6 +414,7 @@ def main(arglist: Optional[Sequence[str]]=None,
|
|||
groups = core.RelatedPostings.group_by_meta(postings, 'invoice')
|
||||
groups = AccrualAccount.filter_paid_accruals(groups) or groups
|
||||
meta_errors = consistency_check(groups)
|
||||
returncode = 0
|
||||
for error in load_errors:
|
||||
bc_printer.print_error(error, file=stderr)
|
||||
returncode |= ReturnFlag.LOAD_ERRORS
|
||||
|
|
2
setup.py
2
setup.py
|
@ -5,7 +5,7 @@ from setuptools import setup
|
|||
setup(
|
||||
name='conservancy_beancount',
|
||||
description="Plugin, library, and reports for reading Conservancy's books",
|
||||
version='1.0.7',
|
||||
version='1.0.8',
|
||||
author='Software Freedom Conservancy',
|
||||
author_email='info@sfconservancy.org',
|
||||
license='GNU AGPLv3+',
|
||||
|
|
Loading…
Reference in a new issue