From df34781554d8d4f26896eb4c446f4b1c2723e47f Mon Sep 17 00:00:00 2001 From: Pablo Ordorica Wiener Date: Sun, 4 Apr 2021 22:21:08 -0400 Subject: [PATCH 1/2] Fix for issue #6 & ignore files in config --- empty-config.yml | 6 ++++-- mv_dwnlds/mv_dwnlds.py | 26 +++++++++++++++++++++----- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/empty-config.yml b/empty-config.yml index df1b8dc..ef85e84 100644 --- a/empty-config.yml +++ b/empty-config.yml @@ -8,8 +8,10 @@ track_dir: # ABSOLUTE PATH TO DEFAULT DOWNLOADS DIRECTORY # Path to new download directory where downloads will be moved to dst_dir: # ABSOLUTE PATH TO NEW DOWNLOADS DIRECTORY -# File that will not be moved from default downloads directory -ignore_file: README!.txt +# Files that will not be moved from default downloads directory +ignore_files: + - README!.txt + - .DS_Store ## ## # macOS Agent config # diff --git a/mv_dwnlds/mv_dwnlds.py b/mv_dwnlds/mv_dwnlds.py index b206c0c..79da20b 100644 --- a/mv_dwnlds/mv_dwnlds.py +++ b/mv_dwnlds/mv_dwnlds.py @@ -2,7 +2,8 @@ from watchdog.events import FileSystemEventHandler from os import listdir, rename -from os.path import dirname, abspath, join +from os.path import dirname, abspath, join, exists +from re import search import time from pathlib import Path from sys import exit @@ -18,13 +19,18 @@ def on_modified(self, event): for file_name in listdir(folder_to_track): f = File(file_name) if f.is_downloaded(): - if f.get_file_name() != ignore_file.get_file_name(): + if f.get_file_name() not in ignore_files_names: temp_folder_destination = folder_destination + f.get_file_type()[1:] temp_folder_destination = temp_folder_destination + '/' if temp_folder_destination[-1] != '/' else temp_folder_destination Path(temp_folder_destination).mkdir(parents=True, exist_ok=True) src = folder_to_track + f.get_file_name() new_destination = temp_folder_destination + f.get_file_name() + + while exists(new_destination): + f.append_num() + new_destination = temp_folder_destination + f.get_file_name() + rename(src, new_destination) cleanup(folder_destination) @@ -38,6 +44,17 @@ def get_file_name(self): def get_file_type(self): return Path(self._file_name).suffix + def append_num(self): + match = search("-\d+\.", self._file_name) + if match: + match_num = search("\d+", self._file_name) + old_num = match_num.group(0) + self._file_name = self._file_name.replace(old_num, str(int(old_num) + 1)) + else: + self._file_name = self._file_name.replace(".", "-1.") + + + def is_downloaded(self): return True if not (self.get_file_name().endswith(".crdownload") or self.get_file_name().startswith(".com.google.Chrome") or @@ -49,12 +66,11 @@ def is_downloaded(self): global folder_to_track global folder_destination - global ignore_file + global ignore_files_names folder_to_track = config['track_dir'] folder_destination = config['dst_dir'] - ignore_file_name = config['ignore_file'] - ignore_file = File(ignore_file_name) + ignore_files_names = config['ignore_files'] if folder_to_track == folder_destination: exit("[ERROR]: The track_dir and dst_dir in the config.yml file can NOT be the same directory") From 7f7269a4afe743f15d620248bba450a9a1101cb6 Mon Sep 17 00:00:00 2001 From: Pablo Ordorica Wiener Date: Sun, 4 Apr 2021 22:26:09 -0400 Subject: [PATCH 2/2] Format code --- mv_dwnlds/mv_dwnlds.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/mv_dwnlds/mv_dwnlds.py b/mv_dwnlds/mv_dwnlds.py index 79da20b..aac48ef 100644 --- a/mv_dwnlds/mv_dwnlds.py +++ b/mv_dwnlds/mv_dwnlds.py @@ -53,8 +53,6 @@ def append_num(self): else: self._file_name = self._file_name.replace(".", "-1.") - - def is_downloaded(self): return True if not (self.get_file_name().endswith(".crdownload") or self.get_file_name().startswith(".com.google.Chrome") or