Changeset - 461536453835
[Not reviewed]
0 8 0
Brett Smith - 4 years ago 2020-07-27 14:54:04
brettcsmith@brettcsmith.org
cliutil: Add ReturnFlag.

Take this opportunity to re-standardize flag values now that it's clear
what's most common.
8 files changed with 37 insertions and 38 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/cliutil.py
Show inline comments
...
 
@@ -134,2 +134,22 @@ class LogLevel(enum.IntEnum):
 

	
 
class ReturnFlag(enum.IntFlag):
 
    """Common return codes for tools
 

	
 
    Tools should combine these flags to report different errors, and then use
 
    ReturnFlag.returncode(flags) to report their final exit status code.
 

	
 
    Values 1, 2, 4, and 8 should be reserved for this class to be shared across
 
    all tools. Flags 16, 32, and 64 are available for tools to report their own
 
    specific errors.
 
    """
 
    LOAD_ERRORS = 1
 
    NOTHING_TO_REPORT = 2
 
    _RESERVED4 = 4
 
    _RESERVED8 = 8
 

	
 
    @classmethod
 
    def returncode(cls, flags: int) -> int:
 
        return 0 if flags == 0 else 16 + flags
 

	
 

	
 
class SearchTerm(NamedTuple):
conservancy_beancount/reports/accrual.py
Show inline comments
...
 
@@ -637,9 +637,2 @@ class ReportType(enum.Enum):
 

	
 
class ReturnFlag(enum.IntFlag):
 
    LOAD_ERRORS = 1
 
    # 2 was used in the past, it can probably be reclaimed.
 
    REPORT_ERRORS = 4
 
    NOTHING_TO_REPORT = 8
 

	
 

	
 
def filter_search(postings: Iterable[data.Posting],
...
 
@@ -724,3 +717,3 @@ def main(arglist: Optional[Sequence[str]]=None,
 
        bc_printer.print_error(error, file=stderr)
 
        returncode |= ReturnFlag.LOAD_ERRORS
 
        returncode |= cliutil.ReturnFlag.LOAD_ERRORS
 

	
...
 
@@ -731,3 +724,3 @@ def main(arglist: Optional[Sequence[str]]=None,
 
        logger.warning("no matching entries found to report")
 
        returncode |= ReturnFlag.NOTHING_TO_REPORT
 
        returncode |= cliutil.ReturnFlag.NOTHING_TO_REPORT
 
    # groups is a mapping of metadata value strings to AccrualPostings.
...
 
@@ -784,6 +777,6 @@ def main(arglist: Optional[Sequence[str]]=None,
 
    if report is None:
 
        returncode |= ReturnFlag.REPORT_ERRORS
 
        returncode |= 16
 
    else:
 
        report.run(groups)
 
    return 0 if returncode == 0 else 16 + returncode
 
    return cliutil.ReturnFlag.returncode(returncode)
 

	
conservancy_beancount/reports/fund.py
Show inline comments
...
 
@@ -285,7 +285,2 @@ class ReportType(enum.Enum):
 

	
 
class ReturnFlag(enum.IntFlag):
 
    LOAD_ERRORS = 1
 
    NOTHING_TO_REPORT = 8
 

	
 

	
 
def parse_arguments(arglist: Optional[Sequence[str]]=None) -> argparse.Namespace:
...
 
@@ -384,3 +379,3 @@ def main(arglist: Optional[Sequence[str]]=None,
 
        bc_printer.print_error(error, file=stderr)
 
        returncode |= ReturnFlag.LOAD_ERRORS
 
        returncode |= cliutil.ReturnFlag.LOAD_ERRORS
 

	
...
 
@@ -405,3 +400,3 @@ def main(arglist: Optional[Sequence[str]]=None,
 
        logger.warning("no matching postings found to report")
 
        returncode |= ReturnFlag.NOTHING_TO_REPORT
 
        returncode |= cliutil.ReturnFlag.NOTHING_TO_REPORT
 
    elif args.report_type is ReportType.TEXT:
...
 
@@ -421,3 +416,3 @@ def main(arglist: Optional[Sequence[str]]=None,
 
        ods_report.save_file(ods_file)
 
    return 0 if returncode == 0 else 16 + returncode
 
    return cliutil.ReturnFlag.returncode(returncode)
 

	
conservancy_beancount/reports/ledger.py
Show inline comments
...
 
@@ -617,7 +617,2 @@ class TransactionODS(LedgerODS):
 

	
 
class ReturnFlag(enum.IntFlag):
 
    LOAD_ERRORS = 1
 
    NOTHING_TO_REPORT = 8
 

	
 

	
 
class CashReportAction(argparse.Action):
...
 
@@ -792,3 +787,3 @@ def main(arglist: Optional[Sequence[str]]=None,
 
        bc_printer.print_error(error, file=stderr)
 
        returncode |= ReturnFlag.LOAD_ERRORS
 
        returncode |= cliutil.ReturnFlag.LOAD_ERRORS
 

	
...
 
@@ -830,3 +825,3 @@ def main(arglist: Optional[Sequence[str]]=None,
 
        logger.warning("no matching postings found to report")
 
        returncode |= ReturnFlag.NOTHING_TO_REPORT
 
        returncode |= cliutil.ReturnFlag.NOTHING_TO_REPORT
 

	
...
 
@@ -842,3 +837,3 @@ def main(arglist: Optional[Sequence[str]]=None,
 
    report.save_file(ods_file)
 
    return 0 if returncode == 0 else 16 + returncode
 
    return cliutil.ReturnFlag.returncode(returncode)
 

	
conservancy_beancount/tools/opening_balances.py
Show inline comments
...
 
@@ -144,6 +144,2 @@ class Posting(data.Posting):
 

	
 
class ReturnFlag(enum.IntFlag):
 
    LOAD_ERRORS = 1
 

	
 

	
 
def parse_arguments(arglist: Optional[Sequence[str]]=None) -> argparse.Namespace:
...
 
@@ -201,3 +197,3 @@ def main(arglist: Optional[Sequence[str]]=None,
 
        bc_printer.print_error(error, file=stderr)
 
        returncode |= ReturnFlag.LOAD_ERRORS
 
        returncode |= cliutil.ReturnFlag.LOAD_ERRORS
 

	
...
 
@@ -253,3 +249,3 @@ def main(arglist: Optional[Sequence[str]]=None,
 
    bc_printer.print_entry(opening, dcontext, file=stdout)
 
    return 0 if returncode == 0 else 16 + returncode
 
    return cliutil.ReturnFlag.returncode(returncode)
 

	
tests/test_reports_accrual.py
Show inline comments
...
 
@@ -777,3 +777,3 @@ def test_main_aging_report(arglist):
 
def test_main_no_books():
 
    errors = check_main_fails([], testutil.TestConfig(), 1 | 8)
 
    errors = check_main_fails([], testutil.TestConfig(), 1 | 2)
 
    testutil.check_lines_match(iter(errors), [
...
 
@@ -788,3 +788,3 @@ def test_main_no_books():
 
def test_main_no_matches(arglist, caplog):
 
    check_main_fails(arglist, None, 8)
 
    check_main_fails(arglist, None, 2)
 
    testutil.check_logs_match(caplog, [
...
 
@@ -797,3 +797,3 @@ def test_main_no_rt(caplog):
 
    )
 
    check_main_fails(['-t', 'out'], config, 4)
 
    check_main_fails(['-t', 'out'], config, 16)
 
    testutil.check_logs_match(caplog, [
tests/test_reports_fund.py
Show inline comments
...
 
@@ -282,3 +282,3 @@ def test_main_no_postings(caplog):
 
    retcode, output, errors = run_main(io.StringIO, ['NonexistentProject'])
 
    assert retcode == 24
 
    assert retcode == 18
 
    assert any(log.levelname == 'WARNING' for log in caplog.records)
tests/test_reports_ledger.py
Show inline comments
...
 
@@ -559,3 +559,3 @@ def test_main_no_postings(caplog):
 
    retcode, output, errors = run_main(['NonexistentProject'])
 
    assert retcode == 24
 
    assert retcode == 18
 
    assert any(log.levelname == 'WARNING' for log in caplog.records)
0 comments (0 inline, 0 general)