Fix for #471
This commit is contained in:
@@ -1,3 +1,3 @@
|
|||||||
""" version info """
|
""" version info """
|
||||||
|
|
||||||
__version__ = "0.42.35"
|
__version__ = "0.42.36"
|
||||||
|
|||||||
@@ -6,17 +6,17 @@ from ._constants import MAX_DIRNAME_LEN, MAX_FILENAME_LEN
|
|||||||
|
|
||||||
|
|
||||||
def sanitize_filepath(filepath):
|
def sanitize_filepath(filepath):
|
||||||
""" sanitize a filepath """
|
"""sanitize a filepath"""
|
||||||
return pathvalidate.sanitize_filepath(filepath, platform="macos")
|
return pathvalidate.sanitize_filepath(filepath, platform="macos")
|
||||||
|
|
||||||
|
|
||||||
def is_valid_filepath(filepath):
|
def is_valid_filepath(filepath):
|
||||||
""" returns True if a filepath is valid otherwise False """
|
"""returns True if a filepath is valid otherwise False"""
|
||||||
return pathvalidate.is_valid_filepath(filepath, platform="macos")
|
return pathvalidate.is_valid_filepath(filepath, platform="macos")
|
||||||
|
|
||||||
|
|
||||||
def sanitize_filename(filename, replacement=":"):
|
def sanitize_filename(filename, replacement=":"):
|
||||||
""" replace any illegal characters in a filename and truncate filename if needed
|
"""replace any illegal characters in a filename and truncate filename if needed
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
filename: str, filename to sanitze
|
filename: str, filename to sanitze
|
||||||
@@ -46,11 +46,11 @@ def sanitize_filename(filename, replacement=":"):
|
|||||||
|
|
||||||
|
|
||||||
def sanitize_dirname(dirname, replacement=":"):
|
def sanitize_dirname(dirname, replacement=":"):
|
||||||
""" replace any illegal characters in a directory name and truncate directory name if needed
|
"""replace any illegal characters in a directory name and truncate directory name if needed
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
dirname: str, directory name to sanitze
|
dirname: str, directory name to sanitize
|
||||||
replacement: str, value to replace any illegal characters with; default = ":"
|
replacement: str, value to replace any illegal characters with; default = ":"; if None, no replacement occurs
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
dirname with any illegal characters replaced by replacement and truncated if necessary
|
dirname with any illegal characters replaced by replacement and truncated if necessary
|
||||||
@@ -61,19 +61,20 @@ def sanitize_dirname(dirname, replacement=":"):
|
|||||||
|
|
||||||
|
|
||||||
def sanitize_pathpart(pathpart, replacement=":"):
|
def sanitize_pathpart(pathpart, replacement=":"):
|
||||||
""" replace any illegal characters in a path part (either directory or filename without extension) and truncate name if needed
|
"""replace any illegal characters in a path part (either directory or filename without extension) and truncate name if needed
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
pathpart: str, path part to sanitze
|
pathpart: str, path part to sanitize
|
||||||
replacement: str, value to replace any illegal characters with; default = ":"
|
replacement: str, value to replace any illegal characters with; default = ":"; if None, no replacement occurs
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
pathpart with any illegal characters replaced by replacement and truncated if necessary
|
pathpart with any illegal characters replaced by replacement and truncated if necessary
|
||||||
"""
|
"""
|
||||||
if pathpart:
|
if pathpart:
|
||||||
pathpart = pathpart.replace("/", replacement)
|
pathpart = (
|
||||||
|
pathpart.replace("/", replacement) if replacement is not None else pathpart
|
||||||
|
)
|
||||||
if len(pathpart) > MAX_DIRNAME_LEN:
|
if len(pathpart) > MAX_DIRNAME_LEN:
|
||||||
drop = len(pathpart) - MAX_DIRNAME_LEN
|
drop = len(pathpart) - MAX_DIRNAME_LEN
|
||||||
pathpart = pathpart[:-drop]
|
pathpart = pathpart[:-drop]
|
||||||
return pathpart
|
return pathpart
|
||||||
|
|
||||||
|
|||||||
@@ -1194,7 +1194,8 @@ class PhotoTemplate:
|
|||||||
if self.filename:
|
if self.filename:
|
||||||
values = [sanitize_pathpart(value) for value in values]
|
values = [sanitize_pathpart(value) for value in values]
|
||||||
elif self.dirname:
|
elif self.dirname:
|
||||||
values = [sanitize_dirname(value) for value in values]
|
# sanitize but don't replace any "/" as user function may want to create sub directories
|
||||||
|
values = [sanitize_dirname(value, replacement=None) for value in values]
|
||||||
|
|
||||||
return values
|
return values
|
||||||
|
|
||||||
|
|||||||
@@ -6443,3 +6443,33 @@ def test_export_post_command_bad_command():
|
|||||||
)
|
)
|
||||||
assert result.exit_code == 0
|
assert result.exit_code == 0
|
||||||
assert 'Error running command "foobar' in result.output
|
assert 'Error running command "foobar' in result.output
|
||||||
|
|
||||||
|
|
||||||
|
def test_export_directory_template_function():
|
||||||
|
"""Test --directory with template function """
|
||||||
|
import os.path
|
||||||
|
import pathlib
|
||||||
|
from osxphotos.cli import cli
|
||||||
|
|
||||||
|
runner = CliRunner()
|
||||||
|
cwd = os.getcwd()
|
||||||
|
# pylint: disable=not-context-manager
|
||||||
|
with runner.isolated_filesystem():
|
||||||
|
with open("foo.py", "w") as f:
|
||||||
|
f.writelines(["def foo(photo, **kwargs):\n"," return 'foo/bar'"])
|
||||||
|
result = runner.invoke(
|
||||||
|
cli,
|
||||||
|
[
|
||||||
|
"export",
|
||||||
|
"--db",
|
||||||
|
os.path.join(cwd, PHOTOS_DB_15_7),
|
||||||
|
".",
|
||||||
|
"--uuid",
|
||||||
|
CLI_EXPORT_UUID,
|
||||||
|
"--directory",
|
||||||
|
"{function:foo.py::foo}"
|
||||||
|
],
|
||||||
|
)
|
||||||
|
assert result.exit_code == 0
|
||||||
|
assert pathlib.Path(f"foo/bar/{CLI_EXPORT_UUID_FILENAME}").is_file()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user