Convert to proper python package
This commit is contained in:
@@ -0,0 +1,185 @@
|
||||
"""Gallery building and rendering logic."""
|
||||
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from typing import Dict, Any, Optional
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
|
||||
from gallery.config import GalleryConfig
|
||||
from gallery.utils.metadata import (
|
||||
load_folder_metadata,
|
||||
merge_metadata,
|
||||
save_metadata_cache,
|
||||
)
|
||||
from gallery.utils.processing import (
|
||||
process_plot_files,
|
||||
needs_update,
|
||||
render_gallery_page,
|
||||
)
|
||||
|
||||
|
||||
def get_template(template_dir: Optional[Path] = None):
|
||||
"""
|
||||
Get the Jinja2 template for gallery rendering.
|
||||
|
||||
Args:
|
||||
template_dir: Path to template directory. If None,
|
||||
uses package default.
|
||||
|
||||
Returns:
|
||||
Jinja2 Template object
|
||||
"""
|
||||
if template_dir is None:
|
||||
# Use package-included template
|
||||
import gallery
|
||||
gallery_module_path = Path(gallery.__file__).parent
|
||||
template_dir = gallery_module_path / "templates"
|
||||
|
||||
env = Environment(loader=FileSystemLoader(str(template_dir)))
|
||||
|
||||
def datetime_from_timestamp(timestamp: float):
|
||||
"""Convert a Unix timestamp to a datetime object."""
|
||||
from datetime import datetime
|
||||
return datetime.fromtimestamp(timestamp)
|
||||
|
||||
def strftime_filter(dt, fmt: str) -> str:
|
||||
"""Format a datetime object using strftime."""
|
||||
return dt.strftime(fmt)
|
||||
|
||||
env.filters['datetime_from_timestamp'] = datetime_from_timestamp
|
||||
env.filters['strftime'] = strftime_filter
|
||||
|
||||
return env.get_template("gallery.html")
|
||||
|
||||
|
||||
def build_gallery(
|
||||
config: GalleryConfig,
|
||||
template,
|
||||
source_dir: Path,
|
||||
web_dir: Path,
|
||||
relative_path: Path = None,
|
||||
inherited_metadata: Optional[Dict[str, Any]] = None,
|
||||
) -> None:
|
||||
"""
|
||||
Recursively build gallery structure from source directory.
|
||||
|
||||
Processes all PDF files in the source directory, converts them to PNG,
|
||||
copies both to the web directory, and generates index.html files with
|
||||
navigation and thumbnails. Includes metadata support.
|
||||
|
||||
Args:
|
||||
config: Gallery configuration object
|
||||
template: Jinja2 template for rendering
|
||||
source_dir: Source directory containing PDF files
|
||||
web_dir: Target web directory for gallery output
|
||||
relative_path: Relative path from gallery root (for navigation)
|
||||
inherited_metadata: Metadata inherited from parent directories
|
||||
"""
|
||||
if relative_path is None:
|
||||
relative_path = Path(".")
|
||||
|
||||
if inherited_metadata is None:
|
||||
inherited_metadata = {}
|
||||
|
||||
folder_metadata = load_folder_metadata(source_dir)
|
||||
current_metadata = merge_metadata(inherited_metadata, folder_metadata)
|
||||
|
||||
# Find all plot files (both PDF and HTML)
|
||||
pdf_files = list(source_dir.glob("*.pdf"))
|
||||
html_files = list(source_dir.glob("*.html"))
|
||||
plot_files = pdf_files + html_files
|
||||
|
||||
items = []
|
||||
plot_metadata_cache = {}
|
||||
|
||||
# Process all plot files (PDFs and HTMLs)
|
||||
for plot_file in plot_files:
|
||||
item = process_plot_files(
|
||||
config=config,
|
||||
plot_file=plot_file,
|
||||
web_dir=web_dir,
|
||||
current_metadata=current_metadata,
|
||||
)
|
||||
items.append(item)
|
||||
plot_metadata_cache[plot_file.stem] = item["metadata"]
|
||||
|
||||
if config.cache_enabled:
|
||||
save_metadata_cache(web_dir, plot_metadata_cache)
|
||||
|
||||
# Process subdirectories
|
||||
subdirs = [d for d in source_dir.iterdir() if d.is_dir()]
|
||||
subdir_names = []
|
||||
for subdir in subdirs:
|
||||
subdir_web = web_dir / subdir.name
|
||||
subdir_web.mkdir(exist_ok=True)
|
||||
subdir_relative = relative_path / subdir.name
|
||||
build_gallery(
|
||||
config,
|
||||
template,
|
||||
subdir,
|
||||
subdir_web,
|
||||
subdir_relative,
|
||||
current_metadata if config.inherit_from_parent else {}
|
||||
)
|
||||
subdir_names.append(subdir.name)
|
||||
|
||||
render_gallery_page(
|
||||
config=config,
|
||||
template=template,
|
||||
web_dir=web_dir,
|
||||
items=items,
|
||||
subdirs=subdir_names,
|
||||
relative_path=relative_path,
|
||||
metadata=current_metadata
|
||||
)
|
||||
|
||||
|
||||
def copy_assets(
|
||||
config: GalleryConfig,
|
||||
assets_src: Optional[Path] = None,
|
||||
verbose: bool = False
|
||||
) -> bool:
|
||||
"""
|
||||
Copy assets to the web directory.
|
||||
|
||||
Args:
|
||||
config: Gallery configuration object
|
||||
assets_src: Path to assets source. If None, uses package default.
|
||||
verbose: Whether to print status messages
|
||||
|
||||
Returns:
|
||||
True if successful, False otherwise
|
||||
"""
|
||||
try:
|
||||
if assets_src is None:
|
||||
# Use package-included assets
|
||||
import gallery
|
||||
gallery_module_path = Path(gallery.__file__).parent
|
||||
assets_src = gallery_module_path / "assets"
|
||||
|
||||
if not assets_src.exists():
|
||||
if verbose:
|
||||
print(
|
||||
f"Warning: Assets directory {assets_src} not found"
|
||||
)
|
||||
return False
|
||||
|
||||
gallery_root = Path(config.web_folder) / config.plot_root
|
||||
assets_dst = gallery_root.parent / "assets"
|
||||
|
||||
main_css_src = assets_src / "css" / "main.css"
|
||||
main_css_dst = assets_dst / "css" / "main.css"
|
||||
|
||||
if (not assets_dst.exists() or
|
||||
needs_update(main_css_src, main_css_dst)):
|
||||
if assets_dst.exists():
|
||||
shutil.rmtree(assets_dst)
|
||||
shutil.copytree(assets_src, assets_dst)
|
||||
if verbose:
|
||||
print(f"Updated assets from {assets_src} to {assets_dst}")
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
if verbose:
|
||||
print(f"Warning: Could not copy assets: {e}")
|
||||
return False
|
||||
Reference in New Issue
Block a user