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

View File

@ -3,12 +3,13 @@ import logging
from os import makedirs, remove
from os.path import isfile
from shutil import rmtree
from typing import Optional
from spip2md.config import CFG
from spip2md.extended_models import (
DeepDict,
DontExportDraftError,
LangNotFoundError,
RecursiveList,
Section,
)
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
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(
f"""\
@ -32,7 +33,7 @@ into the directory {esc(BOLD)}{parent_dir}{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
# Language specified in database can differ from markup, se we force a language
# 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)
for i, s in enumerate(child_sections):
buffer: list[DeepDict] = []
ROOTLOG.debug(f"Begin exporting {lang} root section {i}/{nb}")
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:
ROOTLOG.debug(err) # Log the message
except DontExportDraftError as err: # Will happen in not CFG.export_drafts
ROOTLOG.debug(err) # Log the message
print() # Break line between level 0 sections in output
ROOTLOG.debug(f"Finished exporting {lang} root section {i}/{nb} {s._title}")
output["sections"] = buffer
return output
# Count on outputted tree & print results if finished
def summarize(
tree: list[str | list[str | list]],
indent: str = " ",
tree: DeepDict | list[DeepDict] | list[str],
depth: int = -1,
branches: int = 1,
leaves: int = 0,
) -> tuple[int, int]:
for sub in tree:
if type(sub) == list:
branches, leaves = summarize(sub, indent, depth + 1, branches + 1, leaves)
elif type(sub) == str:
leaves += 1
prevkey: Optional[str] = None,
counter: Optional[dict[str, int]] = None,
) -> dict[str, int]:
if counter is None:
counter = {}
# __import__("pprint").pprint(tree) # DEBUG
if type(tree) == dict:
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
if depth == -1:
TREELOG.debug(tree)
print(
f"""\
Exported a total of {esc(BOLD)}{leaves}{esc()} files, \
stored into {esc(BOLD)}{branches}{esc()} directories"""
)
totals: str = ""
for key, val in counter.items():
totals += f"{esc(BOLD)}{val}{esc()} {key}, "
print(f"Exported a total of {totals[:-2]}")
# Warn about issued warnings in log file
if isfile(CFG.logfile):
print(
f"Logging level was set to {esc(BOLD)}{CFG.loglevel}{esc()}, there are"
+ 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