add: home page
This commit is contained in:
		
							
								
								
									
										32
									
								
								src/album.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/album.py
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										3
									
								
								src/config.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,3 @@ | ||||
|  | ||||
|  | ||||
| CREATE_GENERAL_ALBUM: bool = True | ||||
							
								
								
									
										46
									
								
								src/main.py
									
									
									
									
									
								
							
							
						
						
									
										46
									
								
								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() | ||||
|     main() | ||||
|  | ||||
| @ -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(): | ||||
|  | ||||
| @ -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()) | ||||
|      | ||||
|  | ||||
| @ -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) | ||||
|  | ||||
|          | ||||
							
								
								
									
										3
									
								
								src/templates/album.css
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								src/templates/album.css
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,3 @@ | ||||
| body { | ||||
|     background-color: red; | ||||
| } | ||||
							
								
								
									
										19
									
								
								src/templates/album.jinja
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								src/templates/album.jinja
									
									
									
									
									
										Normal 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> | ||||
		Reference in New Issue
	
	Block a user