This commit is contained in:
Guilhem Fauré 2023-05-31 11:31:58 +02:00
parent 8bdb1e04e9
commit 35829285bf
8 changed files with 70 additions and 68 deletions

View File

@ -29,7 +29,7 @@ pymysql = "^1.0.3"
peewee = "^3.16.2" peewee = "^3.16.2"
[tool.poetry.scripts] [tool.poetry.scripts]
spip2md = "spip2md:main" spip2md = "spip2md.lib:cli"
[build-system] [build-system]
requires = ["poetry-core"] requires = ["poetry-core"]

View File

@ -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())

View File

@ -49,4 +49,4 @@ class Configuration:
setattr(self, attr, config[attr]) setattr(self, attr, config[attr])
CFG = Configuration(config_file()) CFG = Configuration(config_file=config_file())

View File

@ -11,15 +11,7 @@ from slugify import slugify
from yaml import dump from yaml import dump
from spip2md.config import CFG from spip2md.config import CFG
from spip2md.database import ( from spip2md.regexmaps import (
SpipArticles,
SpipAuteurs,
SpipAuteursLiens,
SpipDocuments,
SpipDocumentsLiens,
SpipRubriques,
)
from spip2md.regexmap import (
ARTICLE_LINK, ARTICLE_LINK,
BLOAT, BLOAT,
DOCUMENT_LINK, DOCUMENT_LINK,
@ -33,6 +25,14 @@ from spip2md.regexmap import (
UNKNOWN_ISO, UNKNOWN_ISO,
WARNING_OUTPUT, WARNING_OUTPUT,
) )
from spip2md.spip_models import (
SpipArticles,
SpipAuteurs,
SpipAuteursLiens,
SpipDocuments,
SpipDocumentsLiens,
SpipRubriques,
)
from spip2md.style import BLUE, BOLD, GREEN, WARNING_STYLE, YELLOW, esc from spip2md.style import BLUE, BOLD, GREEN, WARNING_STYLE, YELLOW, esc
@ -65,16 +65,16 @@ class SpipWritable:
translated: str = lang.group(2)[:50].strip() translated: str = lang.group(2)[:50].strip()
logging.info(f"{lang.group(1)} translation of {title}: {translated}") logging.info(f"{lang.group(1)} translation of {title}: {translated}")
# Instantiate & write translated # Instantiate & write translated
for lang, translation in translations.items(): # for lang, translation in translations.items():
if lang == "non existant lang": # if lang == "non existant lang":
new_lang = self.__init__( # new_lang = self.__init__(
texte=translation, # texte=translation,
lang=lang, # lang=lang,
titre=self.titre, # titre=self.titre,
descriptif=self.descriptif, # descriptif=self.descriptif,
profondeur=self.profondeur, # profondeur=self.profondeur,
style=self.style, # style=self.style,
) # )
# Return the translations dict # Return the translations dict
# return translations # return translations
# Return the first detected language # Return the first detected language

View File

@ -1,13 +1,12 @@
# SPIP website to plain Markdown files converter, Copyright (C) 2023 Guilhem Fauré # SPIP website to plain Markdown files converter, Copyright (C) 2023 Guilhem Fauré
import logging import logging
import sys
from os import makedirs, remove from os import makedirs, remove
from os.path import isfile from os.path import isfile
from shutil import rmtree from shutil import rmtree
from spip2md.config import CFG from spip2md.config import CFG
from spip2md.database import DB from spip2md.extended_models import RootRubrique
from spip2md.spipobjects import RootRubrique, Rubrique from spip2md.spip_models import DB
from spip2md.style import BOLD, esc from spip2md.style import BOLD, esc
@ -29,28 +28,47 @@ def count_output(
return (branches, leaves) return (branches, leaves)
# Clear the previous log file if needed # Clear the previous log file if needed, then configure logging
if CFG.clear_log and isfile(CFG.logfile): def init_logging() -> None:
if CFG.clear_log and isfile(CFG.logfile):
remove(CFG.logfile) remove(CFG.logfile)
# Configure logging logging.basicConfig(
logging.basicConfig(
format="%(levelname)s:%(message)s", format="%(levelname)s:%(message)s",
filename=CFG.logfile, filename=CFG.logfile,
encoding="utf-8", encoding="utf-8",
level=CFG.loglevel, level=CFG.loglevel,
) )
# Connect to the MySQL database with Peewee ORM # Summary message at the end of the program
DB.init(CFG.db, host=CFG.db_host, user=CFG.db_user, password=CFG.db_pass) def summary(branches: int, leaves: int) -> None:
DB.connect() 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 # Clear the output dir if needed & create a new
def main(*argv): def clear_output() -> None:
# Allow main to get args when directly executed if CFG.clear_output:
if len(argv) == 0: rmtree(CFG.output_dir, True)
argv = sys.argv 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 # TODO Define max nb of sections/articles to export based on first CLI argument
# if len(argv) >= 2: # if len(argv) >= 2:
@ -58,25 +76,14 @@ def main(*argv):
# else: # else:
# sections_export = CFG.max_sections_export # sections_export = CFG.max_sections_export
# Clear the output dir & create a new init_logging()
if CFG.clear_output: clear_output()
rmtree(CFG.output_dir, True)
makedirs(CFG.output_dir, exist_ok=True)
# Get the virtual id=0 section # Connect to the MySQL database with Peewee ORM
root: Rubrique = RootRubrique() 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 # 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 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()}")

View File

@ -1,3 +1,4 @@
# SPIP website to plain Markdown files converter, Copyright (C) 2023 Guilhem Fauré
# Define styles for terminal printing # Define styles for terminal printing
BOLD = 1 # Bold BOLD = 1 # Bold
ITALIC = 3 # Italic ITALIC = 3 # Italic
@ -7,9 +8,9 @@ RED = 91 # Red
GREEN = 92 # Green GREEN = 92 # Green
YELLOW = 93 # Yellow YELLOW = 93 # Yellow
BLUE = 94 # Blue BLUE = 94 # Blue
C0 = 95 # Color MAGENTA = 95 # Magenta
C1 = 96 # Color CYAN = 96 # Cyan
C2 = 96 # Color WHITE = 97 # Clear White
# Style used for warnings # Style used for warnings
WARNING_STYLE = (BOLD, RED) WARNING_STYLE = (BOLD, RED)