architecture refactor

This commit is contained in:
Guilhem Fauré 2023-05-26 12:18:07 +02:00
parent 2bf6273212
commit 5e7740a414
3 changed files with 49 additions and 53 deletions

View File

@ -3,12 +3,13 @@
import sys import sys
from os import makedirs from os import makedirs
from shutil import rmtree from shutil import rmtree
from typing import Any
from peewee import ModelSelect from peewee import ModelSelect
from spip2md.config import CFG from spip2md.config import CFG
from spip2md.converters import unknown_chars, unknown_chars_context
from spip2md.database import DB from spip2md.database import DB
from spip2md.regexmap import unknown_chars, unknown_chars_context
from spip2md.spipobjects import ( from spip2md.spipobjects import (
Article, Article,
Rubrique, Rubrique,
@ -50,17 +51,6 @@ def highlight(string: str, *start_stop: tuple[int, int], end: str = "") -> None:
print(string[previous_stop:], end=end) print(string[previous_stop:], end=end)
# Plural ?
def ss(nb: int) -> str:
return "s" if nb > 1 else ""
# Indent with 2 spaces
def indent(nb: int = 1) -> None:
for _ in range(nb):
print(" ", end="")
# Query the DB to retrieve all sections without parent, sorted by publication date # Query the DB to retrieve all sections without parent, sorted by publication date
def root_sections(limit: int = 10**3) -> ModelSelect: def root_sections(limit: int = 10**3) -> ModelSelect:
return ( return (
@ -97,6 +87,16 @@ def warn_unknown_chars(article: Article) -> None:
print() # Break line print() # Break line
# Print one root section list output correctly
# sys.setrecursionlimit(2000)
def print_output(tree: list[Any], depth: int = 0, indent: str = " ") -> None:
for sub in tree:
if type(sub) == list:
print_output(sub, depth + 1)
else:
print(indent * depth + sub)
# Connect to the MySQL database with Peewee ORM # 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.init(CFG.db, host=CFG.db_host, user=CFG.db_user, password=CFG.db_pass)
DB.connect() DB.connect()
@ -128,7 +128,7 @@ def main(*argv):
# Write each root sections with its subtree # Write each root sections with its subtree
for i, section in enumerate(sections): for i, section in enumerate(sections):
section.write_tree(CFG.output_dir, i, total) print_output(section.write_tree(CFG.output_dir, i, total))
print() # Break line after exporting the section print() # Break line after exporting the section
# print() # Break line between export & unknown characters warning # print() # Break line between export & unknown characters warning

View File

@ -9,9 +9,7 @@ from peewee import BigAutoField, DateTimeField, ModelSelect
from slugify import slugify from slugify import slugify
from yaml import dump from yaml import dump
from spip2md import BLUE, BOLD, GREEN, RED, YELLOW, highlight, indent, ss, style
from spip2md.config import CFG from spip2md.config import CFG
from spip2md.converters import convert, link_document, unknown_chars
from spip2md.database import ( from spip2md.database import (
SpipArticles, SpipArticles,
SpipAuteurs, SpipAuteurs,
@ -20,6 +18,7 @@ from spip2md.database import (
SpipDocumentsLiens, SpipDocumentsLiens,
SpipRubriques, SpipRubriques,
) )
from spip2md.regexmap import convert, link_document, unknown_chars
class SpipWritable: class SpipWritable:
@ -33,26 +32,22 @@ class SpipWritable:
f"Subclasses need to implement filename(), date: {date}" f"Subclasses need to implement filename(), date: {date}"
) )
def begin_message( def begin_message(self, index: int, limit: int, step: int = 100) -> list[str]:
self, index: int, limit: int, depth: int = 0, step: int = 100 output: list[str] = []
) -> None: # Output the remaining number of objects to export every step object
# Print the remaining number of objects to export every step object
if index % step == 0: if index % step == 0:
indent(depth) output.append(f"Exporting {limit-index}")
print("Exporting", end="")
style(f" {limit-index}", BOLD, self.term_color)
if hasattr(self, "profondeur"): if hasattr(self, "profondeur"):
print(f" level {self.profondeur}", end="") output[-1] += f" level {self.profondeur}"
style(f" {type(self).__name__}{ss(limit-index)}\n") s: str = "s" if limit - index > 1 else ""
# Print the counter & title of the object being exported output[-1] += f" {type(self).__name__}{s}"
indent(depth) # Output the counter & title of the object being exported
style(f"{index + 1}. ") output.append(f"{index + 1}. ")
if len(self.titre) > 0: if len(self.titre) > 0:
highlight(self.titre, *unknown_chars(self.titre)) output[-1] += self.titre
else: else:
print("MISSING NAME", end="") output[-1] += "MISSING NAME"
# + ("EMPTY " if len(self.texte) < 1 else "") return output
# + f"{self.lang} "
# Write object to output destination # Write object to output destination
def write(self, parent_dir: str) -> str: def write(self, parent_dir: str) -> str:
@ -61,11 +56,11 @@ class SpipWritable:
) )
# Output information about file that was just exported # Output information about file that was just exported
def end_message(self, message: str | Exception): def end_message(self, message: str | Exception) -> str:
style(" -> ", BOLD, self.term_color) output: str = " -> "
if message is Exception: if message is Exception:
style("ERROR ", BOLD, RED) output += "ERROR "
print(message) return output + str(message)
class Document(SpipWritable, SpipDocuments): class Document(SpipWritable, SpipDocuments):
@ -77,8 +72,6 @@ class Document(SpipWritable, SpipDocuments):
self.titre: str = convert(self.titre, True) self.titre: str = convert(self.titre, True)
self.descriptif: str = convert(self.descriptif, True) self.descriptif: str = convert(self.descriptif, True)
self.statut: str = "false" if self.statut == "publie" else "true" self.statut: str = "false" if self.statut == "publie" else "true"
# Terminal output color
self.term_color: int = BLUE
# Get slugified name of this file # Get slugified name of this file
def filename(self, date: bool = False) -> str: def filename(self, date: bool = False) -> str:
@ -233,8 +226,6 @@ class Article(SpipObject, SpipArticles):
self.accepter_forum: str = "true" if self.accepter_forum == "oui" else "false" self.accepter_forum: str = "true" if self.accepter_forum == "oui" else "false"
# ID # ID
self.object_id = self.id_article self.object_id = self.id_article
# Terminal output color
self.term_color = YELLOW
def frontmatter(self, append: Optional[dict[str, Any]] = None) -> str: def frontmatter(self, append: Optional[dict[str, Any]] = None) -> str:
meta: dict[str, Any] = { meta: dict[str, Any] = {
@ -286,8 +277,6 @@ class Rubrique(SpipObject, SpipRubriques):
self.object_id = self.id_rubrique self.object_id = self.id_rubrique
# File prefix # File prefix
self.prefix = "_index" self.prefix = "_index"
# Terminal output color
self.term_color = GREEN
def frontmatter(self, append: Optional[dict[str, Any]] = None) -> str: def frontmatter(self, append: Optional[dict[str, Any]] = None) -> str:
meta: dict[str, Any] = { meta: dict[str, Any] = {
@ -300,31 +289,37 @@ class Rubrique(SpipObject, SpipRubriques):
else: else:
return super().frontmatter(meta) return super().frontmatter(meta)
def write_tree(self, parent_dir: str, index: int, total: int): def write_tree(
self.begin_message(index, total, int(self.profondeur)) self, parent_dir: str, index: int, total: int
) -> list[str | list[Any]]:
# Define dictionary output to diplay
output: list[str | list[Any]] = []
for m in self.begin_message(index, total):
output.append(m)
# Get this sections articles documents # Get this sections articles documents
articles = self.articles() articles = self.articles()
documents = self.documents() documents = self.documents()
# Write this section # Write this section
self.link_articles() self.link_articles()
export_path: str = self.write(parent_dir) output[-1] += self.end_message(self.write(parent_dir))
self.end_message(export_path)
# Redefine parent_dir for subtree elements # Redefine parent_dir for subtree elements
parent_dir = parent_dir + self.dir_slug() parent_dir = parent_dir + self.dir_slug()
# Write this sections articles and documents # Write this sections articles and documents
def write_loop(objects: ModelSelect): def write_loop(objects: ModelSelect) -> list[str]:
output: list[str] = []
total = len(objects) total = len(objects)
for i, obj in enumerate(objects): for i, obj in enumerate(objects):
obj.begin_message(i, total, self.profondeur + 1) for m in obj.begin_message(i, total):
output.append(m)
try: try:
export_path: str = obj.write(parent_dir) output[-1] += obj.end_message(obj.write(parent_dir))
obj.end_message(export_path)
except Exception as err: except Exception as err:
obj.end_message(err) output[-1] += obj.end_message(err)
return output
write_loop(articles) output.append(write_loop(articles))
write_loop(documents) output.append(write_loop(documents))
# Get all child section of self # Get all child section of self
child_sections = ( child_sections = (
@ -335,4 +330,5 @@ class Rubrique(SpipObject, SpipRubriques):
nb: int = len(child_sections) nb: int = len(child_sections)
# Do the same for subsections (write their entire subtree) # Do the same for subsections (write their entire subtree)
for i, s in enumerate(child_sections): for i, s in enumerate(child_sections):
s.write_tree(parent_dir, i, nb) output.append(s.write_tree(parent_dir, i, nb))
return output