From 80fd49a98ad8177c164ba13dc7c5010a44a40330 Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Sat, 16 May 2020 15:07:20 -0400 Subject: [PATCH] historical: Introduce --output-format option. Beancount output will be added here. --- oxrlib/commands/historical.py | 16 +++++++++++++++- oxrlib/config.py | 33 +++++++++++++++++++++++++-------- tests/test_historical.py | 2 +- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/oxrlib/commands/historical.py b/oxrlib/commands/historical.py index 3223f94..3f61594 100644 --- a/oxrlib/commands/historical.py +++ b/oxrlib/commands/historical.py @@ -6,6 +6,11 @@ import babel.numbers from .. import rate as oxrrate +try: + import enum +except ImportError: + import enum34 as enum + class Formatter: def __init__(self, rate, signed_currencies=(), base_fmt='#,##0.###'): self.rate = rate @@ -130,13 +135,22 @@ class LedgerFormatter(Formatter): ) +class Formats(enum.Enum): + RAW = Formatter + LEDGER = LedgerFormatter + + @classmethod + def from_arg(cls, s): + return cls[s.upper()] + + def run(config, stdout, stderr): loaders = config.get_loaders() with loaders.historical(config.args.date, config.args.base) as rate_json: rate = oxrrate.Rate.from_json_file(rate_json) if loaders.should_cache(): config.cache.save_rate(rate) - if config.args.ledger: + if config.args.output_format is Formats.LEDGER: formatter = LedgerFormatter(rate, config.args.signed_currencies, denomination=config.args.denomination) else: diff --git a/oxrlib/config.py b/oxrlib/config.py index f9d6e0e..5110ab1 100644 --- a/oxrlib/config.py +++ b/oxrlib/config.py @@ -10,6 +10,7 @@ import babel.dates import babel.numbers from . import cache, loaders +from .commands import historical HOME_PATH = pathlib.Path(os.path.expanduser('~')) @@ -97,22 +98,35 @@ class Configuration: command='historical', amount=None, from_currency=None, - ledger=None, + from_date=None, + to_currency=None, + output_format=None, ) hist_parser.add_argument( '--base', 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`.", + ) + # --ledger and --no-ledger predate --output-format. hist_parser.add_argument( '--ledger', '-L', - action='store_true', - help="Output the rate or conversion in Ledger format", + action='store_const', + dest='output_format', + const=historical.Formats.LEDGER, + help=argparse.SUPPRESS, ) hist_parser.add_argument( '--no-ledger', - action='store_false', dest='ledger', - help="Turn off an earlier --ledger setting", + action='store_const', + dest='output_format', + const=historical.Formats.RAW, + help=argparse.SUPPRESS, ) hist_parser.add_argument( '--denomination', @@ -213,7 +227,12 @@ class Configuration: pref_currency = self.args.denomination or self._user_currency(self.args.base) self._read_from_conffile('signed_currencies', 'Historical', pref_currency, currency_list, convert_fallback=True) - self._read_from_conffile('ledger', 'Historical', False, getter='getboolean') + self._read_from_conffile('output_format', 'Historical', None, historical.Formats.from_arg) + if self.args.output_format is None: + if self.conffile.getboolean('Historical', 'ledger', fallback=None): + self.args.output_format = historical.Formats.LEDGER + else: + self.args.output_format = historical.Formats.RAW raw_words = iter(getattr(self.args, 'word' + c) for c in '123456') words = iter(word for word in raw_words if word is not None) try: @@ -229,8 +248,6 @@ class Configuration: self.args.from_currency = self._convert_or_error(currency_code, next_word) except StopIteration: pass - self.args.to_currency = None - self.args.from_date = None for next_word in words: next_lower = next_word.lower() if next_lower in self.CURRENCY_PREPOSITIONS: diff --git a/tests/test_historical.py b/tests/test_historical.py index 4f17a03..48d4bbc 100644 --- a/tests/test_historical.py +++ b/tests/test_historical.py @@ -58,7 +58,7 @@ def build_config( 'amount': None if amount is None else decimal.Decimal(amount), 'from_currency': from_currency, 'to_currency': base if to_currency is None else to_currency, - 'ledger': ledger, + 'output_format': oxrhist.Formats['LEDGER' if ledger else 'RAW'], 'signed_currencies': [base] if signed_currencies is None else signed_currencies, 'denomination': denomination, })