Skip to content

Commit

Permalink
refactor: change some names and fix typos
Browse files Browse the repository at this point in the history
  • Loading branch information
elkaboussi committed Mar 6, 2024
1 parent 0ac04d0 commit 37e0cf0
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 41 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ $ ./.env/bin/python3 main.py [PATH] [OPTIONS]

* Log the output of the program to a file
```commandline
python3 main.py . --size 100000 -l file.log
python3 main.py . --size '10 MiB' -l file.log
```

* Run unittests inside a Docker container
Expand Down
2 changes: 1 addition & 1 deletion analyzer/analyzer_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class AnalyserInterface(ABC):
@abstractmethod
def add(self, filepath: PathLike):
"""
Absrtact method to add a file to the analyser.
Abstract method to add a file to the analyzer.
"""
pass

Expand Down
10 changes: 5 additions & 5 deletions analyzer/file_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
from analyzer.directory_traversal import walk_through_dir
from analyzer.large_files import LargeFileIdentifier
from analyzer.permissions import FilePermissionsChecker
from analyzer.summary import FileStatisticsCollector
from analyzer.summary import Summary


def process_files(
dir_path: Path,
file_categorization: Categorization,
permissions_checker: FilePermissionsChecker,
large_file_identifier: LargeFileIdentifier,
file_statistics_collector: FileStatisticsCollector,
file_statistics_collector: Summary,
) -> 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)
file_statistics_collector.add(file_path)


def handle_permissions(
Expand Down Expand Up @@ -55,7 +55,7 @@ def process_directory(
file_categorization = Categorization()
permissions_checker = FilePermissionsChecker()
large_file_identifier = LargeFileIdentifier(size_threshold)
file_statistics_collector = FileStatisticsCollector()
file_statistics_collector = Summary()

process_files(
dir_path,
Expand All @@ -70,4 +70,4 @@ def process_directory(
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()
file_statistics_collector.report()
8 changes: 5 additions & 3 deletions analyzer/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import bitmath
import rich

from analyzer.analyzer_interface import AnalyserInterface, PathLike

class FileStatisticsCollector:

class Summary(AnalyserInterface):
total_files: int = 0
total_size: int = 0
average_size: float = 0.0
Expand All @@ -16,7 +18,7 @@ class FileStatisticsCollector:
start_time: float = time.time()
report_key_len: int = len("Smallest File Size: ")

def add_file(self, file_path: Path) -> None:
def add(self, file_path: PathLike) -> None:
try:
file_size = stat(file_path).st_size
except (FileNotFoundError, OSError):
Expand All @@ -32,7 +34,7 @@ def _format_size_line(self, key: str, value: Union[int, float]) -> str:
formatted_value = bitmath.Byte(value).best_prefix(bitmath.SI)
return f"{key.ljust(self.report_key_len)} {formatted_value}"

def report_statistics(self) -> None:
def report(self) -> None:
end_time = time.time()
elapsed_time = end_time - self.start_time
if self.total_files == 0:
Expand Down
62 changes: 31 additions & 31 deletions test/test_file_statistics_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest
from pyfakefs.fake_filesystem import FakeFilesystem

from analyzer.summary import FileStatisticsCollector
from analyzer.summary import Summary


def get_total_size() -> int:
Expand All @@ -15,96 +15,96 @@ def get_total_size() -> int:


@pytest.fixture(scope="function")
def collector(fs, app_file_system): # noqa F811
file_statistics_collector = FileStatisticsCollector()
def summary(fs, app_file_system): # noqa F811
summary_instance = Summary()
for file in fake_filesystem_files:
file_statistics_collector.add_file(Path(file["name"]))
yield file_statistics_collector
summary_instance.add(Path(file["name"]))
yield summary_instance


def test_add_file_increments_total_files_and_total_size(fs: FakeFilesystem, collector):
previous_total_files = collector.total_files
assert collector.total_files == len(fake_filesystem_files)
assert collector.total_size > 0
def test_add_file_increments_total_files_and_total_size(fs: FakeFilesystem, summary):
previous_total_files = summary.total_files
assert summary.total_files == len(fake_filesystem_files)
assert summary.total_size > 0

file_1 = "/root_dir/test_file"
create_fakefs_file(fs=fs, filepath=file_1)
collector.add_file(Path(file_1))
assert collector.total_files == previous_total_files + 1
summary.add(Path(file_1))
assert summary.total_files == previous_total_files + 1

file_2 = "/root_dir/test_file_2"
create_fakefs_file(fs=fs, filepath=file_2)
collector.add_file(Path(file_2))
assert collector.total_files == previous_total_files + 2
summary.add(Path(file_2))
assert summary.total_files == previous_total_files + 2


def test_total_files_size(fs: FakeFilesystem, collector):
def test_total_files_size(fs: FakeFilesystem, summary):
excepted = get_total_size()
output = collector.total_size
output = summary.total_size
assert output == excepted

# Add a new file with 95 bytes
create_fakefs_file(fs=fs, filepath="/root_dir/test_file", size=bitmath.Byte(95))
collector.add_file(Path("/root_dir/test_file"))
assert collector.total_size == excepted + 95
summary.add(Path("/root_dir/test_file"))
assert summary.total_size == excepted + 95


def test_average_size(fs: FakeFilesystem, collector, capsys: pytest.CaptureFixture):
def test_average_size(fs: FakeFilesystem, summary, capsys: pytest.CaptureFixture):
capsys.readouterr()
excepted_1 = bitmath.Byte(
get_total_size() / len(fake_filesystem_files)
).best_prefix(bitmath.SI)
# because we're only calculating the average once in the report phase (performance
# reason)
collector.report_statistics()
summary.report()

assert str(excepted_1) in capsys.readouterr().out

# add large file to upper the average
create_fakefs_file(
fs=fs, filepath="/root_dir/test_file", size=bitmath.MiB(400).to_Byte()
)
collector.add_file(Path("/root_dir/test_file"))
collector.report_statistics()
summary.add(Path("/root_dir/test_file"))
summary.report()

excepted_2 = bitmath.Byte(
get_total_size() / (len(fake_filesystem_files) + 1)
).best_prefix(bitmath.SI)
assert str(excepted_2) in capsys.readouterr().out


def test_smallest_file_size(fs: FakeFilesystem, collector):
def test_smallest_file_size(fs: FakeFilesystem, summary):

smallest_file = "/root_dir/smallest_file"

excepted = min(file["size"] for file in fake_filesystem_files)
output = collector.smallest_file_size
output = summary.smallest_file_size
assert output == excepted

# Add a new file with 1 byte
create_fakefs_file(fs=fs, filepath=smallest_file, size=bitmath.Byte(1))
collector.add_file(Path(smallest_file))
assert collector.smallest_file_size == Path(smallest_file).stat().st_size
summary.add(Path(smallest_file))
assert summary.smallest_file_size == Path(smallest_file).stat().st_size


def test_largest_file_size(fs: FakeFilesystem, collector):
def test_largest_file_size(fs: FakeFilesystem, summary):

largest_file = "/root_dir/largest_file"

excepted = max(file["size"] for file in fake_filesystem_files)
output = collector.largest_file_size
output = summary.largest_file_size
assert output == excepted

# Add a new file with 1 GiB
create_fakefs_file(fs=fs, filepath=largest_file, size=bitmath.GiB(1).to_Byte())
collector.add_file(Path(largest_file))
assert collector.largest_file_size == Path(largest_file).stat().st_size
summary.add(Path(largest_file))
assert summary.largest_file_size == Path(largest_file).stat().st_size


# Now test them all combined
def test_report_statistics(collector, capsys: pytest.CaptureFixture):
def test_report_statistics(summary, capsys: pytest.CaptureFixture):
capsys.readouterr()
collector.report_statistics()
summary.report()
output = capsys.readouterr().out

total_files = len(fake_filesystem_files)
Expand Down

0 comments on commit 37e0cf0

Please sign in to comment.