add logging

This commit is contained in:
stev 2023-09-11 22:38:50 +02:00
parent 7fe3e6d434
commit bbfb536b23
2 changed files with 17 additions and 11 deletions

View file

@ -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:

View file

@ -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'^```.*', "<pre>", newline)
in_preformated = True if n else False
if n:
in_preformated = True
continue
if re.match(r'^[a-zA-Z]', newline):
newline = "<p>" + newline.strip() + "</p>\n"