mirror of
https://github.com/Garmelon/PFERD.git
synced 2023-12-21 10:23:01 +01:00
Improve PFERD error handling
This commit is contained in:
parent
b4d97cd545
commit
9889ce6b57
@ -115,7 +115,12 @@ def main() -> None:
|
|||||||
dump_config(args, config)
|
dump_config(args, config)
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
|
try:
|
||||||
pferd = Pferd(config)
|
pferd = Pferd(config)
|
||||||
|
except ConfigOptionError as e:
|
||||||
|
log.error(str(e))
|
||||||
|
exit(1)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
asyncio.run(pferd.run())
|
asyncio.run(pferd.run())
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
|
@ -1,71 +1,58 @@
|
|||||||
from typing import Dict
|
from typing import Dict
|
||||||
|
|
||||||
from rich import print
|
|
||||||
from rich.markup import escape
|
from rich.markup import escape
|
||||||
|
|
||||||
from .authenticator import Authenticator
|
from .authenticator import Authenticator
|
||||||
from .authenticators import AUTHENTICATORS
|
from .authenticators import AUTHENTICATORS
|
||||||
from .config import Config
|
from .config import Config, ConfigOptionError
|
||||||
from .crawler import Crawler
|
from .crawler import Crawler, CrawlError
|
||||||
from .crawlers import CRAWLERS
|
from .crawlers import CRAWLERS
|
||||||
|
from .logging import log
|
||||||
|
|
||||||
class PferdLoadException(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class Pferd:
|
class Pferd:
|
||||||
def __init__(self, config: Config):
|
def __init__(self, config: Config):
|
||||||
|
"""
|
||||||
|
May throw ConfigOptionError.
|
||||||
|
"""
|
||||||
|
|
||||||
self._config = config
|
self._config = config
|
||||||
self._authenticators: Dict[str, Authenticator] = {}
|
self._authenticators: Dict[str, Authenticator] = {}
|
||||||
self._crawlers: Dict[str, Crawler] = {}
|
self._crawlers: Dict[str, Crawler] = {}
|
||||||
|
|
||||||
|
self._load_authenticators()
|
||||||
|
self._load_crawlers()
|
||||||
|
|
||||||
def _load_authenticators(self) -> None:
|
def _load_authenticators(self) -> None:
|
||||||
abort = False
|
|
||||||
for name, section in self._config.authenticator_sections():
|
for name, section in self._config.authenticator_sections():
|
||||||
print(f"[bold bright_cyan]Loading[/] {escape(name)}")
|
log.print(f"[bold bright_cyan]Loading[/] {escape(name)}")
|
||||||
authenticator_type = section.get("type")
|
auth_type = section.get("type")
|
||||||
authenticator_constructor = AUTHENTICATORS.get(authenticator_type)
|
authenticator_constructor = AUTHENTICATORS.get(auth_type)
|
||||||
if authenticator_constructor is None:
|
if authenticator_constructor is None:
|
||||||
abort = True
|
raise ConfigOptionError(name, "type", f"Unknown authenticator type: {auth_type!r}")
|
||||||
t = escape(repr(authenticator_type))
|
|
||||||
print(f"[red]Error: Unknown authenticator type {t}")
|
|
||||||
continue
|
|
||||||
|
|
||||||
authenticator = authenticator_constructor(name, section, self._config)
|
authenticator = authenticator_constructor(name, section, self._config)
|
||||||
self._authenticators[name] = authenticator
|
self._authenticators[name] = authenticator
|
||||||
|
|
||||||
if abort:
|
|
||||||
raise PferdLoadException()
|
|
||||||
|
|
||||||
def _load_crawlers(self) -> None:
|
def _load_crawlers(self) -> None:
|
||||||
abort = False
|
|
||||||
for name, section in self._config.crawler_sections():
|
for name, section in self._config.crawler_sections():
|
||||||
print(f"[bold bright_cyan]Loading[/] {escape(name)}")
|
log.print(f"[bold bright_cyan]Loading[/] {escape(name)}")
|
||||||
crawler_type = section.get("type")
|
crawl_type = section.get("type")
|
||||||
crawler_constructor = CRAWLERS.get(crawler_type)
|
crawler_constructor = CRAWLERS.get(crawl_type)
|
||||||
if crawler_constructor is None:
|
if crawler_constructor is None:
|
||||||
abort = True
|
raise ConfigOptionError(name, "type", f"Unknown crawler type: {crawl_type!r}")
|
||||||
t = escape(repr(crawler_type))
|
|
||||||
print(f"[red]Error: Unknown crawler type {t}")
|
|
||||||
continue
|
|
||||||
|
|
||||||
crawler = crawler_constructor(name, section, self._config, self._authenticators)
|
crawler = crawler_constructor(name, section, self._config, self._authenticators)
|
||||||
self._crawlers[name] = crawler
|
self._crawlers[name] = crawler
|
||||||
|
|
||||||
if abort:
|
|
||||||
raise PferdLoadException()
|
|
||||||
|
|
||||||
async def run(self) -> None:
|
async def run(self) -> None:
|
||||||
try:
|
|
||||||
self._load_authenticators()
|
|
||||||
self._load_crawlers()
|
|
||||||
except PferdLoadException:
|
|
||||||
print("[bold red]Could not initialize PFERD properly")
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
for name, crawler in self._crawlers.items():
|
for name, crawler in self._crawlers.items():
|
||||||
print()
|
log.print("")
|
||||||
print(f"[bold bright_cyan]Running[/] {escape(name)}")
|
log.print(f"[bold bright_cyan]Running[/] {escape(name)}")
|
||||||
|
|
||||||
|
try:
|
||||||
await crawler.run()
|
await crawler.run()
|
||||||
|
except CrawlError as e:
|
||||||
|
log.error(str(e))
|
||||||
|
except Exception:
|
||||||
|
log.unexpected_exception()
|
||||||
|
Loading…
Reference in New Issue
Block a user