balance_sheet: Support rewrite rules.

This commit is contained in:
Brett Smith 2020-08-29 17:18:15 -04:00
parent f3e577f0d5
commit 153361ce9f

View file

@ -50,6 +50,7 @@ import odf.table # type:ignore[import]
from beancount.parser import printer as bc_printer from beancount.parser import printer as bc_printer
from . import core from . import core
from . import rewrite
from .. import books from .. import books
from .. import cliutil from .. import cliutil
from .. import config as configmod from .. import config as configmod
@ -638,6 +639,15 @@ The default is one year ago.
type=cliutil.date_arg, type=cliutil.date_arg,
help="""Date to stop reporting entries, exclusive, in YYYY-MM-DD format. help="""Date to stop reporting entries, exclusive, in YYYY-MM-DD format.
The default is a year after the start date. 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.
""") """)
parser.add_argument( parser.add_argument(
'--fund-metadata-key', '-m', '--fund-metadata-key', '-m',
@ -695,8 +705,18 @@ def main(arglist: Optional[Sequence[str]]=None,
bc_printer.print_error(error, file=stderr) bc_printer.print_error(error, file=stderr)
data.Account.load_from_books(entries, options_map) data.Account.load_from_books(entries, options_map)
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 os.EX_DATAERR
postings = ruleset.rewrite(postings)
balances = Balances( balances = Balances(
data.Posting.from_entries(entries), postings,
args.start_date, args.start_date,
args.stop_date, args.stop_date,
args.fund_metadata_key, args.fund_metadata_key,