From 97f05cee5e312c2230d1dacf52d90425c53f458d Mon Sep 17 00:00:00 2001 From: starnakin Date: Tue, 15 Apr 2025 18:06:05 +0200 Subject: [PATCH] add: home page --- src/album.py | 32 +++++++++++++++++++++++++++ src/config.py | 3 +++ src/main.py | 46 +++++++++++++++++++++++++++++++++------ src/page.py | 3 +-- src/path.py | 9 ++++++++ src/picture.py | 13 ++++++++--- src/templates/album.css | 3 +++ src/templates/album.jinja | 19 ++++++++++++++++ 8 files changed, 116 insertions(+), 12 deletions(-) create mode 100644 src/album.py create mode 100644 src/config.py create mode 100644 src/templates/album.css create mode 100644 src/templates/album.jinja diff --git a/src/album.py b/src/album.py new file mode 100644 index 0000000..515328a --- /dev/null +++ b/src/album.py @@ -0,0 +1,32 @@ +from __future__ import annotations + +from jinja2 import Environment, FileSystemLoader + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from picture import Picture + +from path import Path + +env = Environment(loader=FileSystemLoader('src/templates')) +album_template = env.get_template('album.jinja') + +class Album(): + + def __init__(self, name: str, albums_path: Path, pictures: list[Picture] = None, is_repertoried: bool = False): + self._name: str = name + self._pictures: list[Picture] = pictures or [] + self._is_repertoried: bool = is_repertoried + self._path: Path = Path(albums_path, f"{name}.html") + + def add_picture(self, picture: Picture) -> None: + self._pictures.append(picture) + + def _to_html(self) -> str|None: + html_rendered = album_template.render(album=self) + return html_rendered + + def create(self) -> Path: + with open(self._path.get_absolute_path(), "w") as f: + f.write(self._to_html()) \ No newline at end of file diff --git a/src/config.py b/src/config.py new file mode 100644 index 0000000..2f2786b --- /dev/null +++ b/src/config.py @@ -0,0 +1,3 @@ + + +CREATE_GENERAL_ALBUM: bool = True \ No newline at end of file diff --git a/src/main.py b/src/main.py index 6bb2be4..0314d4d 100644 --- a/src/main.py +++ b/src/main.py @@ -1,10 +1,13 @@ import sys import shutil +import os from progress.bar import Bar from path import Path from picture import Picture from page import Page +from album import Album +import config def argument_parsing(): if (len(sys.argv) < 2): @@ -15,18 +18,18 @@ def argument_parsing(): def scan_pages(folders: list[Path]) -> list[Page]: pages: list[Page] = [] prev: Page = None - with Bar("scaning...", max=len(folders)) as bar: + with Bar("Scanning Pages...", max=len(folders)) as bar: for folder in folders: files: list[Path] = folder.get_files() - page: Page = Page(folder.get_absolute_path(), folder.get_name(), prev=prev) + page: Page = Page(folder, folder.get_name(), prev=prev) - raw: Path = Path(folder.get_absolute_path(), folder.get_name() + ".NEF") + raw: Path = Path(folder, folder.get_name() + ".NEF") for file in files: if file.get_name().endswith(".png"): page.add_picture(Picture(file, page=prev, raw=raw)) - if len(page.get_picture()) == 0: + if len(page.get_pictures()) == 0: bar.next() continue @@ -41,16 +44,45 @@ def scan_pages(folders: list[Path]) -> list[Page]: return pages def create_pages(pages: list[Page]) -> None: - with Bar("generating...", max=len(pages)) as bar: + with Bar("Generating Pages...", max=len(pages)) as bar: for page in pages: page.create() bar.next() +def scan_albums(pages: list[Page], albums_path: Path) -> list[Album]: + albums: dict[str, Album] = {} + with Bar("Scanning pages...", max=len(pages)) as bar: + for page in pages: + for picture in page.get_pictures(): + for album_name in picture.get_albums_name(): + album: Album | None = albums.get(album_name) + if (album is None): + album = Album(album_name, albums_path) + albums.update({album_name: album}) + album.add_picture(picture) + bar.next() + return (albums.values()) + +def create_albums(albums: list[Album]) -> None: + with Bar("Generating albums...", max=len(albums)) as bar: + for album in albums: + album.create() + bar.next() + + def main(): - site_path: Path = Path(argument_parsing()) + site_path = Path(argument_parsing()) pages: list[Page] = scan_pages(site_path.get_dirs()) shutil.copy2("./src/templates/page.css", site_path.get_absolute_path()) create_pages(pages) + album_path: Path = Path(site_path, "albums") + if (not album_path.exist()): + album_path.create() + albums: list[Album] = scan_albums(pages, album_path) + create_albums(albums) + if config.CREATE_GENERAL_ALBUM: + shutil.move(os.path.join(album_path.get_absolute_path(), "general.html"), os.path.join(site_path.get_absolute_path(), "index.html")) + shutil.copy2("./src/templates/album.css", site_path.get_absolute_path()) if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/src/page.py b/src/page.py index e56de93..e7c7e69 100644 --- a/src/page.py +++ b/src/page.py @@ -20,7 +20,7 @@ class Page(): def __init__(self, path: Path, name: str, pictures: list[Picture] = None, prev: Page|None = None, next: Page|None = None): self.name: str = name self._path: Path = path - self._pictures: list[Picture] = [] if pictures is None else pictures + self._pictures: list[Picture] = pictures or [] self._exif: Path = Path(self._path, "exif.txt") self._readme: Path = Path(self._path, "readme.md") self._raw: Path = Path(self._path, name + ".NEF") @@ -56,7 +56,6 @@ class Page(): def create(self) -> Path: with open(self.html.get_absolute_path(), "w") as f: f.write(self._to_html()) - self.created = True def _render_readme(self) -> str|None: if not self._readme.exist(): diff --git a/src/path.py b/src/path.py index 20437e0..a6bb560 100644 --- a/src/path.py +++ b/src/path.py @@ -1,6 +1,7 @@ from __future__ import annotations import os +import sys class Path(): @@ -8,6 +9,7 @@ class Path(): self._absolute_path: str = "" for path in paths: self._absolute_path = os.path.join(self._absolute_path, path._absolute_path if isinstance(path, Path) else path) + self._absolute_path = os.path.abspath(self._absolute_path) self._name: str = os.path.basename(self._absolute_path) self._dirpath: str = os.path.dirname(self._absolute_path) @@ -17,6 +19,10 @@ class Path(): def get_dirpath(self): return self._dirpath + def get_site_path(self): + site_path: Path = Path(sys.argv[1]) + return self._absolute_path[len(site_path._absolute_path):] + def get_absolute_path(self): return self._absolute_path @@ -25,6 +31,9 @@ class Path(): return None return self._name + def create(self) -> None: + os.makedirs(self.get_absolute_path()) + def exist(self) -> bool: return os.path.exists(self.get_absolute_path()) diff --git a/src/picture.py b/src/picture.py index feb788a..d9aa056 100644 --- a/src/picture.py +++ b/src/picture.py @@ -7,20 +7,27 @@ from path import Path from typing import TYPE_CHECKING +import config + if TYPE_CHECKING: from page import Page from album import Album class Picture(): - def __init__(self, picture_path: Path, page = None, raw: Path|None = None, albums: list[Album] = [], is_repertoried: bool = True): + def __init__(self, picture_path: Path, page = None, raw: Path|None = None, albums_name: list[str] = None, is_repertoried: bool = True): self._large: Path = picture_path self._small: Path = Path(picture_path.get_absolute_path()[:-4] + "_small.jpg") self._export_file: Path = Path(picture_path.get_absolute_path() + ".out.pp3") self._raw: Path|None = raw self._page: Page = page - self._albums: list[Album] = albums + self._albums_name: list[str] = albums_name or [] + if (config.CREATE_GENERAL_ALBUM): + self._albums_name.append("general") self._is_reperoried: bool = is_repertoried + def get_albums_name(self): + return self._albums_name + def get_small(self): if not self._small.exist(): self.gen_small() @@ -33,7 +40,7 @@ class Picture(): return self._export_file def gen_small(self): - im = Image.open(self.large.get_absolute_path()).convert("RGB") + im = Image.open(self._large.get_absolute_path()).convert("RGB") im.save(self._small.get_absolute_path(), quality=95, optimize=True) \ No newline at end of file diff --git a/src/templates/album.css b/src/templates/album.css new file mode 100644 index 0000000..438954d --- /dev/null +++ b/src/templates/album.css @@ -0,0 +1,3 @@ +body { + background-color: red; +} \ No newline at end of file diff --git a/src/templates/album.jinja b/src/templates/album.jinja new file mode 100644 index 0000000..9289948 --- /dev/null +++ b/src/templates/album.jinja @@ -0,0 +1,19 @@ + + + + + + + + + {{album.name}} + caaaa +

{{ album._name }}

+
+ {% for picture in album._pictures %} + + {% endfor %} +
+ + + \ No newline at end of file