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:
parent
0d10752b5a
commit
3300886120
@ -133,7 +133,7 @@ SUBPARSERS = PARSER.add_subparsers(title="crawlers")
|
||||
|
||||
LOCAL_CRAWLER = SUBPARSERS.add_parser(
|
||||
"local",
|
||||
parents=[GENERAL_PARSER, CRAWLER_PARSER],
|
||||
parents=[CRAWLER_PARSER],
|
||||
)
|
||||
LOCAL_CRAWLER.set_defaults(command="local")
|
||||
LOCAL_CRAWLER_GROUP = LOCAL_CRAWLER.add_argument_group(
|
||||
@ -194,12 +194,16 @@ def load_local_crawler(
|
||||
def load_parser(
|
||||
args: argparse.Namespace,
|
||||
) -> configparser.ConfigParser:
|
||||
log.explain_topic("Loading config")
|
||||
parser = configparser.ConfigParser()
|
||||
|
||||
if args.command is None:
|
||||
log.explain("No CLI command specified, loading config from file")
|
||||
Config.load_parser(parser, path=args.config)
|
||||
elif args.command == "local":
|
||||
load_local_crawler(args, parser)
|
||||
else:
|
||||
log.explain(f"CLI command specified, creating config for {args.command!r}")
|
||||
if args.command == "local":
|
||||
load_local_crawler(args, parser)
|
||||
|
||||
load_general(args, parser)
|
||||
prune_crawlers(args, parser)
|
||||
@ -230,6 +234,8 @@ def main() -> None:
|
||||
# Configure log levels set by command line arguments
|
||||
if args.explain is not None:
|
||||
log.output_explain = args.explain
|
||||
if args.dump_config:
|
||||
log.output_explain = False
|
||||
|
||||
if args.version:
|
||||
print(f"{NAME} {VERSION}")
|
||||
@ -237,7 +243,9 @@ def main() -> None:
|
||||
|
||||
try:
|
||||
config = Config(load_parser(args))
|
||||
except ConfigLoadException:
|
||||
except ConfigLoadException as e:
|
||||
log.error(f"Failed to load config file at path {str(e.path)!r}")
|
||||
log.error_contd(f"Reason: {e.reason}")
|
||||
exit(1)
|
||||
|
||||
# Configure log levels set in the config file
|
||||
|
@ -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:
|
||||
|
@ -101,6 +101,15 @@ class Log:
|
||||
else:
|
||||
self.console.print(text)
|
||||
|
||||
def warn(self, text: str) -> None:
|
||||
self.print(f"[bold bright_red]Warning[/] {escape(text)}")
|
||||
|
||||
def error(self, text: str) -> None:
|
||||
self.print(f"[bold bright_red]Error[/] [red]{escape(text)}")
|
||||
|
||||
def error_contd(self, text: str) -> None:
|
||||
self.print(f"[red]{escape(text)}")
|
||||
|
||||
def explain_topic(self, text: str) -> None:
|
||||
if self.output_explain:
|
||||
self.print(f"[cyan]{escape(text)}")
|
||||
|
Loading…
Reference in New Issue
Block a user