Premier commit
This commit is contained in:
92
main.py
Executable file
92
main.py
Executable file
@@ -0,0 +1,92 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import os
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
from blog.page import new_page
|
||||
from blog.config import Config
|
||||
from blog.utils import clean_output
|
||||
from blog.blog import Blog
|
||||
|
||||
# from blog.blog import Blog
|
||||
|
||||
actions_list = ["new", "make", "clean"]
|
||||
blog_dir = "./inbox/"
|
||||
|
||||
|
||||
def load_args():
|
||||
"""Charge l'action et les paramêtres communs a toutes les actions."""
|
||||
parser = argparse.ArgumentParser(description="Gestion du blog")
|
||||
parser.add_argument("action", choices=actions_list, help="L'action a réaliser.")
|
||||
parser.add_argument(
|
||||
"-c",
|
||||
"--config",
|
||||
default=".blog",
|
||||
help="Chemin vers le fichier de configuration",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-i",
|
||||
"--inbox",
|
||||
default="./inbox",
|
||||
help="Chemin vers les fichiers markdown du blog",
|
||||
)
|
||||
parser.add_argument("all", nargs=argparse.REMAINDER)
|
||||
return vars(parser.parse_args())
|
||||
|
||||
|
||||
def load_make_args(args: str) -> dict:
|
||||
"""Charge les paramêtres spécifiques à l'action make"""
|
||||
parser = argparse.ArgumentParser(description="Compile le blog")
|
||||
parser.add_argument(
|
||||
"-t",
|
||||
"--theme",
|
||||
default="./themes/default",
|
||||
help="Chemin vers le theme utilisé pour exporter le blog",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-o",
|
||||
"--output",
|
||||
default="./output",
|
||||
help="Nom du dossier ou sera exporté le blog en html",
|
||||
)
|
||||
return vars(parser.parse_args(args))
|
||||
|
||||
|
||||
def main():
|
||||
"""Fonction principale"""
|
||||
args = load_args()
|
||||
|
||||
if not Path(args["config"]).is_file():
|
||||
# TODO gérer le cas où le fichier n'existe pas. Le créer ? Prendre les valeurs par défaut ?
|
||||
print("Le fichier de configuration n'existe pas.")
|
||||
os._exit(1)
|
||||
|
||||
conf = Config(args["config"])
|
||||
conf.overload(args)
|
||||
|
||||
match args["action"]:
|
||||
case "new":
|
||||
page_title = " ".join(args["all"])
|
||||
new_page(page_title)
|
||||
os._exit(0)
|
||||
|
||||
case "make":
|
||||
args = load_make_args(args["all"])
|
||||
conf.overload(args)
|
||||
|
||||
blog = Blog(conf)
|
||||
blog.make()
|
||||
|
||||
os._exit(0)
|
||||
|
||||
case "clean":
|
||||
clean_output(conf.inbox)
|
||||
os._exit(0)
|
||||
|
||||
case _:
|
||||
print("error")
|
||||
os._exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user