diff --git a/README.md b/README.md index 4fac3b0..6a8f068 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ be used as the title of the HTML page. - [X] write the `gen_gmi` function - [X] make the line "---\n" of the footer only present in the code, not in the footer file -- [-] make a config file for things like +- [X] make a config file for things like - certain variables should be made `CONSTANT` in the `__init__.py` - [X] also update the "/gemlog/index.gmi" with the list of articles - [X] write the sync features @@ -33,8 +33,11 @@ be used as the title of the HTML page. - [ ] write tests for it - [ ] move config file to `~/.config` with placeholder values - [X] check for a config file in `~/.config` -- [ ] maybe save the old Bash script somewhere for posterity ? -- [ ] reorganise the directory where the content is +- [X] maybe save the old Bash script somewhere for posterity ? +- [X] reorganise the directory where the content is +- [X] replace `print`s with `logging` +- [ ] make a call graph +- [ ] rewrite `process_file` to make it easier to test ## My setup diff --git a/pyproject.toml b/pyproject.toml index 48693b5..ebb701f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ requires = ["hatchling"] build-backend = "hatchling.build" [project] -name = "Gmirator" +name = "gmirator" version = "0.0.1" authors = [ { name="Steven GIBONE", email="steven.gibone@zaclys.net" }, diff --git a/src/gmirator/__init__.py b/src/gmirator/__init__.py index d466b4a..16af267 100644 --- a/src/gmirator/__init__.py +++ b/src/gmirator/__init__.py @@ -1,7 +1,10 @@ +import logging from pathlib import Path import json +logging.basicConfig(level=logging.INFO, format='%(message)s') + xdg_path = Path("~/.config/gmirator/config.json").expanduser() cfg_file = xdg_path if xdg_path.exists() else "config.json" with open(cfg_file) as f: diff --git a/src/gmirator/main.py b/src/gmirator/__main__.py similarity index 51% rename from src/gmirator/main.py rename to src/gmirator/__main__.py index 5ea1dcf..878e4f1 100644 --- a/src/gmirator/main.py +++ b/src/gmirator/__main__.py @@ -1,14 +1,14 @@ import typer -import generate -import synchronise +import gmirator.generate +import gmirator.synchronise app = typer.Typer( add_completion=False, help="gmirator helps you publish in both gmi and html" ) -app.add_typer(generate.app, name='gen') -app.add_typer(synchronise.app, name='sync') +app.add_typer(gmirator.generate.app, name='gen') +app.add_typer(gmirator.synchronise.app, name='sync') if __name__ == "__main__": app() diff --git a/src/gmirator/generate.py b/src/gmirator/generate.py index 5697b69..de001c0 100644 --- a/src/gmirator/generate.py +++ b/src/gmirator/generate.py @@ -1,3 +1,4 @@ +import logging import typer import re import shutil @@ -16,13 +17,13 @@ app = typer.Typer(help="generates your gmi and html files") @app.command("gmi") def gen_gmi(): - print("Generating GMI files...") + logging.info(" Generating GMI files...") make_index([DIR_SOURCE / "index.gmi", DIR_SOURCE / "gemlog/index.gmi"]) make_footer(GMI_FOOTER, DIR_SOURCE) - print("Done!") + logging.info(" Done!") def make_index(listing: list[Path]): - print("Populating gemlog index ...") + logging.info(" Populating gemlog index ...") title, date = "", "" files = (DIR_SOURCE / "gemlog").rglob(r'*.gmi') files = [ \ @@ -32,7 +33,7 @@ def make_index(listing: list[Path]): articles = "" for file in files[::-1]: title, date = find_title_date(file) - print(f"\u2713 {date} {title.strip()}") + logging.info(f" \u2713 {date} {title.strip()}") articles += f"=> /gemlog/{file.stem}.gmi {date} - {title}" for path in listing: @@ -50,7 +51,7 @@ def make_footer(footer_path: Path, directory: Path, footer_str: str ="\n---\n"): with open(footer_path) as file: footer = file.read() - print("Adding footers...") + logging.info(" Adding footers...") files = directory.rglob(r'*.gmi') for file in files: with open(file, "rb+") as f: @@ -60,8 +61,8 @@ def make_footer(footer_path: Path, directory: Path, footer_str: str ="\n---\n"): break f.write((footer_str + footer).encode("utf-8")) f.truncate() - print(f"\u2713 {file}") - print("Footers added !") + logging.info(f" \u2713 {file}") + logging.info(" Footers added !") def find_title_date(filename: Path): title, date = "", filename.stem[:10] @@ -77,9 +78,9 @@ def gen_html(): for file in files: gmi2html(file.as_posix()) - print("---\nDone processing gmi files\n") + logging.info("---\nDone processing gmi files") - print("\nCopying static files...\n") + logging.info("Copying static files...\n") shutil.copytree(DIR_ASSETS, DIR_O_HTML, dirs_exist_ok=True) def gmi2html(file: str): @@ -87,7 +88,7 @@ def gmi2html(file: str): new_file_path = Path(new_file_path).with_suffix('.html') new_file_path.parent.mkdir(parents=True, exist_ok=True) - print(f"Processing: {new_file_path}") + logging.info(f"Processing: {new_file_path}") with open(file, 'r') as gemlog: contents, title = process_file(gemlog) @@ -122,7 +123,9 @@ def process_file(ifile: TextIOWrapper): continue else: newline, n = re.subn(r'^```.*', "
", newline) - in_preformated = True if n else False + if n: + in_preformated = True + continue if re.match(r'^[a-zA-Z]', newline): newline = "" + newline.strip() + "
\n" diff --git a/src/gmirator/synchronise.py b/src/gmirator/synchronise.py index 2dbf4ec..b669f36 100644 --- a/src/gmirator/synchronise.py +++ b/src/gmirator/synchronise.py @@ -1,3 +1,5 @@ +from typing import Annotated +import logging import typer import subprocess import shlex @@ -12,18 +14,31 @@ from gmirator import ( app = typer.Typer(help="syncs your files with configured remote") @app.command("gmi") -def sync_gmi(): - print("Synchronising GMI content with remote...") - print(f'rsync -aP -e "ssh -p {RPORT}" {DIR_SOURCE} {REM_GMI}') - subprocess.call( - shlex.split(f'rsync -aP -e "ssh -p {RPORT}" {DIR_SOURCE} {REM_GMI}') - ) - print("Done!") +def sync_gmi( + dry_run: Annotated[ + bool, typer.Option(help="dry runs the synchronisation") + ] = False +): + logging.info(" Start synchronising GMI content with remote...") + sync_with_remote(RPORT, DIR_SOURCE, REM_GMI, dry_run) + logging.info(" Synchronisation of Gemini content with remote : done!") @app.command("html") -def sync_html(): - print("Synchronising HTML content with remote...") +def sync_html( + dry_run: Annotated[ + bool, typer.Option(help="dry runs the synchronisation") + ] = False +): + logging.info(" Start synchronising HTML content with remote...") + sync_with_remote(RPORT, DIR_O_HTML, REM_HTML, dry_run) + logging.info(" Synchronisation of HTML content with remote: done!") + +def sync_with_remote(port: str, source, dest, dry_run: bool): + if dry_run: + rsync_command = f'rsync -aP --dry-run -e "ssh -p {port}" {source} {dest}' + else: + rsync_command = f'rsync -aP -e "ssh -p {port}" {source} {dest}' + logging.info(" " + rsync_command) subprocess.call( - shlex.split(f'rsync -aP -e "ssh -p {RPORT}" {DIR_O_HTML} {REM_HTML}') + shlex.split(rsync_command) ) - print("Done!")