more precise end message

This commit is contained in:
Guilhem Fauré 2023-06-07 14:39:22 +02:00
parent 3ab58288f7
commit 89ab4226b9
2 changed files with 59 additions and 38 deletions

View File

@ -42,7 +42,8 @@ from spip2md.spip_models import (
from spip2md.style import BOLD, CYAN, GREEN, WARNING_STYLE, YELLOW, esc from spip2md.style import BOLD, CYAN, GREEN, WARNING_STYLE, YELLOW, esc
# Define recursive list type # Define recursive list type
RecursiveList = list["str | RecursiveList"] # DeepDict = dict[str, "list[DeepDict] | list[str] | str"]
DeepDict = dict[str, "list[DeepDict] | list[str] | str"]
# Define logger for this files logs # Define logger for this files logs
LOG = logging.getLogger(CFG.logname + ".models") LOG = logging.getLogger(CFG.logname + ".models")
@ -548,9 +549,9 @@ class RedactionalObject(WritableObject):
) )
# Write all the children of this object # Write all the children of this object
def write_documents(self) -> RecursiveList: def write_documents(self) -> list[str]:
LOG.debug(f"Writing children of {type(self).__name__} `{self._title}`") LOG.debug(f"Writing children of {type(self).__name__} `{self._title}`")
output: RecursiveList = [] output: list[str] = []
children = self.documents() children = self.documents()
total = len(children) total = len(children)
for i, obj in enumerate(children): for i, obj in enumerate(children):
@ -622,12 +623,12 @@ class Article(RedactionalObject, NormalizedArticle):
# Perform all the write steps of this object # Perform all the write steps of this object
def write_all( def write_all(
self, forced_lang: str, parentdepth: int, parentdir: str, index: int, total: int self, forced_lang: str, parentdepth: int, parentdir: str, index: int, total: int
) -> RecursiveList: ) -> DeepDict:
self.convert(forced_lang) self.convert(forced_lang)
return [ return {
super().write_all(parentdepth, parentdir, index, total), "msg": super().write_all(parentdepth, parentdir, index, total),
self.write_documents(), "documents": self.write_documents(),
] }
class Section(RedactionalObject, NormalizedSection): class Section(RedactionalObject, NormalizedSection):
@ -666,15 +667,22 @@ class Section(RedactionalObject, NormalizedSection):
) )
# Write all the children of this object # Write all the children of this object
def write_children(self, forcedlang: str) -> RecursiveList: def write_children(self, forcedlang: str) -> DeepDict:
super().write_documents()
LOG.debug(f"Writing children of {type(self).__name__} `{self._title}`") LOG.debug(f"Writing children of {type(self).__name__} `{self._title}`")
output: RecursiveList = [] output: DeepDict = {
for children in (self.articles(), self.sections()): "documents": super().write_documents(),
"articles": [],
"sections": [],
}
for name, children in (
("articles", self.articles()),
("sections", self.sections()),
):
buffer: list[DeepDict] = []
total = len(children) total = len(children)
for i, obj in enumerate(children): for i, obj in enumerate(children):
try: try:
output.append( buffer.append(
obj.write_all( obj.write_all(
forcedlang, self._depth, self.dest_directory(), i, total forcedlang, self._depth, self.dest_directory(), i, total
) )
@ -683,14 +691,14 @@ class Section(RedactionalObject, NormalizedSection):
logging.debug(err) logging.debug(err)
except DontExportDraftError as err: except DontExportDraftError as err:
logging.debug(err) logging.debug(err)
output[name] = buffer
return output return output
# Perform all the write steps of this object # Perform all the write steps of this object
def write_all( def write_all(
self, forced_lang: str, parentdepth: int, parentdir: str, index: int, total: int self, forced_lang: str, parentdepth: int, parentdir: str, index: int, total: int
) -> RecursiveList: ) -> DeepDict:
self.convert(forced_lang) self.convert(forced_lang)
return [ return {
super().write_all(parentdepth, parentdir, index, total), "msg": super().write_all(parentdepth, parentdir, index, total),
self.write_children(forced_lang), } | self.write_children(forced_lang)
]

View File

@ -3,12 +3,13 @@ import logging
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 typing import Optional
from spip2md.config import CFG from spip2md.config import CFG
from spip2md.extended_models import ( from spip2md.extended_models import (
DeepDict,
DontExportDraftError, DontExportDraftError,
LangNotFoundError, LangNotFoundError,
RecursiveList,
Section, Section,
) )
from spip2md.spip_models import DB from spip2md.spip_models import DB
@ -22,7 +23,7 @@ DB.init(CFG.db, host=CFG.db_host, user=CFG.db_user, password=CFG.db_pass)
# Write the root sections and their subtrees # Write the root sections and their subtrees
def write_root(parent_dir: str, parent_id: int = 0) -> RecursiveList: def write_root(parent_dir: str, parent_id: int = 0) -> DeepDict:
# Print starting message # Print starting message
print( print(
f"""\ f"""\
@ -32,7 +33,7 @@ into the directory {esc(BOLD)}{parent_dir}{esc()}, \
as database user {esc(BOLD)}{CFG.db_user}{esc()} as database user {esc(BOLD)}{CFG.db_user}{esc()}
""" """
) )
output: RecursiveList = [] # Define dictionary output output: DeepDict = {"sections": []} # Define dictionary output
# Write each sections (write their entire subtree) for each export language # Write each sections (write their entire subtree) for each export language
# Language specified in database can differ from markup, se we force a language # Language specified in database can differ from markup, se we force a language
# and remove irrelevant ones at each looping # and remove irrelevant ones at each looping
@ -46,46 +47,58 @@ as database user {esc(BOLD)}{CFG.db_user}{esc()}
) )
nb: int = len(child_sections) nb: int = len(child_sections)
for i, s in enumerate(child_sections): for i, s in enumerate(child_sections):
buffer: list[DeepDict] = []
ROOTLOG.debug(f"Begin exporting {lang} root section {i}/{nb}") ROOTLOG.debug(f"Begin exporting {lang} root section {i}/{nb}")
try: try:
output.append(s.write_all(lang, -1, CFG.output_dir, i, nb)) buffer.append(s.write_all(lang, -1, CFG.output_dir, i, nb))
except LangNotFoundError as err: except LangNotFoundError as err:
ROOTLOG.debug(err) # Log the message ROOTLOG.debug(err) # Log the message
except DontExportDraftError as err: # Will happen in not CFG.export_drafts except DontExportDraftError as err: # Will happen in not CFG.export_drafts
ROOTLOG.debug(err) # Log the message ROOTLOG.debug(err) # Log the message
print() # Break line between level 0 sections in output print() # Break line between level 0 sections in output
ROOTLOG.debug(f"Finished exporting {lang} root section {i}/{nb} {s._title}") ROOTLOG.debug(f"Finished exporting {lang} root section {i}/{nb} {s._title}")
output["sections"] = buffer
return output return output
# Count on outputted tree & print results if finished # Count on outputted tree & print results if finished
def summarize( def summarize(
tree: list[str | list[str | list]], tree: DeepDict | list[DeepDict] | list[str],
indent: str = " ",
depth: int = -1, depth: int = -1,
branches: int = 1, prevkey: Optional[str] = None,
leaves: int = 0, counter: Optional[dict[str, int]] = None,
) -> tuple[int, int]: ) -> dict[str, int]:
for sub in tree: if counter is None:
if type(sub) == list: counter = {}
branches, leaves = summarize(sub, indent, depth + 1, branches + 1, leaves) # __import__("pprint").pprint(tree) # DEBUG
elif type(sub) == str: if type(tree) == dict:
leaves += 1 for key, sub in tree.items():
if type(sub) == list:
counter = summarize(sub, depth + 1, key, counter)
# if type of sub is str, its just the name, dont count
if type(tree) == list:
for sub in tree:
if prevkey is not None:
if prevkey not in counter:
counter[prevkey] = 0
counter[prevkey] += 1
if type(sub) == dict:
counter = summarize(sub, depth + 1, None, counter)
# End message only if its the root one # End message only if its the root one
if depth == -1: if depth == -1:
TREELOG.debug(tree) TREELOG.debug(tree)
print( totals: str = ""
f"""\ for key, val in counter.items():
Exported a total of {esc(BOLD)}{leaves}{esc()} files, \ totals += f"{esc(BOLD)}{val}{esc()} {key}, "
stored into {esc(BOLD)}{branches}{esc()} directories""" print(f"Exported a total of {totals[:-2]}")
)
# Warn about issued warnings in log file # Warn about issued warnings in log file
if isfile(CFG.logfile): if isfile(CFG.logfile):
print( print(
f"Logging level was set to {esc(BOLD)}{CFG.loglevel}{esc()}, there are" f"Logging level was set to {esc(BOLD)}{CFG.loglevel}{esc()}, there are"
+ f" warnings and informations in {esc(BOLD)}{CFG.logfile}{esc()}" + f" warnings and informations in {esc(BOLD)}{CFG.logfile}{esc()}"
) )
return (branches, leaves) return counter
# Clear the previous log file if needed, then configure logging # Clear the previous log file if needed, then configure logging