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
...
 
@@ -7,2 +7,5 @@ import pathlib
 

	
 
import babel
 
import babel.numbers
 

	
 
from . import cache, loaders
...
 
@@ -22,3 +25,4 @@ class Configuration:
 
    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'])
...
 
@@ -108,3 +112,3 @@ class Configuration:
 
            '--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",
...
 
@@ -136,3 +140,3 @@ class Configuration:
 
            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.",
 
        )
...
 
@@ -172,2 +176,9 @@ class Configuration:
 

	
 
    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):
...
 
@@ -178,3 +189,3 @@ class Configuration:
 
        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
...
 
@@ -182,6 +193,6 @@ class Configuration:
 
            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')
...
 
@@ -204,3 +215,3 @@ class Configuration:
 
        except StopIteration:
 
            pass
 
            self.args.to_currency = pref_currency
 

	
oxrlib_example.ini
Show inline comments
...
 
@@ -37,3 +37,7 @@ 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
...
 
@@ -4,2 +4,3 @@ import os
 

	
 
import babel
 
import pytest
...
 
@@ -58,2 +59,5 @@ def test_historical_default_base(ini_filename, expected_currency, use_switch, an
 
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()]
...
 
@@ -62,6 +66,6 @@ def test_historical_argparsing_success(amount, from_curr, preposition, to_curr,
 
    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
 

	
0 comments (0 inline, 0 general)