init foundations
This commit is contained in:
parent
80c26fb467
commit
23d8702dbe
@ -56,15 +56,21 @@ def esc(*args: int) -> str:
|
|||||||
|
|
||||||
# Extend Site class to add terminal output capabilities
|
# Extend Site class to add terminal output capabilities
|
||||||
class PrintableSite(WritableSite):
|
class PrintableSite(WritableSite):
|
||||||
def write_all(self) -> None:
|
def write(self) -> str:
|
||||||
pass
|
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):
|
def main(*argv: str):
|
||||||
cfg = Configuration(*argv) # Get the configuration
|
cfg = Configuration(*argv) # Get the configuration
|
||||||
|
|
||||||
# Initialize the database with settings from CFG
|
init_db(cfg)
|
||||||
DB.init(cfg.db, host=cfg.db_host, user=cfg.db_user, password=cfg.db_pass)
|
|
||||||
|
|
||||||
# Eventually remove already existing output dir
|
# Eventually remove already existing output dir
|
||||||
if cfg.clear_output:
|
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
|
with DB: # Connect to the database where SPIP site is stored in this block
|
||||||
# Write everything while printing the output human-readably
|
# Write everything while printing the output human-readably
|
||||||
PrintableSite(cfg).write_all()
|
PrintableSite(cfg).write()
|
||||||
|
|
||||||
|
|
||||||
# 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, it’s just the name, don’t 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 it’s 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
|
|
||||||
|
@ -23,7 +23,7 @@ from yaml import Loader, load
|
|||||||
|
|
||||||
# Global configuration object
|
# Global configuration object
|
||||||
class Configuration:
|
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
|
name: str = "spip2md" # Name of program, notably used in logs
|
||||||
|
|
||||||
@ -80,7 +80,7 @@ class Configuration:
|
|||||||
# Return the first path that actually exists
|
# Return the first path that actually exists
|
||||||
for path in config_locations:
|
for path in config_locations:
|
||||||
if isfile(path):
|
if isfile(path):
|
||||||
self.config_file = path
|
# self.config_file = path
|
||||||
return path
|
return path
|
||||||
# If not found, raise error
|
# If not found, raise error
|
||||||
raise FileNotFoundError
|
raise FileNotFoundError
|
||||||
@ -88,7 +88,9 @@ class Configuration:
|
|||||||
def __init__(self, *argv: str):
|
def __init__(self, *argv: str):
|
||||||
try:
|
try:
|
||||||
# Read config from config file
|
# 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)
|
config = load(f.read(), Loader=Loader)
|
||||||
# Assign configuration for each attribute in config file
|
# Assign configuration for each attribute in config file
|
||||||
for attr in config:
|
for attr in config:
|
||||||
@ -100,7 +102,5 @@ class Configuration:
|
|||||||
setattr(self, attr, directory)
|
setattr(self, attr, directory)
|
||||||
else:
|
else:
|
||||||
setattr(self, attr, config[attr])
|
setattr(self, attr, config[attr])
|
||||||
# Tell user about config
|
|
||||||
print(f"Successfully read configuration file from {self.config_file}")
|
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print("No configuration file found, using defaults")
|
print("No configuration file found, using defaults")
|
||||||
|
1081
spip2md/convert.py
1081
spip2md/convert.py
File diff suppressed because it is too large
Load Diff
@ -20,46 +20,5 @@ from spip2md.convert import ConvertableSite
|
|||||||
|
|
||||||
|
|
||||||
class WritableSite(ConvertableSite):
|
class WritableSite(ConvertableSite):
|
||||||
def write(self):
|
def write(self) -> str:
|
||||||
pass
|
return "write path"
|
||||||
|
|
||||||
|
|
||||||
# # 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}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user