Skip to content

Commit

Permalink
Merge pull request #258 from jacebrowning/home-paths
Browse files Browse the repository at this point in the history
Add support for paths relative to home directories
  • Loading branch information
jacebrowning committed Mar 28, 2022
2 parents 4771d3c + 7386496 commit afd5bde
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 66 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ $(MKDOCS_INDEX): docs/requirements.txt mkdocs.yml docs/*.md

docs/requirements.txt: poetry.lock
@ rm -f $@
@ poetry export --dev --without-hashes | grep jinja2 >> $@
@ poetry export --dev --without-hashes | grep markdown >> $@
@ poetry export --dev --without-hashes | grep mkdocs >> $@
@ poetry export --dev --without-hashes | grep pygments >> $@
Expand Down
2 changes: 2 additions & 0 deletions datafiles/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions datafiles/tests/test_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions docs/api/model.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
1 change: 1 addition & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
jinja2==3.0.3; python_version >= "3.6"
markdown==3.3.4; python_version >= "3.6"
mkdocs==1.2.3; python_version >= "3.6"
jupyterlab-pygments==0.1.2; python_version >= "3.6"
Expand Down
114 changes: 49 additions & 65 deletions poetry.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]

name = "datafiles"
version = "1.2"
version = "1.3a1"
description = "File-based ORM for dataclasses."

license = "MIT"
Expand Down Expand Up @@ -86,6 +86,7 @@ coveragespace = "^4.0"
# Documentation
mkdocs = "^1.2.3"
pygments = "^2.10"
jinja2 = "~3.0.3" # fix for 'mkdocs' incompatiblity

# Notebooks
idna = "^3.3" # papermill dependency
Expand Down

0 comments on commit afd5bde

Please sign in to comment.