mirror of
https://github.com/Garmelon/PFERD.git
synced 2023-12-21 10:23:01 +01:00
Explain config file loading
This commit is contained in:
@ -6,11 +6,14 @@ from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Any, List, NoReturn, Optional, Tuple
|
||||
|
||||
from .logging import log
|
||||
from .utils import prompt_yes_no
|
||||
|
||||
|
||||
@dataclass
|
||||
class ConfigLoadException(Exception):
|
||||
pass
|
||||
path: Path
|
||||
reason: str
|
||||
|
||||
|
||||
class ConfigDumpException(Exception):
|
||||
@ -77,20 +80,18 @@ class Config:
|
||||
def default_section(self) -> DefaultSection:
|
||||
return self._default_section
|
||||
|
||||
@staticmethod
|
||||
def _fail_load(path: Path, reason: str) -> None:
|
||||
print(f"Failed to load config file at {path}")
|
||||
print(f"Reason: {reason}")
|
||||
raise ConfigLoadException()
|
||||
|
||||
@staticmethod
|
||||
def load_parser(parser: ConfigParser, path: Optional[Path] = None) -> None:
|
||||
"""
|
||||
May throw a ConfigLoadException.
|
||||
"""
|
||||
|
||||
if not path:
|
||||
if path:
|
||||
log.explain("Using custom path")
|
||||
else:
|
||||
log.explain("Using default path")
|
||||
path = Config._default_path()
|
||||
log.explain(f"Loading {str(path)!r}")
|
||||
|
||||
# Using config.read_file instead of config.read because config.read
|
||||
# would just ignore a missing file and carry on.
|
||||
@ -98,11 +99,11 @@ class Config:
|
||||
with open(path) as f:
|
||||
parser.read_file(f, source=str(path))
|
||||
except FileNotFoundError:
|
||||
Config._fail_load(path, "File does not exist")
|
||||
raise ConfigLoadException(path, "File does not exist")
|
||||
except IsADirectoryError:
|
||||
Config._fail_load(path, "That's a directory, not a file")
|
||||
raise ConfigLoadException(path, "That's a directory, not a file")
|
||||
except PermissionError:
|
||||
Config._fail_load(path, "Insufficient permissions")
|
||||
raise ConfigLoadException(path, "Insufficient permissions")
|
||||
|
||||
@staticmethod
|
||||
def _fail_dump(path: Path, reason: str) -> None:
|
||||
|
Reference in New Issue
Block a user