-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #725 from pyiron/directory
Introduce DirectoryObject and FileObject in Workflow
- Loading branch information
Showing
6 changed files
with
195 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
from pathlib import Path | ||
|
||
|
||
def delete_files_and_directories_recursively(path): | ||
if not path.exists(): | ||
return | ||
for item in path.rglob("*"): | ||
if item.is_file(): | ||
item.unlink() | ||
else: | ||
delete_files_and_directories_recursively(item) | ||
path.rmdir() | ||
|
||
|
||
def categorize_folder_items(folder_path): | ||
types = [ | ||
"dir", | ||
"file", | ||
"mount", | ||
"symlink", | ||
"block_device", | ||
"char_device", | ||
"fifo", | ||
"socket", | ||
] | ||
results = {t: [] for t in types} | ||
|
||
for item in folder_path.iterdir(): | ||
for tt in types: | ||
try: | ||
if getattr(item, f"is_{tt}")(): | ||
results[tt].append(str(item)) | ||
except NotImplementedError: | ||
pass | ||
return results | ||
|
||
|
||
class DirectoryObject: | ||
def __init__(self, directory): | ||
self.path = Path(directory) | ||
self.create() | ||
|
||
def create(self): | ||
self.path.mkdir(parents=True, exist_ok=True) | ||
|
||
def delete(self): | ||
delete_files_and_directories_recursively(self.path) | ||
|
||
def list_content(self): | ||
return categorize_folder_items(self.path) | ||
|
||
def __len__(self): | ||
return sum([len(cc) for cc in self.list_content().values()]) | ||
|
||
def __repr__(self): | ||
return f"DirectoryObject(directory='{self.path}')\n{self.list_content()}" | ||
|
||
def get_path(self, file_name): | ||
return self.path / file_name | ||
|
||
def file_exists(self, file_name): | ||
return self.get_path(file_name).is_file() | ||
|
||
def write(self, file_name, content, mode="w"): | ||
with self.get_path(file_name).open(mode=mode) as f: | ||
f.write(content) | ||
|
||
def create_subdirectory(self, path): | ||
return DirectoryObject(self.path / path) | ||
|
||
def create_file(self, file_name): | ||
return FileObject(file_name, self) | ||
|
||
|
||
class FileObject: | ||
def __init__(self, file_name: str, directory: DirectoryObject): | ||
self.directory = directory | ||
self._file_name = file_name | ||
|
||
@property | ||
def file_name(self): | ||
return self._file_name | ||
|
||
@property | ||
def path(self): | ||
return self.directory.path / Path(self._file_name) | ||
|
||
def write(self, content, mode="x"): | ||
self.directory.write(file_name=self.file_name, content=content, mode=mode) | ||
|
||
def read(self, mode="r"): | ||
with open(self.path, mode=mode) as f: | ||
return f.read() | ||
|
||
def is_file(self): | ||
return self.directory.file_exists(self.file_name) | ||
|
||
def delete(self): | ||
self.path.unlink() |
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,50 @@ | ||
import unittest | ||
from pyiron_contrib.workflow.files import DirectoryObject, FileObject | ||
from pathlib import Path | ||
|
||
|
||
class TestFiles(unittest.TestCase): | ||
def setUp(cls): | ||
cls.directory = DirectoryObject("test") | ||
|
||
def tearDown(cls): | ||
cls.directory.delete() | ||
|
||
def test_directory_exists(self): | ||
self.assertTrue(Path("test").exists() and Path("test").is_dir()) | ||
|
||
def test_write(self): | ||
self.directory.write(file_name="test.txt", content="something") | ||
self.assertTrue(self.directory.file_exists("test.txt")) | ||
self.assertTrue( | ||
"test/test.txt" in [ | ||
ff.replace("\\", "/") | ||
for ff in self.directory.list_content()['file'] | ||
] | ||
) | ||
self.assertEqual(len(self.directory), 1) | ||
|
||
def test_create_subdirectory(self): | ||
self.directory.create_subdirectory("another_test") | ||
self.assertTrue(Path("test/another_test").exists()) | ||
|
||
def test_path(self): | ||
f = FileObject("test.txt", self.directory) | ||
self.assertEqual(str(f.path).replace("\\", "/"), "test/test.txt") | ||
|
||
def test_read_and_write(self): | ||
f = FileObject("test.txt", self.directory) | ||
f.write("something") | ||
self.assertEqual(f.read(), "something") | ||
|
||
def test_is_file(self): | ||
f = FileObject("test.txt", self.directory) | ||
self.assertFalse(f.is_file()) | ||
f.write("something") | ||
self.assertTrue(f.is_file()) | ||
f.delete() | ||
self.assertFalse(f.is_file()) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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