mirror of
				https://github.com/Garmelon/PFERD.git
				synced 2025-10-31 21:02:42 +01:00 
			
		
		
		
	Explain config file loading
This commit is contained in:
		| @@ -133,7 +133,7 @@ SUBPARSERS = PARSER.add_subparsers(title="crawlers") | |||||||
|  |  | ||||||
| LOCAL_CRAWLER = SUBPARSERS.add_parser( | LOCAL_CRAWLER = SUBPARSERS.add_parser( | ||||||
|     "local", |     "local", | ||||||
|     parents=[GENERAL_PARSER, CRAWLER_PARSER], |     parents=[CRAWLER_PARSER], | ||||||
| ) | ) | ||||||
| LOCAL_CRAWLER.set_defaults(command="local") | LOCAL_CRAWLER.set_defaults(command="local") | ||||||
| LOCAL_CRAWLER_GROUP = LOCAL_CRAWLER.add_argument_group( | LOCAL_CRAWLER_GROUP = LOCAL_CRAWLER.add_argument_group( | ||||||
| @@ -194,12 +194,16 @@ def load_local_crawler( | |||||||
| def load_parser( | def load_parser( | ||||||
|         args: argparse.Namespace, |         args: argparse.Namespace, | ||||||
| ) -> configparser.ConfigParser: | ) -> configparser.ConfigParser: | ||||||
|  |     log.explain_topic("Loading config") | ||||||
|     parser = configparser.ConfigParser() |     parser = configparser.ConfigParser() | ||||||
|  |  | ||||||
|     if args.command is None: |     if args.command is None: | ||||||
|  |         log.explain("No CLI command specified, loading config from file") | ||||||
|         Config.load_parser(parser, path=args.config) |         Config.load_parser(parser, path=args.config) | ||||||
|     elif args.command == "local": |     else: | ||||||
|         load_local_crawler(args, parser) |         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) |     load_general(args, parser) | ||||||
|     prune_crawlers(args, parser) |     prune_crawlers(args, parser) | ||||||
| @@ -230,6 +234,8 @@ def main() -> None: | |||||||
|     # Configure log levels set by command line arguments |     # Configure log levels set by command line arguments | ||||||
|     if args.explain is not None: |     if args.explain is not None: | ||||||
|         log.output_explain = args.explain |         log.output_explain = args.explain | ||||||
|  |     if args.dump_config: | ||||||
|  |         log.output_explain = False | ||||||
|  |  | ||||||
|     if args.version: |     if args.version: | ||||||
|         print(f"{NAME} {VERSION}") |         print(f"{NAME} {VERSION}") | ||||||
| @@ -237,7 +243,9 @@ def main() -> None: | |||||||
|  |  | ||||||
|     try: |     try: | ||||||
|         config = Config(load_parser(args)) |         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) |         exit(1) | ||||||
|  |  | ||||||
|     # Configure log levels set in the config file |     # Configure log levels set in the config file | ||||||
|   | |||||||
| @@ -6,11 +6,14 @@ from dataclasses import dataclass | |||||||
| from pathlib import Path | from pathlib import Path | ||||||
| from typing import Any, List, NoReturn, Optional, Tuple | from typing import Any, List, NoReturn, Optional, Tuple | ||||||
|  |  | ||||||
|  | from .logging import log | ||||||
| from .utils import prompt_yes_no | from .utils import prompt_yes_no | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @dataclass | ||||||
| class ConfigLoadException(Exception): | class ConfigLoadException(Exception): | ||||||
|     pass |     path: Path | ||||||
|  |     reason: str | ||||||
|  |  | ||||||
|  |  | ||||||
| class ConfigDumpException(Exception): | class ConfigDumpException(Exception): | ||||||
| @@ -77,20 +80,18 @@ class Config: | |||||||
|     def default_section(self) -> DefaultSection: |     def default_section(self) -> DefaultSection: | ||||||
|         return self._default_section |         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 |     @staticmethod | ||||||
|     def load_parser(parser: ConfigParser, path: Optional[Path] = None) -> None: |     def load_parser(parser: ConfigParser, path: Optional[Path] = None) -> None: | ||||||
|         """ |         """ | ||||||
|         May throw a ConfigLoadException. |         May throw a ConfigLoadException. | ||||||
|         """ |         """ | ||||||
|  |  | ||||||
|         if not path: |         if path: | ||||||
|  |             log.explain("Using custom path") | ||||||
|  |         else: | ||||||
|  |             log.explain("Using default path") | ||||||
|             path = Config._default_path() |             path = Config._default_path() | ||||||
|  |         log.explain(f"Loading {str(path)!r}") | ||||||
|  |  | ||||||
|         # Using config.read_file instead of config.read because config.read |         # Using config.read_file instead of config.read because config.read | ||||||
|         # would just ignore a missing file and carry on. |         # would just ignore a missing file and carry on. | ||||||
| @@ -98,11 +99,11 @@ class Config: | |||||||
|             with open(path) as f: |             with open(path) as f: | ||||||
|                 parser.read_file(f, source=str(path)) |                 parser.read_file(f, source=str(path)) | ||||||
|         except FileNotFoundError: |         except FileNotFoundError: | ||||||
|             Config._fail_load(path, "File does not exist") |             raise ConfigLoadException(path, "File does not exist") | ||||||
|         except IsADirectoryError: |         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: |         except PermissionError: | ||||||
|             Config._fail_load(path, "Insufficient permissions") |             raise ConfigLoadException(path, "Insufficient permissions") | ||||||
|  |  | ||||||
|     @staticmethod |     @staticmethod | ||||||
|     def _fail_dump(path: Path, reason: str) -> None: |     def _fail_dump(path: Path, reason: str) -> None: | ||||||
|   | |||||||
| @@ -101,6 +101,15 @@ class Log: | |||||||
|         else: |         else: | ||||||
|             self.console.print(text) |             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: |     def explain_topic(self, text: str) -> None: | ||||||
|         if self.output_explain: |         if self.output_explain: | ||||||
|             self.print(f"[cyan]{escape(text)}") |             self.print(f"[cyan]{escape(text)}") | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Joscha
					Joscha