Files
photohub/src/main.py

148 lines
4.9 KiB
Python

from progress.bar import Bar
from path import Path
from picture import Picture
from bulk_page import BulkPage
from bulk_category import BulkCategory
from page import Page
from photodown import Photodown
import argparse
import config
from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('src/templates'))
home_template = env.get_template('home.jinja')
def argument_parsing():
parser = argparse.ArgumentParser()
parser.add_argument("site_path", help="the folder of your site MUST BE ALWAYS BEFORT PARAMETERS", type=str)
parser.add_argument("-r", '--regen', help="Regenerate (t)thumb (s)small", type=str)
args = parser.parse_args()
return args
def scan_bulk_pages(folders: list[Path]) -> list[BulkPage]:
pages: list[BulkPage] = []
prev: BulkPage = None
with Bar("Scanning Pages...", max=len(folders)) as bar:
for folder in folders:
files: list[Path] = folder.get_files()
page: BulkPage = BulkPage(folder, folder.get_name(), prev=prev)
raw: Path = Path(folder, folder.get_name() + ".NEF")
id: int = 0
for file in files:
if file.get_name().endswith(".png"):
page.add_picture(Picture(file, id, page=page, raw=raw))
id += 1
if len(page.get_pictures()) == 0:
bar.next()
continue
pages.append(page)
if prev is not None:
prev.next = page
prev = page
bar.next()
pages[-1].next = pages[0]
pages[0].prev = pages[-1]
return pages
def create_pages(pages: list[BulkPage]) -> None:
with Bar("Generating Pages...", max=len(pages)) as bar:
for page in pages:
page.create()
bar.next()
def scan_categories(pages: list[BulkPage], categories_path: Path) -> list[BulkCategory]:
categories: dict[str, BulkCategory] = {}
with Bar("Scanning pages...", max=len(pages)) as bar:
for page in pages:
for picture in page.get_pictures():
for category_name in picture.get_categories_name():
category: BulkCategory | None = categories.get(category_name)
if (category is None):
category = BulkCategory(category_name, categories_path)
categories.update({category_name: category})
picture.categories.append(category)
category.add_picture(picture)
bar.next()
return (categories.values())
def create_categories(categories: list[BulkCategory]) -> None:
with Bar("Generating categories...", max=len(categories)) as bar:
for category in categories:
category.create()
bar.next()
def gen_bulk(bulk_path: Path, markdown: Photodown):
category_path: Path = Path(bulk_path, "categories")
pages: list[BulkPage] = scan_bulk_pages(bulk_path.get_dirs())
categories: list[BulkCategory] = scan_categories(pages, category_path)
bulk: list[Picture] = []
for page in pages:
for picture in page.get_pictures():
bulk.append(picture)
markdown.add_bulk(bulk)
if config.CREATE_GENERAL_CATEGORY:
for category in categories:
if (category.name == "general"):
category.path = Path(bulk_path, "index.html")
Path("./src/templates/bulk_page.css").copy_to(Path(bulk_path, "bulk_page.css"))
create_pages(pages)
Path("./src/templates/bulk_category.css").copy_to(Path(bulk_path, "bulk_category.css"))
if (not category_path.exist()):
category_path.create()
create_categories(categories)
def scan_pages(site_path: Path, markdown: Photodown):
for path in [f for f in site_path.get_files() if f.get_name().endswith(config.PAGE_EXT)]:
page: Page = Page(path, markdown)
page.create()
def gen_pages(site_path: Path, markdown: Photodown):
scan_pages(site_path, markdown)
def regen(bulk_path: Path, is_thumb: bool, is_small: bool):
pages: list[BulkPage] = scan_bulk_pages(bulk_path.get_dirs())
with Bar("Regenerating assets...", max=len(pages)) as bar:
for page in pages:
for picture in page.get_pictures():
if is_thumb:
picture.gen_thumb()
if is_small:
picture.gen_small()
bar.next()
def gen_home(site_path: Path) -> None:
home_path = Path(site_path, "index.html")
with open(home_path.get_absolute_path(), "w") as f:
f.write(home_template.render())
def main():
args = argument_parsing()
site_path: Path = Path(args.site_path)
bulk_path: Path = Path(site_path, "bulk/")
if args.regen is not None:
regen(bulk_path, 't' in args.regen, 's' in args.regen)
markdown = Photodown()
gen_bulk(bulk_path, markdown)
gen_pages(site_path, markdown)
gen_home(site_path)
if __name__ == "__main__":
main()