Feature custom sidecar zero length (#1138)
* Added skip_zero option to --sidecar-template * Added missing test file
This commit is contained in:
parent
ed1486fd4b
commit
db6caa5fa4
@ -348,7 +348,15 @@ from .verbose import get_verbose_console, verbose_print
|
||||
[
|
||||
click.Path(dir_okay=False, file_okay=True, exists=True),
|
||||
TemplateString(),
|
||||
CSVOptions(["write_skipped", "strip_whitespace", "strip_lines", "none"])
|
||||
CSVOptions(
|
||||
[
|
||||
"write_skipped",
|
||||
"strip_whitespace",
|
||||
"strip_lines",
|
||||
"skip_zero",
|
||||
"none",
|
||||
]
|
||||
),
|
||||
]
|
||||
),
|
||||
help="Create a custom sidecar file for each photo exported with user provided Mako template (MAKO_TEMPLATE_FILE). "
|
||||
@ -361,11 +369,12 @@ from .verbose import get_verbose_console, verbose_print
|
||||
"The `{filepath}` template variable may be used in the SIDECAR_FILENAME_TEMPLATE to refer to the filename of the "
|
||||
"photo being exported. "
|
||||
"OPTIONS is a comma-separated list of strings providing additional options to the template. "
|
||||
"Valid options are: write_skipped, strip_whitespace, strip_lines, none. "
|
||||
"Valid options are: write_skipped, strip_whitespace, strip_lines, skip_zero, none. "
|
||||
"write_skipped will cause the sidecar file to be written even if the photo is skipped during export. "
|
||||
"If write_skipped is not passed as an option, the sidecar file will not be written if the photo is skipped during export. "
|
||||
"strip_whitespace and strip_lines indicate whether or not to strip whitespace and blank lines, respectively, "
|
||||
"from the resulting sidecar file. "
|
||||
"skip_zero causes the sidecar file to be skipped if the rendered template is zero-length. "
|
||||
"For example, to create a sidecar file with extension .xmp using a template file named 'sidecar.mako' "
|
||||
"and write a sidecar for skipped photos and strip blank lines but not whitespace: "
|
||||
"`--sidecar-template sidecar.mako '{filepath}.xmp' write_skipped,strip_lines`. "
|
||||
|
||||
@ -53,6 +53,7 @@ def generate_user_sidecar(
|
||||
strip_whitespace = "strip_whitespace" in options
|
||||
strip_lines = "strip_lines" in options
|
||||
write_skipped = "write_skipped" in options
|
||||
skip_zero = "skip_zero" in options
|
||||
|
||||
if not write_skipped:
|
||||
# skip writing sidecar if photo not exported
|
||||
@ -97,6 +98,8 @@ def generate_user_sidecar(
|
||||
filepath=filepath,
|
||||
strip_whitespace=strip_whitespace,
|
||||
strip_lines=strip_lines,
|
||||
skip_zero=skip_zero,
|
||||
verbose=verbose,
|
||||
dry_run=dry_run,
|
||||
)
|
||||
sidecar_results.sidecar_user_written.append(template_filename)
|
||||
@ -129,6 +132,8 @@ def _render_sidecar_and_write_data(
|
||||
filepath: str,
|
||||
strip_whitespace: bool,
|
||||
strip_lines: bool,
|
||||
skip_zero: bool,
|
||||
verbose: Callable[..., None],
|
||||
dry_run: bool,
|
||||
):
|
||||
"""Render sidecar template and write data to file"""
|
||||
@ -149,5 +154,8 @@ def _render_sidecar_and_write_data(
|
||||
)
|
||||
if not dry_run:
|
||||
# write sidecar file
|
||||
if skip_zero and not sidecar_data:
|
||||
verbose(f"Skipping empty sidecar file [filepath]{template_filename}[/]")
|
||||
return
|
||||
with open(template_filename, "w") as f:
|
||||
f.write(sidecar_data)
|
||||
|
||||
3
tests/custom_sidecar_zero.mako
Normal file
3
tests/custom_sidecar_zero.mako
Normal file
@ -0,0 +1,3 @@
|
||||
<%doc> custom mako sidecar template that renders to zero length for some photos; use with options
|
||||
"strip_whitespace,strip_lines,skip_zero" </%doc>
|
||||
${",".join(photo.keywords)}
|
||||
@ -33,6 +33,9 @@ Sidecar: wedding.jpg.sidecar
|
||||
Rating: ★★★★★
|
||||
"""
|
||||
|
||||
PHOTO_UUID_NO_KEYWORD = "4D521201-92AC-43E5-8F7C-59BC41C37A96" # IMG_1997.CR2
|
||||
SIDECAR_FILENAME_NO_KEYWORD = "IMG_1997.CR2.txt"
|
||||
|
||||
|
||||
def test_export_sidecar_template_1():
|
||||
"""test basic export with --sidecar-template"""
|
||||
@ -61,6 +64,7 @@ def test_export_sidecar_template_1():
|
||||
sidecar_data = sidecar_file.read_text()
|
||||
assert sidecar_data == SIDECAR_DATA
|
||||
|
||||
|
||||
def test_export_sidecar_template_option_case():
|
||||
"""test basic export with --sidecar-template and option case insensitivity"""
|
||||
runner = CliRunner()
|
||||
@ -89,7 +93,6 @@ def test_export_sidecar_template_option_case():
|
||||
assert sidecar_data == SIDECAR_DATA
|
||||
|
||||
|
||||
|
||||
def test_export_sidecar_template_strip_whitespace():
|
||||
"""test basic export with --sidecar-template and STRIP_WHITESPACE = True"""
|
||||
runner = CliRunner()
|
||||
@ -182,6 +185,7 @@ def test_export_sidecar_template_strip_lines_strip_whitespace():
|
||||
)
|
||||
assert sidecar_data == sidecar_expected
|
||||
|
||||
|
||||
def test_export_sidecar_template_strip_lines_strip_whitespace_option_space():
|
||||
"""test basic export with --sidecar-template and STRIP_LINES = True and STRIP_WHITESPACE = True with space in option"""
|
||||
runner = CliRunner()
|
||||
@ -212,6 +216,7 @@ def test_export_sidecar_template_strip_lines_strip_whitespace_option_space():
|
||||
)
|
||||
assert sidecar_data == sidecar_expected
|
||||
|
||||
|
||||
def test_export_sidecar_template_update_no():
|
||||
"""test basic export with --sidecar-template and WRITE_SKIPPED = False, also test --cleanup"""
|
||||
runner = CliRunner()
|
||||
@ -505,3 +510,37 @@ def test_export_sidecar_template_full_library():
|
||||
],
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
|
||||
|
||||
def test_export_sidecar_template_skip_zero():
|
||||
"""test basic export with --sidecar-template with skip_zero option"""
|
||||
runner = CliRunner()
|
||||
cwd = os.getcwd()
|
||||
# pylint: disable=not-context-manager
|
||||
with runner.isolated_filesystem():
|
||||
result = runner.invoke(
|
||||
export,
|
||||
[
|
||||
"--library",
|
||||
os.path.join(cwd, PHOTOS_DB),
|
||||
".",
|
||||
"-V",
|
||||
"--uuid",
|
||||
PHOTO_UUID,
|
||||
"--uuid",
|
||||
PHOTO_UUID_NO_KEYWORD,
|
||||
"--sidecar-template",
|
||||
os.path.join(cwd, "tests", "custom_sidecar_zero.mako"),
|
||||
"{filepath}.txt",
|
||||
"strip_whitespace,strip_lines,skip_zero",
|
||||
],
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
|
||||
assert "Skipping empty sidecar file" in result.output
|
||||
|
||||
sidecar_file = pathlib.Path(SIDECAR_FILENAME)
|
||||
assert sidecar_file.exists()
|
||||
|
||||
sidecar_file = pathlib.Path(SIDECAR_FILENAME_NO_KEYWORD)
|
||||
assert not sidecar_file.exists()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user