Skip to content

Commit

Permalink
refactor: add file_processing module and refactor main.py
Browse files Browse the repository at this point in the history
  • Loading branch information
elkaboussi committed Mar 2, 2024
1 parent 3c4b26d commit 8dec872
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 54 deletions.
73 changes: 73 additions & 0 deletions analyzer/file_processing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from pathlib import Path
from typing import Optional

from rich.prompt import Confirm

from analyzer.Categorization import Categorization
from analyzer.directory_traversal import walk_through_dir
from analyzer.LargeFiles import LargeFileIdentifier
from analyzer.Permission import FilePermissionsChecker
from analyzer.Summary import FileStatisticsCollector


def process_files(
dir_path: Path,
file_categorization: Categorization,
permissions_checker: FilePermissionsChecker,
large_file_identifier: LargeFileIdentifier,
file_statistics_collector: FileStatisticsCollector,
) -> None:
for file_path in walk_through_dir(dir_path):
file_categorization.add(file_path)
permissions_checker.add(file_path)
large_file_identifier.add(file_path)
file_statistics_collector.add_file(file_path)


def handle_permissions(
delete_files: bool,
permissions_checker: FilePermissionsChecker,
log_file: Optional[str],
) -> None:
if delete_files and not permissions_checker.is_report_empty() and not log_file:
confirm = Confirm.ask("Do you want to delete the files with bad permissions?")
if confirm:
permissions_checker.delete_reported_files()


def handle_large_files(
delete_files: bool,
large_file_identifier: LargeFileIdentifier,
log_file: Optional[str],
) -> None:
if delete_files and large_file_identifier.large_files and not log_file:
confirm = Confirm.ask("Do you want to delete the large files?")
if confirm:
large_file_identifier.delete_reported_files()


def process_directory(
dir_path: Path,
size_threshold: Optional[str],
delete_files: bool,
log_file: Optional[str],
) -> None:
file_categorization = Categorization()
permissions_checker = FilePermissionsChecker()
large_file_identifier = LargeFileIdentifier(size_threshold)
file_statistics_collector = FileStatisticsCollector()

process_files(
dir_path,
file_categorization,
permissions_checker,
large_file_identifier,
file_statistics_collector,
)

file_categorization.report()
permissions_checker.report()
handle_permissions(delete_files, permissions_checker, log_file)
large_file_identifier.report()
handle_large_files(delete_files, large_file_identifier, log_file)
file_statistics_collector.report_statistics()
55 changes: 1 addition & 54 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,8 @@
from pathlib import Path
from typing import Optional

from rich.prompt import Confirm

from analyzer.Categorization import Categorization
from analyzer.directory_traversal import walk_through_dir
from analyzer.LargeFiles import LargeFileIdentifier
from analyzer.Permission import FilePermissionsChecker
from analyzer.Summary import FileStatisticsCollector
from analyzer.file_processing import process_directory
from analyzer.utils.logger import setup_logging
from analyzer.utils.parser import parse_args


def process_directory(
dir_path: Path,
size_threshold: Optional[str],
delete_files: bool,
log_file: Optional[str],
) -> None:
"""
Process the target directory.
Args:
dir_path (Path): Path to the target directory.
size_threshold (Optional[str]): Size threshold for large files.
delete_files (bool): Whether to delete reported files.
log_file (Optional[str]): Path to the log file.
"""
file_categorization = Categorization()
permissions_checker = FilePermissionsChecker()
large_file_identifier = LargeFileIdentifier(size_threshold)
file_statistics_collector = FileStatisticsCollector()

for file_path in walk_through_dir(dir_path):
file_categorization.add(file_path)
permissions_checker.add(file_path)
large_file_identifier.add(file_path)
file_statistics_collector.add_file(file_path)

file_categorization.report()
permissions_checker.report()

if delete_files and not permissions_checker.is_report_empty() and not log_file:
confirm = Confirm.ask("Do you want to delete the files with bad permissions?")
if confirm:
permissions_checker.delete_reported_files()

large_file_identifier.report()

if delete_files and large_file_identifier.large_files and not log_file:
confirm = Confirm.ask("Do you want to delete the large files?")
if confirm:
large_file_identifier.delete_reported_files()

file_statistics_collector.report_statistics()


def main():
arguments = parse_args()
if arguments.target_dir is None:
Expand Down

0 comments on commit 8dec872

Please sign in to comment.