From 3eb045cd4a343088f4f9d589153f4d7d5844a13c Mon Sep 17 00:00:00 2001 From: stanislas Date: Mon, 23 Sep 2024 23:46:15 +0200 Subject: [PATCH] Able to connect to perform operations on local files --- src/losoup/file_operations/__init__.py | 0 src/losoup/file_operations/local_files.py | 24 ++++++++++++++++++++ test/losoup/test_local_files.py | 27 +++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 src/losoup/file_operations/__init__.py create mode 100644 src/losoup/file_operations/local_files.py create mode 100644 test/losoup/test_local_files.py diff --git a/src/losoup/file_operations/__init__.py b/src/losoup/file_operations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/losoup/file_operations/local_files.py b/src/losoup/file_operations/local_files.py new file mode 100644 index 0000000..9b612c8 --- /dev/null +++ b/src/losoup/file_operations/local_files.py @@ -0,0 +1,24 @@ +from pathlib import Path + +from losoup.models import Software, Asset + + +class LocalFile: + def __init__(self, software: Software): + self.software = software + + def get_files_in_folder(self) -> list[str]: + return [file.name for file in Path(self.software.folder).glob("*")] + + def is_file_present(self): + files = self.get_files_in_folder() + return self.software.filename in files + + def is_file_absent(self): + return not self.is_file_present() + + def write_file(self, file: Asset) -> None: + self.software.absolute_path.write_bytes(file.content) + + def delete_file(self): + self.software.absolute_path.unlink() diff --git a/test/losoup/test_local_files.py b/test/losoup/test_local_files.py new file mode 100644 index 0000000..1dc00d1 --- /dev/null +++ b/test/losoup/test_local_files.py @@ -0,0 +1,27 @@ +from losoup.file_operations.local_files import LocalFile +from losoup.models import Asset +import typer + + +def test_local_files_init(keepassxc): + local_file = LocalFile(keepassxc) + assert local_file.software == keepassxc + + +def test_get_local_files(local_file): + assert "KeePassXC-2.7.9-x86_64.AppImage" in local_file.get_files_in_folder() + + +def test_is_file_present(local_file): + assert local_file.is_file_present() + + +def test_is_file_absent(local_file_not_present): + assert local_file_not_present.is_file_absent() + + +def test_write_file(keepassxc_digest_file: LocalFile, asset_to_write: Asset): + assert keepassxc_digest_file.is_file_absent() + keepassxc_digest_file.write_file(asset_to_write) + assert keepassxc_digest_file.is_file_present() + keepassxc_digest_file.delete_file()