diff --git a/CHANGELOG.md b/CHANGELOG.md index 14aa4421..dbc80134 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.3 (alpha) + +- Added support for paths relative to the user's home directory. + # 1.2 (2022-02-24) - Added a `frozen()` context manager to temporarily disable file updates. diff --git a/datafiles/mapper.py b/datafiles/mapper.py index 11675023..3fdffecb 100644 --- a/datafiles/mapper.py +++ b/datafiles/mapper.py @@ -54,6 +54,8 @@ def path(self) -> Optional[Path]: path = Path(self._pattern.format(self=self._instance)) if path.is_absolute() or self._pattern.startswith("./"): return path.resolve() + if self._pattern.startswith("~/"): + return path.expanduser() cls = self._instance.__class__ try: diff --git a/datafiles/tests/test_mapper.py b/datafiles/tests/test_mapper.py index 74330a7a..be754aa5 100644 --- a/datafiles/tests/test_mapper.py +++ b/datafiles/tests/test_mapper.py @@ -58,6 +58,11 @@ def is_relative_to_cwd_when_specified(expect, mapper): path = Path.cwd() / "foobar" / "sample.yml" expect(mapper.path) == path + def is_relative_to_home_when_specified(expect, mapper): + mapper._pattern = "~/foobar/sample.yml" + path = Path.home() / "foobar" / "sample.yml" + expect(mapper.path) == path + def describe_relpath(): def when_cwd_is_parent(expect, mapper): mapper._pattern = "../../tmp/sample.yml" diff --git a/docs/api/model.md b/docs/api/model.md index f4750fd1..eb67dbee 100644 --- a/docs/api/model.md +++ b/docs/api/model.md @@ -57,6 +57,7 @@ Filename patterns are relative to the file in which the model is defined unless - Absolute: `/tmp/items/{self.name}.yml` - Relative to model: `items/{self.name}.yml` - Relative to current directory: `./items/{self.name}.yml` +- Relative to the user's home directory: `~/items/{self.name}.yml` Attributes included in the filename pattern and those with default value are automatically excluded from serialization since these redundant values are not required to restore objects from disk. diff --git a/poetry.lock b/poetry.lock index db3f81ba..e7c6ad1c 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2147,10 +2147,6 @@ requests = [ {file = "ruamel.yaml-0.17.20.tar.gz", hash = "sha256:4b8a33c1efb2b443a93fcaafcfa4d2e445f8e8c29c528d9f5cdafb7cc9e4004c"}, ] "ruamel.yaml.clib" = [ - {file = "ruamel.yaml.clib-0.2.6-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:6e7be2c5bcb297f5b82fee9c665eb2eb7001d1050deaba8471842979293a80b0"}, - {file = "ruamel.yaml.clib-0.2.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl", hash = "sha256:221eca6f35076c6ae472a531afa1c223b9c29377e62936f61bc8e6e8bdc5f9e7"}, - {file = "ruamel.yaml.clib-0.2.6-cp310-cp310-win32.whl", hash = "sha256:1070ba9dd7f9370d0513d649420c3b362ac2d687fe78c6e888f5b12bf8bc7bee"}, - {file = "ruamel.yaml.clib-0.2.6-cp310-cp310-win_amd64.whl", hash = "sha256:77df077d32921ad46f34816a9a16e6356d8100374579bc35e15bab5d4e9377de"}, {file = "ruamel.yaml.clib-0.2.6-cp35-cp35m-macosx_10_6_intel.whl", hash = "sha256:cfdb9389d888c5b74af297e51ce357b800dd844898af9d4a547ffc143fa56751"}, {file = "ruamel.yaml.clib-0.2.6-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:7b2927e92feb51d830f531de4ccb11b320255ee95e791022555971c466af4527"}, {file = "ruamel.yaml.clib-0.2.6-cp35-cp35m-win32.whl", hash = "sha256:ada3f400d9923a190ea8b59c8f60680c4ef8a4b0dfae134d2f2ff68429adfab5"}, diff --git a/pyproject.toml b/pyproject.toml index c3e00eb9..c52dd7b7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [tool.poetry] name = "datafiles" -version = "1.2" +version = "1.3a1" description = "File-based ORM for dataclasses." license = "MIT"