add logging
This commit is contained in:
parent
7fe3e6d434
commit
bbfb536b23
2 changed files with 17 additions and 11 deletions
|
@ -1,7 +1,10 @@
|
||||||
|
import logging
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO, format='%(message)s')
|
||||||
|
|
||||||
xdg_path = Path("~/.config/gmirator/config.json").expanduser()
|
xdg_path = Path("~/.config/gmirator/config.json").expanduser()
|
||||||
cfg_file = xdg_path if xdg_path.exists() else "config.json"
|
cfg_file = xdg_path if xdg_path.exists() else "config.json"
|
||||||
with open(cfg_file) as f:
|
with open(cfg_file) as f:
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import logging
|
||||||
import typer
|
import typer
|
||||||
import re
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
|
@ -16,13 +17,13 @@ app = typer.Typer(help="generates your gmi and html files")
|
||||||
|
|
||||||
@app.command("gmi")
|
@app.command("gmi")
|
||||||
def gen_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_index([DIR_SOURCE / "index.gmi", DIR_SOURCE / "gemlog/index.gmi"])
|
||||||
make_footer(GMI_FOOTER, DIR_SOURCE)
|
make_footer(GMI_FOOTER, DIR_SOURCE)
|
||||||
print("Done!")
|
logging.info(" Done!")
|
||||||
|
|
||||||
def make_index(listing: list[Path]):
|
def make_index(listing: list[Path]):
|
||||||
print("Populating gemlog index ...")
|
logging.info(" Populating gemlog index ...")
|
||||||
title, date = "", ""
|
title, date = "", ""
|
||||||
files = (DIR_SOURCE / "gemlog").rglob(r'*.gmi')
|
files = (DIR_SOURCE / "gemlog").rglob(r'*.gmi')
|
||||||
files = [ \
|
files = [ \
|
||||||
|
@ -32,7 +33,7 @@ def make_index(listing: list[Path]):
|
||||||
articles = ""
|
articles = ""
|
||||||
for file in files[::-1]:
|
for file in files[::-1]:
|
||||||
title, date = find_title_date(file)
|
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}"
|
articles += f"=> /gemlog/{file.stem}.gmi {date} - {title}"
|
||||||
|
|
||||||
for path in listing:
|
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:
|
with open(footer_path) as file:
|
||||||
footer = file.read()
|
footer = file.read()
|
||||||
|
|
||||||
print("Adding footers...")
|
logging.info(" Adding footers...")
|
||||||
files = directory.rglob(r'*.gmi')
|
files = directory.rglob(r'*.gmi')
|
||||||
for file in files:
|
for file in files:
|
||||||
with open(file, "rb+") as f:
|
with open(file, "rb+") as f:
|
||||||
|
@ -60,8 +61,8 @@ def make_footer(footer_path: Path, directory: Path, footer_str: str ="\n---\n"):
|
||||||
break
|
break
|
||||||
f.write((footer_str + footer).encode("utf-8"))
|
f.write((footer_str + footer).encode("utf-8"))
|
||||||
f.truncate()
|
f.truncate()
|
||||||
print(f"\u2713 {file}")
|
logging.info(f" \u2713 {file}")
|
||||||
print("Footers added !")
|
logging.info(" Footers added !")
|
||||||
|
|
||||||
def find_title_date(filename: Path):
|
def find_title_date(filename: Path):
|
||||||
title, date = "", filename.stem[:10]
|
title, date = "", filename.stem[:10]
|
||||||
|
@ -77,9 +78,9 @@ def gen_html():
|
||||||
|
|
||||||
for file in files:
|
for file in files:
|
||||||
gmi2html(file.as_posix())
|
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)
|
shutil.copytree(DIR_ASSETS, DIR_O_HTML, dirs_exist_ok=True)
|
||||||
|
|
||||||
def gmi2html(file: str):
|
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 = Path(new_file_path).with_suffix('.html')
|
||||||
new_file_path.parent.mkdir(parents=True, exist_ok=True)
|
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:
|
with open(file, 'r') as gemlog:
|
||||||
contents, title = process_file(gemlog)
|
contents, title = process_file(gemlog)
|
||||||
|
@ -122,7 +123,9 @@ def process_file(ifile: TextIOWrapper):
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
newline, n = re.subn(r'^```.*', "<pre>", newline)
|
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):
|
if re.match(r'^[a-zA-Z]', newline):
|
||||||
newline = "<p>" + newline.strip() + "</p>\n"
|
newline = "<p>" + newline.strip() + "</p>\n"
|
||||||
|
|
Loading…
Reference in a new issue