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"
[tool.poetry.scripts]
spip2md = "spip2md:main"
spip2md = "spip2md.lib:cli"
[build-system]
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])
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 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

View File

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

View File

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