mirror of
https://github.com/fergalmoran/picard.git
synced 2026-02-05 23:33:59 +00:00
PICARD-2382: Add option to keep duplicates in $copymerge() function. (#2020)
This commit is contained in:
@@ -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 ""
|
||||
|
||||
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user