From a82a0b19c2193c6817ae07361889de8fd392868e Mon Sep 17 00:00:00 2001 From: I-Al-Istannen Date: Sun, 7 Nov 2021 21:40:22 +0100 Subject: [PATCH] Collect crawler warnings/errors and include them in the report --- PFERD/crawl/crawler.py | 8 ++++++-- PFERD/pferd.py | 8 ++++++++ PFERD/report.py | 24 +++++++++++++++++++++++- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/PFERD/crawl/crawler.py b/PFERD/crawl/crawler.py index c492ee9..53f43e9 100644 --- a/PFERD/crawl/crawler.py +++ b/PFERD/crawl/crawler.py @@ -47,10 +47,12 @@ def noncritical(f: Wrapped) -> Wrapped: try: f(*args, **kwargs) except (CrawlWarning, OutputDirError, MarkDuplicateError, MarkConflictError) as e: + crawler.report.add_warning(str(e)) log.warn(str(e)) crawler.error_free = False - except: # noqa: E722 do not use bare 'except' + except Exception as e: crawler.error_free = False + crawler.report.add_error(str(e)) raise return wrapper # type: ignore @@ -83,8 +85,10 @@ def anoncritical(f: AWrapped) -> AWrapped: except (CrawlWarning, OutputDirError, MarkDuplicateError, MarkConflictError) as e: log.warn(str(e)) crawler.error_free = False - except: # noqa: E722 do not use bare 'except' + crawler.report.add_warning(str(e)) + except Exception as e: crawler.error_free = False + crawler.report.add_error(str(e)) raise return None diff --git a/PFERD/pferd.py b/PFERD/pferd.py index 726ed45..079053b 100644 --- a/PFERD/pferd.py +++ b/PFERD/pferd.py @@ -182,5 +182,13 @@ class Pferd: something_changed = True log.report(f" [bold bright_magenta]Not deleted[/] {fmt_path(path)}") + for warning in crawler.report.encountered_warnings: + something_changed = True + log.report(f" [bold bright_red]Warning[/] {warning}") + + for error in crawler.report.encountered_errors: + something_changed = True + log.report(f" [bold bright_red]Error[/] {error}") + if not something_changed: log.report(" Nothing changed") diff --git a/PFERD/report.py b/PFERD/report.py index 99a4661..0e0c789 100644 --- a/PFERD/report.py +++ b/PFERD/report.py @@ -67,8 +67,14 @@ class Report: self.deleted_files: Set[PurePath] = set() # Files that should have been deleted by the cleanup but weren't self.not_deleted_files: Set[PurePath] = set() + + # Custom crawler-specific data self.custom: Dict[str, Any] = dict() + # Encountered errors and warnings + self.encountered_warnings: List[str] = [] + self.encountered_errors: List[str] = [] + @staticmethod def _get_list_of_strs(data: Dict[str, Any], key: str) -> List[str]: result: Any = data.get(key, []) @@ -119,6 +125,8 @@ class Report: for elem in self._get_list_of_strs(data, "not_deleted"): self.not_delete_file(PurePath(elem)) self.custom = self._get_str_dictionary(data, "custom") + self.encountered_errors = self._get_list_of_strs(data, "encountered_errors") + self.encountered_warnings = self._get_list_of_strs(data, "encountered_warnings") return self @@ -135,7 +143,9 @@ class Report: "changed": [str(path) for path in sorted(self.changed_files)], "deleted": [str(path) for path in sorted(self.deleted_files)], "not_deleted": [str(path) for path in sorted(self.not_deleted_files)], - "custom": self.custom + "custom": self.custom, + "encountered_warnings": self.encountered_warnings, + "encountered_errors": self.encountered_errors, } with open(path, "w") as f: @@ -214,3 +224,15 @@ class Report: Retrieves a custom value for the given key. """ return self.custom.get(key) + + def add_error(self, error: str) -> None: + """ + Adds an error to this report's error list. + """ + self.encountered_errors.append(error) + + def add_warning(self, warning: str) -> None: + """ + Adds a warning to this report's warning list. + """ + self.encountered_warnings.append(warning)