File diff 992c91fc90de → 55f5833aa071
oxrlib/config.py
Show inline comments
...
 
@@ -17,12 +17,13 @@ def currency_code(s):
 
def currency_list(s):
 
    return [currency_code(code.strip()) for code in s.split(',')]
 

	
 
class Configuration:
 
    DATE_SEPS = frozenset('.-/ ')
 
    DEFAULT_CONFIG_PATH = pathlib.Path(HOME_PATH, '.config', 'oxrlib.ini')
 
    NO_DENOMINATION = object()
 
    PREPOSITIONS = frozenset(['in', 'to', 'into'])
 
    TODAY = datetime.date.today()
 

	
 
    def __init__(self, arglist):
 
        argparser = self._build_argparser()
 
        self.error = argparser.error
...
 
@@ -94,12 +95,23 @@ class Configuration:
 
        )
 
        hist_parser.add_argument(
 
            '--no-ledger',
 
            action='store_false', dest='ledger',
 
            help="Turn off an earlier --ledger setting",
 
        )
 
        hist_parser.add_argument(
 
            '--denomination',
 
            metavar='CODE', type=currency_code,
 
            help="In Ledger conversion output, always show rates to convert "
 
            "to this currency",
 
        )
 
        hist_parser.add_argument(
 
            '--no-denomination',
 
            dest='denomination', action='store_const', const=self.NO_DENOMINATION,
 
            help="Turn off an earlier --denomination setting",
 
        )
 
        hist_parser.add_argument(
 
            '--signed-currency', '--sign-currency',
 
            type=currency_code, action='append', dest='signed_currencies',
 
            metavar='CODE',
 
            help="In Ledger output, use a sign for this currency if known. "
 
            "Can be specified multiple times.",
...
 
@@ -129,20 +141,22 @@ class Configuration:
 
        return prog_parser
 

	
 
    def _build_conffile(self):
 
        return configparser.ConfigParser()
 

	
 
    def _read_from_conffile(self, argname, sectionname, fallback, convert_to=None,
 
                            confname=None, getter='get', unset=None):
 
                            confname=None, getter='get', unset=None,
 
                            *, convert_fallback=False):
 
        if getattr(self.args, argname) is not unset:
 
            return
 
        elif confname is None:
 
            confname = argname
 
        get_method = getattr(self.conffile, getter)
 
        value = get_method(sectionname, confname, fallback=fallback)
 
        if convert_to is not None:
 
        if (convert_to is not None
 
            and (value is not fallback or convert_fallback)):
 
            value = self._convert_or_error(convert_to, value, confname)
 
        setattr(self.args, argname, value)
 

	
 
    def _convert_or_error(self, argtype, s_value, argname=None, typename=None):
 
        try:
 
            return argtype(s_value)
...
 
@@ -159,13 +173,18 @@ class Configuration:
 
    def _post_hook_historical(self):
 
        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)
 
        self._read_from_conffile('signed_currencies', 'Historical', self.args.base, currency_list)
 
        if self.args.denomination is self.NO_DENOMINATION:
 
            self.args.denomination = None
 
        else:
 
            self._read_from_conffile('denomination', 'Historical', None, currency_code)
 
        self._read_from_conffile('signed_currencies', 'Historical', self.args.base,
 
                                 currency_list, convert_fallback=True)
 
        self._read_from_conffile('ledger', 'Historical', False, getter='getboolean')
 
        self.args.to_currency = self.args.base
 
        if self.args.word4 and (self.args.word3.lower() in self.PREPOSITIONS):
 
            self.args.word3 = self.args.word4
 
        if self.args.word1 is None:
 
            pass