From 35829285bfe3974eead011af1c809e547d0d5676 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guilhem=20Faur=C3=A9?= Date: Wed, 31 May 2023 11:31:58 +0200 Subject: [PATCH] refactor --- pyproject.toml | 2 +- spip2md/__main__.py | 6 -- spip2md/config.py | 2 +- .../{spipobjects.py => extended_models.py} | 38 ++++----- spip2md/{__init__.py => lib.py} | 83 ++++++++++--------- spip2md/{regexmap.py => regexmaps.py} | 0 spip2md/{database.py => spip_models.py} | 0 spip2md/style.py | 7 +- 8 files changed, 70 insertions(+), 68 deletions(-) delete mode 100644 spip2md/__main__.py rename spip2md/{spipobjects.py => extended_models.py} (97%) rename spip2md/{__init__.py => lib.py} (58%) rename spip2md/{regexmap.py => regexmaps.py} (100%) rename spip2md/{database.py => spip_models.py} (100%) diff --git a/pyproject.toml b/pyproject.toml index 13197d8..616f880 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,7 @@ pymysql = "^1.0.3" peewee = "^3.16.2" [tool.poetry.scripts] -spip2md = "spip2md:main" +spip2md = "spip2md.lib:cli" [build-system] requires = ["poetry-core"] diff --git a/spip2md/__main__.py b/spip2md/__main__.py deleted file mode 100644 index 1e260cc..0000000 --- a/spip2md/__main__.py +++ /dev/null @@ -1,6 +0,0 @@ -# SPIP website to plain Markdown files converter, Copyright (C) 2023 Guilhem Fauré -import sys - -from spip2md import main - -sys.exit(main()) diff --git a/spip2md/config.py b/spip2md/config.py index 1b21012..b48736a 100644 --- a/spip2md/config.py +++ b/spip2md/config.py @@ -49,4 +49,4 @@ class Configuration: setattr(self, attr, config[attr]) -CFG = Configuration(config_file()) +CFG = Configuration(config_file=config_file()) diff --git a/spip2md/spipobjects.py b/spip2md/extended_models.py similarity index 97% rename from spip2md/spipobjects.py rename to spip2md/extended_models.py index cf6d4a5..fdc8c0c 100644 --- a/spip2md/spipobjects.py +++ b/spip2md/extended_models.py @@ -11,15 +11,7 @@ from slugify import slugify from yaml import dump from spip2md.config import CFG -from spip2md.database import ( - SpipArticles, - SpipAuteurs, - SpipAuteursLiens, - SpipDocuments, - SpipDocumentsLiens, - SpipRubriques, -) -from spip2md.regexmap import ( +from spip2md.regexmaps import ( ARTICLE_LINK, BLOAT, DOCUMENT_LINK, @@ -33,6 +25,14 @@ from spip2md.regexmap import ( UNKNOWN_ISO, WARNING_OUTPUT, ) +from spip2md.spip_models import ( + SpipArticles, + SpipAuteurs, + SpipAuteursLiens, + SpipDocuments, + SpipDocumentsLiens, + SpipRubriques, +) from spip2md.style import BLUE, BOLD, GREEN, WARNING_STYLE, YELLOW, esc @@ -65,16 +65,16 @@ class SpipWritable: translated: str = lang.group(2)[:50].strip() logging.info(f"{lang.group(1)} translation of {title}: {translated}") # Instantiate & write translated - for lang, translation in translations.items(): - if lang == "non existant lang": - new_lang = self.__init__( - texte=translation, - lang=lang, - titre=self.titre, - descriptif=self.descriptif, - profondeur=self.profondeur, - style=self.style, - ) + # for lang, translation in translations.items(): + # if lang == "non existant lang": + # new_lang = self.__init__( + # texte=translation, + # lang=lang, + # titre=self.titre, + # descriptif=self.descriptif, + # profondeur=self.profondeur, + # style=self.style, + # ) # Return the translations dict # return translations # Return the first detected language diff --git a/spip2md/__init__.py b/spip2md/lib.py similarity index 58% rename from spip2md/__init__.py rename to spip2md/lib.py index 293ba14..fa1417e 100644 --- a/spip2md/__init__.py +++ b/spip2md/lib.py @@ -1,13 +1,12 @@ # SPIP website to plain Markdown files converter, Copyright (C) 2023 Guilhem Fauré import logging -import sys from os import makedirs, remove from os.path import isfile from shutil import rmtree from spip2md.config import CFG -from spip2md.database import DB -from spip2md.spipobjects import RootRubrique, Rubrique +from spip2md.extended_models import RootRubrique +from spip2md.spip_models import DB from spip2md.style import BOLD, esc @@ -29,28 +28,47 @@ def count_output( return (branches, leaves) -# Clear the previous log file if needed -if CFG.clear_log and isfile(CFG.logfile): - remove(CFG.logfile) -# Configure logging -logging.basicConfig( - format="%(levelname)s:%(message)s", - filename=CFG.logfile, - encoding="utf-8", - level=CFG.loglevel, -) +# Clear the previous log file if needed, then configure logging +def init_logging() -> None: + if CFG.clear_log and isfile(CFG.logfile): + remove(CFG.logfile) + logging.basicConfig( + format="%(levelname)s:%(message)s", + filename=CFG.logfile, + encoding="utf-8", + level=CFG.loglevel, + ) -# Connect to the MySQL database with Peewee ORM -DB.init(CFG.db, host=CFG.db_host, user=CFG.db_user, password=CFG.db_pass) -DB.connect() +# Summary message at the end of the program +def summary(branches: int, leaves: int) -> None: + print( + f"""\ +Exported a total of {esc(BOLD)}{leaves}{esc()} Markdown files, \ +stored into {esc(BOLD)}{branches}{esc()} directories""" + ) + # Warn about issued warnings in log file + if isfile(CFG.logfile): + print(f"\nTake a look at warnings and infos in {esc(BOLD)}{CFG.logfile}{esc()}") -# Main loop to execute only if script is directly executed -def main(*argv): - # Allow main to get args when directly executed - if len(argv) == 0: - argv = sys.argv +# Clear the output dir if needed & create a new +def clear_output() -> None: + if CFG.clear_output: + rmtree(CFG.output_dir, True) + makedirs(CFG.output_dir, exist_ok=True) + + +# Define the virtual id=0 section +ROOT = RootRubrique() + + +# To execute when script is directly executed as a script +def cli(): + # def cli(*addargv: str): + # import sys + + # argv: list[str] = sys.argv + list(addargv) # TODO Define max nb of sections/articles to export based on first CLI argument # if len(argv) >= 2: @@ -58,25 +76,14 @@ def main(*argv): # else: # sections_export = CFG.max_sections_export - # Clear the output dir & create a new - if CFG.clear_output: - rmtree(CFG.output_dir, True) - makedirs(CFG.output_dir, exist_ok=True) + init_logging() + clear_output() - # Get the virtual id=0 section - root: Rubrique = RootRubrique() + # Connect to the MySQL database with Peewee ORM + DB.init(CFG.db, host=CFG.db_host, user=CFG.db_user, password=CFG.db_pass) + DB.connect() # Write everything while printing the output human-readably - branches, leaves = count_output(root.write_tree(CFG.output_dir)) + summary(*count_output(ROOT.write_tree(CFG.output_dir))) DB.close() # Close the connection with the database - - print( # End, summary message - f"""\ -Exported a total of {esc(BOLD)}{leaves}{esc()} Markdown files, \ -stored into {esc(BOLD)}{branches}{esc()} directories""" - ) - - # Warn about issued warnings in log file - if isfile(CFG.logfile): - print(f"\nTake a look at warnings and infos in {esc(BOLD)}{CFG.logfile}{esc()}") diff --git a/spip2md/regexmap.py b/spip2md/regexmaps.py similarity index 100% rename from spip2md/regexmap.py rename to spip2md/regexmaps.py diff --git a/spip2md/database.py b/spip2md/spip_models.py similarity index 100% rename from spip2md/database.py rename to spip2md/spip_models.py diff --git a/spip2md/style.py b/spip2md/style.py index 3806b02..d45cf89 100644 --- a/spip2md/style.py +++ b/spip2md/style.py @@ -1,3 +1,4 @@ +# SPIP website to plain Markdown files converter, Copyright (C) 2023 Guilhem Fauré # Define styles for terminal printing BOLD = 1 # Bold ITALIC = 3 # Italic @@ -7,9 +8,9 @@ RED = 91 # Red GREEN = 92 # Green YELLOW = 93 # Yellow BLUE = 94 # Blue -C0 = 95 # Color -C1 = 96 # Color -C2 = 96 # Color +MAGENTA = 95 # Magenta +CYAN = 96 # Cyan +WHITE = 97 # Clear White # Style used for warnings WARNING_STYLE = (BOLD, RED)