From 975dfdffa9db39f620a9521ba89d372b0519fadb Mon Sep 17 00:00:00 2001 From: Bob Swift Date: Thu, 13 Jan 2022 23:58:35 -0700 Subject: [PATCH] PICARD-2382: Add option to keep duplicates in `$copymerge()` function. (#2020) --- picard/script/functions.py | 8 +++++--- test/test_script.py | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/picard/script/functions.py b/picard/script/functions.py index 091a1a023..a8587c73d 100644 --- a/picard/script/functions.py +++ b/picard/script/functions.py @@ -492,20 +492,22 @@ def func_copy(parser, new, old): @script_function(documentation=N_( - """`$copymerge(new,old)` + """`$copymerge(new,old[,keep_duplicates])` Merges metadata from variable `old` into `new`, removing duplicates and appending to the end, so retaining the original ordering. Like `$copy`, this will also copy multi-valued variables without flattening them. +If `keep_duplicates` is set, then the duplicates will not be removed from the result. + _Since Picard 1.0_""" )) -def func_copymerge(parser, new, old): +def func_copymerge(parser, new, old, keep_duplicates=False): new = normalize_tagname(new) old = normalize_tagname(old) newvals = parser.context.getall(new) oldvals = parser.context.getall(old) - parser.context[new] = uniqify(newvals + oldvals) + parser.context[new] = newvals + oldvals if keep_duplicates else uniqify(newvals + oldvals) return "" diff --git a/test/test_script.py b/test/test_script.py index e640ec4fb..736194d59 100644 --- a/test/test_script.py +++ b/test/test_script.py @@ -700,6 +700,27 @@ class ScriptParserTest(PicardTestCase): context["source"] = "sourceval" self._eval_and_check_copymerge(context, ["targetval", "sourceval"]) + def test_cmd_copymerge_empty_keepdupes(self): + context = Metadata() + context["target"] = ["tag1", "tag2", "tag1"] + context["source"] = ["tag2", "tag3", "tag2"] + self.parser.eval("$copymerge(target,source,)", context) + self.assertEqual(self.parser.context.getall("target"), ["tag1", "tag2", "tag3"]) + + def test_cmd_copymerge_keepdupes(self): + context = Metadata() + context["target"] = ["tag1", "tag2", "tag1"] + context["source"] = ["tag2", "tag3", "tag2"] + self.parser.eval("$copymerge(target,source,keep)", context) + self.assertEqual(self.parser.context.getall("target"), ["tag1", "tag2", "tag1", "tag2", "tag3", "tag2"]) + + def test_cmd_copymerge_nonlist_keepdupes(self): + context = Metadata() + context["target"] = "targetval" + context["source"] = "targetval" + self.parser.eval("$copymerge(target,source,keep)", context) + self.assertEqual(self.parser.context.getall("target"), ["targetval", "targetval"]) + def test_cmd_eq_any(self): self.assertScriptResultEquals("$eq_any(abc,def,ghi,jkl)", "") self.assertScriptResultEquals("$eq_any(abc,def,ghi,jkl,abc)", "1")