Changeset - 35804db617a1
[Not reviewed]
0 7 0
Brett Smith - 4 years ago 2020-08-31 18:19:00
brettcsmith@brettcsmith.org
reports: All reports support rewrite rules.

I realized that if ledger-report supported rewrite rules, then it would
include all the information necessary to reproduce the numbers on the
statement of functional expenses.

With that, it was easy enough to add support to the rest of the reports for
consistency's sake.
7 files changed with 51 insertions and 20 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/cliutil.py
Show inline comments
...
 
@@ -120,4 +120,5 @@ class ExitCode(enum.IntEnum):
 
    NoDataFiltered = os.EX_DATAERR
 
    NoDataLoaded = os.EX_NOINPUT
 
    RewriteRulesError = os.EX_DATAERR
 

	
 
    # Our own exit codes, working down from that range
...
 
@@ -254,4 +255,15 @@ def add_loglevel_argument(parser: argparse.ArgumentParser,
 
    )
 

	
 
def add_rewrite_rules_argument(parser: argparse.ArgumentParser) -> argparse.Action:
 
    return parser.add_argument(
 
        '--rewrite-rules', '--rewrites', '-r',
 
        action='append',
 
        default=[],
 
        metavar='PATH',
 
        type=Path,
 
        help="""Use rewrite rules from the given YAML file. You can specify
 
this option multiple times to load multiple sets of rewrite rules in order.
 
""")
 

	
 
def add_version_argument(parser: argparse.ArgumentParser) -> argparse.Action:
 
    progname = parser.prog or sys.argv[0]
conservancy_beancount/reports/accrual.py
Show inline comments
...
 
@@ -116,4 +116,5 @@ from beancount.parser import printer as bc_printer
 

	
 
from . import core
 
from . import rewrite
 
from .. import books
 
from .. import cliutil
...
 
@@ -648,4 +649,5 @@ def parse_arguments(arglist: Optional[Sequence[str]]=None) -> argparse.Namespace
 
    parser = argparse.ArgumentParser(prog=PROGNAME)
 
    cliutil.add_version_argument(parser)
 
    cliutil.add_rewrite_rules_argument(parser)
 
    parser.add_argument(
 
        '--report-type', '-t',
...
 
@@ -722,7 +724,14 @@ def main(arglist: Optional[Sequence[str]]=None,
 
        bc_printer.print_error(error, file=stderr)
 

	
 
    postings = list(filter_search(
 
        data.Posting.from_entries(entries), args.search_terms,
 
    ))
 
    postings_src = data.Posting.from_entries(entries)
 
    for rewrite_path in args.rewrite_rules:
 
        try:
 
            ruleset = rewrite.RewriteRuleset.from_yaml(rewrite_path)
 
        except ValueError as error:
 
            logger.critical("failed loading rewrite rules from %s: %s",
 
                            rewrite_path, error.args[0])
 
            return cliutil.ExitCode.RewriteRulesError
 
        postings_src = ruleset.rewrite(postings_src)
 
    postings = list(filter_search(postings_src, args.search_terms))
 
    if not postings:
 
        logger.warning("no matching entries found to report")
conservancy_beancount/reports/balance_sheet.py
Show inline comments
...
 
@@ -21,5 +21,4 @@ import enum
 
import logging
 
import operator
 
import os
 
import sys
 

	
...
 
@@ -634,13 +633,5 @@ The default is one year ago.
 
The default is a year after the start date.
 
""")
 
    parser.add_argument(
 
        '--rewrite-rules', '--rewrites', '-r',
 
        action='append',
 
        default=[],
 
        metavar='PATH',
 
        type=Path,
 
        help="""Use rewrite rules from the given YAML file. You can specify
 
this option multiple times to load multiple sets of rewrite rules in order.
 
""")
 
    cliutil.add_rewrite_rules_argument(parser)
 
    parser.add_argument(
 
        '--fund-metadata-key', '-m',
...
 
@@ -706,5 +697,5 @@ def main(arglist: Optional[Sequence[str]]=None,
 
            logger.critical("failed loading rewrite rules from %s: %s",
 
                            rewrite_path, error.args[0])
 
            return os.EX_DATAERR
 
            return cliutil.ExitCode.RewriteRulesError
 
        postings = ruleset.rewrite(postings)
 

	
conservancy_beancount/reports/fund.py
Show inline comments
...
 
@@ -75,4 +75,5 @@ from beancount.parser import printer as bc_printer
 

	
 
from . import core
 
from . import rewrite
 
from .. import books
 
from .. import cliutil
...
 
@@ -309,4 +310,5 @@ The default is one year ago.
 
The default is a year after the start date.
 
""")
 
    cliutil.add_rewrite_rules_argument(parser)
 
    parser.add_argument(
 
        '--report-type', '-t',
...
 
@@ -379,9 +381,17 @@ def main(arglist: Optional[Sequence[str]]=None,
 
        bc_printer.print_error(error, file=stderr)
 

	
 
    postings = (
 
    postings = iter(
 
        post
 
        for post in data.Posting.from_entries(entries)
 
        if post.meta.date < args.stop_date
 
    )
 
    for rewrite_path in args.rewrite_rules:
 
        try:
 
            ruleset = rewrite.RewriteRuleset.from_yaml(rewrite_path)
 
        except ValueError as error:
 
            logger.critical("failed loading rewrite rules from %s: %s",
 
                            rewrite_path, error.args[0])
 
            return cliutil.ExitCode.RewriteRulesError
 
        postings = ruleset.rewrite(postings)
 
    for search_term in args.search_terms:
 
        postings = search_term.filter_postings(postings)
conservancy_beancount/reports/ledger.py
Show inline comments
...
 
@@ -79,4 +79,5 @@ from beancount.parser import printer as bc_printer
 

	
 
from . import core
 
from . import rewrite
 
from .. import books
 
from .. import cliutil
...
 
@@ -702,4 +703,5 @@ classification from metadata. If not specified, the default set adapts to your
 
search criteria.
 
""")
 
    cliutil.add_rewrite_rules_argument(parser)
 
    parser.add_argument(
 
        '--show-totals', '-S',
...
 
@@ -798,4 +800,12 @@ def main(arglist: Optional[Sequence[str]]=None,
 
    data.Account.load_from_books(entries, options)
 
    postings = data.Posting.from_entries(entries)
 
    for rewrite_path in args.rewrite_rules:
 
        try:
 
            ruleset = rewrite.RewriteRuleset.from_yaml(rewrite_path)
 
        except ValueError as error:
 
            logger.critical("failed loading rewrite rules from %s: %s",
 
                            rewrite_path, error.args[0])
 
            return cliutil.ExitCode.RewriteRulesError
 
        postings = ruleset.rewrite(postings)
 
    for search_term in args.search_terms:
 
        postings = search_term.filter_postings(postings)
conservancy_beancount/tools/audit_report.py
Show inline comments
...
 
@@ -197,4 +197,6 @@ def main(arglist: Optional[Sequence[str]]=None,
 
        out_path = args.output_directory / out_name
 
        output_reports.append(out_path)
 
        for path in args.rewrite_rules:
 
            yield f'--rewrite-rules={path}'
 
        yield f'--output-file={out_path}'
 
        yield from arglist
...
 
@@ -209,8 +211,5 @@ def main(arglist: Optional[Sequence[str]]=None,
 
        (ledger.main, list(common_args('Receipts', next_year, '--receipts'))),
 
        (accrual.main, list(common_args('AgingReport.ods'))),
 
        (balance_sheet.main, list(common_args(
 
            'Summary', args.audit_year,
 
            *(f'--rewrite-rules={path}' for path in args.rewrite_rules),
 
        ))),
 
        (balance_sheet.main, list(common_args('Summary', args.audit_year))),
 
        (fund.main, list(common_args('FundReport', args.audit_year))),
 
        (fund.main, list(common_args('FundReport', next_year))),
setup.py
Show inline comments
...
 
@@ -6,5 +6,5 @@ setup(
 
    name='conservancy_beancount',
 
    description="Plugin, library, and reports for reading Conservancy's books",
 
    version='1.9.0',
 
    version='1.9.1',
 
    author='Software Freedom Conservancy',
 
    author_email='info@sfconservancy.org',
0 comments (0 inline, 0 general)