Files @ ae3e4617d31e
Branch filter:

Location: NPO-Accounting/oxrlib/oxrlib/__main__.py

Brett Smith
historical: Always format rates with the same precision.

When we format a rate as a price, we don't know how much precision
is "enough" to do the conversion, because we don't know what's
being converted to. As a result, we may (=will almost certainly)
end up formatting the rate with different precision on the cost
date vs. the price date, and that causes Beancount/Ledger to fail
to make the connection between them.

Using a constant of 6 is enough to make the current test for
"enough" precision pass, so just do that for now. This might need
further refinement in the future.
import decimal
import importlib
import sys

import oxrlib.config

def decimal_context(base=decimal.BasicContext):
    context = base.copy()
    context.rounding = decimal.ROUND_HALF_EVEN
    context.traps = {
        decimal.Clamped: True,
        decimal.DivisionByZero: True,
        decimal.FloatOperation: True,
        decimal.Inexact: False,
        decimal.InvalidOperation: True,
        decimal.Overflow: True,
        decimal.Rounded: False,
        decimal.Subnormal: True,
        decimal.Underflow: True,
    }
    return context

def main(arglist=None, stdout=sys.stdout, stderr=sys.stderr):
    config = oxrlib.config.Configuration(arglist)
    subcmd_module = importlib.import_module('.commands.' + config.args.command, 'oxrlib')
    with decimal.localcontext(decimal_context()):
        subcmd_module.run(config, stdout, stderr)
    return 0

if __name__ == '__main__':
    exit(main())