Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding cached text and bytes properties #19

Merged
merged 3 commits into from
Oct 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mutapath/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"__delattr__", "__setattr__", "__getattr__", "joinpath", "clone", "__exit__", "__fspath__",
"'_Path__wrap_attribute'", "__wrap_decorator", "_op_context", "__hash__", "__enter__", "_norm", "open", "lock",
"getcwd", "dirname", "owner", "uncshare", "posix_format", "posix_string", "__add__", "__radd__", "_set_contained",
"with_poxis_enabled", "_hash_cache", "_serialize", "_deserialize", "string_repr_enabled", "_shorten_duplicates"
"with_poxis_enabled", "_hash_cache", "_serialize", "_deserialize", "string_repr_enabled", "_shorten_duplicates",
"text", "bytes"
]

__MUTABLE_FUNCTIONS = {"rename", "renames", "copy", "copy2", "copyfile", "copymode", "copystat", "copytree", "move",
Expand Down
22 changes: 22 additions & 0 deletions mutapath/immutapath.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,28 @@ def glob(self, pattern):
paths = self.to_pathlib.glob(pattern)
return (self.clone(g) for g in paths)

@cached_property
def text(self):
"""
Read the file as text stream and return its content.
This property caches the returned value.
Clone this object to have a new path with a cleared cache or simply use :meth:`~pathlib.Path.read_text`.

.. seealso:: :meth:`pathlib.Path.read_text`
"""
return self.read_text()

@cached_property
def bytes(self):
"""
Read the file as bytes stream and return its content.
This property caches the returned value.
Clone this object to have a new path with a cleared cache or simply use :meth:`~pathlib.Path.read_bytes`.

.. seealso:: :meth:`pathlib.Path.read_bytes`
"""
return self.read_bytes()

@cached_property
def lock(self) -> filelock.BaseFileLock:
"""
Expand Down
22 changes: 20 additions & 2 deletions tests/test_with_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_open(self, test_file: Path):
expected = "test"
with test_file.open("w") as w:
w.write(expected)
actual = test_file.text()
actual = test_file.read_text()
self.assertEqual(expected, actual)

@file_test(equal=False)
Expand Down Expand Up @@ -190,7 +190,7 @@ def test_copying(self, test_file: Path):
with test_file.copying() as mut:
mut.stem = "new"
mut.suffix = ".txt"
self.assertEqual(expected.text(), test_file.text())
self.assertEqual(expected.read_text(), test_file.read_text())
return expected

@file_test(equal=False, instance=False, exists=False)
Expand All @@ -205,3 +205,21 @@ def test_moving(self, test_file: Path):
self.assertEqual(expected, from_here)
self.assertIsInstance(from_here, Path)
self.assertTrue(from_here.exists(), "File has to exist")

@file_test(equal=False)
def test_text(self, test_file: Path):
expected = "test"
with test_file.open("w") as w:
w.write(expected)
actual = test_file.text
actual2 = test_file.read_text()
self.assertEqual(expected, actual)
self.assertEqual(expected, actual2)

@file_test(equal=False)
def test_bytes(self, test_file: Path):
with test_file.open("w") as w:
w.write("test")
expected = test_file.read_bytes()
actual = test_file.bytes
self.assertEqual(expected, actual)