added json logger

This commit is contained in:
2024-09-16 22:18:36 +02:00
parent a95a6e1722
commit 8ec9701aa9
30 changed files with 183 additions and 15 deletions

View File

@@ -11,10 +11,12 @@ from typing import Dict, List, Tuple
from tqdm.auto import tqdm
from PIL import Image, ImageOps
from modules.logger import logger
from modules.argumentparser import parse_arguments, Args
from modules.svg_handling import icons, webmanifest, extract_colorscheme
from modules.generate_html import list_folder, EXCLUDES
# fmt: off
# Constants
if __package__ is None:
@@ -32,6 +34,7 @@ RAW_EXTENSIONS = [
]
IMG_EXTENSIONS = [".jpg", ".jpeg", ".png"]
NOT_LIST = ["*/Galleries/*", "Archives"]
LOG_FILE = os.path.join(SCRIPTDIR, "log.json")
# fmt: on
pbardict: Dict[str, tqdm] = {}
@@ -77,10 +80,13 @@ def copy_static_files(_args: Args) -> None:
static_dir = os.path.join(_args.root_directory, ".static")
if os.path.exists(static_dir):
print("Removing existing .static folder...")
logger.info("removing existing .static folder")
shutil.rmtree(static_dir)
print("Copying static files...")
logger.info("copying static files")
shutil.copytree(STATIC_FILES_DIR, static_dir, dirs_exist_ok=True)
logger.info("reading theme file", extra={"theme": _args.theme_path})
with open(_args.theme_path, "r", encoding="utf-8") as f:
theme = f.read()
split = theme.split(".foldericon {")
@@ -92,20 +98,26 @@ def copy_static_files(_args: Args) -> None:
for match in re.finditer(r"content: (.*);", foldericon):
foldericon = match[1]
foldericon = foldericon.replace('"', "")
logger.info("found foldericon", extra={"foldericon": foldericon})
break
if "url" in foldericon:
shutil.copyfile(_args.theme_path, os.path.join(static_dir, "theme.css"))
logger.info("foldericon in theme file, using it")
return
with open(os.path.join(SCRIPTDIR, foldericon), "r", encoding="utf-8") as f:
logger.info("Reading foldericon svg")
svg = f.read()
if "svg.j2" in foldericon:
logger.info("foldericon in theme file is a jinja2 template")
colorscheme = extract_colorscheme(_args.theme_path)
svg = svg.replace("{{ color1 }}", colorscheme["color1"])
svg = svg.replace("{{ color2 }}", colorscheme["color2"])
svg = svg.replace("{{ color3 }}", colorscheme["color3"])
svg = svg.replace("{{ color4 }}", colorscheme["color4"])
logger.info("replaced colors in svg")
svg = urllib.parse.quote(svg)
with open(os.path.join(static_dir, "theme.css"), "x", encoding="utf-8") as f:
logger.info("writing theme file")
f.write(themehead + '\n.foldericon {\n content: url("data:image/svg+xml,' + svg + '");\n}\n' + themetail)
@@ -119,6 +131,7 @@ def generate_thumbnail(arguments: Tuple[str, str, str]) -> None:
A tuple containing the folder, item, root directory, and regenerate thumbnails flag.
"""
folder, item, root_directory = arguments
image = os.path.join(folder, item)
path = os.path.join(root_directory, ".thumbnails", folder.removeprefix(root_directory), item) + ".jpg"
oldpath = os.path.join(root_directory, ".thumbnails", folder.removeprefix(root_directory), os.path.splitext(item)[0]) + ".jpg"
if os.path.exists(oldpath):
@@ -127,14 +140,19 @@ def generate_thumbnail(arguments: Tuple[str, str, str]) -> None:
except FileNotFoundError:
pass
if not os.path.exists(path):
logger.info("generating thumbnail for %s", item, extra={"path": image})
try:
with Image.open(os.path.join(folder, item)) as imgfile:
with Image.open(image) as imgfile:
imgrgb = imgfile.convert("RGB")
img = ImageOps.exif_transpose(imgrgb)
img.thumbnail((512, 512))
img.save(path, "JPEG", quality=75, optimize=True, mode="RGB")
except OSError:
print(f"Failed to generate thumbnail for {os.path.join(folder, item)}")
logger.error("Failed to generate thumbnail for %s", item, extra={"path": image})
print(f"Failed to generate thumbnail for {image}")
return
else:
logger.info("thumbnail already exists for %s", item, extra={"path": image})
def get_total_folders(folder: str, _args: Args, _total: int = 0) -> int:
@@ -163,6 +181,7 @@ def get_total_folders(folder: str, _args: Args, _total: int = 0) -> int:
for item in items:
if item not in EXCLUDES and os.path.isdir(os.path.join(folder, item)) and not item.startswith("."):
if item not in _args.exclude_folders and not any(fnmatch.fnmatchcase(os.path.join(folder, item), exclude) for exclude in _args.exclude_folders):
logger.debug("Found folder %s in %s", item, folder)
_total = get_total_folders(os.path.join(folder, item), _args, _total)
return _total
@@ -179,12 +198,15 @@ def main() -> None:
lock_file = os.path.join(args.root_directory, ".lock")
if os.path.exists(lock_file):
print("Another instance of this program is running.")
logger.info("nother instance of this program is running")
exit()
try:
Path(lock_file).touch()
if args.regenerate_thumbnails:
logger.warning("regenerate thumbnails flag is set to true, all thumbnails will be regenerated")
if os.path.exists(os.path.join(args.root_directory, ".thumbnails")):
logger.info("removing old thumbnails folder")
shutil.rmtree(os.path.join(args.root_directory, ".thumbnails"))
os.makedirs(os.path.join(args.root_directory, ".thumbnails"), exist_ok=True)
@@ -192,17 +214,21 @@ def main() -> None:
icons(args)
if args.generate_webmanifest:
logger.info("generating webmanifest")
print("Generating webmanifest...")
webmanifest(args)
if args.non_interactive_mode:
logger.info("generating HTML files")
print("Generating HTML files...")
thumbnails = list_folder(0, args.root_directory, args.site_title, args, raw, VERSION)
with Pool(os.cpu_count()) as pool:
logger.info("generating thumbnails")
print("Generating thumbnails...")
pool.map(generate_thumbnail, thumbnails)
else:
pbardict["traversingbar"] = tqdm(desc="Traversing filesystem", unit="folders", ascii=True, dynamic_ncols=True)
logger.info("getting total number of folders to process")
total = get_total_folders(args.root_directory, args)
pbardict["traversingbar"].desc = "Traversing filesystem"
pbardict["traversingbar"].update(0)
@@ -211,6 +237,7 @@ def main() -> None:
thumbnails = list_folder(total, args.root_directory, args.site_title, args, raw, VERSION)
with Pool(os.cpu_count()) as pool:
logger.info("generating thumbnails")
for _ in tqdm(
pool.imap_unordered(generate_thumbnail, thumbnails),
total=len(thumbnails),