Changeset - d0f5f1547ced
[Not reviewed]
0 3 0
Brett Smith - 7 years ago 2017-06-29 21:18:47
brettcsmith@brettcsmith.org
config: Error out when historical arguments are ambiguous.
3 files changed with 25 insertions and 2 deletions:
0 comments (0 inline, 0 general)
oxrlib/config.py
Show inline comments
...
 
@@ -3,12 +3,13 @@ import configparser
 
import datetime
 
import decimal
 
import os.path
 
import pathlib
 

	
 
import babel
 
import babel.dates
 
import babel.numbers
 

	
 
from . import cache, loaders
 

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

	
...
 
@@ -71,13 +72,13 @@ class Configuration:
 
                pass
 
            elif 'month' in replacements:
 
                retval = retval.replace(year=retval.year - 1)
 
            else:
 
                retval -= datetime.timedelta(days=replacements['day'])
 
                retval = retval.replace(**replacements)
 
        return retval
 
        return retval, replacements
 

	
 
    def _build_argparser(self):
 
        prog_parser = argparse.ArgumentParser()
 
        prog_parser.add_argument(
 
            '--config-file', '-c',
 
            action='append', type=pathlib.Path,
...
 
@@ -188,12 +189,13 @@ class Configuration:
 
            return babel.numbers.get_territory_currencies(
 
                self.LOCALE.territory, start_date=self.TODAY)[0]
 
        except IndexError:
 
            return default
 

	
 
    def _post_hook_historical(self):
 
        self.args.date, date_spec = self.args.date
 
        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.SENTINEL:
...
 
@@ -220,12 +222,24 @@ class Configuration:
 
            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:
 
            self.args.to_currency = pref_currency
 
        if ((len(date_spec) == 1)
 
            and self.args.from_currency
 
            and (self.args.amount is None)):
 
            self.error(("ambiguous input: "
 
                        "Did you want rates for {args.from_currency} on {date}, "
 
                        "or to convert {amt} {args.from_currency} to {args.to_currency}?\n"
 
                        "Specify more of the date to disambiguate."
 
                        ).format(
 
                            args=self.args,
 
                            date=babel.dates.format_date(self.args.date),
 
                            amt=date_spec['day'],
 
                        ))
 

	
 
    def _build_cache_loader(self):
 
        kwargs = dict(self.conffile.items('Cache'))
 
        try:
 
            kwargs['dir_path'] = pathlib.Path(kwargs.pop('directory'))
 
        except KeyError:
setup.py
Show inline comments
...
 
@@ -2,13 +2,13 @@
 

	
 
from setuptools import setup
 

	
 
setup(
 
    name='oxrlib',
 
    description="Library to query the Open Exchange Rates (OXR) API",
 
    version='1.6',
 
    version='1.7',
 
    author='Brett Smith',
 
    author_email='brettcsmith@brettcsmith.org',
 
    license='GNU AGPLv3+',
 

	
 
    install_requires=['babel'],
 
    setup_requires=['pytest-runner'],
tests/test_Configuration.py
Show inline comments
...
 
@@ -125,6 +125,15 @@ def test_bad_date_parsing(date_s):
 
    try:
 
        config = config_from(os.devnull, ['historical', date_s])
 
    except SystemExit:
 
        pass
 
    else:
 
        assert not config.args.date, "date parsed from {!r}".format(date_s)
 

	
 
def test_ambiguous_arglist_failure():
 
    try:
 
        # It's ambiguous if "2" is "the 2nd" or "2 EUR".
 
        config = config_from(os.devnull, ['historical', '2', 'eur'])
 
    except SystemExit:
 
        pass
 
    else:
 
        assert not config.args, "ambiguous args parsed"
0 comments (0 inline, 0 general)