From 994d6490f5bc21f3b79281d5a7e67d855cd7ba35 Mon Sep 17 00:00:00 2001 From: Nikolai Prokoschenko Date: Sun, 4 Jan 2009 17:47:48 +0100 Subject: [PATCH 1/5] implement get_num_matched_tracks --- picard/album.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/picard/album.py b/picard/album.py index 8065b66ac..7cf69df70 100644 --- a/picard/album.py +++ b/picard/album.py @@ -394,6 +394,13 @@ class Album(DataObject, Item): def can_refresh(self): return True + def get_num_matched_tracks(self): + num = 0 + for track in self.tracks: + if track.is_linked(): + num += 1 + return num + def get_num_unmatched_files(self): return len(self.unmatched_files.files) From 78bd8a6b7f58cd21c924ff71c941a3721f063dd4 Mon Sep 17 00:00:00 2001 From: Nikolai Prokoschenko Date: Sun, 4 Jan 2009 17:48:37 +0100 Subject: [PATCH 2/5] make file object available to the script evaluator --- picard/file.py | 2 +- picard/script.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/picard/file.py b/picard/file.py index c0e858d47..9bbda2704 100644 --- a/picard/file.py +++ b/picard/file.py @@ -188,7 +188,7 @@ class File(LockableObject, Item): for name in metadata.keys(): if isinstance(metadata[name], basestring): metadata[name] = sanitize_filename(metadata[name]) - filename = ScriptParser().eval(format, metadata) + filename = ScriptParser().eval(format, metadata, self) # replace incompatible characters if settings["windows_compatible_filenames"] or sys.platform == "win32": filename = replace_win32_incompat(filename) diff --git a/picard/script.py b/picard/script.py index 2b7ea6da4..24ba448ba 100644 --- a/picard/script.py +++ b/picard/script.py @@ -218,9 +218,10 @@ Grammar: self.load_functions() return self.parse_expression(True)[0] - def eval(self, script, context={}): + def eval(self, script, context={}, file=None): """Parse and evaluate the script.""" self.context = context + self.file = file self.load_functions() key = hash(script) if key not in ScriptParser._cache: From b17f0c01dac7e8a2c18f2fcae0b05ce8021befcc Mon Sep 17 00:00:00 2001 From: Nikolai Prokoschenko Date: Sun, 4 Jan 2009 17:49:02 +0100 Subject: [PATCH 3/5] add matchedtracks plugin --- contrib/plugins/matchedtracks.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 contrib/plugins/matchedtracks.py diff --git a/contrib/plugins/matchedtracks.py b/contrib/plugins/matchedtracks.py new file mode 100644 index 000000000..1b9c80413 --- /dev/null +++ b/contrib/plugins/matchedtracks.py @@ -0,0 +1,17 @@ +PLUGIN_NAME = 'Number of matched tracks' +PLUGIN_AUTHOR = 'Nikolai Prokoschenko' +PLUGIN_DESCRIPTION = '''Provides a scripting function called $matchedtracks, which returns the number of tracks matched in an album. Useful for distinguishing complete and incomplete releases''' +PLUGIN_VERSION = "0.1" +PLUGIN_API_VERSIONS = ["0.9.0", "0.10"] + + +from picard.script import register_script_function + +def matchedtracks(parser, arg): + # FIXME arg is actually not needed, why is it used by eval? + if parser.file: + if parser.file.parent: + return str(parser.file.parent.album.get_num_matched_tracks()) + return "0" + +register_script_function(matchedtracks) From 8e77bf588c71a8cadb3952f09921acbc34c545b4 Mon Sep 17 00:00:00 2001 From: Nikolai Prokoschenko Date: Sun, 4 Jan 2009 18:08:08 +0100 Subject: [PATCH 4/5] now included in picard, not a plugin anymore --- contrib/plugins/matchedtracks.py | 17 ----------------- picard/script.py | 8 ++++++++ 2 files changed, 8 insertions(+), 17 deletions(-) delete mode 100644 contrib/plugins/matchedtracks.py diff --git a/contrib/plugins/matchedtracks.py b/contrib/plugins/matchedtracks.py deleted file mode 100644 index 1b9c80413..000000000 --- a/contrib/plugins/matchedtracks.py +++ /dev/null @@ -1,17 +0,0 @@ -PLUGIN_NAME = 'Number of matched tracks' -PLUGIN_AUTHOR = 'Nikolai Prokoschenko' -PLUGIN_DESCRIPTION = '''Provides a scripting function called $matchedtracks, which returns the number of tracks matched in an album. Useful for distinguishing complete and incomplete releases''' -PLUGIN_VERSION = "0.1" -PLUGIN_API_VERSIONS = ["0.9.0", "0.10"] - - -from picard.script import register_script_function - -def matchedtracks(parser, arg): - # FIXME arg is actually not needed, why is it used by eval? - if parser.file: - if parser.file.parent: - return str(parser.file.parent.album.get_num_matched_tracks()) - return "0" - -register_script_function(matchedtracks) diff --git a/picard/script.py b/picard/script.py index 24ba448ba..8300f427c 100644 --- a/picard/script.py +++ b/picard/script.py @@ -447,6 +447,13 @@ def func_performer(parser, pattern="", join=", "): values.append(value) return join.join(values) +def func_matchedtracks(parser): + if parser.file: + if parser.file.parent: + return str(parser.file.parent.album.get_num_matched_tracks()) + return "0" + + 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) @@ -482,3 +489,4 @@ register_script_function(func_in, "in") register_script_function(func_copy, "copy") register_script_function(func_len, "len") register_script_function(func_performer, "performer") +register_script_function(func_matchedtracks, "matchedtracks") From fca91f432558924592c463bc97fc143586550abe Mon Sep 17 00:00:00 2001 From: Nikolai Prokoschenko Date: Sun, 4 Jan 2009 18:17:06 +0100 Subject: [PATCH 5/5] fixed argument list --- picard/script.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/picard/script.py b/picard/script.py index 8300f427c..ccb51af28 100644 --- a/picard/script.py +++ b/picard/script.py @@ -447,7 +447,7 @@ def func_performer(parser, pattern="", join=", "): values.append(value) return join.join(values) -def func_matchedtracks(parser): +def func_matchedtracks(parser, arg): if parser.file: if parser.file.parent: return str(parser.file.parent.album.get_num_matched_tracks())