Skip to content

Commit

Permalink
public get_temp_filename
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsca committed Oct 31, 2022
1 parent f0cc3bb commit f990460
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
34 changes: 21 additions & 13 deletions image_processing/image_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
if TYPE_CHECKING:
from typing import Callable, Union

TStrorPath = Union[str, Path]
TStrOrPath = Union[str, Path]


DEFAULT_FORMAT = "jpeg"


class ImageProcessing:
def __init__(self, source: "TStrorPath" = "", *, temp_folder: "TStrorPath" = ""):
def __init__(self, source: "TStrOrPath" = "", *, temp_folder: "TStrOrPath" = ""):
self._processor = VipsProcessor()
self._source: str = str(source)
self._loader: dict = {}
Expand Down Expand Up @@ -45,7 +45,7 @@ def operation(*args, **kw) -> "ImageProcessing":

return operation

def source(self, path: "TStrorPath") -> "ImageProcessing":
def source(self, path: "TStrOrPath") -> "ImageProcessing":
""" """
copy = self._copy()
copy._source = str(path)
Expand Down Expand Up @@ -80,7 +80,7 @@ def convert(self, format: str) -> "ImageProcessing":
copy._format = format
return copy

def save(self, destination: "TStrorPath" = "", save: bool = True) -> str:
def save(self, destination: "TStrOrPath" = "", save: bool = True) -> str:
"""
Run the defined processing and get the result. Allows specifying
the source file and destination.
Expand All @@ -101,6 +101,15 @@ def save(self, destination: "TStrorPath" = "", save: bool = True) -> str:
save=save,
)

def get_temp_filename(self, destination: "TStrOrPath" = "") -> str:
"""Return a filename that, for the same source path, options,
operations (in the same order), etc., will be the same.
"""
ops = str(self.options).encode("utf8", errors="ignore")
hash = md5(ops).hexdigest()
format = self._get_destination_format(destination)
return f"{hash}.{format}"

# Private

def _copy(self) -> "ImageProcessing":
Expand All @@ -112,7 +121,7 @@ def _copy(self) -> "ImageProcessing":
copy._operations = self._operations[:]
return copy

def _get_destination_format(self, destination: "TStrorPath") -> str:
def _get_destination_format(self, destination: "TStrOrPath") -> str:
format = ""
if destination:
format = self._get_format(destination)
Expand All @@ -123,19 +132,18 @@ def _get_destination_format(self, destination: "TStrorPath") -> str:
or DEFAULT_FORMAT
)

def _get_destination(self, destination: "TStrorPath", format: str) -> str:
destination = Path(destination) if destination else self._get_temp_destination()
def _get_destination(self, destination: "TStrOrPath", format: str) -> str:
if destination:
destination = Path(destination)
else:
destination = self._get_temp_destination()
return str(destination.with_suffix(f".{format}"))

def _get_temp_destination(self) -> "Path":
filename = self._get_temp_filename()
filename = self.get_temp_filename()
if not self._temp_folder:
self._temp_folder = Path(tempfile.mkdtemp())
return self._temp_folder / filename

def _get_temp_filename(self) -> str:
ops = str(self.options).encode("utf8", errors="ignore")
return md5(ops).hexdigest()

def _get_format(self, file_path: "TStrorPath") -> str:
def _get_format(self, file_path: "TStrOrPath") -> str:
return Path(file_path).suffix.lstrip(".")
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = image-processing-egg
version = 0.2
version = 0.3
url = https://github.com/jpsca/image-processing-egg
project_urls =
Issue tracker = https://github.com/jpsca/image-processing-egg/issues
Expand Down

0 comments on commit f990460

Please sign in to comment.