2023-08-18 15:13:28 +00:00
|
|
|
import base64
|
|
|
|
import io
|
2023-02-14 03:14:24 +00:00
|
|
|
import os
|
2024-04-25 03:53:30 +00:00
|
|
|
import re
|
|
|
|
import unicodedata
|
2023-02-14 03:14:24 +00:00
|
|
|
from pathlib import Path
|
2021-12-21 02:23:41 +00:00
|
|
|
|
2024-03-19 05:54:04 +00:00
|
|
|
from PIL import Image
|
2023-02-14 03:14:24 +00:00
|
|
|
|
2024-02-04 22:23:10 +00:00
|
|
|
|
2024-04-25 03:53:30 +00:00
|
|
|
def slugify(value: str, allow_unicode: bool = False) -> str:
|
|
|
|
"""
|
|
|
|
Convert to ASCII if 'allow_unicode' is False. Convert spaces or repeated
|
|
|
|
dashes to single dashes. Remove characters that aren't alphanumerics,
|
2024-04-28 16:24:36 +00:00
|
|
|
underscores, or hyphens. Replace slashes with underscores.
|
|
|
|
Convert to lowercase. Also strip leading and
|
2024-04-25 03:53:30 +00:00
|
|
|
trailing whitespace, dashes, and underscores.
|
|
|
|
|
|
|
|
Adapted from Django: https://github.com/django/django/blob/main/django/utils/text.py
|
|
|
|
"""
|
|
|
|
value = str(value)
|
|
|
|
if allow_unicode:
|
|
|
|
value = unicodedata.normalize("NFKC", value)
|
|
|
|
else:
|
|
|
|
value = unicodedata.normalize("NFKD", value).encode("ascii", "ignore").decode("ascii")
|
|
|
|
value = re.sub(r"[/]", "_", value.lower())
|
2024-04-28 16:24:36 +00:00
|
|
|
value = re.sub(r"[^.\w\s-]", "", value.lower())
|
2024-04-25 03:53:30 +00:00
|
|
|
return re.sub(r"[-\s]+", "-", value).strip("-_")
|
|
|
|
|
|
|
|
|
2024-04-28 16:24:36 +00:00
|
|
|
def safe_filename(directory: Path, value: str) -> str:
|
|
|
|
"""Make a string safe to use as a filename."""
|
|
|
|
escaped_string = slugify(value)
|
2024-04-28 18:42:40 +00:00
|
|
|
max_name_length = os.pathconf(directory, "PC_NAME_MAX") if hasattr(os, "pathconf") else 256
|
2024-04-28 16:24:36 +00:00
|
|
|
return escaped_string[len(escaped_string) - max_name_length :]
|
|
|
|
|
|
|
|
|
2024-02-04 03:55:09 +00:00
|
|
|
def directory_size(directory: Path) -> int:
|
|
|
|
"""
|
|
|
|
Return the aggregate size of all files in a directory (bytes).
|
|
|
|
"""
|
|
|
|
sum = 0
|
|
|
|
for root, dirs, files in os.walk(directory):
|
|
|
|
for f in files:
|
|
|
|
sum += Path(root, f).stat().st_size
|
|
|
|
for d in dirs:
|
|
|
|
sum += Path(root, d).stat().st_size
|
|
|
|
return sum
|
2023-03-03 06:02:00 +00:00
|
|
|
|
2024-02-04 22:23:10 +00:00
|
|
|
|
2023-03-15 12:46:35 +00:00
|
|
|
def image_to_dataURL(image: Image.Image, image_format: str = "PNG") -> str:
|
|
|
|
"""
|
|
|
|
Converts an image into a base64 image dataURL.
|
|
|
|
"""
|
|
|
|
buffered = io.BytesIO()
|
|
|
|
image.save(buffered, format=image_format)
|
|
|
|
mime_type = Image.MIME.get(image_format.upper(), "image/" + image_format.lower())
|
|
|
|
image_base64 = f"data:{mime_type};base64," + base64.b64encode(buffered.getvalue()).decode("UTF-8")
|
|
|
|
return image_base64
|
2023-06-25 22:50:15 +00:00
|
|
|
|
2023-07-27 14:54:01 +00:00
|
|
|
|
2023-06-25 22:50:15 +00:00
|
|
|
class Chdir(object):
|
|
|
|
"""Context manager to chdir to desired directory and change back after context exits:
|
|
|
|
Args:
|
|
|
|
path (Path): The path to the cwd
|
2023-07-27 14:54:01 +00:00
|
|
|
"""
|
|
|
|
|
2023-06-25 22:50:15 +00:00
|
|
|
def __init__(self, path: Path):
|
|
|
|
self.path = path
|
|
|
|
self.original = Path().absolute()
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
os.chdir(self.path)
|
|
|
|
|
|
|
|
def __exit__(self, *args):
|
|
|
|
os.chdir(self.original)
|