mirror of
https://github.com/greflm13/StaticGalleryBuilder.git
synced 2026-02-05 02:59:27 +00:00
log file truncation and modification of log messages
This commit is contained in:
@@ -212,7 +212,6 @@ def main() -> None:
|
|||||||
icons(args)
|
icons(args)
|
||||||
|
|
||||||
if args.generate_webmanifest:
|
if args.generate_webmanifest:
|
||||||
logger.info("generating webmanifest")
|
|
||||||
print("Generating webmanifest...")
|
print("Generating webmanifest...")
|
||||||
webmanifest(args)
|
webmanifest(args)
|
||||||
|
|
||||||
|
|||||||
@@ -89,11 +89,13 @@ def get_image_info(item: str, folder: str) -> Dict[str, Any]:
|
|||||||
Returns:
|
Returns:
|
||||||
Dict[str, Any]: A dictionary containing image width, height, and EXIF data.
|
Dict[str, Any]: A dictionary containing image width, height, and EXIF data.
|
||||||
"""
|
"""
|
||||||
with Image.open(os.path.join(folder, item)) as img:
|
file = os.path.join(folder, item)
|
||||||
logger.info("extracting image information", extra={"file": item})
|
with Image.open(file) as img:
|
||||||
|
logger.info("extracting image information", extra={"file": file})
|
||||||
exif = img.getexif()
|
exif = img.getexif()
|
||||||
width, height = img.size
|
width, height = img.size
|
||||||
if exif:
|
if exif:
|
||||||
|
logger.info("extracting EXIF data", extra={"file": file})
|
||||||
ifd = exif.get_ifd(ExifTags.IFD.Exif)
|
ifd = exif.get_ifd(ExifTags.IFD.Exif)
|
||||||
exifdatas = dict(exif.items()) | ifd
|
exifdatas = dict(exif.items()) | ifd
|
||||||
exifdata = {}
|
exifdata = {}
|
||||||
@@ -113,6 +115,7 @@ def get_image_info(item: str, folder: str) -> Dict[str, Any]:
|
|||||||
content = newtuple
|
content = newtuple
|
||||||
exifdata[tag] = content
|
exifdata[tag] = content
|
||||||
if "Orientation" in exifdata and exifdata["Orientation"] in [6, 8]:
|
if "Orientation" in exifdata and exifdata["Orientation"] in [6, 8]:
|
||||||
|
logger.info("image is rotated", extra={"file": file})
|
||||||
width, height = height, width
|
width, height = height, width
|
||||||
for key in ["PrintImageMatching", "UserComment", "MakerNote"]:
|
for key in ["PrintImageMatching", "UserComment", "MakerNote"]:
|
||||||
if key in exifdata:
|
if key in exifdata:
|
||||||
@@ -155,12 +158,15 @@ def process_image(item: str, folder: str, _args: Args, baseurl: str, sizelist: D
|
|||||||
thumbnails.append((folder, item, _args.root_directory))
|
thumbnails.append((folder, item, _args.root_directory))
|
||||||
|
|
||||||
for _raw in raw:
|
for _raw in raw:
|
||||||
if os.path.exists(os.path.join(folder, extsplit[0] + _raw)):
|
file = os.path.join(folder, extsplit[0] + _raw)
|
||||||
url = urllib.parse.quote(extsplit[0]) + _raw
|
if os.path.exists(file):
|
||||||
|
url = f"{_args.web_root_url}{baseurl}{urllib.parse.quote(extsplit[0])}{_raw}"
|
||||||
if _raw in (".tif", ".tiff"):
|
if _raw in (".tif", ".tiff"):
|
||||||
image["tiff"] = f"{_args.web_root_url}{baseurl}{url}"
|
logger.info("tiff file found", extra={"file": file})
|
||||||
|
image["tiff"] = url
|
||||||
else:
|
else:
|
||||||
image["raw"] = f"{_args.web_root_url}{baseurl}{url}"
|
logger.info("raw file found", extra={"file": file, "extension": _raw})
|
||||||
|
image["raw"] = url
|
||||||
return image
|
return image
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -48,14 +48,14 @@ def log_format(keys):
|
|||||||
|
|
||||||
def rotate_log_file(compress=False):
|
def rotate_log_file(compress=False):
|
||||||
"""
|
"""
|
||||||
Renames the existing 'latest.jsonl' file to a timestamped file based on the first log entry's asctime.
|
Truncates the 'latest.jsonl' file after optionally compressing its contents to a timestamped file.
|
||||||
Optionally compresses the old log file using gzip.
|
The 'latest.jsonl' file is not deleted or moved, just emptied.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
compress (bool): If True, compress the old log file using gzip.
|
compress (bool): If True, compress the old log file using gzip.
|
||||||
"""
|
"""
|
||||||
if os.path.exists(LATEST_LOG_FILE):
|
if os.path.exists(LATEST_LOG_FILE):
|
||||||
with open(LATEST_LOG_FILE, "r", encoding="utf-8") as f:
|
with open(LATEST_LOG_FILE, "r+", encoding="utf-8") as f:
|
||||||
first_line = f.readline()
|
first_line = f.readline()
|
||||||
try:
|
try:
|
||||||
first_log = json.loads(first_line)
|
first_log = json.loads(first_line)
|
||||||
@@ -67,7 +67,10 @@ def rotate_log_file(compress=False):
|
|||||||
safe_timestamp = first_timestamp.replace(":", "-").replace(" ", "_")
|
safe_timestamp = first_timestamp.replace(":", "-").replace(" ", "_")
|
||||||
old_log_filename = os.path.join(LOG_DIR, f"{safe_timestamp}.jsonl")
|
old_log_filename = os.path.join(LOG_DIR, f"{safe_timestamp}.jsonl")
|
||||||
|
|
||||||
os.rename(LATEST_LOG_FILE, old_log_filename)
|
# Write contents to the new file
|
||||||
|
with open(old_log_filename, "w", encoding="utf-8") as old_log_file:
|
||||||
|
f.seek(0) # Go back to the beginning of the file
|
||||||
|
shutil.copyfileobj(f, old_log_file)
|
||||||
|
|
||||||
if compress:
|
if compress:
|
||||||
with open(old_log_filename, "rb") as f_in:
|
with open(old_log_filename, "rb") as f_in:
|
||||||
@@ -75,6 +78,10 @@ def rotate_log_file(compress=False):
|
|||||||
shutil.copyfileobj(f_in, f_out)
|
shutil.copyfileobj(f_in, f_out)
|
||||||
os.remove(old_log_filename)
|
os.remove(old_log_filename)
|
||||||
|
|
||||||
|
# Truncate the original file
|
||||||
|
f.seek(0)
|
||||||
|
f.truncate()
|
||||||
|
|
||||||
|
|
||||||
def setup_logger(level=logging.INFO):
|
def setup_logger(level=logging.INFO):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -178,8 +178,8 @@ def create_icons_from_svg(files: List[str], iconspath: str, _args: Args) -> List
|
|||||||
List[Icon]
|
List[Icon]
|
||||||
List of icons created from the SVG file.
|
List of icons created from the SVG file.
|
||||||
"""
|
"""
|
||||||
logger.info("creating icons for web application", extra={"iconspath": iconspath})
|
|
||||||
svg = [file for file in files if file.endswith(".svg")][0]
|
svg = [file for file in files if file.endswith(".svg")][0]
|
||||||
|
logger.info("creating icons for web application", extra={"iconspath": iconspath, "svg": svg})
|
||||||
icon_list = [
|
icon_list = [
|
||||||
{"src": f"{_args.web_root_url}.static/icons/{svg}", "type": "image/svg+xml", "sizes": "512x512", "purpose": "maskable"},
|
{"src": f"{_args.web_root_url}.static/icons/{svg}", "type": "image/svg+xml", "sizes": "512x512", "purpose": "maskable"},
|
||||||
{"src": f"{_args.web_root_url}.static/icons/{svg}", "type": "image/svg+xml", "sizes": "512x512", "purpose": "any"},
|
{"src": f"{_args.web_root_url}.static/icons/{svg}", "type": "image/svg+xml", "sizes": "512x512", "purpose": "any"},
|
||||||
@@ -188,7 +188,7 @@ def create_icons_from_svg(files: List[str], iconspath: str, _args: Args) -> List
|
|||||||
tmpimg = BytesIO()
|
tmpimg = BytesIO()
|
||||||
sizes = size.split("x")
|
sizes = size.split("x")
|
||||||
iconpath = os.path.join(iconspath, os.path.splitext(svg)[0] + "-" + size + ".png")
|
iconpath = os.path.join(iconspath, os.path.splitext(svg)[0] + "-" + size + ".png")
|
||||||
logger.info("converting svg to png", extra={"iconpath": iconpath, "size": size})
|
logger.info("converting svg to png", extra={"svg": svg, "size": size})
|
||||||
cairosvg.svg2png(
|
cairosvg.svg2png(
|
||||||
url=os.path.join(iconspath, svg),
|
url=os.path.join(iconspath, svg),
|
||||||
write_to=tmpimg,
|
write_to=tmpimg,
|
||||||
@@ -240,7 +240,7 @@ def create_icons_from_png(iconspath: str, web_root_url: str) -> List[Icon]:
|
|||||||
continue
|
continue
|
||||||
with Image.open(os.path.join(iconspath, icon)) as iconfile:
|
with Image.open(os.path.join(iconspath, icon)) as iconfile:
|
||||||
iconsize = f"{iconfile.size[0]}x{iconfile.size[1]}"
|
iconsize = f"{iconfile.size[0]}x{iconfile.size[1]}"
|
||||||
logger.info("using icon", extra={"icon": icon, "size": iconsize})
|
logger.info("using icon", extra={"iconspath": iconspath, "icon": icon, "size": iconsize})
|
||||||
icon_list.append({"src": f"{web_root_url}.static/icons/{icon}", "sizes": iconsize, "type": "image/png", "purpose": "maskable"})
|
icon_list.append({"src": f"{web_root_url}.static/icons/{icon}", "sizes": iconsize, "type": "image/png", "purpose": "maskable"})
|
||||||
icon_list.append({"src": f"{web_root_url}.static/icons/{icon}", "sizes": iconsize, "type": "image/png", "purpose": "any"})
|
icon_list.append({"src": f"{web_root_url}.static/icons/{icon}", "sizes": iconsize, "type": "image/png", "purpose": "any"})
|
||||||
return icon_list
|
return icon_list
|
||||||
@@ -255,6 +255,8 @@ def webmanifest(_args: Args) -> None:
|
|||||||
_args : Args
|
_args : Args
|
||||||
Parsed command-line arguments.
|
Parsed command-line arguments.
|
||||||
"""
|
"""
|
||||||
|
logger.info("generating webmanifest")
|
||||||
|
|
||||||
iconspath = os.path.join(_args.root_directory, ".static", "icons")
|
iconspath = os.path.join(_args.root_directory, ".static", "icons")
|
||||||
files = os.listdir(iconspath)
|
files = os.listdir(iconspath)
|
||||||
icon_list = create_icons_from_svg(files, iconspath, _args) if SVGSUPPORT and any(file.endswith(".svg") for file in files) else create_icons_from_png(iconspath, _args.web_root_url)
|
icon_list = create_icons_from_svg(files, iconspath, _args) if SVGSUPPORT and any(file.endswith(".svg") for file in files) else create_icons_from_png(iconspath, _args.web_root_url)
|
||||||
|
|||||||
BIN
test/example/DSC03508.ARW
Executable file
BIN
test/example/DSC03508.ARW
Executable file
Binary file not shown.
Reference in New Issue
Block a user