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
...
 
@@ -174,2 +174,24 @@ class LedgerFormatter(Formatter):
 

	
 
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):
...
 
@@ -177,2 +199,3 @@ class Formats(enum.Enum):
 
    LEDGER = LedgerFormatter
 
    BEANCOUNT = BeancountFormatter
 

	
oxrlib/config.py
Show inline comments
...
 
@@ -112,4 +112,5 @@ class Configuration:
 
            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`.",
 
        )
tests/test_historical.py
Show inline comments
...
 
@@ -42,2 +42,3 @@ parametrize_format = pytest.mark.parametrize('output_format', [
 
    oxrhist.Formats.LEDGER,
 
    oxrhist.Formats.BEANCOUNT,
 
])
...
 
@@ -87,13 +88,18 @@ def check_fx_amount(config, lines, amount, cost, fx_code, fx_sign=None, price=No
 
        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>")
...
 
@@ -101,2 +107,15 @@ def check_fx_amount(config, lines, amount, cost, fx_code, fx_sign=None, price=No
 

	
 
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):
...
 
@@ -146,3 +165,3 @@ def test_ledger_conversion(single_responder, output, any_date, output_format):
 
    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
...
 
@@ -175,3 +194,3 @@ def test_redundant_denomination(single_responder, output, any_date, output_forma
 
    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
...
 
@@ -184,3 +203,3 @@ def test_from_denomination(single_responder, output, any_date, output_format):
 
    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', '$')
...
 
@@ -199,3 +218,3 @@ def test_rate_precision_added_as_needed(single_responder, output, any_date, outp
 
    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
0 comments (0 inline, 0 general)