Changeset - 3a3afb79786b
[Not reviewed]
0 3 0
Brett Smith - 4 years ago 2020-05-17 18:05:49
brettcsmith@brettcsmith.org
historical: Add Beancount output format.
3 files changed with 57 insertions and 14 deletions:
0 comments (0 inline, 0 general)
oxrlib/commands/historical.py
Show inline comments
...
 
@@ -169,15 +169,38 @@ class LedgerFormatter(Formatter):
 
        return "{}\n{}".format(
 
            self.format_denominated_rate(from_amt, from_curr, to_curr),
 
            self.format_denominated_rate(to_amt, to_curr, None),
 
        )
 

	
 

	
 
class BeancountFormatter(LedgerFormatter):
 
    COST_FMT = '{{{}}}'
 

	
 
    def __init__(self, cost_rates, price_rates=None,
 
                 signed_currencies=(), base_fmt='###0.###',
 
                 rate_precision=5, denomination=None):
 
        super().__init__(
 
            cost_rates,
 
            price_rates,
 
            (),
 
            base_fmt.replace(',', ''),
 
            rate_precision,
 
            denomination,
 
        )
 

	
 
    def price_rate(self, from_amt, from_curr, to_curr):
 
        if self.price_rates is None:
 
            return None
 
        else:
 
            return self.price_rates.convert(from_amt, from_curr, to_curr)
 

	
 

	
 
class Formats(enum.Enum):
 
    RAW = Formatter
 
    LEDGER = LedgerFormatter
 
    BEANCOUNT = BeancountFormatter
 

	
 
    @classmethod
 
    def from_arg(cls, s):
 
        return cls[s.upper()]
 

	
 

	
oxrlib/config.py
Show inline comments
...
 
@@ -107,14 +107,15 @@ class Configuration:
 
            metavar='CODE', type=currency_code,
 
            help="Base currency (default USD)",
 
        )
 
        hist_parser.add_argument(
 
            '--output-format',
 
            type=historical.Formats.from_arg,
 
            choices=[fmt.name.lower() for fmt in historical.Formats],
 
            help="Output format. Choices are %(choices)s. Default `raw`.",
 
            help="Output format."
 
            " Choices are `raw`, `ledger`, `beancount`."
 
            " Default `raw`.",
 
        )
 
        # --ledger and --no-ledger predate --output-format.
 
        hist_parser.add_argument(
 
            '--ledger', '-L',
 
            action='store_const',
 
            dest='output_format',
tests/test_historical.py
Show inline comments
...
 
@@ -37,12 +37,13 @@ class FakeConfig:
 
        return self.responder
 

	
 

	
 
output = pytest.fixture(lambda: io.StringIO())
 
parametrize_format = pytest.mark.parametrize('output_format', [
 
    oxrhist.Formats.LEDGER,
 
    oxrhist.Formats.BEANCOUNT,
 
])
 

	
 
@pytest.fixture(scope='module')
 
def single_responder():
 
    return FakeResponder(relpath('historical1.json'))
 

	
...
 
@@ -82,26 +83,44 @@ def lines_from_run(config, output):
 
    output.seek(0)
 
    return iter(output)
 

	
 
def check_fx_amount(config, lines, amount, cost, fx_code, fx_sign=None, price=None):
 
    if price is None:
 
        price = cost
 
    rate_fmt = f'{{}} {re.escape(fx_code)}'
 
    cost = re.escape(cost) + r'\d*'
 
    price = re.escape(price) + r'\d*'
 
    if fx_sign is not None and fx_code in config.args.signed_currencies:
 
        rate_fmt = f'{re.escape(fx_sign)}{{}}'
 
    if config.args.output_format is oxrhist.Formats.LEDGER:
 
        if fx_sign is not None and fx_code in config.args.signed_currencies:
 
            rate_fmt = f'{re.escape(fx_sign)}{{}}'
 
        cost_re = '{{={}}}'.format(rate_fmt.format(cost))
 
        price_re = ' @ {}'.format(rate_fmt.format(price))
 
    else:
 
        rate_fmt = f'{{}} {re.escape(fx_code)}'
 
    pattern = r'^{} {{={}}} @ {}$'.format(
 
        re.escape(amount),
 
        rate_fmt.format(cost),
 
        rate_fmt.format(price),
 
    )
 
        amount = amount.replace(',', '')
 
        cost_re = '{{{}}}'.format(rate_fmt.format(cost))
 
        if config.args.from_date is None:
 
            price_re = ''
 
        else:
 
            price_re = ' @ {}'.format(rate_fmt.format(price))
 
    pattern = r'^{} {}{}$'.format(re.escape(amount), cost_re, price_re)
 
    line = next(lines, "<EOF>")
 
    assert re.match(pattern, line)
 

	
 
def check_nonfx_amount(config, lines, amount, code=None, sign=None):
 
    if config.args.output_format is oxrhist.Formats.LEDGER:
 
        if code is None:
 
            code = 'USD'
 
            sign = '$'
 
        if code in config.args.signed_currencies and sign is not None:
 
            expected = f'{sign}{amount}\n'
 
        else:
 
            expected = f'{amount} {code}\n'
 
    else:
 
        expected = f'{amount.replace(",", "")} {code or "USD"}\n'
 
    assert next(lines, "<EOF>") == expected
 

	
 
def test_rate_list(single_responder, output, any_date):
 
    config = build_config(single_responder, any_date)
 
    lines = lines_from_run(config, output)
 
    assert next(lines).startswith('1 AED = 0.27229')
 
    assert next(lines) == '1 USD = 3.67246 AED\n'
 
    assert next(lines).startswith('1 ALL = 0.0069189')
...
 
@@ -141,13 +160,13 @@ def test_ledger_rate(single_responder, output, any_date, output_format):
 
@parametrize_format
 
def test_ledger_conversion(single_responder, output, any_date, output_format):
 
    config = build_config(single_responder, any_date, from_currency='ALL',
 
                          amount=300, output_format=output_format)
 
    lines = lines_from_run(config, output)
 
    check_fx_amount(config, lines, '300 ALL', '0.00691', 'USD', '$')
 
    assert next(lines) == '$2.08\n'
 
    check_nonfx_amount(config, lines, '2.08')
 
    assert next(lines, None) is None
 

	
 
@parametrize_format
 
def test_signed_currencies(single_responder, output, any_date, output_format):
 
    config = build_config(single_responder, any_date, from_currency='AED',
 
                          output_format=output_format, signed_currencies=['EUR'])
...
 
@@ -170,22 +189,22 @@ def test_denomination(single_responder, output, any_date, output_format):
 
def test_redundant_denomination(single_responder, output, any_date, output_format):
 
    config = build_config(single_responder, any_date,
 
                          from_currency='ANG', to_currency='USD', amount=10,
 
                          output_format=output_format, denomination='USD')
 
    lines = lines_from_run(config, output)
 
    check_fx_amount(config, lines, '10.00 ANG', '0.558', 'USD', '$')
 
    assert next(lines) == '$5.59\n'
 
    check_nonfx_amount(config, lines, '5.59')
 
    assert next(lines, None) is None
 

	
 
@parametrize_format
 
def test_from_denomination(single_responder, output, any_date, output_format):
 
    config = build_config(single_responder, any_date,
 
                          from_currency='USD', to_currency='ALL', amount=10,
 
                          output_format=output_format, denomination='USD')
 
    lines = lines_from_run(config, output)
 
    assert next(lines) == '$10.00\n'
 
    check_nonfx_amount(config, lines, '10.00')
 
    check_fx_amount(config, lines, '1,445 ALL', '0.00691', 'USD', '$')
 
    assert next(lines, None) is None
 

	
 
@parametrize_format
 
def test_rate_precision_added_as_needed(single_responder, output, any_date, output_format):
 
    config = build_config(single_responder, any_date,
...
 
@@ -194,13 +213,13 @@ def test_rate_precision_added_as_needed(single_responder, output, any_date, outp
 
    lines = lines_from_run(config, output)
 
    # 63,805 / 57.0763 (the RUB rate) == $1,117.89
 
    # But using the truncated rate: 63,805 * .01752 == $1,117.86
 
    # Make sure the rate is specified with enough precision to get the
 
    # correct conversion amount.
 
    check_fx_amount(config, lines, '63,805.00 RUB', '0.0175204', 'USD', '$')
 
    assert next(lines) == '$1,117.89\n'
 
    check_nonfx_amount(config, lines, '1,117.89')
 
    assert next(lines, None) is None
 

	
 
@parametrize_format
 
def test_from_date_rates(alternate_responder, output, any_date, output_format):
 
    config = build_config(alternate_responder, any_date,
 
                          from_currency='ANG', to_currency='AED',
0 comments (0 inline, 0 general)