config: Error out when historical arguments are ambiguous.
This commit is contained in:
parent
b270db02e8
commit
d0f5f1547c
3 changed files with 25 additions and 2 deletions
|
@ -6,6 +6,7 @@ import os.path
|
||||||
import pathlib
|
import pathlib
|
||||||
|
|
||||||
import babel
|
import babel
|
||||||
|
import babel.dates
|
||||||
import babel.numbers
|
import babel.numbers
|
||||||
|
|
||||||
from . import cache, loaders
|
from . import cache, loaders
|
||||||
|
@ -74,7 +75,7 @@ class Configuration:
|
||||||
else:
|
else:
|
||||||
retval -= datetime.timedelta(days=replacements['day'])
|
retval -= datetime.timedelta(days=replacements['day'])
|
||||||
retval = retval.replace(**replacements)
|
retval = retval.replace(**replacements)
|
||||||
return retval
|
return retval, replacements
|
||||||
|
|
||||||
def _build_argparser(self):
|
def _build_argparser(self):
|
||||||
prog_parser = argparse.ArgumentParser()
|
prog_parser = argparse.ArgumentParser()
|
||||||
|
@ -191,6 +192,7 @@ class Configuration:
|
||||||
return default
|
return default
|
||||||
|
|
||||||
def _post_hook_historical(self):
|
def _post_hook_historical(self):
|
||||||
|
self.args.date, date_spec = self.args.date
|
||||||
year = self.args.date.year
|
year = self.args.date.year
|
||||||
if year < 100:
|
if year < 100:
|
||||||
# Don't let the user specify ambiguous dates.
|
# Don't let the user specify ambiguous dates.
|
||||||
|
@ -223,6 +225,18 @@ class Configuration:
|
||||||
self.args.to_currency = self._convert_or_error(currency_code, next_word)
|
self.args.to_currency = self._convert_or_error(currency_code, next_word)
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
self.args.to_currency = pref_currency
|
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):
|
def _build_cache_loader(self):
|
||||||
kwargs = dict(self.conffile.items('Cache'))
|
kwargs = dict(self.conffile.items('Cache'))
|
||||||
|
|
2
setup.py
2
setup.py
|
@ -5,7 +5,7 @@ from setuptools import setup
|
||||||
setup(
|
setup(
|
||||||
name='oxrlib',
|
name='oxrlib',
|
||||||
description="Library to query the Open Exchange Rates (OXR) API",
|
description="Library to query the Open Exchange Rates (OXR) API",
|
||||||
version='1.6',
|
version='1.7',
|
||||||
author='Brett Smith',
|
author='Brett Smith',
|
||||||
author_email='brettcsmith@brettcsmith.org',
|
author_email='brettcsmith@brettcsmith.org',
|
||||||
license='GNU AGPLv3+',
|
license='GNU AGPLv3+',
|
||||||
|
|
|
@ -128,3 +128,12 @@ def test_bad_date_parsing(date_s):
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
assert not config.args.date, "date parsed from {!r}".format(date_s)
|
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"
|
||||||
|
|
Loading…
Reference in a new issue