-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(local files): Added local folder files
- Loading branch information
1 parent
1dfb36f
commit 6a1d0ba
Showing
8 changed files
with
232 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
[mypy] | ||
files = your_package, tests | ||
ignore_missing_imports = True | ||
mypy_path = src | ||
ignore_missing_imports = True | ||
strict = True | ||
namespace_packages = True | ||
explicit_package_bases = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
### Local File Manager | ||
|
||
::: numerous.files.local.FileManager |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
"""Local Folder File manager.""" | ||
import shutil | ||
from pathlib import Path | ||
|
||
from numerous.files.file_manager import FileManager as FileManagerInterface | ||
from numerous.files.file_manager import StrOrPath | ||
|
||
|
||
class FileManager(FileManagerInterface): | ||
|
||
""" | ||
Local Folder File manager. | ||
This class provides an local folder implementation of the FileManagerInterface. | ||
Use this class for testing or when you want to store files in a local folder only. | ||
""" | ||
|
||
def __init__(self, workfolder: StrOrPath="./tmp") -> None: | ||
""" | ||
Initialize the LocalFolderFileManager. | ||
Args: | ||
workfolder: Path to the working folder. | ||
""" | ||
self._workfolder = Path(workfolder) | ||
|
||
# Ensure the workfolder exists. | ||
self._workfolder.mkdir(parents=True, exist_ok=True) | ||
|
||
def put(self, src: StrOrPath, dst: StrOrPath ) -> None: | ||
""" | ||
Upload a file to a path. | ||
Args: | ||
src: Source path. | ||
dst: Destination path. | ||
""" | ||
_path = self._workfolder / Path(dst) | ||
# Ensure the parent folder exists. | ||
_path.parent.mkdir(parents=True, exist_ok=True) | ||
|
||
with Path.open(Path(src), "rb") as f: | ||
shutil.copyfileobj(f, Path.open(_path, "wb")) | ||
|
||
def remove(self, path: StrOrPath) -> None: | ||
""" | ||
Remove a file at a path. | ||
Args: | ||
path: Path to file. | ||
""" | ||
_path = self._workfolder / Path(path) | ||
Path(_path).unlink() | ||
|
||
def list(self, path: StrOrPath|None) -> list[str]: | ||
""" | ||
List files at a path. | ||
Args: | ||
path: Path to list files at. | ||
""" | ||
_path = self._workfolder if path is None else self._workfolder / Path(path) | ||
|
||
# Make all paths relative to the workfolder. | ||
return [str(p.relative_to(self._workfolder)) | ||
for p in _path.rglob("*") if p.is_file()] | ||
|
||
def move(self, src: StrOrPath, dst: StrOrPath) -> None: | ||
""" | ||
Move a file from a source to a destination. | ||
Args: | ||
src: Source path. | ||
dst: Destination path. | ||
""" | ||
_src = self._workfolder / Path(src) | ||
_dst = self._workfolder / Path(dst) | ||
# Ensure the parent folder exists. | ||
_dst.parent.mkdir(parents=True, exist_ok=True) | ||
shutil.move(_src, _dst) | ||
|
||
|
||
def copy(self, src: StrOrPath, dst: StrOrPath) -> None: | ||
""" | ||
Copy a file from a source to a destination. | ||
Args: | ||
src: Source path. | ||
dst: Destination path. | ||
""" | ||
_src = self._workfolder / Path(src) | ||
_dst = self._workfolder / Path(dst) | ||
# Ensure the parent folder exists. | ||
_dst.parent.mkdir(parents=True, exist_ok=True) | ||
shutil.copy(_src, _dst) | ||
|
||
def get(self, src: StrOrPath, dest: StrOrPath) -> None: | ||
""" | ||
Download a file from a source to a destination. | ||
Args: | ||
src: Source path. | ||
dest: Destination path. | ||
""" | ||
_src = self._workfolder / Path(src) | ||
# Ensure the parent folder exists. | ||
Path(dest).parent.mkdir(parents=True, exist_ok=True) | ||
shutil.copy(_src, dest) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
from pathlib import Path | ||
from typing import Generator | ||
|
||
import pytest | ||
from numerous.files.local import FileManager | ||
|
||
|
||
@pytest.fixture() | ||
def test_file_create() -> Generator[Path, None, None]: | ||
|
||
path_to_file = Path("test.txt") | ||
|
||
with Path.open(path_to_file, "w") as f: | ||
f.write("Hello World!") | ||
yield path_to_file | ||
Path.unlink(path_to_file) | ||
|
||
|
||
def test_file_manager_create() -> None: | ||
|
||
file_manager = FileManager() # noqa: F841 | ||
|
||
def test_file_put(test_file_create: str) -> None: | ||
|
||
file_manager = FileManager() | ||
|
||
file_manager.put(test_file_create, "tests/test_memory_filemanager.py") | ||
|
||
def test_file_remove(test_file_create: str) -> None: | ||
|
||
file_manager = FileManager() | ||
|
||
upload_path = "tests/test_memory_filemanager.py" | ||
|
||
file_manager.put(test_file_create, upload_path) | ||
file_manager.remove(upload_path) | ||
|
||
def test_file_list(test_file_create: str) -> None: | ||
|
||
file_manager = FileManager() | ||
|
||
upload_path = "tests/test_memory_filemanager.py" | ||
|
||
file_manager.put(test_file_create, upload_path) | ||
results = file_manager.list("tests/") | ||
# Replace \\ with / for Windows compatibility | ||
results = [r.replace("\\", "/") for r in results] | ||
assert upload_path in results | ||
|
||
def test_file_move(test_file_create: str) -> None: | ||
|
||
file_manager = FileManager() | ||
|
||
upload_path = "tests/test_memory_filemanager.py" | ||
|
||
file_manager.put(test_file_create, upload_path) | ||
file_manager.move(upload_path, "tests/test_memory_filemanager2.py") | ||
|
||
results = file_manager.list("tests/") | ||
# Replace \\ with / for Windows compatibility | ||
results = [r.replace("\\", "/") for r in results] | ||
|
||
assert "tests/test_memory_filemanager2.py" in results | ||
assert upload_path not in results | ||
|
||
def test_file_copy(test_file_create: str) -> None: | ||
|
||
file_manager = FileManager() | ||
|
||
upload_path = "tests/test_memory_filemanager.py" | ||
|
||
file_manager.put(test_file_create, upload_path) | ||
file_manager.copy(upload_path, "tests/test_memory_filemanager2.py") | ||
|
||
file_manager.list("tests/") | ||
|
||
results = file_manager.list("tests/") | ||
# Replace \\ with / for Windows compatibility | ||
results = [r.replace("\\", "/") for r in results] | ||
|
||
assert "tests/test_memory_filemanager2.py" in results | ||
assert upload_path in results | ||
|
||
def test_file_get(test_file_create: str) -> None: | ||
|
||
file_manager = FileManager() | ||
|
||
upload_path = "tests/test_memory_filemanager.py" | ||
|
||
file_manager.put(test_file_create, upload_path) | ||
file_manager.get(upload_path, "test_memory_filemanager.py") | ||
|
||
with Path.open(Path("test_memory_filemanager.py")) as f: | ||
assert f.read() == "Hello World!" | ||
|
||
Path.unlink(Path("test_memory_filemanager.py")) |