Skip to content

Commit

Permalink
Use source filename as part of the temp outputfile, if a custom temp …
Browse files Browse the repository at this point in the history
…folder is defined
  • Loading branch information
jpsca committed Oct 29, 2022
1 parent e323716 commit 341d05d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
38 changes: 27 additions & 11 deletions image_processing/image_processing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import re
import tempfile
import unicodedata
import uuid
from pathlib import Path
from typing import TYPE_CHECKING
Expand All @@ -13,7 +15,8 @@


class ImageProcessing:
def __init__(self,
def __init__(
self,
source: "Union[str, Path]" = "",
*,
temp_folder: "Union[str, Path, None]" = None,
Expand Down Expand Up @@ -66,7 +69,8 @@ def saver(self, **kw) -> "ImageProcessing":
return copy

def convert(self, format: str) -> "ImageProcessing":
"""Specifies the output format.
"""
Specifies the output format.
```python
pipeline = ImageProcessing(image)
Expand All @@ -82,8 +86,10 @@ def convert(self, format: str) -> "ImageProcessing":
return copy

def save(self, destination: "Union[str, Path, None]" = "", save: bool = True) -> str:
"""Run the defined processing and get the result. Allows specifying
the source file and destination."""
"""
Run the defined processing and get the result. Allows specifying
the source file and destination.
"""
if not self._source:
raise ValueError("You must define a source path using `.source(path)`")

Expand Down Expand Up @@ -120,14 +126,24 @@ def _get_destination_format(self, destination: "Optional[Path]") -> str:
return format or DEFAULT_FORMAT

def _get_destination(self, destination: "Optional[Path]", format: str) -> str:
if not destination:
if self._temp_folder:
destination = self._temp_folder / uuid.uuid4().hex
else:
dest = tempfile.NamedTemporaryFile(delete=False).name
destination = Path(dest)

destination = destination or self._get_temp_destination()
return str(destination.with_suffix(f".{format}"))

def _get_temp_destination(self) -> Path:
if not self._temp_folder:
return Path(tempfile.NamedTemporaryFile(delete=False).name)

filename = self._slugify(self._source)
return self._temp_folder / uuid.uuid4().hex / filename

def _get_format(self, file_path: "Union[str, Path]") -> str:
return Path(file_path).suffix.lstrip(".")

def _slugify(self, value):
"""
Converts to lowercase, removes non-word characters (alphanumerics and
underscores) and converts internal spaces to hyphens.
"""
value = unicodedata.normalize("NFKD", value).encode("ascii", "ignore").decode("ascii")
value = re.sub(r"[^\w\s-]", "", value).strip().lower()
return re.sub(r"[-\s]+", "-", value)
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tox]
skipsdist = True
envlist = py311,py310,py39,py38,py37
envlist = py311,py310,py39

[testenv]
skip_install = true
Expand Down

0 comments on commit 341d05d

Please sign in to comment.