File diff 9b6d562d46f5 → 27dbe14b94eb
oxrlib/config.py
Show inline comments
...
 
@@ -26,7 +26,7 @@ class Configuration:
 
    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):
...
 
@@ -152,7 +152,13 @@ class Configuration:
 
            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)
 
        hist_parser.add_argument(
 
            '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
 

	
...
 
@@ -208,7 +214,7 @@ class Configuration:
 
        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)
...
 
@@ -221,11 +227,35 @@ class Configuration:
 
                # 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 = 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'
 
                next_word = next(words, 'none given')
 
            elif next_lower == 'from':
 
                attr_to_set = 'from_date'
 
                next_word = next(words, 'none given')
 
            elif next_word.isalpha():
 
                attr_to_set = 'to_currency'
 
            else:
 
                attr_to_set = 'from_date'
 
            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'
 
            else:
 
                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