Changeset - 936237eceb32
[Not reviewed]
0 3 0
Brett Smith - 7 years ago 2017-06-09 17:22:12
brettcsmith@brettcsmith.org
config: Nicer determination of default currency for converting/signing.

base defaults to USD more as an API restriction than anything else, so avoid
using it as a default setting. Instead, use the user's books denomination
or locale setting.
3 files changed with 29 insertions and 10 deletions:
0 comments (0 inline, 0 general)
oxrlib/config.py
Show inline comments
 
import argparse
 
import configparser
 
import datetime
 
import decimal
 
import os.path
 
import pathlib
 

	
 
import babel
 
import babel.numbers
 

	
 
from . import cache, loaders
 

	
 
HOME_PATH = pathlib.Path(os.path.expanduser('~'))
 

	
 
def currency_code(s):
 
    if not ((len(s) == 3) and s.isalpha()):
 
        raise ValueError("bad currency code: {!r}".format(s))
 
    return s.upper()
 

	
 
def currency_list(s):
 
    return [currency_code(code.strip()) for code in s.split(',')]
 

	
 
class Configuration:
 
    DATE_SEPS = frozenset('.-/ ')
 
    DEFAULT_CONFIG_PATH = pathlib.Path(HOME_PATH, '.config', 'oxrlib.ini')
 
    NO_DENOMINATION = object()
 
    LOCALE = babel.core.Locale.default()
 
    SENTINEL = object()
 
    PREPOSITIONS = frozenset(['in', 'to', 'into'])
 
    TODAY = datetime.date.today()
 

	
 
    def __init__(self, arglist):
 
        argparser = self._build_argparser()
 
        self.error = argparser.error
 
        self.args = argparser.parse_args(arglist)
 

	
 
        if self.args.config_file is None:
 
            self.args.config_file = [self.DEFAULT_CONFIG_PATH]
 
        self.conffile = self._build_conffile()
 
        conffile_paths = [path.as_posix() for path in self.args.config_file]
...
 
@@ -97,25 +101,25 @@ class Configuration:
 
            '--no-ledger',
 
            action='store_false', dest='ledger',
 
            help="Turn off an earlier --ledger setting",
 
        )
 
        hist_parser.add_argument(
 
            '--denomination',
 
            metavar='CODE', type=currency_code,
 
            help="In Ledger conversion output, always show rates to convert "
 
            "to this currency",
 
        )
 
        hist_parser.add_argument(
 
            '--no-denomination',
 
            dest='denomination', action='store_const', const=self.NO_DENOMINATION,
 
            dest='denomination', action='store_const', const=self.SENTINEL,
 
            help="Turn off an earlier --denomination setting",
 
        )
 
        hist_parser.add_argument(
 
            '--signed-currency', '--sign-currency',
 
            type=currency_code, action='append', dest='signed_currencies',
 
            metavar='CODE',
 
            help="In Ledger output, use a sign for this currency if known. "
 
            "Can be specified multiple times.",
 
        )
 
        hist_parser.add_argument(
 
            'date',
 
            type=self._date_from_s,
...
 
@@ -125,25 +129,25 @@ class Configuration:
 
        hist_parser.add_argument(
 
            'word1', nargs='?', metavar='amount',
 
            help="Convert this amount of currency. If not specified, show rates.",
 
        )
 
        hist_parser.add_argument(
 
            'word2', nargs='?', metavar='first code',
 
            help="Convert or show rates from this currency, in three-letter code format. "
 
            "If not specified, show all rates on the given date.",
 
        )
 
        hist_parser.add_argument(
 
            'word3', nargs='?', metavar='second code',
 
            help="Convert or show rates to this currency, in three-letter code format. "
 
            "If not specified, defaults to the base currency.",
 
            "If not specified, defaults to the user's preferred currency.",
 
        )
 
        hist_parser.add_argument('word4', nargs='?', help=argparse.SUPPRESS)
 

	
 
        return prog_parser
 

	
 
    def _build_conffile(self):
 
        return configparser.ConfigParser()
 

	
 
    def _read_from_conffile(self, argname, sectionname, fallback, convert_to=None,
 
                            confname=None, getter='get', unset=None,
 
                            *, convert_fallback=False):
 
        if getattr(self.args, argname) is not unset:
...
 
@@ -161,57 +165,64 @@ class Configuration:
 
        try:
 
            return argtype(s_value)
 
        except (decimal.InvalidOperation, TypeError, ValueError):
 
            errmsg = []
 
            if argname:
 
                errmsg.append("argument {}".format(argname))
 
            if typename is None:
 
                typename = argtype.__name__.replace('_', ' ')
 
            errmsg.append("invalid {} value".format(typename))
 
            errmsg.append(repr(s_value))
 
            self.error(': '.join(errmsg))
 

	
 
    def _user_currency(self, default=SENTINEL):
 
        try:
 
            return babel.numbers.get_territory_currencies(
 
                self.LOCALE.territory, start_date=self.TODAY)[0]
 
        except IndexError:
 
            return default
 

	
 
    def _post_hook_historical(self):
 
        year = self.args.date.year
 
        if year < 100:
 
            # Don't let the user specify ambiguous dates.
 
            self.error("historical data not available from year {}".format(year))
 
        self._read_from_conffile('base', 'Historical', 'USD', currency_code)
 
        if self.args.denomination is self.NO_DENOMINATION:
 
        if self.args.denomination is self.SENTINEL:
 
            self.args.denomination = None
 
        else:
 
            self._read_from_conffile('denomination', 'Historical', None, currency_code)
 
        self._read_from_conffile('signed_currencies', 'Historical', self.args.base,
 
        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.args.to_currency = self.args.base
 
        raw_words = iter(getattr(self.args, 'word' + c) for c in '1234')
 
        words = iter(word for word in raw_words if word is not None)
 
        try:
 
            next_word = next(words)
 
            try:
 
                self.args.amount = decimal.Decimal(next_word)
 
            except decimal.InvalidOperation:
 
                pass
 
            else:
 
                # If an amount was given, a currency code must be given too.
 
                # If it wasn't, set a value that can't be parsed as a currency.
 
                next_word = next(words, 'none given')
 
            self.args.from_currency = self._convert_or_error(currency_code, next_word)
 
            next_word = next(words)
 
            if next_word.lower() in self.PREPOSITIONS:
 
                next_word = next(words, next_word)
 
            self.args.to_currency = self._convert_or_error(currency_code, next_word)
 
        except StopIteration:
 
            pass
 
            self.args.to_currency = pref_currency
 

	
 
    def _build_cache_loader(self):
 
        kwargs = dict(self.conffile.items('Cache'))
 
        try:
 
            kwargs['dir_path'] = pathlib.Path(kwargs.pop('directory'))
 
        except KeyError:
 
            pass
 
        self.cache = cache.CacheWriter(**kwargs)
 
        return loaders.FileCache(**kwargs)
 

	
 
    def _build_oxrapi_loader(self):
 
        kwargs = dict(self.conffile.items('OXR'))
oxrlib_example.ini
Show inline comments
...
 
@@ -26,14 +26,18 @@ base = USD
 

	
 
# Write output in Ledger format.
 
ledger = no
 

	
 
# Denominate Ledger books in this currency.
 
# Ledger-formatted conversions will always show a rate to convert to this
 
# currency, even when converting between two other currencies.
 
# If not specified, output will show the rate for the currency you're
 
# converting to.
 
denomination = USD
 

	
 
# Use signs for these currencies in Ledger output.
 
# If not specified, defaults to the base currency.
 
# If not specified, defaults to the user's preferred currency, which is
 
# the first setting found from:
 
# 1. the denomination setting above
 
# 2. the user's locale
 
# 3. the base currency (which defaults to USD)
 
signed_currencies = USD, EUR
tests/test_Configuration.py
Show inline comments
 
import datetime
 
import decimal
 
import os
 

	
 
import babel
 
import pytest
 

	
 
from . import any_date, relpath
 

	
 
import oxrlib.cache
 
import oxrlib.config
 
import oxrlib.loaders
 

	
 
INI_DIR_PATH = relpath('config_ini')
 

	
 
def config_from(ini_filename, arglist=None):
 
    if arglist is None:
...
 
@@ -47,32 +48,35 @@ def test_historical_default_base(ini_filename, expected_currency, use_switch, an
 
    config = config_from(ini_filename, arglist)
 
    assert config.args.base == expected_currency
 

	
 
@pytest.mark.parametrize('amount,from_curr,preposition,to_curr', [
 
    (None, 'JPY', None, None),
 
    (None, 'gbp', None, 'Aud'),
 
    (None, 'CHF', 'to', 'eur'),
 
    (decimal.Decimal('1000'), 'chf', None, None),
 
    (decimal.Decimal('999'), 'Eur', None, 'Chf'),
 
    (decimal.Decimal('12.34'), 'gbp', 'IN', 'eur'),
 
])
 
def test_historical_argparsing_success(amount, from_curr, preposition, to_curr, any_date):
 
    oxrlib.config.Configuration.TODAY = datetime.date(2017, 1, 1)
 
    # This locale's currency should not be used in any test cases above.
 
    oxrlib.config.Configuration.LOCALE = babel.core.Locale('en', 'IN')
 
    arglist = ['historical', any_date.isoformat()]
 
    arglist.extend(str(s) for s in [amount, from_curr, preposition, to_curr]
 
                   if s is not None)
 
    config = config_from(os.devnull, arglist)
 
    expect_to_curr = 'INR' if to_curr is None else to_curr.upper()
 
    assert config.args.amount == amount
 
    assert config.args.from_currency == from_curr.upper()
 
    if to_curr is not None:
 
        assert config.args.to_currency == to_curr.upper()
 
    assert config.args.to_currency == expect_to_curr
 

	
 
@pytest.mark.parametrize('arglist', [
 
    ['100'],
 
    ['120', 'dollars'],
 
    ['to', '42', 'usd'],
 
    ['99', 'usd', 'minus', 'jpy'],
 
    ['usdjpy'],
 
    ['44', 'eur', 'in', 'chf', 'pronto'],
 
    ['eur', 'into'],
 
    ['50', 'jpy', 'in'],
 
])
 
def test_historical_argparsing_failure(arglist, any_date):
0 comments (0 inline, 0 general)