Changeset - d3e0a380736d
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-05-28 20:42:36
brettcsmith@brettcsmith.org
accrual: Introduce logging infrastructure.
2 files changed with 43 insertions and 34 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/reports/accrual.py
Show inline comments
...
 
@@ -64,2 +64,3 @@ import datetime
 
import enum
 
import logging
 
import re
...
 
@@ -100,2 +101,4 @@ RTObject = Mapping[str, str]
 

	
 
_logger = logging.getLogger('conservancy_beancount.reports.accrual')
 

	
 
class Account(NamedTuple):
...
 
@@ -131,5 +134,5 @@ class AccrualAccount(enum.Enum):
 
class BaseReport:
 
    def __init__(self, out_file: TextIO, err_file: TextIO) -> None:
 
    def __init__(self, out_file: TextIO) -> None:
 
        self.out_file = out_file
 
        self.err_file = err_file
 
        self.logger = _logger.getChild(type(self).__name__)
 

	
...
 
@@ -172,7 +175,6 @@ class BalanceReport(BaseReport):
 
class OutgoingReport(BaseReport):
 
    def __init__(self, rt_client: rt.Rt, out_file: TextIO, err_file: TextIO) -> None:
 
    def __init__(self, rt_client: rt.Rt, out_file: TextIO) -> None:
 
        super().__init__(out_file)
 
        self.rt_client = rt_client
 
        self.rt_wrapper = rtutil.RT(rt_client)
 
        self.out_file = out_file
 
        self.err_file = err_file
 

	
...
 
@@ -204,6 +206,6 @@ class OutgoingReport(BaseReport):
 
        if ticket is None:
 
            print("error: can't generate outgoings report for {}"
 
                  " because no RT ticket available: {}".format(
 
                      invoice, errmsg,
 
                  ), file=self.err_file)
 
            self.logger.error(
 
                "can't generate outgoings report for %s because no RT ticket available: %s",
 
                invoice, errmsg,
 
            )
 
            return
...
 
@@ -381,2 +383,9 @@ metadata to match. A single ticket number is a shortcut for
 

	
 
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,
...
 
@@ -385,3 +394,8 @@ def main(arglist: Optional[Sequence[str]]=None,
 
         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
...
 
@@ -414,3 +428,3 @@ def main(arglist: Optional[Sequence[str]]=None,
 
    if not groups:
 
        print("warning: no matching entries found to report", file=stderr)
 
        logger.warning("no matching entries found to report")
 
        returncode |= ReturnFlag.NOTHING_TO_REPORT
...
 
@@ -420,10 +434,7 @@ def main(arglist: Optional[Sequence[str]]=None,
 
        if rt_client is None:
 
            print(
 
                "error: unable to generate outgoing report: RT client is required",
 
                file=stderr,
 
            )
 
            logger.error("unable to generate outgoing report: RT client is required")
 
        else:
 
            report = OutgoingReport(rt_client, stdout, stderr)
 
            report = OutgoingReport(rt_client, stdout)
 
    else:
 
        report = args.report_type.value(stdout, stderr)
 
        report = args.report_type.value(stdout)
 
    if report is None:
tests/test_reports_accrual.py
Show inline comments
...
 
@@ -261,6 +261,5 @@ def run_outgoing(invoice, postings, rt_client=None):
 
    output = io.StringIO()
 
    errors = io.StringIO()
 
    report = accrual.OutgoingReport(rt_client, output, errors)
 
    report = accrual.OutgoingReport(rt_client, output)
 
    report.run({invoice: postings})
 
    return output, errors
 
    return output
 

	
...
 
@@ -272,14 +271,13 @@ def run_outgoing(invoice, postings, rt_client=None):
 
])
 
def test_balance_report(accrual_postings, invoice, expected):
 
def test_balance_report(accrual_postings, invoice, expected, caplog):
 
    related = relate_accruals_by_meta(accrual_postings, invoice)
 
    output = io.StringIO()
 
    errors = io.StringIO()
 
    report = accrual.BalanceReport(output, errors)
 
    report = accrual.BalanceReport(output)
 
    report.run({invoice: related})
 
    assert not errors.getvalue()
 
    assert not caplog.records
 
    check_output(output, [invoice, expected])
 

	
 
def test_outgoing_report(accrual_postings):
 
def test_outgoing_report(accrual_postings, caplog):
 
    invoice = 'rt:510/6100'
 
    output, errors = run_outgoing(invoice, accrual_postings)
 
    output = run_outgoing(invoice, accrual_postings)
 
    rt_url = RTClient.DEFAULT_URL[:-9]
...
 
@@ -287,3 +285,3 @@ def test_outgoing_report(accrual_postings):
 
    contract_url = rf'\b{re.escape(f"{rt_url}Ticket/Attachment/4000/4000/contract.pdf")}\b'
 
    print(output.getvalue())
 
    assert not caplog.records
 
    check_output(output, [
...
 
@@ -306,7 +304,7 @@ def test_outgoing_report(accrual_postings):
 

	
 
def test_outgoing_report_custom_field_fallbacks(accrual_postings):
 
def test_outgoing_report_custom_field_fallbacks(accrual_postings, caplog):
 
    invoice = 'rt:510/6100'
 
    rt_client = RTClient(want_cfs=False)
 
    output, errors = run_outgoing(invoice, accrual_postings, rt_client)
 
    assert not errors.getvalue()
 
    output = run_outgoing(invoice, accrual_postings, rt_client)
 
    assert not caplog.records
 
    check_output(output, [
...
 
@@ -318,6 +316,6 @@ def test_outgoing_report_custom_field_fallbacks(accrual_postings):
 

	
 
def test_outgoing_report_fx_amounts(accrual_postings):
 
def test_outgoing_report_fx_amounts(accrual_postings, caplog):
 
    invoice = 'rt:520/5200'
 
    output, errors = run_outgoing(invoice, accrual_postings)
 
    assert not errors.getvalue()
 
    output = run_outgoing(invoice, accrual_postings)
 
    assert not caplog.records
 
    check_output(output, [
...
 
@@ -419,3 +417,3 @@ def test_main_no_matches(arglist):
 
    check_main_fails(arglist, None, 8, [
 
        r'^warning: no matching entries found to report$',
 
        r': WARNING: no matching entries found to report$',
 
    ])
...
 
@@ -427,3 +425,3 @@ def test_main_no_rt():
 
    check_main_fails(['-t', 'out'], config, 4, [
 
        r'^error: unable to generate outgoing report: RT client is required\b',
 
        r': ERROR: unable to generate outgoing report: RT client is required\b',
 
    ])
0 comments (0 inline, 0 general)