add: home page

This commit is contained in:
starnakin 2025-04-15 18:06:05 +02:00
parent d911dfe1cf
commit 97f05cee5e
8 changed files with 116 additions and 12 deletions

32
src/album.py Normal file
View File

@ -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())

3
src/config.py Normal file
View File

@ -0,0 +1,3 @@
CREATE_GENERAL_ALBUM: bool = True

View File

@ -1,10 +1,13 @@
import sys import sys
import shutil import shutil
import os
from progress.bar import Bar from progress.bar import Bar
from path import Path from path import Path
from picture import Picture from picture import Picture
from page import Page from page import Page
from album import Album
import config
def argument_parsing(): def argument_parsing():
if (len(sys.argv) < 2): if (len(sys.argv) < 2):
@ -15,18 +18,18 @@ def argument_parsing():
def scan_pages(folders: list[Path]) -> list[Page]: def scan_pages(folders: list[Path]) -> list[Page]:
pages: list[Page] = [] pages: list[Page] = []
prev: Page = None prev: Page = None
with Bar("scaning...", max=len(folders)) as bar: with Bar("Scanning Pages...", max=len(folders)) as bar:
for folder in folders: for folder in folders:
files: list[Path] = folder.get_files() 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: for file in files:
if file.get_name().endswith(".png"): if file.get_name().endswith(".png"):
page.add_picture(Picture(file, page=prev, raw=raw)) page.add_picture(Picture(file, page=prev, raw=raw))
if len(page.get_picture()) == 0: if len(page.get_pictures()) == 0:
bar.next() bar.next()
continue continue
@ -41,16 +44,45 @@ def scan_pages(folders: list[Path]) -> list[Page]:
return pages return pages
def create_pages(pages: list[Page]) -> None: 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: for page in pages:
page.create() page.create()
bar.next() 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(): def main():
site_path: Path = Path(argument_parsing()) site_path = Path(argument_parsing())
pages: list[Page] = scan_pages(site_path.get_dirs()) pages: list[Page] = scan_pages(site_path.get_dirs())
shutil.copy2("./src/templates/page.css", site_path.get_absolute_path()) shutil.copy2("./src/templates/page.css", site_path.get_absolute_path())
create_pages(pages) 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__": if __name__ == "__main__":
main() main()

View File

@ -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): def __init__(self, path: Path, name: str, pictures: list[Picture] = None, prev: Page|None = None, next: Page|None = None):
self.name: str = name self.name: str = name
self._path: Path = path 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._exif: Path = Path(self._path, "exif.txt")
self._readme: Path = Path(self._path, "readme.md") self._readme: Path = Path(self._path, "readme.md")
self._raw: Path = Path(self._path, name + ".NEF") self._raw: Path = Path(self._path, name + ".NEF")
@ -56,7 +56,6 @@ class Page():
def create(self) -> Path: def create(self) -> Path:
with open(self.html.get_absolute_path(), "w") as f: with open(self.html.get_absolute_path(), "w") as f:
f.write(self._to_html()) f.write(self._to_html())
self.created = True
def _render_readme(self) -> str|None: def _render_readme(self) -> str|None:
if not self._readme.exist(): if not self._readme.exist():

View File

@ -1,6 +1,7 @@
from __future__ import annotations from __future__ import annotations
import os import os
import sys
class Path(): class Path():
@ -8,6 +9,7 @@ class Path():
self._absolute_path: str = "" self._absolute_path: str = ""
for path in paths: 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.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._name: str = os.path.basename(self._absolute_path)
self._dirpath: str = os.path.dirname(self._absolute_path) self._dirpath: str = os.path.dirname(self._absolute_path)
@ -17,6 +19,10 @@ class Path():
def get_dirpath(self): def get_dirpath(self):
return self._dirpath 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): def get_absolute_path(self):
return self._absolute_path return self._absolute_path
@ -25,6 +31,9 @@ class Path():
return None return None
return self._name return self._name
def create(self) -> None:
os.makedirs(self.get_absolute_path())
def exist(self) -> bool: def exist(self) -> bool:
return os.path.exists(self.get_absolute_path()) return os.path.exists(self.get_absolute_path())

View File

@ -7,20 +7,27 @@ from path import Path
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
import config
if TYPE_CHECKING: if TYPE_CHECKING:
from page import Page from page import Page
from album import Album from album import Album
class Picture(): 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._large: Path = picture_path
self._small: Path = Path(picture_path.get_absolute_path()[:-4] + "_small.jpg") 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._export_file: Path = Path(picture_path.get_absolute_path() + ".out.pp3")
self._raw: Path|None = raw self._raw: Path|None = raw
self._page: Page = page 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 self._is_reperoried: bool = is_repertoried
def get_albums_name(self):
return self._albums_name
def get_small(self): def get_small(self):
if not self._small.exist(): if not self._small.exist():
self.gen_small() self.gen_small()
@ -33,7 +40,7 @@ class Picture():
return self._export_file return self._export_file
def gen_small(self): 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) im.save(self._small.get_absolute_path(), quality=95, optimize=True)

3
src/templates/album.css Normal file
View File

@ -0,0 +1,3 @@
body {
background-color: red;
}

19
src/templates/album.jinja Normal file
View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="album.css">
</head>
<body>
{{album.name}}
caaaa
<h1>{{ album._name }}</h1>
<div class="picture_container">
{% for picture in album._pictures %}
<img src="{{ picture.get_small().get_absolute_path() }}">
{% endfor %}
</div>
</body>
</html>