organize module to make it executable

This commit is contained in:
Guilhem Fauré 2023-05-26 10:35:28 +02:00
parent f9e5e15c4a
commit 8e8fd4aaf8
5 changed files with 29 additions and 21 deletions

View File

@ -29,7 +29,7 @@ pymysql = "^1.0.3"
peewee = "^3.16.2" peewee = "^3.16.2"
[tool.poetry.scripts] [tool.poetry.scripts]
spip2md = "spip2md:main" spip2md = "spip2md.cli:main"
[build-system] [build-system]
requires = ["poetry-core"] requires = ["poetry-core"]

View File

4
spip2md/__main__.py Normal file
View File

@ -0,0 +1,4 @@
# SPIP website to plain Markdown files converter, Copyright (C) 2023 Guilhem Fauré
from spip2md.cli import main
main()

29
spip2md/main.py → spip2md/cli.py Executable file → Normal file
View File

@ -1,18 +1,19 @@
# SPIP website to plain Markdown files converter, Copyright (C) 2023 Guilhem Fauré # SPIP website to plain Markdown files converter, Copyright (C) 2023 Guilhem Fauré
#!python # Top level functions
import sys
from os import makedirs from os import makedirs
from shutil import rmtree 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 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, Article,
Rubrique, 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 # 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 # 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 # Define max nb of articles to export based on first CLI argument
if len(argv) >= 2: if len(argv) >= 2:
max_articles_export = int(argv[1]) articles_export = int(argv[1])
else: 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 # Define max nb of sections to export based on second CLI argument
if len(argv) >= 3: if len(argv) >= 3:
max_sections_export = int(argv[2]) sections_export = int(argv[2])
else: else:
max_sections_export = CFG.max_sections_export sections_export = CFG.max_sections_export
# Clear the output dir & create a new # Clear the output dir & create a new
if CFG.clear_output: if CFG.clear_output:
@ -75,7 +78,7 @@ if __name__ == "__main__":
makedirs(CFG.output_dir, exist_ok=True) makedirs(CFG.output_dir, exist_ok=True)
# Get the first max_sections_export root sections # 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) total: int = len(sections)
# Write each root sections with its subtree # Write each root sections with its subtree

View File

@ -5,9 +5,13 @@ from re import finditer
from shutil import copyfile from shutil import copyfile
from typing import Any, Optional from typing import Any, Optional
from config import CFG from peewee import BigAutoField, DateTimeField, ModelSelect
from converters import convert, link_document, unknown_chars from slugify import slugify
from database import ( from yaml import dump
from spip2md.config import CFG
from spip2md.converters import convert, link_document, unknown_chars
from spip2md.database import (
SpipArticles, SpipArticles,
SpipAuteurs, SpipAuteurs,
SpipAuteursLiens, SpipAuteursLiens,
@ -15,10 +19,7 @@ from database import (
SpipDocumentsLiens, SpipDocumentsLiens,
SpipRubriques, SpipRubriques,
) )
from peewee import BigAutoField, DateTimeField, ModelSelect from spip2md.styling import BLUE, BOLD, GREEN, RED, YELLOW, highlight, indent, ss, style
from slugify import slugify
from styling import BLUE, BOLD, GREEN, RED, YELLOW, highlight, indent, ss, style
from yaml import dump
class SpipWritable: class SpipWritable: