mirror of
https://github.com/lilydjwg/nvchecker
synced 2025-01-12 01:09:25 +00:00
give better error messages when failing to load file
This commit is contained in:
parent
a8073e2ad5
commit
b83f92cc67
@ -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)
|
||||
|
||||
|
@ -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']
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user