Skip to content

Commit

Permalink
feat: adding cached text and bytes properties (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
matfax authored Oct 26, 2019
1 parent 765f7b5 commit 82653d7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
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)

0 comments on commit 82653d7

Please sign in to comment.