core: use class based code

This commit is contained in:
2025-04-14 19:29:54 +02:00
parent c68f393703
commit 8a123180c5
5 changed files with 163 additions and 87 deletions

View File

@ -1,13 +1,10 @@
import sys
import os
from progress.bar import Bar
from jinja2 import Environment, FileSystemLoader
import subprocess
import time
import markdown
env = Environment(loader=FileSystemLoader('src/templates'))
page_template = env.get_template('page.jinja')
from path import Path
from picture import Picture
from page import Page
def argument_parsing():
if (len(sys.argv) < 2):
@ -15,94 +12,37 @@ def argument_parsing():
exit(1)
return sys.argv[1]
def get_exif(folder: str, raw: str):
absolut_path = os.path.join(folder, "exif.txt")
if os.path.exists(absolut_path):
return absolut_path
if os.system(f"exiftool {raw} > {absolut_path} 2>/dev/null") == 0:
return absolut_path
os.remove(absolut_path)
return None
def get_images(folder: str, files: list[str]):
length: int = len(folder) + 1
images_datas: list[str] = []
images: list[str] = [file for file in files if os.path.isfile(file) and file.endswith(".png")]
for image in images:
export_data_file: str = image + ".out.pp3"
if (os.path.exists(export_data_file) and os.path.isfile(export_data_file)):
images_datas.append((image[length:], export_data_file[length:]))
else:
images_datas.append((image[length:],))
return images_datas
def get_updated_folders(path: str):
updated_folders: list[str] = []
for element_name in os.listdir(path):
absolut_path: str = os.path.join(path, element_name)
if not os.path.isdir(absolut_path):
continue
(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(absolut_path)
folder_modified_date = time.ctime(mtime)
html_path: str = os.path.join(absolut_path, f"{element_name}.html")
if not os.path.exists(html_path):
updated_folders.append(absolut_path)
continue
(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(absolut_path)
html_modified_date = time.ctime(mtime)
if (html_modified_date < folder_modified_date):
updated_folders.append(absolut_path)
continue
return updated_folders
def get_readme(folder):
absolut_path: str = os.path.join(folder, "readme.md")
if (os.path.exists(absolut_path)):
return absolut_path
return None
def get_raw(folder, files: list[str]):
raws: list[str] = [file for file in files if file.endswith(".NEF")]
raw: str = raws[0] if len(raws) > 0 else None
return raw[len(folder) + 1:]
def get_html_readme(readme_path: str):
if readme_path is None:
return None
with open(readme_path, 'r') as f:
text = f.read()
html = markdown.markdown(text)
return html
def gen_pages(folders: list[str]):
with Bar("generating...", max=len(folders)) as bar:
def scan_pages(folders: list[Path]) -> list[Page]:
pages: list[Page] = []
prev: Page = None
with Bar("scaning...", max=len(folders)) as bar:
for folder in folders:
files: list[Path] = folder.get_files()
files: list[str] = [os.path.join(folder, file) for file in os.listdir(folder)]
images: list[str] = get_images(folder, files)
raw: str = get_raw(folder, files)
prev: Page = Page(folder.get_absolute_path(), folder.get_name(), None, None, None)
raw: Path = Path(folder.get_absolute_path(), folder.get_name() + ".NEF")
images: list[Path] = [Picture(file, page=prev, raw=raw) for file in files if file.get_name().endswith(".png")]
prev.images = images
if len(images) == 0:
bar.next()
continue
pages.append(prev)
bar.next()
return pages
exif: str = get_exif(folder, raw)
readme: str = get_readme(folder)
readme_html: str = get_html_readme(readme)
html_rendered = page_template.render(exif=exif, raw=raw, images=images, readme=readme_html)
with open(os.path.join(folder, "page.html"), "w") as f:
f.write(html_rendered)
def create_pages(pages: list[Page]) -> None:
with Bar("generating...", max=len(pages)) as bar:
for page in pages:
page.create()
bar.next()
def main():
site_path: str = argument_parsing()
folders = get_updated_folders(site_path)
gen_pages(folders)
site_path: Path = Path(argument_parsing())
pages: list[Page] = scan_pages(site_path.get_dirs())
create_pages(pages)
if __name__ == "__main__":
main()