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) 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..ccb51af28 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: @@ -446,6 +447,13 @@ def func_performer(parser, pattern="", join=", "): values.append(value) return join.join(values) +def func_matchedtracks(parser, arg): + 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) @@ -481,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")