From a269e55fa5811da89c1abd8d4cfe48d43d8dc1ae Mon Sep 17 00:00:00 2001 From: matfax Date: Thu, 24 Oct 2019 15:53:39 +0200 Subject: [PATCH 1/3] feat: add serialization support for mashumaro --- tests/serializable.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/serializable.py diff --git a/tests/serializable.py b/tests/serializable.py new file mode 100644 index 0000000..7cab268 --- /dev/null +++ b/tests/serializable.py @@ -0,0 +1,39 @@ +from dataclasses import dataclass + +from mashumaro import DataClassDictMixin + +from mutapath import Path +from tests.helper import PathTest + + +@dataclass +class DataClass(DataClassDictMixin): + path: Path = Path() + + +class TestSerialization(PathTest): + + def test_empty_serialization(self): + expected = {'path': ''} + actual = DataClass().to_dict() + self.assertEqual(expected, actual) + + def test_serialization(self): + expected = {'path': "/A/B/test1.txt"} + actual = DataClass(Path("/A/B/test1.txt", posix=True)).to_dict() + self.assertEqual(expected, actual) + + def test_empty_deserialization(self): + expected = DataClass() + actual = DataClass().from_dict({'path': ''}) + self.assertEqual(expected, actual) + + def test_deserialization(self): + expected = DataClass(Path("/A/B/test1.txt", posix=True)) + actual = DataClass().from_dict({'path': "/A/B/test1.txt"}) + self.assertEqual(expected, actual) + + def test_deserialization_static(self): + expected = DataClass(Path("/A/B/test1.txt", posix=True)) + actual = DataClass.from_dict({'path': "/A/B/test1.txt"}) + self.assertEqual(expected, actual) From 0adc37e72a0fe70b08c0438cf30a1567be8e2473 Mon Sep 17 00:00:00 2001 From: matfax Date: Thu, 24 Oct 2019 18:26:48 +0200 Subject: [PATCH 2/3] fix(tests): on python 3.8 --- tests/serializable.py | 39 --------------------------------------- 1 file changed, 39 deletions(-) delete mode 100644 tests/serializable.py diff --git a/tests/serializable.py b/tests/serializable.py deleted file mode 100644 index 7cab268..0000000 --- a/tests/serializable.py +++ /dev/null @@ -1,39 +0,0 @@ -from dataclasses import dataclass - -from mashumaro import DataClassDictMixin - -from mutapath import Path -from tests.helper import PathTest - - -@dataclass -class DataClass(DataClassDictMixin): - path: Path = Path() - - -class TestSerialization(PathTest): - - def test_empty_serialization(self): - expected = {'path': ''} - actual = DataClass().to_dict() - self.assertEqual(expected, actual) - - def test_serialization(self): - expected = {'path': "/A/B/test1.txt"} - actual = DataClass(Path("/A/B/test1.txt", posix=True)).to_dict() - self.assertEqual(expected, actual) - - def test_empty_deserialization(self): - expected = DataClass() - actual = DataClass().from_dict({'path': ''}) - self.assertEqual(expected, actual) - - def test_deserialization(self): - expected = DataClass(Path("/A/B/test1.txt", posix=True)) - actual = DataClass().from_dict({'path': "/A/B/test1.txt"}) - self.assertEqual(expected, actual) - - def test_deserialization_static(self): - expected = DataClass(Path("/A/B/test1.txt", posix=True)) - actual = DataClass.from_dict({'path': "/A/B/test1.txt"}) - self.assertEqual(expected, actual) From a62a674474d9ceea668efbe8bd4174c736a29b04 Mon Sep 17 00:00:00 2001 From: matfax Date: Sat, 26 Oct 2019 19:43:20 +0200 Subject: [PATCH 3/3] feat: adding cached text and bytes properties --- mutapath/decorator.py | 3 ++- mutapath/immutapath.py | 22 ++++++++++++++++++++++ tests/test_with_path.py | 22 ++++++++++++++++++++-- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/mutapath/decorator.py b/mutapath/decorator.py index 64cb969..5f7ec5e 100644 --- a/mutapath/decorator.py +++ b/mutapath/decorator.py @@ -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", diff --git a/mutapath/immutapath.py b/mutapath/immutapath.py index 88814cd..37b2923 100644 --- a/mutapath/immutapath.py +++ b/mutapath/immutapath.py @@ -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: """ diff --git a/tests/test_with_path.py b/tests/test_with_path.py index 1bc5796..05fa8aa 100644 --- a/tests/test_with_path.py +++ b/tests/test_with_path.py @@ -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) @@ -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) @@ -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)