From c3284a4a42852116b9b2206d3e00ecfe4830829b Mon Sep 17 00:00:00 2001 From: Sam Waseda Date: Tue, 5 Aug 2025 21:38:33 +0200 Subject: [PATCH 1/5] Protect folder if it exists before it was declared --- pyiron_snippets/files.py | 14 ++++++++++---- tests/unit/test_files.py | 10 ++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/pyiron_snippets/files.py b/pyiron_snippets/files.py index e992211..be0cca7 100644 --- a/pyiron_snippets/files.py +++ b/pyiron_snippets/files.py @@ -54,7 +54,7 @@ def __init__( self, directory: str | Path | DirectoryObject = ".", generate_unique_directory: bool | None = None, - protected: bool = False, + protected: bool | None = None, ): """ Initialize a DirectoryObject. @@ -65,8 +65,9 @@ def __init__( generate_unique_directory (bool | None): If True, generates a unique directory name, otherwise it still generates a unique name if the directory is "." and this parameter is None. - protected (bool): If True, prevents deletion of the directory object - on garbage collection. + protected (bool | None): If True, prevents deletion of the + directory object on garbage collection. If None, it defaults to + False if the directory does not exist. """ if isinstance(directory, str): path = Path(directory) @@ -78,9 +79,14 @@ def __init__( directory == "." and generate_unique_directory is None ) or generate_unique_directory: path = path / f"data_{uuid.uuid4().hex}" + if protected is None: + if path.exists(): + protected = True + else: + protected = False + self._protected = protected self.path: Path = path self.create() - self._protected = protected def __getstate__(self): self._protected = True diff --git a/tests/unit/test_files.py b/tests/unit/test_files.py index f810578..8e2ac04 100644 --- a/tests/unit/test_files.py +++ b/tests/unit/test_files.py @@ -22,6 +22,16 @@ def test_directory_instantiation(self): self.assertTrue(str(directory.path).startswith("data")) self.assertEqual(len(str(directory.path)), 37) + def test_protected(self): + directory = DirectoryObject("protected", protected=True) + self.assertTrue(directory._protected) + directory = DirectoryObject("protected") + self.assertTrue(directory._protected) + directory.delete() + directory = DirectoryObject("protected") + self.assertFalse(directory._protected) + directory.delete() + def test_directory_exists(self): self.assertTrue(Path("test").exists() and Path("test").is_dir()) From 653f61c93e5f713b5608ce8e479cdbaf0d83327a Mon Sep 17 00:00:00 2001 From: Sam Waseda Date: Tue, 5 Aug 2025 21:42:40 +0200 Subject: [PATCH 2/5] ruff-check --- pyiron_snippets/files.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pyiron_snippets/files.py b/pyiron_snippets/files.py index be0cca7..4980272 100644 --- a/pyiron_snippets/files.py +++ b/pyiron_snippets/files.py @@ -80,10 +80,7 @@ def __init__( ) or generate_unique_directory: path = path / f"data_{uuid.uuid4().hex}" if protected is None: - if path.exists(): - protected = True - else: - protected = False + protected = True if path.is_absolute() else False self._protected = protected self.path: Path = path self.create() From 5aa1a4d6baae89d0069a750756de06e0902f4ddd Mon Sep 17 00:00:00 2001 From: Sam Waseda Date: Tue, 5 Aug 2025 21:48:09 +0200 Subject: [PATCH 3/5] Copilot made a mistake and I didn't even realize --- pyiron_snippets/files.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyiron_snippets/files.py b/pyiron_snippets/files.py index 4980272..7df4488 100644 --- a/pyiron_snippets/files.py +++ b/pyiron_snippets/files.py @@ -80,7 +80,7 @@ def __init__( ) or generate_unique_directory: path = path / f"data_{uuid.uuid4().hex}" if protected is None: - protected = True if path.is_absolute() else False + protected = True if path.exists() else False self._protected = protected self.path: Path = path self.create() From 2ef435b7830e7009f1692ef5af6b6f7ace39f70f Mon Sep 17 00:00:00 2001 From: Sam Waseda Date: Tue, 5 Aug 2025 21:52:03 +0200 Subject: [PATCH 4/5] Very nice comment from ruff-check --- pyiron_snippets/files.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyiron_snippets/files.py b/pyiron_snippets/files.py index 7df4488..16876bd 100644 --- a/pyiron_snippets/files.py +++ b/pyiron_snippets/files.py @@ -80,7 +80,7 @@ def __init__( ) or generate_unique_directory: path = path / f"data_{uuid.uuid4().hex}" if protected is None: - protected = True if path.exists() else False + protected = path.exists() self._protected = protected self.path: Path = path self.create() From 947654627f9fe65f75019944d119a30f249b1b06 Mon Sep 17 00:00:00 2001 From: Sam Dareska <37879103+samwaseda@users.noreply.github.com> Date: Wed, 6 Aug 2025 07:08:33 +0200 Subject: [PATCH 5/5] Update pyiron_snippets/files.py Co-authored-by: Liam Huber --- pyiron_snippets/files.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyiron_snippets/files.py b/pyiron_snippets/files.py index 16876bd..1e324a7 100644 --- a/pyiron_snippets/files.py +++ b/pyiron_snippets/files.py @@ -67,7 +67,7 @@ def __init__( the directory is "." and this parameter is None. protected (bool | None): If True, prevents deletion of the directory object on garbage collection. If None, it defaults to - False if the directory does not exist. + True if the directory already exists. """ if isinstance(directory, str): path = Path(directory)