init foundations

This commit is contained in:
Guilhem Fauré 2023-06-22 14:11:03 +02:00
parent 80c26fb467
commit 23d8702dbe
4 changed files with 171 additions and 1017 deletions

View File

@ -56,15 +56,21 @@ def esc(*args: int) -> str:
# Extend Site class to add terminal output capabilities
class PrintableSite(WritableSite):
def write_all(self) -> None:
pass
def write(self) -> str:
return "write path"
# Initialize DB database connection from config
def init_db(cfg: Configuration):
DB.init( # type: ignore
cfg.db, host=cfg.db_host, user=cfg.db_user, password=cfg.db_pass
)
def main(*argv: str):
cfg = Configuration(*argv) # Get the configuration
# Initialize the database with settings from CFG
DB.init(cfg.db, host=cfg.db_host, user=cfg.db_user, password=cfg.db_pass)
init_db(cfg)
# Eventually remove already existing output dir
if cfg.clear_output:
@ -73,40 +79,4 @@ def main(*argv: str):
with DB: # Connect to the database where SPIP site is stored in this block
# Write everything while printing the output human-readably
PrintableSite(cfg).write_all()
# def summarize(
# tree: dict[Any, Any] | list[Any],
# depth: int = -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:
# LOG.debug(tree)
# 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(LOGFILE):
# print(f"Check out warnings and infos in {esc(BOLD)}{LOGFILE}{esc()}")
# return counter
PrintableSite(cfg).write()

View File

@ -23,7 +23,7 @@ from yaml import Loader, load
# Global configuration object
class Configuration:
config_file: Optional[str] = None # Location of the config file
# config_file: Optional[str] = None # Location of the config file
name: str = "spip2md" # Name of program, notably used in logs
@ -80,7 +80,7 @@ class Configuration:
# Return the first path that actually exists
for path in config_locations:
if isfile(path):
self.config_file = path
# self.config_file = path
return path
# If not found, raise error
raise FileNotFoundError
@ -88,7 +88,9 @@ class Configuration:
def __init__(self, *argv: str):
try:
# Read config from config file
with open(self._find_config_file(*argv)) as f:
with open(self._find_config_file(*argv[1:])) as f:
# Tell user about config
print(f"Read configuration file from {f.name}")
config = load(f.read(), Loader=Loader)
# Assign configuration for each attribute in config file
for attr in config:
@ -100,7 +102,5 @@ class Configuration:
setattr(self, attr, directory)
else:
setattr(self, attr, config[attr])
# Tell user about config
print(f"Successfully read configuration file from {self.config_file}")
except FileNotFoundError:
print("No configuration file found, using defaults")

File diff suppressed because it is too large Load Diff

View File

@ -20,46 +20,5 @@ from spip2md.convert import ConvertableSite
class WritableSite(ConvertableSite):
def write(self):
pass
# # Write the root sections and their subtrees
# def write_root(parent_dir: str, parent_id: int = 0) -> DeepDict:
# # Print starting message
# print(
# f"""\
# Begin exporting {esc(BOLD)}{CFG.db}@{CFG.db_host}{esc()} SPIP database to plain \
# Markdown+YAML files,
# into the directory {esc(BOLD)}{parent_dir}{esc()}, \
# as database user {esc(BOLD)}{CFG.db_user}{esc()}
# """
# )
# buffer: list[DeepDict] = [] # Define temporary storage for 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
# for lang in CFG.export_languages:
# ROOTLOG.debug("Initialize root sections")
# # Get all sections of parentID ROOTID
# child_sections: tuple[Section, ...] = (
# Section.select()
# .where(Section.id_parent == parent_id)
# .order_by(Section.date.desc())
# )
# nb: int = len(child_sections)
# for i, s in enumerate(child_sections):
# ROOTLOG.debug(f"Begin exporting {lang} root section {i}/{nb}")
# try:
# buffer.append(s.write_all(-1, CFG.output_dir, i, nb, lang))
# except LangNotFoundError as err:
# ROOTLOG.debug(err) # Log the message
# except DontExportDraftError as err: # If not CFG.export_drafts
# ROOTLOG.debug(err) # Log the message
# except IgnoredPatternError as err:
# 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._url_title}"
# )
# return {"sections": buffer}
def write(self) -> str:
return "write path"