From 643fb7f6ea45095f8f1687548dc4f5dbc99e489c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guilhem=20Faur=C3=A9?= Date: Wed, 7 Jun 2023 15:35:04 +0200 Subject: [PATCH] fix logging & deduplicate write_children --- spip2md/extended_models.py | 80 ++++++++++++++------------------------ spip2md/lib.py | 2 +- 2 files changed, 30 insertions(+), 52 deletions(-) diff --git a/spip2md/extended_models.py b/spip2md/extended_models.py index 57f3fb7..7a34f18 100644 --- a/spip2md/extended_models.py +++ b/spip2md/extended_models.py @@ -506,6 +506,26 @@ class RedactionalObject(WritableObject): body += "\n\n# EXTRA\n\n" + self._extra return body + # Write all the documents of this object + def write_children( + self, children: tuple[WritableObject], **kwargs: str + ) -> list[str]: + LOG.debug(f"Writing documents of {type(self).__name__} `{self._title}`") + output: list[str] = [] + total = len(children) + for i, obj in enumerate(children): + try: + output.append( + obj.write_all( + self._depth, self.dest_directory(), i, total, **kwargs + ) + ) + except LangNotFoundError as err: + LOG.debug(err) + except DontExportDraftError as err: + LOG.debug(err) + return output + # Write object to output destination def write(self) -> str: # Make a directory for this object if there isn’t @@ -516,7 +536,7 @@ class RedactionalObject(WritableObject): # or to write into a directory without the same fileprefix directory = self.dest_directory() for file in listdir(directory): - logging.debug( + LOG.debug( f"Testing if {type(self).__name__} `{self.dest_path()}` of prefix " + f"{self._fileprefix} can be written along with `{file}` " + f"of prefix `{file.split('.')[0]}` in `{self.dest_directory()}`" @@ -525,7 +545,7 @@ class RedactionalObject(WritableObject): self.dest_directory() + file == self.dest_path() or file.split(".")[0] != self._fileprefix ): - logging.debug( + LOG.debug( f"Not writing {self._title} in {self.dest_directory()} along " + file ) @@ -548,23 +568,6 @@ class RedactionalObject(WritableObject): + f" {forced_lang} translation in Markup either" ) - # Write all the children of this object - def write_documents(self) -> list[str]: - LOG.debug(f"Writing children of {type(self).__name__} `{self._title}`") - output: list[str] = [] - children = self.documents() - total = len(children) - for i, obj in enumerate(children): - try: - output.append( - obj.write_all(self._depth, self.dest_directory(), i, total) - ) - except LangNotFoundError as err: - logging.debug(err) - except DontExportDraftError as err: - logging.debug(err) - return output - class Article(RedactionalObject, NormalizedArticle): class Meta: @@ -622,12 +625,12 @@ class Article(RedactionalObject, NormalizedArticle): # Perform all the write steps of this object def write_all( - self, forced_lang: str, parentdepth: int, parentdir: str, index: int, total: int + self, parentdepth: int, parentdir: str, index: int, total: int, forced_lang: str ) -> DeepDict: self.convert(forced_lang) return { "msg": super().write_all(parentdepth, parentdir, index, total), - "documents": self.write_documents(), + "documents": self.write_children(self.documents()), } @@ -666,39 +669,14 @@ class Section(RedactionalObject, NormalizedSection): .limit(limit) ) - # Write all the children of this object - def write_children(self, forcedlang: str) -> DeepDict: - LOG.debug(f"Writing children of {type(self).__name__} `{self._title}`") - output: DeepDict = { - "documents": super().write_documents(), - "articles": [], - "sections": [], - } - for name, children in ( - ("articles", self.articles()), - ("sections", self.sections()), - ): - buffer: list[DeepDict] = [] - total = len(children) - for i, obj in enumerate(children): - try: - buffer.append( - obj.write_all( - forcedlang, self._depth, self.dest_directory(), i, total - ) - ) - except LangNotFoundError as err: - logging.debug(err) - except DontExportDraftError as err: - logging.debug(err) - output[name] = buffer - return output - # Perform all the write steps of this object def write_all( - self, forced_lang: str, parentdepth: int, parentdir: str, index: int, total: int + self, parentdepth: int, parentdir: str, index: int, total: int, forced_lang: str ) -> DeepDict: self.convert(forced_lang) return { "msg": super().write_all(parentdepth, parentdir, index, total), - } | self.write_children(forced_lang) + "documents": self.write_children(self.documents()), + "articles": self.write_children(self.articles(), forced_lang=forced_lang), + "sections": self.write_children(self.sections(), forced_lang=forced_lang), + } diff --git a/spip2md/lib.py b/spip2md/lib.py index 63c1545..2fd8574 100644 --- a/spip2md/lib.py +++ b/spip2md/lib.py @@ -50,7 +50,7 @@ as database user {esc(BOLD)}{CFG.db_user}{esc()} buffer: list[DeepDict] = [] ROOTLOG.debug(f"Begin exporting {lang} root section {i}/{nb}") try: - buffer.append(s.write_all(lang, -1, CFG.output_dir, i, nb)) + buffer.append(s.write_all(-1, CFG.output_dir, i, nb, lang)) except LangNotFoundError as err: ROOTLOG.debug(err) # Log the message except DontExportDraftError as err: # Will happen in not CFG.export_drafts