Added --post-command, implements #443

This commit is contained in:
Rhet Turnbull
2021-06-18 09:04:36 -07:00
parent ee0b369086
commit fa29f51aeb
11 changed files with 423 additions and 22 deletions

View File

@@ -6363,3 +6363,83 @@ def test_export_filepath_template():
assert exifdata[0]["XMP:Description"] == os.path.join(
isolated_cwd, CLI_TEMPLATE_FILENAME
)
def test_export_post_command():
"""Test --post-command"""
import os.path
from osxphotos.cli import cli
runner = CliRunner()
cwd = os.getcwd()
# pylint: disable=not-context-manager
with runner.isolated_filesystem():
result = runner.invoke(
cli,
[
"export",
"--db",
os.path.join(cwd, PHOTOS_DB_15_7),
".",
"--post-command",
"exported",
"echo {filepath.name|shell_quote} >> {export_dir}/exported.txt",
"--name",
"Park",
"--skip-original-if-edited",
],
)
assert result.exit_code == 0
with open("exported.txt") as f:
lines = [line.strip() for line in f]
assert lines[0] == "St James Park_edited.jpeg"
# run again with --update to test skipped
result = runner.invoke(
cli,
[
"export",
"--db",
os.path.join(cwd, PHOTOS_DB_15_7),
".",
"--post-command",
"skipped",
"echo {filepath.name|shell_quote} >> {export_dir}/skipped.txt",
"--name",
"Park",
"--skip-original-if-edited",
"--update",
],
)
assert result.exit_code == 0
with open("skipped.txt") as f:
lines = [line.strip() for line in f]
assert lines[0] == "St James Park_edited.jpeg"
def test_export_post_command_bad_command():
"""Test --post-command with bad command"""
import os.path
from osxphotos.cli import cli
runner = CliRunner()
cwd = os.getcwd()
# pylint: disable=not-context-manager
with runner.isolated_filesystem():
result = runner.invoke(
cli,
[
"export",
"--db",
os.path.join(cwd, PHOTOS_DB_15_7),
".",
"--post-command",
"exported",
"foobar {filepath.name|shell_quote} >> {export_dir}/exported.txt",
"--name",
"Park",
"--skip-original-if-edited",
],
)
assert result.exit_code == 0
assert 'Error running command "foobar' in result.output

View File

@@ -66,6 +66,7 @@ TEMPLATE_VALUES_MULTI_KEYWORDS = {
"{keyword|lower}": ["flowers", "wedding"],
"{keyword|titlecase}": ["Flowers", "Wedding"],
"{keyword|capitalize}": ["Flowers", "Wedding"],
"{keyword|shell_quote}": ["flowers", "wedding"],
"{+keyword}": ["flowerswedding"],
"{+keyword|titlecase}": ["Flowerswedding"],
"{+keyword|capitalize}": ["Flowerswedding"],
@@ -81,6 +82,7 @@ TEMPLATE_VALUES_TITLE = {
"{title|titlecase}": ["Tulips Tied Together At A Flower Shop"],
"{title|upper}": ["TULIPS TIED TOGETHER AT A FLOWER SHOP"],
"{title|titlecase|lower|upper}": ["TULIPS TIED TOGETHER AT A FLOWER SHOP"],
"{title|titlecase|lower|upper|shell_quote}": ["'TULIPS TIED TOGETHER AT A FLOWER SHOP'"],
"{title|upper|titlecase}": ["Tulips Tied Together At A Flower Shop"],
"{title|capitalize}": ["Tulips tied together at a flower shop"],
"{title[ ,_]}": ["Tulips_tied_together_at_a_flower_shop"],
@@ -90,6 +92,7 @@ TEMPLATE_VALUES_TITLE = {
"{+title}": ["Tulips tied together at a flower shop"],
"{,+title}": ["Tulips tied together at a flower shop"],
"{, +title}": ["Tulips tied together at a flower shop"],
"{title|shell_quote}": ["'Tulips tied together at a flower shop'"],
}
# Boolean type values that render to True
@@ -385,7 +388,7 @@ def test_lookup_multi(photosdb_places):
lookup_str = re.match(r"\{([^\\,}]+)\}", subst).group(1)
if subst in ["{exiftool}", "{photo}", "{function}"]:
continue
lookup = template.get_template_value_multi(lookup_str, path_sep=os.path.sep)
lookup = template.get_template_value_multi(lookup_str, path_sep=os.path.sep, default=[])
assert isinstance(lookup, list)