@@ -23,13 +23,13 @@ def currency_list(s):
class Configuration:
DATE_SEPS = frozenset('.-/ ')
DEFAULT_CONFIG_PATH = pathlib.Path(HOME_PATH, '.config', 'oxrlib.ini')
LOCALE = babel.core.Locale.default()
SENTINEL = object()
PREPOSITIONS = frozenset(['in', 'to', 'into'])
CURRENCY_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)
@@ -149,13 +149,19 @@ class Configuration:
)
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 user's preferred currency.",
hist_parser.add_argument('word4', nargs='?', help=argparse.SUPPRESS)
'word4', nargs='?', metavar='from date',
help="Include source rates for this date, if provided. "
"Raw output format does not show source rates.",
hist_parser.add_argument('word5', nargs='?', help=argparse.SUPPRESS)
hist_parser.add_argument('word6', nargs='?', help=argparse.SUPPRESS)
return prog_parser
def _build_conffile(self):
return configparser.ConfigParser()
@@ -205,30 +211,54 @@ class Configuration:
else:
self._read_from_conffile('denomination', 'Historical', None, currency_code)
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')
raw_words = iter(getattr(self.args, 'word' + c) for c in '1234')
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:
next_word = next(words)
self.args.amount = decimal.Decimal(next_word)
except decimal.InvalidOperation:
pass
# 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)
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 = None
self.args.from_date = None
for next_word in words:
next_lower = next_word.lower()
if next_lower in self.CURRENCY_PREPOSITIONS:
attr_to_set = 'to_currency'
elif next_lower == 'from':
attr_to_set = 'from_date'
elif next_word.isalpha():
have_arg = getattr(self.args, attr_to_set)
if have_arg is not None:
self.error(f"tried to set {attr_to_set.replace('_', ' ')} multiple times")
elif attr_to_set == 'from_date':
convert_func = lambda s: self._date_from_s(s)[0]
typename = 'date'
convert_func = currency_code
typename = None
setattr(self.args, attr_to_set, self._convert_or_error(
convert_func, next_word, attr_to_set, typename,
))
if self.args.to_currency is None:
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}, "