This commit is contained in:
Kylian Schmidt
2025-07-07 15:26:01 +02:00
parent 9041abb72f
commit b5b2c676b3
3 changed files with 406 additions and 85 deletions
+82 -2
View File
@@ -143,18 +143,88 @@ def build_gallery(source_dir: Path, web_dir: Path,
else:
title = f"Gallery: {relative_path}"
# Calculate statistics for current directory
current_stats = calculate_directory_stats(web_dir)
stats = {
"file_count": len(items),
"folder_count": len(subdir_names),
"total_size": format_file_size(current_stats["total_size"]),
"total_size_bytes": current_stats["total_size"]
}
with output_html.open("w") as f:
f.write(template.render(
title=title,
items=items,
subdirs=subdir_names,
relpath=str(relative_path),
ui=CONFIG.ui
ui=CONFIG.ui,
stats=stats
))
print(f"Generated {output_html}")
def calculate_directory_stats(directory: Path) -> dict:
"""
Calculate statistics for a directory.
Args:
directory: Path to the directory to analyze
Returns:
Dictionary containing file count, folder count, and total size
"""
stats = {
"file_count": 0,
"folder_count": 0,
"total_size": 0,
"pdf_size": 0,
"png_size": 0
}
if not directory.exists():
return stats
for item in directory.rglob("*"):
if item.is_file():
stats["file_count"] += 1
size = item.stat().st_size
stats["total_size"] += size
if item.suffix.lower() == '.pdf':
stats["pdf_size"] += size
elif item.suffix.lower() == '.png':
stats["png_size"] += size
elif item.is_dir():
stats["folder_count"] += 1
return stats
def format_file_size(size_bytes: int) -> str:
"""
Format file size in human readable format.
Args:
size_bytes: Size in bytes
Returns:
Formatted size string
"""
if size_bytes == 0:
return "0 B"
size_names = ["B", "KB", "MB", "GB", "TB"]
size = float(size_bytes)
i = 0
while size >= 1024 and i < len(size_names) - 1:
size /= 1024
i += 1
return f"{size:.1f} {size_names[i]}"
def main() -> None:
"""
Main entry point for gallery generation.
@@ -206,6 +276,15 @@ def main() -> None:
"png_href": png_name
}]
# Calculate statistics for single file
current_stats = calculate_directory_stats(source_web_dir)
stats = {
"file_count": 1,
"folder_count": 0,
"total_size": format_file_size(current_stats["total_size"]),
"total_size_bytes": current_stats["total_size"]
}
output_html = source_web_dir / "index.html"
with output_html.open("w") as f:
f.write(template.render(
@@ -213,7 +292,8 @@ def main() -> None:
items=items,
subdirs=[],
relpath=source.name,
ui=CONFIG.ui
ui=CONFIG.ui,
stats=stats
))
print(f"Generated {output_html}")