diff --git a/nvchecker/__main__.py b/nvchecker/__main__.py index 9401b58..b05b722 100755 --- a/nvchecker/__main__.py +++ b/nvchecker/__main__.py @@ -33,14 +33,14 @@ def main() -> None: try: entries, options = core.load_file( args.file, use_keymanager=not bool(args.keyfile)) + + if args.keyfile: + keymanager = KeyManager(Path(args.keyfile)) + else: + keymanager = options.keymanager except core.FileLoadError as e: sys.exit(str(e)) - if args.keyfile: - keymanager = KeyManager(Path(args.keyfile)) - else: - keymanager = options.keymanager - if options.proxy is not None: ctx_proxy.set(options.proxy) diff --git a/nvchecker/core.py b/nvchecker/core.py index d40a1e8..ae182e4 100644 --- a/nvchecker/core.py +++ b/nvchecker/core.py @@ -29,6 +29,7 @@ from . import slogconf from .util import ( Entry, Entries, KeyManager, RawResult, Result, VersData, FunctionWorker, GetVersionError, + FileLoadError, ) from . import __version__ from .sortversion import sort_version_keys @@ -149,22 +150,14 @@ class Options(NamedTuple): httplib: Optional[str] http_timeout: int -class FileLoadError(Exception): - def __init__(self, kind, exc): - self.kind = kind - self.exc = exc - - def __str__(self): - return f'failed to load {self.kind}: {self.exc}' - def load_file( file: str, *, use_keymanager: bool, ) -> Tuple[Entries, Options]: try: config = toml.load(file) - except OSError as e: - raise FileLoadError('version configuration file', e) + except (OSError, toml.TomlDecodeError) as e: + raise FileLoadError('version configuration file', file, e) ver_files: Optional[Tuple[Path, Path]] = None keymanager = KeyManager(None) @@ -189,10 +182,7 @@ def load_file( keyfile_s = os.path.expandvars( os.path.expanduser(c.get('keyfile'))) keyfile = d / keyfile_s - try: - keymanager = KeyManager(keyfile) - except OSError as e: - raise FileLoadError('keyfile', e) + keymanager = KeyManager(keyfile) if 'source' in c: source_configs = c['source'] diff --git a/nvchecker/util.py b/nvchecker/util.py index 01d47f6..b443a43 100644 --- a/nvchecker/util.py +++ b/nvchecker/util.py @@ -37,14 +37,26 @@ VersionResult.__doc__ = '''The result of a `get_version` check. * `Exception` - An error occurred. ''' +class FileLoadError(Exception): + def __init__(self, kind, filename, exc): + self.kind = kind + self.filename = filename + self.exc = exc + + def __str__(self): + return f'failed to load {self.kind} {self.filename!r}: {self.exc}' + class KeyManager: '''Manages data in the keyfile.''' def __init__( self, file: Optional[Path], ) -> None: if file is not None: - with file.open() as f: - keys = toml.load(f)['keys'] + try: + with file.open() as f: + keys = toml.load(f)['keys'] + except (OSError, toml.TomlDecodeError) as e: + raise FileLoadError('keyfile', str(file), e) else: keys = {} self.keys = keys