From 91e5bdbd6c0538e32d7ef4edf9998929467180e2 Mon Sep 17 00:00:00 2001 From: Sambhav Kothari Date: Thu, 29 Dec 2016 05:56:21 +0530 Subject: [PATCH 1/7] Implement case-insensitive move_additional_files --- picard/file.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/picard/file.py b/picard/file.py index 412fa089c..567a6d05b 100644 --- a/picard/file.py +++ b/picard/file.py @@ -18,7 +18,8 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -import glob +import fnmatch +import os import os.path import shutil import sys @@ -353,16 +354,17 @@ class File(QtCore.QObject, Item): patterns = encode_filename(config.setting["move_additional_files_pattern"]) patterns = filter(bool, [p.strip() for p in patterns.split()]) for pattern in patterns: - # FIXME glob1 is not documented, maybe we need our own implementation? - for old_file in glob.glob1(old_path, pattern): - new_file = os.path.join(new_path, old_file) - old_file = os.path.join(old_path, old_file) - # FIXME we shouldn't do this from a thread! - if self.tagger.files.get(decode_filename(old_file)): - log.debug("File loaded in the tagger, not moving %r", old_file) - continue - log.debug("Moving %r to %r", old_file, new_file) - shutil.move(old_file, new_file) + pattern_regex = re.compile(fnmatch.translate(pattern), re.IGNORECASE) + for old_file in os.listdir(old_path): + if pattern_regex.match(old_file): + new_file = os.path.join(new_path, old_file) + old_file = os.path.join(old_path, old_file) + # FIXME we shouldn't do this from a thread! + if self.tagger.files.get(decode_filename(old_file)): + log.debug("File loaded in the tagger, not moving %r", old_file) + continue + log.debug("Moving %r to %r", old_file, new_file) + shutil.move(old_file, new_file) def remove(self, from_parent=True): if from_parent and self.parent: From 31d3fcff78bb1e85e371b0b50ee6572a2bdabe6e Mon Sep 17 00:00:00 2001 From: Sambhav Kothari Date: Thu, 29 Dec 2016 17:16:33 +0530 Subject: [PATCH 2/7] Make new_file variable ony after checking if old file exists --- picard/file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/picard/file.py b/picard/file.py index 567a6d05b..e1e93303c 100644 --- a/picard/file.py +++ b/picard/file.py @@ -357,12 +357,12 @@ class File(QtCore.QObject, Item): pattern_regex = re.compile(fnmatch.translate(pattern), re.IGNORECASE) for old_file in os.listdir(old_path): if pattern_regex.match(old_file): - new_file = os.path.join(new_path, old_file) old_file = os.path.join(old_path, old_file) # FIXME we shouldn't do this from a thread! if self.tagger.files.get(decode_filename(old_file)): log.debug("File loaded in the tagger, not moving %r", old_file) continue + new_file = os.path.join(new_path, old_file) log.debug("Moving %r to %r", old_file, new_file) shutil.move(old_file, new_file) From 83dfe51b23c58bc336910aa94ba98c077f80a8ca Mon Sep 17 00:00:00 2001 From: Sambhav Kothari Date: Thu, 29 Dec 2016 19:22:52 +0530 Subject: [PATCH 3/7] Add checks for path names and ignore hidden files --- picard/file.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/picard/file.py b/picard/file.py index e1e93303c..84624e51b 100644 --- a/picard/file.py +++ b/picard/file.py @@ -355,7 +355,13 @@ class File(QtCore.QObject, Item): patterns = filter(bool, [p.strip() for p in patterns.split()]) for pattern in patterns: pattern_regex = re.compile(fnmatch.translate(pattern), re.IGNORECASE) - for old_file in os.listdir(old_path): + try: + names = os.listdir(old_path) + except os.error: + return + if pattern[0] != '.': + names = filter(lambda x: x[0] != '.', names) + for old_file in names: if pattern_regex.match(old_file): old_file = os.path.join(old_path, old_file) # FIXME we shouldn't do this from a thread! From 3b24f76445ea378d51d98d0fae1c1b3fe7b24b02 Mon Sep 17 00:00:00 2001 From: Sambhav Kothari Date: Mon, 2 Jan 2017 16:13:54 +0530 Subject: [PATCH 4/7] Add error to log and refactor code --- picard/file.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/picard/file.py b/picard/file.py index 84624e51b..5b67d2c32 100644 --- a/picard/file.py +++ b/picard/file.py @@ -353,15 +353,18 @@ class File(QtCore.QObject, Item): new_path = encode_filename(os.path.dirname(new_filename)) patterns = encode_filename(config.setting["move_additional_files_pattern"]) patterns = filter(bool, [p.strip() for p in patterns.split()]) + try: + names = os.listdir(old_path) + except os.error: + log.debug("Error: {} directory not found".format(old_path)) + return + filtered_names = filter(lambda x: x[0] != '.', names) for pattern in patterns: pattern_regex = re.compile(fnmatch.translate(pattern), re.IGNORECASE) - try: - names = os.listdir(old_path) - except os.error: - return + file_names = names if pattern[0] != '.': - names = filter(lambda x: x[0] != '.', names) - for old_file in names: + file_names = filtered_names + for old_file in file_names: if pattern_regex.match(old_file): old_file = os.path.join(old_path, old_file) # FIXME we shouldn't do this from a thread! From ca144eb9219defc69c735ee34c910a5a370cb945 Mon Sep 17 00:00:00 2001 From: Sambhav Kothari Date: Mon, 2 Jan 2017 18:23:52 +0530 Subject: [PATCH 5/7] Show error messages in error instead of debug log --- picard/file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/picard/file.py b/picard/file.py index 5b67d2c32..c98bcf126 100644 --- a/picard/file.py +++ b/picard/file.py @@ -356,7 +356,7 @@ class File(QtCore.QObject, Item): try: names = os.listdir(old_path) except os.error: - log.debug("Error: {} directory not found".format(old_path)) + log.error("Error: {} directory not found".format(old_path)) return filtered_names = filter(lambda x: x[0] != '.', names) for pattern in patterns: From ded108f4a4bbfe06c0aed00a04a08550fff254ff Mon Sep 17 00:00:00 2001 From: Sambhav Kothari Date: Tue, 3 Jan 2017 22:38:30 +0530 Subject: [PATCH 6/7] Change the ui label to reflect case insensitivity --- picard/ui/ui_options_renaming.py | 2 +- ui/options_renaming.ui | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/picard/ui/ui_options_renaming.py b/picard/ui/ui_options_renaming.py index 843b83846..0860fa7d0 100644 --- a/picard/ui/ui_options_renaming.py +++ b/picard/ui/ui_options_renaming.py @@ -163,7 +163,7 @@ class Ui_RenamingOptionsPage(object): self.move_files.setTitle(_("Move files when saving")) self.label.setText(_("Destination directory:")) self.move_files_to_browse.setText(_("Browse...")) - self.move_additional_files.setText(_("Move additional files:")) + self.move_additional_files.setText(_("Move additional files(case insensitive):")) self.delete_empty_dirs.setText(_("Delete empty directories")) self.rename_files.setTitle(_("Rename files when saving")) self.ascii_filenames.setText(_("Replace non-ASCII characters")) diff --git a/ui/options_renaming.ui b/ui/options_renaming.ui index 917483926..94a70752d 100644 --- a/ui/options_renaming.ui +++ b/ui/options_renaming.ui @@ -72,7 +72,7 @@ false - Move additional files: + Move additional files(case insensitive): From 2e50518047d54e6d951bfe0fc5cc9414b4310744 Mon Sep 17 00:00:00 2001 From: Sambhav Kothari Date: Wed, 4 Jan 2017 17:24:54 +0530 Subject: [PATCH 7/7] Add space before parentheses --- picard/ui/ui_options_renaming.py | 2 +- ui/options_renaming.ui | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/picard/ui/ui_options_renaming.py b/picard/ui/ui_options_renaming.py index 0860fa7d0..a5bb24a2d 100644 --- a/picard/ui/ui_options_renaming.py +++ b/picard/ui/ui_options_renaming.py @@ -163,7 +163,7 @@ class Ui_RenamingOptionsPage(object): self.move_files.setTitle(_("Move files when saving")) self.label.setText(_("Destination directory:")) self.move_files_to_browse.setText(_("Browse...")) - self.move_additional_files.setText(_("Move additional files(case insensitive):")) + self.move_additional_files.setText(_("Move additional files (case insensitive):")) self.delete_empty_dirs.setText(_("Delete empty directories")) self.rename_files.setTitle(_("Rename files when saving")) self.ascii_filenames.setText(_("Replace non-ASCII characters")) diff --git a/ui/options_renaming.ui b/ui/options_renaming.ui index 94a70752d..98e4ed0cb 100644 --- a/ui/options_renaming.ui +++ b/ui/options_renaming.ui @@ -72,7 +72,7 @@ false - Move additional files(case insensitive): + Move additional files (case insensitive):