From d15ad5fd8e9cee0685a24687327e92ef227cbdbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guilhem=20Faur=C3=A9?= Date: Wed, 17 May 2023 12:17:54 +0200 Subject: [PATCH] =?UTF-8?q?start=20big=20refactoring=20to=20iterate=20over?= =?UTF-8?q?=20sections,=20then=20section=E2=80=99s=20articles?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spip2md/{articles.py => lib.py} | 37 +++++++++++++++++++++++++++++++-- spip2md/main.py | 37 +++++++++++++++++---------------- 2 files changed, 54 insertions(+), 20 deletions(-) rename spip2md/{articles.py => lib.py} (86%) diff --git a/spip2md/articles.py b/spip2md/lib.py similarity index 86% rename from spip2md/articles.py rename to spip2md/lib.py index 0566e7d..d2cdbec 100644 --- a/spip2md/articles.py +++ b/spip2md/lib.py @@ -137,14 +137,17 @@ class Section: self.depth: int = section.profondeur self.agenda: int = section.agenda + def get_articles(self, limit: int): + return Articles(limit) + class Articles: exported: int = 0 - def __init__(self, maxexport: int) -> None: + def __init__(self, limit: int) -> None: # Query the DB to retrieve all articles sorted by publication date self.articles = ( - SpipArticles.select().order_by(SpipArticles.date.desc()).limit(maxexport) + SpipArticles.select().order_by(SpipArticles.date.desc()).limit(limit) ) self.toExport: int = len(self.articles) @@ -163,3 +166,33 @@ class Articles: {"exported": self.exported, "remaining": self.remaining()}, article, ) + + +class Sections: + exported: int = 0 + + def __init__(self, limit: int = 0) -> None: + # Query the DB to retrieve all articles sorted by publication date + if limit > 0: + self.articles = ( + SpipArticles.select().order_by(SpipArticles.date.desc()).limit(limit) + ) + else: + self.articles = SpipArticles.select().order_by(SpipArticles.date.desc()) + self.toExport: int = len(self.articles) + + def remaining(self): + return self.toExport - self.exported + + def __iter__(self): + return self + + def __next__(self): + if self.remaining() <= 0: + raise StopIteration + self.exported += 1 + section = Section(self.articles[self.exported - 1]) + return ( + {"exported": self.exported, "remaining": self.remaining()}, + section, + ) diff --git a/spip2md/main.py b/spip2md/main.py index 20eea96..202d839 100755 --- a/spip2md/main.py +++ b/spip2md/main.py @@ -4,10 +4,10 @@ import sys from os import makedirs, mkdir from shutil import rmtree -from articles import Article, Articles from config import config from converter import highlight_unknown_chars from database import db +from lib import Article, Sections # Define terminal escape sequences to stylize output R: str = "\033[91m" @@ -35,25 +35,26 @@ if __name__ == "__main__": unknown_chars_articles: list[Article] = [] # Loop among first maxexport articles & export them - for counter, article in Articles(maxexport): - if (counter["exported"] - 1) % 100 == 0: + for counter, section in Sections(): + for counter, article in section.get_articles(maxexport): + if (counter["exported"] - 1) % 100 == 0: + print( + f"\n{BOLD}Exporting {R}{counter['remaining']+1}{RESET}" + + f"{BOLD} SPIP articles to Markdown & YAML files{RESET}\n" + ) + empty: str = "EMPTY " if len(article.text) < 1 else "" print( - f"\n{BOLD}Exporting {R}{counter['remaining']+1}{RESET}" - + f"{BOLD} SPIP articles to Markdown & YAML files{RESET}\n" + f"{BOLD}{counter['exported']}. {empty}{RESET}" + + highlight_unknown_chars(article.title, R, RESET) ) - empty: str = "EMPTY " if len(article.text) < 1 else "" - print( - f"{BOLD}{counter['exported']}. {empty}{RESET}" - + highlight_unknown_chars(article.title, R, RESET) - ) - fullpath: str = config.output_dir + "/" + article.get_path() - print(f"{BOLD}>{RESET} {fullpath}{article.get_filename()}") - makedirs(fullpath, exist_ok=True) - with open(fullpath + article.get_filename(), "w") as f: - f.write(article.get_article()) - # Store detected unknown characters - if len(article.get_unknown_chars()) > 0: - unknown_chars_articles.append(article) + fullpath: str = config.output_dir + "/" + article.get_path() + print(f"{BOLD}>{RESET} {fullpath}{article.get_filename()}") + makedirs(fullpath, exist_ok=True) + with open(fullpath + article.get_filename(), "w") as f: + f.write(article.get_article()) + # Store detected unknown characters + if len(article.get_unknown_chars()) > 0: + unknown_chars_articles.append(article) for article in unknown_chars_articles: unknown_chars_apparitions: list[str] = article.get_unknown_chars()