diff --git a/pyproject.toml b/pyproject.toml index 13197d8..7531de4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,7 @@ pymysql = "^1.0.3" peewee = "^3.16.2" [tool.poetry.scripts] -spip2md = "spip2md:main" +spip2md = "spip2md.cli:main" [build-system] requires = ["poetry-core"] diff --git a/spip2md/__init__.py b/spip2md/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/spip2md/__main__.py b/spip2md/__main__.py new file mode 100644 index 0000000..882c03b --- /dev/null +++ b/spip2md/__main__.py @@ -0,0 +1,4 @@ +# SPIP website to plain Markdown files converter, Copyright (C) 2023 Guilhem Fauré +from spip2md.cli import main + +main() diff --git a/spip2md/main.py b/spip2md/cli.py old mode 100755 new mode 100644 similarity index 82% rename from spip2md/main.py rename to spip2md/cli.py index f7a1394..7f41e8b --- a/spip2md/main.py +++ b/spip2md/cli.py @@ -1,18 +1,19 @@ # SPIP website to plain Markdown files converter, Copyright (C) 2023 Guilhem Fauré -#!python +# Top level functions +import sys from os import makedirs from shutil import rmtree -from sys import argv -from config import CFG -from converters import unknown_chars, unknown_chars_context -from database import DB from peewee import ModelSelect -from spipobjects import ( + +from spip2md.config import CFG +from spip2md.converters import unknown_chars, unknown_chars_context +from spip2md.database import DB +from spip2md.spipobjects import ( Article, Rubrique, ) -from styling import highlight, style +from spip2md.styling import highlight, style # Query the DB to retrieve all sections without parent, sorted by publication date @@ -57,17 +58,19 @@ DB.connect() # Main loop to execute only if script is directly executed -if __name__ == "__main__": +def main(*argv): + if len(argv) == 0: + argv = sys.argv # Define max nb of articles to export based on first CLI argument if len(argv) >= 2: - max_articles_export = int(argv[1]) + articles_export = int(argv[1]) else: - max_articles_export = CFG.max_articles_export + articles_export = CFG.max_articles_export # Define max nb of sections to export based on second CLI argument if len(argv) >= 3: - max_sections_export = int(argv[2]) + sections_export = int(argv[2]) else: - max_sections_export = CFG.max_sections_export + sections_export = CFG.max_sections_export # Clear the output dir & create a new if CFG.clear_output: @@ -75,7 +78,7 @@ if __name__ == "__main__": makedirs(CFG.output_dir, exist_ok=True) # Get the first max_sections_export root sections - sections: ModelSelect = root_sections(max_sections_export) + sections: ModelSelect = root_sections(sections_export) total: int = len(sections) # Write each root sections with its subtree diff --git a/spip2md/spipobjects.py b/spip2md/spipobjects.py index 428d1b7..c6399ba 100644 --- a/spip2md/spipobjects.py +++ b/spip2md/spipobjects.py @@ -5,9 +5,13 @@ from re import finditer from shutil import copyfile from typing import Any, Optional -from config import CFG -from converters import convert, link_document, unknown_chars -from database import ( +from peewee import BigAutoField, DateTimeField, ModelSelect +from slugify import slugify +from yaml import dump + +from spip2md.config import CFG +from spip2md.converters import convert, link_document, unknown_chars +from spip2md.database import ( SpipArticles, SpipAuteurs, SpipAuteursLiens, @@ -15,10 +19,7 @@ from database import ( SpipDocumentsLiens, SpipRubriques, ) -from peewee import BigAutoField, DateTimeField, ModelSelect -from slugify import slugify -from styling import BLUE, BOLD, GREEN, RED, YELLOW, highlight, indent, ss, style -from yaml import dump +from spip2md.styling import BLUE, BOLD, GREEN, RED, YELLOW, highlight, indent, ss, style class SpipWritable: