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: support serialization using mashumaro #14

Merged
merged 3 commits into from
Oct 24, 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
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ green = "==3.0.0"
coverage = "==4.5.4"
codecov = "==2.0.15"
wheel = "==0.33.6"
mashumaro = "==1.7"

[packages]
path-py = "==12.0.1"
Expand Down
53 changes: 52 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mutapath/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"__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"
"with_poxis_enabled", "_hash_cache", "_serialize", "_deserialize"
]

__MUTABLE_FUNCTIONS = {"rename", "renames", "copy", "copy2", "copyfile", "copymode", "copystat", "copytree", "move",
Expand Down
16 changes: 15 additions & 1 deletion mutapath/immutapath.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@
from mutapath.exceptions import PathException
from mutapath.lock_dummy import DummyFileLock

try:
from mashumaro.types import SerializableType
except ImportError:
SerializableType = object
except NotImplementedError:
SerializableType = object

POSIX_ENABLED_DEFAULT = False


@path_wrapper
class Path(object):
class Path(SerializableType):
"""Immutable Path"""
_contained: Union[path.Path, pathlib.PurePath, str] = path.Path("")
__always_posix_format: bool
Expand Down Expand Up @@ -147,6 +154,13 @@ def __invert__(self):
from mutapath import MutaPath
return MutaPath(self._contained, self.posix_enabled)

def _serialize(self) -> str:
return str(self._contained)

@classmethod
def _deserialize(cls, value: str) -> Path:
return cls(value)

@property
def to_pathlib(self) -> pathlib.Path:
"""
Expand Down
43 changes: 43 additions & 0 deletions tests/test_serializable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from dataclasses import dataclass

from mutapath import Path
from tests.helper import PathTest

try:
from mashumaro.types import DataClassDictMixin
except ImportError:
DataClassDictMixin = object
except NotImplementedError:
DataClassDictMixin = object
else:

@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)