Skip to content

Commit

Permalink
Merge pull request #9 from pablordoricaw/bug/issue-6
Browse files Browse the repository at this point in the history
Fix for issue 6
  • Loading branch information
pablordoricaw authored Apr 5, 2021
2 parents 34b2fee + 7f7269a commit 012d56f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
6 changes: 4 additions & 2 deletions empty-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 #
Expand Down
24 changes: 19 additions & 5 deletions mv_dwnlds/mv_dwnlds.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -38,6 +44,15 @@ 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
Expand All @@ -49,12 +64,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")
Expand Down

0 comments on commit 012d56f

Please sign in to comment.