Skip to content

Commit

Permalink
refactor: prefer inbuilt methods to custom path concatenation
Browse files Browse the repository at this point in the history
  • Loading branch information
roryai committed Dec 13, 2023
1 parent 41eef8d commit 77789f1
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 15 deletions.
4 changes: 3 additions & 1 deletion app/app_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@

class AppController:

def __init__(self, source_directory, destination_directory):
def __init__(self, source_directory, destination_directory, display_extensions):
self.source_directory = source_directory
self.destination_directory = destination_directory
self.display_extensions = display_extensions

def run(self):
DirectoryManager().check_if_directory_exists(self.source_directory)
FileGateway().wipe_database() # TODO dev only, remove later
self.__create_db_records_for_files_to_be_copied()
# if self.display_extensions:
StatPresenter().present_analysis_of_candidate_files(self.source_directory, self.destination_directory)
if self.__user_confirmation_of_copy():
FileCopier().copy_source_files_to_destination_directory()
Expand Down
10 changes: 3 additions & 7 deletions app/filepath_generator.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
from datetime import datetime
from pathlib import Path as p
import os
import re


class FilepathGenerator:

def __init__(self, source_filepath, destination_directory):
self.source_filepath = source_filepath
self.destination_directory = self.__sanitise_filepath(destination_directory)
self.destination_directory = destination_directory

def generate_destination_filepath(self):
filename = p(self.source_filepath).name
file_birthtime = self.__get_file_birthtime()
quarter = self.__determine_quarter(file_birthtime.month)
prospective_destination_filepath = f'{self.destination_directory}{file_birthtime.year}/{quarter}/{filename}'
prospective_destination_filepath = os.path.join(self.destination_directory, str(file_birthtime.year), quarter, filename)

return self.__detect_duplicates(prospective_destination_filepath)

def __sanitise_filepath(self, filepath):
if filepath[-1] != '/':
filepath += '/'
return filepath

def __get_file_birthtime(self):
birthtime_in_seconds = p(self.source_filepath).stat().st_birthtime
return datetime.fromtimestamp(birthtime_in_seconds)
Expand Down
8 changes: 2 additions & 6 deletions app/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
VALID_VIDEO_EXTENSIONS = ['.mp4', '.mov', '.avi', '.wmv', '.mkv']
VALID_EXTENSIONS = VALID_PHOTO_EXTENSIONS + VALID_VIDEO_EXTENSIONS


class Scanner:

def scan_directory(self, source_dir):
Expand All @@ -12,9 +13,4 @@ def scan_directory(self, source_dir):
for file in files:
_, extension = os.path.splitext(file)
if extension in VALID_EXTENSIONS:
yield self.__sanitise_filepath(root) + file

def __sanitise_filepath(self, filepath):
if filepath[-1] != '/':
filepath += '/'
return filepath
yield os.path.join(root, file)
2 changes: 1 addition & 1 deletion test/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def clear_database():


def get_destination_directory(source_filepath):
return destination_root_directory + determine_year_and_quarter(source_filepath)
return os.path.join(destination_root_directory, determine_year_and_quarter(source_filepath))


def determine_year_and_quarter(filepath):
Expand Down

0 comments on commit 77789f1

Please sign in to comment.