From eebf7f46ff31b522b5e04a2f5a4d0488c7f1b966 Mon Sep 17 00:00:00 2001 From: Sophist Date: Thu, 6 Mar 2014 14:29:41 +0000 Subject: [PATCH 01/12] Move swapprefix to Picard proper... ... and add similar delprefix --- contrib/plugins/swapprefix.py | 51 ----------------------------------- picard/script.py | 38 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 51 deletions(-) delete mode 100644 contrib/plugins/swapprefix.py diff --git a/contrib/plugins/swapprefix.py b/contrib/plugins/swapprefix.py deleted file mode 100644 index 20a59f802..000000000 --- a/contrib/plugins/swapprefix.py +++ /dev/null @@ -1,51 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Picard plugin swapprefix -# Adds the swapprefix tagger script function. -# This function offers the same functionality as the one in Foobar2000. -# See http://wiki.hydrogenaudio.org/index.php?title=Foobar2000:Title_Formatting_Reference -# -# Copyright (C) 2010 Philipp Wolfer -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -PLUGIN_NAME = 'swapprefix function' -PLUGIN_AUTHOR = 'Philipp Wolfer' -PLUGIN_DESCRIPTION = 'Moves the specified prefixes to the end of a string.' -PLUGIN_VERSION = "0.1" -PLUGIN_API_VERSIONS = ["0.9.0", "0.10", "0.11", "0.12", "0.15"] - -from picard.script import register_script_function -import re - - -def swapprefix(parser, text, *prefixes): - """ - Moves the specified prefixes to the end of text. - If no prefix is specified 'A' and 'The' are taken - as default. - """ - if not prefixes: - prefixes = ('A', 'The') - for prefix in prefixes: - pattern = re.compile('^' + re.escape(prefix) + '\s') - match = pattern.match(text) - if match: - rest = pattern.split(text)[1].strip() - if rest: - return ", ".join((rest, match.group(0).rstrip())) - return text - -register_script_function(swapprefix) diff --git a/picard/script.py b/picard/script.py index b7d7f157c..063d1127d 100644 --- a/picard/script.py +++ b/picard/script.py @@ -632,6 +632,42 @@ def func_truncate(parser, text, length): return text[:length].rstrip() +def func_swapprefix(parser, text, *prefixes): + """ + Moves the specified prefixes to the end of text. + If no prefix is specified 'A' and 'The' are taken + as default. + """ + if not prefixes: + prefixes = ('A', 'The') + for prefix in prefixes: + pattern = re.compile('^' + re.escape(prefix) + '\s') + match = pattern.match(text) + if match: + rest = pattern.split(text)[1].strip() + if rest: + return ", ".join((rest, match.group(0).rstrip())) + return text + + +def func_delprefix(parser, text, *prefixes): + """ + Deletes the specified prefixes. + If no prefix is specified 'A' and 'The' are taken + as default. + """ + if not prefixes: + prefixes = ('A', 'The') + for prefix in prefixes: + pattern = re.compile('^' + re.escape(prefix) + '\s') + match = pattern.match(text) + if match: + rest = pattern.split(text)[1].strip() + if rest: + return rest + return text + + register_script_function(func_if, "if", eval_args=False) register_script_function(func_if2, "if2", eval_args=False) register_script_function(func_noop, "noop", eval_args=False) @@ -675,3 +711,5 @@ register_script_function(func_firstalphachar, "firstalphachar") register_script_function(func_initials, "initials") register_script_function(func_firstwords, "firstwords") register_script_function(func_truncate, "truncate") +register_script_function(func_swapprefix, "swapprefix") +register_script_function(func_delprefix, "delprefix") From 2a0c939ad3b3319408b6a56bfe5c57bf2527e890 Mon Sep 17 00:00:00 2001 From: Sophist Date: Fri, 21 Mar 2014 01:32:37 +0000 Subject: [PATCH 02/12] Integrate a couple more plugins ... eq2 and ne2 by Brian Schweitzer. --- picard/script.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/picard/script.py b/picard/script.py index 063d1127d..7644c1d79 100644 --- a/picard/script.py +++ b/picard/script.py @@ -637,6 +637,7 @@ def func_swapprefix(parser, text, *prefixes): Moves the specified prefixes to the end of text. If no prefix is specified 'A' and 'The' are taken as default. + Integrated from the swapprefix plugin by Philipp Wolfer. """ if not prefixes: prefixes = ('A', 'The') @@ -655,6 +656,7 @@ def func_delprefix(parser, text, *prefixes): Deletes the specified prefixes. If no prefix is specified 'A' and 'The' are taken as default. + Modified by Sophist from the swapprefix plugin by Philipp Wolfer. """ if not prefixes: prefixes = ('A', 'The') @@ -668,6 +670,28 @@ def func_delprefix(parser, text, *prefixes): return text +def func_eq2(parser, x, *args): + """ + Compare a variable against more than one string. + Example: $if($eq2(%artist%,foo,bar,baz),$set(engineer,test)) + Integrated from the eq2 plugin by Brian Schweitzer. + """ + for i in (i for i in args if i == x): + return "1" + return '' + + +def func_ne2(parser, x, *args): + """ + Compare a variable against more than one string. + Example: $if($ne2(%artist%,foo,bar,baz),$set(engineer,test)) + Integrated from the ne2 plugin by Brian Schweitzer. + """ + for i in (i for i in args if i == x): + return "" + return "1" + + register_script_function(func_if, "if", eval_args=False) register_script_function(func_if2, "if2", eval_args=False) register_script_function(func_noop, "noop", eval_args=False) @@ -713,3 +737,5 @@ register_script_function(func_firstwords, "firstwords") register_script_function(func_truncate, "truncate") register_script_function(func_swapprefix, "swapprefix") register_script_function(func_delprefix, "delprefix") +register_script_function(func_eq2, "eq2") +register_script_function(func_ne2, "ne2") From 278f848ba93daf3c622e3cf2ab22fc209195dc0a Mon Sep 17 00:00:00 2001 From: Sophist Date: Fri, 21 Mar 2014 22:03:09 +0000 Subject: [PATCH 03/12] Add NEWS.txt item. --- NEWS.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.txt b/NEWS.txt index 950e37096..f373700f1 100644 --- a/NEWS.txt +++ b/NEWS.txt @@ -42,7 +42,7 @@ * Use MusicBrainz Server translations for release groups, medium formats and cover art types * Add checkbox to toggle debug at runtime in log/debug view dialog * Add a plugin to add Artist Official Homepage relationships to the website tag (ID3 WOAR tag) - + * Integrate plugin script functions $eq2, $ne2, $swapprefix and add $delprefix. Version 1.2 - 2013-03-30 From 601f6e45ebf940aea0bffdf6c6001776389f8937 Mon Sep 17 00:00:00 2001 From: Sophist Date: Sun, 23 Mar 2014 07:55:14 +0000 Subject: [PATCH 04/12] Extend eq / ne reather than new eq2 / ne2 --- picard/script.py | 56 +++++++++++++++++------------------------------- 1 file changed, 20 insertions(+), 36 deletions(-) diff --git a/picard/script.py b/picard/script.py index 7644c1d79..c1c14f407 100644 --- a/picard/script.py +++ b/picard/script.py @@ -522,21 +522,28 @@ def func_not(parser, x): return "" -def func_eq(parser, x, y): - """Returns true, if ``x`` equals ``y``.""" - if x == y: +def func_eq(parser, x, *args): + """ + Return True if one string matches one or more other strings. + $eq(a,b,c ...) is functionally equivalent to $or($eq(a,b),$eq(a,c) ...) + Example: $if($eq(%artist%,foo,bar,baz),$set(engineer,test)) + Idea from the eq2 plugin by Brian Schweitzer. + """ + for i in (i for i in args if i == x): return "1" - else: + return '' + + +def func_ne(parser, x, *args): + """ + Return True if one string doesn't match any of one or more other strings. + $ne(a,b,c ...) is functionally equivalent to $and($ne(a,b),$ne(a,c) ...) + Example: $if($ne(%artist%,foo,bar,baz),$set(engineer,test)) + Idea from the ne2 plugin by Brian Schweitzer. + """ + for i in (i for i in args if i == x): return "" - - -def func_ne(parser, x, y): - """Returns true, if ``x`` not equals ``y``.""" - if x != y: - return "1" - else: - return "" - + return "1" def func_lt(parser, x, y): """Returns true, if ``x`` is lower than ``y``.""" @@ -670,27 +677,6 @@ def func_delprefix(parser, text, *prefixes): return text -def func_eq2(parser, x, *args): - """ - Compare a variable against more than one string. - Example: $if($eq2(%artist%,foo,bar,baz),$set(engineer,test)) - Integrated from the eq2 plugin by Brian Schweitzer. - """ - for i in (i for i in args if i == x): - return "1" - return '' - - -def func_ne2(parser, x, *args): - """ - Compare a variable against more than one string. - Example: $if($ne2(%artist%,foo,bar,baz),$set(engineer,test)) - Integrated from the ne2 plugin by Brian Schweitzer. - """ - for i in (i for i in args if i == x): - return "" - return "1" - register_script_function(func_if, "if", eval_args=False) register_script_function(func_if2, "if2", eval_args=False) @@ -737,5 +723,3 @@ register_script_function(func_firstwords, "firstwords") register_script_function(func_truncate, "truncate") register_script_function(func_swapprefix, "swapprefix") register_script_function(func_delprefix, "delprefix") -register_script_function(func_eq2, "eq2") -register_script_function(func_ne2, "ne2") From 4ec996bcd6afde964177bf5c0b6ecfea99b860c7 Mon Sep 17 00:00:00 2001 From: Sophist Date: Sun, 23 Mar 2014 12:02:08 +0000 Subject: [PATCH 05/12] Revert reuse of eq/ne and rename again --- picard/script.py | 58 +++++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/picard/script.py b/picard/script.py index c1c14f407..1411edaaf 100644 --- a/picard/script.py +++ b/picard/script.py @@ -522,28 +522,21 @@ def func_not(parser, x): return "" -def func_eq(parser, x, *args): - """ - Return True if one string matches one or more other strings. - $eq(a,b,c ...) is functionally equivalent to $or($eq(a,b),$eq(a,c) ...) - Example: $if($eq(%artist%,foo,bar,baz),$set(engineer,test)) - Idea from the eq2 plugin by Brian Schweitzer. - """ - for i in (i for i in args if i == x): +def func_eq(parser, x, y): + """Returns true, if ``x`` equals ``y``.""" + if x == y: return "1" - return '' - - -def func_ne(parser, x, *args): - """ - Return True if one string doesn't match any of one or more other strings. - $ne(a,b,c ...) is functionally equivalent to $and($ne(a,b),$ne(a,c) ...) - Example: $if($ne(%artist%,foo,bar,baz),$set(engineer,test)) - Idea from the ne2 plugin by Brian Schweitzer. - """ - for i in (i for i in args if i == x): + else: return "" - return "1" + + +def func_ne(parser, x, y): + """Returns true, if ``x`` not equals ``y``.""" + if x != y: + return "1" + else: + return "" + def func_lt(parser, x, y): """Returns true, if ``x`` is lower than ``y``.""" @@ -677,6 +670,29 @@ def func_delprefix(parser, text, *prefixes): return text +def func_eq_any(parser, x, *args): + """ + Return True if one string matches any of one or more other strings. + $eq_any(a,b,c ...) is functionally equivalent to $or($eq(a,b),$eq(a,c) ...) + Example: $if_any($eq(%artist%,foo,bar,baz),$set(engineer,test)) + Idea from the eq2 plugin by Brian Schweitzer. + """ + for i in (i for i in args if i == x): + return "1" + return '' + + +def func_ne_all(parser, x, *args): + """ + Return True if one string doesn't match all of one or more other strings. + $ne_all(a,b,c ...) is functionally equivalent to $and($ne(a,b),$ne(a,c) ...) + Example: $if($ne_all(%artist%,foo,bar,baz),$set(engineer,test)) + Idea from the ne2 plugin by Brian Schweitzer. + """ + for i in (i for i in args if i == x): + return "" + return "1" + register_script_function(func_if, "if", eval_args=False) register_script_function(func_if2, "if2", eval_args=False) @@ -723,3 +739,5 @@ register_script_function(func_firstwords, "firstwords") register_script_function(func_truncate, "truncate") register_script_function(func_swapprefix, "swapprefix") register_script_function(func_delprefix, "delprefix") +register_script_function(func_eq_any, "eq2") +register_script_function(func_ne_all, "ne2") From fa004dc9c9e0dbf262704d1c9d9159e9f4f7c5a1 Mon Sep 17 00:00:00 2001 From: Sophist Date: Sun, 23 Mar 2014 21:06:46 +0000 Subject: [PATCH 06/12] Adopt simplification proposed by mineo --- picard/script.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/picard/script.py b/picard/script.py index 1411edaaf..fe8154dcb 100644 --- a/picard/script.py +++ b/picard/script.py @@ -689,9 +689,7 @@ def func_ne_all(parser, x, *args): Example: $if($ne_all(%artist%,foo,bar,baz),$set(engineer,test)) Idea from the ne2 plugin by Brian Schweitzer. """ - for i in (i for i in args if i == x): - return "" - return "1" + return func_not(parser, func_eq_any(parser, x, *args)) register_script_function(func_if, "if", eval_args=False) From 6bb175bd9abb3bee8618576bb999c13397ef4fc6 Mon Sep 17 00:00:00 2001 From: Sophist Date: Sun, 23 Mar 2014 22:37:27 +0000 Subject: [PATCH 07/12] Fix typo in function documentation. --- picard/script.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/picard/script.py b/picard/script.py index fe8154dcb..292761c52 100644 --- a/picard/script.py +++ b/picard/script.py @@ -674,7 +674,7 @@ def func_eq_any(parser, x, *args): """ Return True if one string matches any of one or more other strings. $eq_any(a,b,c ...) is functionally equivalent to $or($eq(a,b),$eq(a,c) ...) - Example: $if_any($eq(%artist%,foo,bar,baz),$set(engineer,test)) + Example: $if($eq_any(%artist%,foo,bar,baz),$set(engineer,test)) Idea from the eq2 plugin by Brian Schweitzer. """ for i in (i for i in args if i == x): From cc6f9b679cf8b15040fd3dde67f62c33a60442ec Mon Sep 17 00:00:00 2001 From: Sophist Date: Sun, 23 Mar 2014 23:58:32 +0000 Subject: [PATCH 08/12] Use regex, fix typos. --- picard/script.py | 54 +++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/picard/script.py b/picard/script.py index 292761c52..82f055b3e 100644 --- a/picard/script.py +++ b/picard/script.py @@ -635,39 +635,37 @@ def func_truncate(parser, text, length): def func_swapprefix(parser, text, *prefixes): """ Moves the specified prefixes to the end of text. - If no prefix is specified 'A' and 'The' are taken - as default. - Integrated from the swapprefix plugin by Philipp Wolfer. + If no prefix is specified 'A' and 'The' are taken as default. + Inspired by the swapprefix plugin by Philipp Wolfer. """ - if not prefixes: - prefixes = ('A', 'The') - for prefix in prefixes: - pattern = re.compile('^' + re.escape(prefix) + '\s') - match = pattern.match(text) - if match: - rest = pattern.split(text)[1].strip() - if rest: - return ", ".join((rest, match.group(0).rstrip())) + text, prefix = _delete_prefix(parser, text, *prefixes) + if prefix != '': + return text + ', ' + prefix return text - def func_delprefix(parser, text, *prefixes): """ Deletes the specified prefixes. - If no prefix is specified 'A' and 'The' are taken - as default. - Modified by Sophist from the swapprefix plugin by Philipp Wolfer. + If no prefix is specified 'A' and 'The' are taken as default. + Inspired by the swapprefix plugin by Philipp Wolfer. + """ + return _delete_prefix(parser, text, *prefixes)[0] + +def _delete_prefix(parser, text, *prefixes): + """ + Worker function to deletes the specified prefixes. + Returns remaining string and deleted part separately. + If no prefix is specified 'A' and 'The' used. + Inspired by the swapprefix plugin by Philipp Wolfer. """ if not prefixes: prefixes = ('A', 'The') - for prefix in prefixes: - pattern = re.compile('^' + re.escape(prefix) + '\s') - match = pattern.match(text) - if match: - rest = pattern.split(text)[1].strip() - if rest: - return rest - return text + text = text.strip() + match = re.match('(' + r'\s+)|('.join(prefixes) + r'\s+)', text) + if match: + pref = match.group() + return text[len(pref):], pref.strip() + return text, '' def func_eq_any(parser, x, *args): @@ -735,7 +733,7 @@ register_script_function(func_firstalphachar, "firstalphachar") register_script_function(func_initials, "initials") register_script_function(func_firstwords, "firstwords") register_script_function(func_truncate, "truncate") -register_script_function(func_swapprefix, "swapprefix") -register_script_function(func_delprefix, "delprefix") -register_script_function(func_eq_any, "eq2") -register_script_function(func_ne_all, "ne2") +register_script_function(func_swapprefix, "swapprefix", check_argcount=False) +register_script_function(func_delprefix, "delprefix", check_argcount=False) +register_script_function(func_eq_any, "eq_any", check_argcount=False) +register_script_function(func_ne_all, "ne_all", check_argcount=False) From 9291fcbb466bb1bf77f56fb0653469d082adb67f Mon Sep 17 00:00:00 2001 From: Sophist Date: Sun, 23 Mar 2014 23:51:58 +0000 Subject: [PATCH 09/12] Add tests --- test/test_script.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/test_script.py b/test/test_script.py index c79095374..0ed67d7b7 100644 --- a/test/test_script.py +++ b/test/test_script.py @@ -254,3 +254,31 @@ class ScriptParserTest(unittest.TestCase): context["target"] = "targetval" context["source"] = "sourceval" self._eval_and_check_copymerge(context, ["targetval", "sourceval"]) + + def test_cmd_eq_any(self): + self.assertEqual(self.parser.eval("$eq_any(abc,def,ghi,jkl)"), "") + self.assertEqual(self.parser.eval("$eq_any(abc,def,ghi,jkl,abc)"), "1") + + def test_cmd_ne_all(self): + self.assertEqual(self.parser.eval("$ne_all(abc,def,ghi,jkl)"), "1") + self.assertEqual(self.parser.eval("$ne_all(abc,def,ghi,jkl,abc)"), "") + + def test_cmd_swapprefix(self): + self.assertEqual(self.parser.eval("$swapprefix(A stitch in time)"), "stitch in time, A") + self.assertEqual(self.parser.eval("$swapprefix(The quick brown fox)"), "quick brown fox, The") + self.assertEqual(self.parser.eval("$swapprefix(How now brown cow)"), "How now brown cow") + self.assertEqual(self.parser.eval("$swapprefix(When the red red robin)"), "When the red red robin") + self.assertEqual(self.parser.eval("$swapprefix(A stitch in time,How,When,Who)"), "A stitch in time") + self.assertEqual(self.parser.eval("$swapprefix(The quick brown fox,How,When,Who)"), "The quick brown fox") + self.assertEqual(self.parser.eval("$swapprefix(How now brown cow,How,When,Who)"), "now brown cow, How") + self.assertEqual(self.parser.eval("$swapprefix(When the red red robin,How,When,Who)"), "the red red robin, When") + + def test_cmd_delprefix(self): + self.assertEqual(self.parser.eval("$delprefix(A stitch in time)"), "stitch in time") + self.assertEqual(self.parser.eval("$delprefix(The quick brown fox)"), "quick brown fox") + self.assertEqual(self.parser.eval("$delprefix(How now brown cow)"), "How now brown cow") + self.assertEqual(self.parser.eval("$delprefix(When the red red robin)"), "When the red red robin") + self.assertEqual(self.parser.eval("$delprefix(A stitch in time,How,When,Who)"), "A stitch in time") + self.assertEqual(self.parser.eval("$delprefix(The quick brown fox,How,When,Who)"), "The quick brown fox") + self.assertEqual(self.parser.eval("$delprefix(How now brown cow,How,When,Who)"), "now brown cow") + self.assertEqual(self.parser.eval("$delprefix(When the red red robin,How,When,Who)"), "the red red robin") From 6a7446a1594acbb97469743eda3c56839a40a1d4 Mon Sep 17 00:00:00 2001 From: Sophist Date: Mon, 24 Mar 2014 00:02:26 +0000 Subject: [PATCH 10/12] Adjust NEWS.txt --- NEWS.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.txt b/NEWS.txt index f373700f1..881f1bd19 100644 --- a/NEWS.txt +++ b/NEWS.txt @@ -42,7 +42,7 @@ * Use MusicBrainz Server translations for release groups, medium formats and cover art types * Add checkbox to toggle debug at runtime in log/debug view dialog * Add a plugin to add Artist Official Homepage relationships to the website tag (ID3 WOAR tag) - * Integrate plugin script functions $eq2, $ne2, $swapprefix and add $delprefix. + * Add integrated functions $eq_any, $ne_all, $swapprefix and $delprefix. Version 1.2 - 2013-03-30 From dd03540f1586a2a9236e229b83434552d101e415 Mon Sep 17 00:00:00 2001 From: Sophist Date: Mon, 24 Mar 2014 20:07:17 +0000 Subject: [PATCH 11/12] Add eq_all, ne_any as requested by Freso --- NEWS.txt | 2 +- picard/script.py | 42 ++++++++++++++++++++++++++++++++++-------- test/test_script.py | 8 ++++++++ 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/NEWS.txt b/NEWS.txt index 881f1bd19..8383c0420 100644 --- a/NEWS.txt +++ b/NEWS.txt @@ -42,7 +42,7 @@ * Use MusicBrainz Server translations for release groups, medium formats and cover art types * Add checkbox to toggle debug at runtime in log/debug view dialog * Add a plugin to add Artist Official Homepage relationships to the website tag (ID3 WOAR tag) - * Add integrated functions $eq_any, $ne_all, $swapprefix and $delprefix. + * Add integrated functions $eq_any, $ne_all, $eq_all, $ne_any, $swapprefix and $delprefix. Version 1.2 - 2013-03-30 diff --git a/picard/script.py b/picard/script.py index 82f055b3e..6dab91ab6 100644 --- a/picard/script.py +++ b/picard/script.py @@ -636,8 +636,9 @@ def func_swapprefix(parser, text, *prefixes): """ Moves the specified prefixes to the end of text. If no prefix is specified 'A' and 'The' are taken as default. - Inspired by the swapprefix plugin by Philipp Wolfer. """ + # Inspired by the swapprefix plugin by Philipp Wolfer. + text, prefix = _delete_prefix(parser, text, *prefixes) if prefix != '': return text + ', ' + prefix @@ -647,8 +648,9 @@ def func_delprefix(parser, text, *prefixes): """ Deletes the specified prefixes. If no prefix is specified 'A' and 'The' are taken as default. - Inspired by the swapprefix plugin by Philipp Wolfer. """ + # Inspired by the swapprefix plugin by Philipp Wolfer. + return _delete_prefix(parser, text, *prefixes)[0] def _delete_prefix(parser, text, *prefixes): @@ -656,8 +658,9 @@ def _delete_prefix(parser, text, *prefixes): Worker function to deletes the specified prefixes. Returns remaining string and deleted part separately. If no prefix is specified 'A' and 'The' used. - Inspired by the swapprefix plugin by Philipp Wolfer. """ + # Inspired by the swapprefix plugin by Philipp Wolfer. + if not prefixes: prefixes = ('A', 'The') text = text.strip() @@ -673,23 +676,44 @@ def func_eq_any(parser, x, *args): Return True if one string matches any of one or more other strings. $eq_any(a,b,c ...) is functionally equivalent to $or($eq(a,b),$eq(a,c) ...) Example: $if($eq_any(%artist%,foo,bar,baz),$set(engineer,test)) - Idea from the eq2 plugin by Brian Schweitzer. """ - for i in (i for i in args if i == x): - return "1" - return '' + # Inspired by the eq2 plugin by Brian Schweitzer. + for i in (i for i in args if x == i): + return '1' + return '' def func_ne_all(parser, x, *args): """ Return True if one string doesn't match all of one or more other strings. $ne_all(a,b,c ...) is functionally equivalent to $and($ne(a,b),$ne(a,c) ...) Example: $if($ne_all(%artist%,foo,bar,baz),$set(engineer,test)) - Idea from the ne2 plugin by Brian Schweitzer. """ + # Inspired by the ne2 plugin by Brian Schweitzer. + return func_not(parser, func_eq_any(parser, x, *args)) +def func_eq_all(parser, x, *args): + """ + Return True if all string are equal. + $eq_all(a,b,c ...) is functionally equivalent to $and($eq(a,b),$eq(a,c) ...) + Example: $if($eq_all(%albumartist%,%artist%,Justin Bieber),$set(engineer,Meat Loaf)) + """ + for i in (i for i in args if x != i): + return '' + return '1' + + +def func_ne_any(parser, x, *args): + """ + Return True if all strings are not equal. + $ne_any(a,b,c ...) is functionally equivalent to $or($ne(a,b),$ne(a,c) ...) + Example: $if($ne_any(%albumartist%,%trackartist%,%composer%),$set(lyricist,%composer%)) + """ + return func_not(parser, func_eq_all(parser, x, *args)) + + register_script_function(func_if, "if", eval_args=False) register_script_function(func_if2, "if2", eval_args=False) register_script_function(func_noop, "noop", eval_args=False) @@ -737,3 +761,5 @@ register_script_function(func_swapprefix, "swapprefix", check_argcount=False) register_script_function(func_delprefix, "delprefix", check_argcount=False) register_script_function(func_eq_any, "eq_any", check_argcount=False) register_script_function(func_ne_all, "ne_all", check_argcount=False) +register_script_function(func_eq_all, "eq_all", check_argcount=False) +register_script_function(func_ne_any, "ne_any", check_argcount=False) diff --git a/test/test_script.py b/test/test_script.py index 0ed67d7b7..a0840dd81 100644 --- a/test/test_script.py +++ b/test/test_script.py @@ -263,6 +263,14 @@ class ScriptParserTest(unittest.TestCase): self.assertEqual(self.parser.eval("$ne_all(abc,def,ghi,jkl)"), "1") self.assertEqual(self.parser.eval("$ne_all(abc,def,ghi,jkl,abc)"), "") + def test_cmd_eq_all(self): + self.assertEqual(self.parser.eval("$eq_all(abc,abc,abc,abc)"), "1") + self.assertEqual(self.parser.eval("$eq_all(abc,abc,def,ghi)"), "") + + def test_cmd_ne_any(self): + self.assertEqual(self.parser.eval("$ne_any(abc,abc,abc,abc)"), "") + self.assertEqual(self.parser.eval("$ne_any(abc,abc,def,ghi)"), "1") + def test_cmd_swapprefix(self): self.assertEqual(self.parser.eval("$swapprefix(A stitch in time)"), "stitch in time, A") self.assertEqual(self.parser.eval("$swapprefix(The quick brown fox)"), "quick brown fox, The") From 687a3766717cf12f3795fbf70454c1a2c16d652c Mon Sep 17 00:00:00 2001 From: Sophist Date: Tue, 25 Mar 2014 06:59:42 +0000 Subject: [PATCH 12/12] Remove unnecessary for loops (bitmap) --- picard/script.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/picard/script.py b/picard/script.py index 6dab91ab6..b0e4145ca 100644 --- a/picard/script.py +++ b/picard/script.py @@ -678,10 +678,7 @@ def func_eq_any(parser, x, *args): Example: $if($eq_any(%artist%,foo,bar,baz),$set(engineer,test)) """ # Inspired by the eq2 plugin by Brian Schweitzer. - - for i in (i for i in args if x == i): - return '1' - return '' + return '1' if x in args else '' def func_ne_all(parser, x, *args): """ @@ -690,8 +687,7 @@ def func_ne_all(parser, x, *args): Example: $if($ne_all(%artist%,foo,bar,baz),$set(engineer,test)) """ # Inspired by the ne2 plugin by Brian Schweitzer. - - return func_not(parser, func_eq_any(parser, x, *args)) + return '1' if x not in args else '' def func_eq_all(parser, x, *args): @@ -700,11 +696,11 @@ def func_eq_all(parser, x, *args): $eq_all(a,b,c ...) is functionally equivalent to $and($eq(a,b),$eq(a,c) ...) Example: $if($eq_all(%albumartist%,%artist%,Justin Bieber),$set(engineer,Meat Loaf)) """ - for i in (i for i in args if x != i): - return '' + for i in args: + if x != i: + return '' return '1' - def func_ne_any(parser, x, *args): """ Return True if all strings are not equal.