Skip to content

Commit

Permalink
Create delete duplicate rule
Browse files Browse the repository at this point in the history
  • Loading branch information
EdBarrancos committed Mar 12, 2024
1 parent b2b3f5f commit 2e171c9
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ cover/

# Project specific
settings.json
logs/*
logs/*
testing/*
21 changes: 8 additions & 13 deletions file_sorter/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def __init__(
depth: int,
file_type: str):
self.name = name
self.path = path
self.full_path = path
self.depth = depth
self.file_type = file_type

Expand All @@ -18,29 +18,24 @@ def __str__(self) -> str:

def __repr__(self) -> str:
return self.__str__()

def full_name(self) -> str:
return self.name + '.' + self.file_type

def is_duplicate(self) -> bool:
if len(self.name) < 3:
return False
return re.search("\([1-9]+[0-9]*\)$", self.name) is not None

def delete(self) -> None:
os.remove(self.full_path)

def build_file(
root: str,
name : str,
depth: str):
return File(
name,
name.split('.')[0],
os.path.join(root, name),
depth,
name.split('.')[-1])

class FileAction:
def __init__(self, target_file: File) -> None:
self.target_file = target_file

def execute() -> None:
pass

class FileActionQueue:
def __init__(self) -> None:
self.queue = []
27 changes: 27 additions & 0 deletions file_sorter/file_actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from file import File
from common.logger import Logger

class FileAction:
def __init__(self, target_file: File) -> None:
self.target_file = target_file

def execute(self) -> None:
pass

class DeleteFileAction(FileAction):
def execute(self) -> None:
self.target_file.delete()
Logger.info(f'Deleting {self.target_file.full_name()}')


class FileActionQueue:
def __init__(self) -> None:
self.queue = []

def queue_action(self, new_action: FileAction):
self.queue.append(new_action)
return self

def execute_actions(self):
for action in self.queue:
action.execute()
7 changes: 5 additions & 2 deletions file_sorter/file_sorter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from rules import AbstractRule
import rules

from file import File, FileActionQueue
from file import File
from file_actions import FileActionQueue

def build_file_list(directory_path: str, depth: int) -> Tuple[File]:
files = ()
Expand Down Expand Up @@ -37,4 +38,6 @@ def run_file_sorter(directory_path: str, rules_class_name: str) -> None:
queue = FileActionQueue()

for file in target_files:
rules_class.invokate(file, queue)
rules_class.invokate(file, queue)

queue.execute_actions()
8 changes: 7 additions & 1 deletion file_sorter/rules.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from file import File, FileActionQueue
from file import File
from file_actions import FileActionQueue, DeleteFileAction

class AbstractRule:
def invokate(self, file: File, queue: FileActionQueue) -> None:
pass

""" Add your rules here """

class DeleteDuplicateRule(AbstractRule):
def invokate(self, file: File, queue: FileActionQueue) -> None:
if file.is_duplicate():
queue.queue_action(DeleteFileAction(file))

class PrintName(AbstractRule):
def invokate(self, file: File, queue: FileActionQueue):
print(file.name)
Expand Down

0 comments on commit 2e171c9

Please sign in to comment.