From 77789f19bfb388598600404018977c913242c3b8 Mon Sep 17 00:00:00 2001 From: Rory Date: Wed, 13 Dec 2023 12:45:01 +0000 Subject: [PATCH] refactor: prefer inbuilt methods to custom path concatenation --- app/app_controller.py | 4 +++- app/filepath_generator.py | 10 +++------- app/scanner.py | 8 ++------ test/helpers.py | 2 +- 4 files changed, 9 insertions(+), 15 deletions(-) diff --git a/app/app_controller.py b/app/app_controller.py index 8460445..1a6b55b 100644 --- a/app/app_controller.py +++ b/app/app_controller.py @@ -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() diff --git a/app/filepath_generator.py b/app/filepath_generator.py index d229d30..6f9bf64 100644 --- a/app/filepath_generator.py +++ b/app/filepath_generator.py @@ -1,5 +1,6 @@ from datetime import datetime from pathlib import Path as p +import os import re @@ -7,21 +8,16 @@ 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) diff --git a/app/scanner.py b/app/scanner.py index c4a1657..201566b 100644 --- a/app/scanner.py +++ b/app/scanner.py @@ -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): @@ -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) diff --git a/test/helpers.py b/test/helpers.py index 779e433..2af07e5 100644 --- a/test/helpers.py +++ b/test/helpers.py @@ -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):