Skip to content

Commit

Permalink
Update failure reason
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Dec 26, 2019
1 parent 4914d61 commit e8fc6fd
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 50 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*.egg-info
__pycache__
.ipynb_checkpoints
setup.py

# Temporary OS files
Icon*
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 0.6 (unreleased)

- Added a registration system for custom formatter classes.
- Fixed loading of missing attribute from disk for ORM methods.

# 0.5.1 (2019-11-14)

Expand Down
38 changes: 19 additions & 19 deletions datafiles/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,19 @@
Missing = dataclasses._MISSING_TYPE


class HasDatafile(Protocol):
datafile: Mapper


class Splats:
def __getattr__(self, name):
return '*'


class Manager:
def __init__(self, cls):
self.model = cls

def all(self) -> Iterator[HasDatafile]:
root = Path(inspect.getfile(self.model)).parent
pattern = str(root / self.model.Meta.datafile_pattern)
splatted = pattern.format(self=Splats())
log.info(f'Finding files matching pattern: {splatted}')
for filename in iglob(splatted):
log.debug(f'Found matching path: {filename}')
results = parse(pattern, filename)
yield self.get(*results.named.values())

def get(self, *args, **kwargs) -> HasDatafile:
fields = dataclasses.fields(self.model)
missing_args = [Missing] * (len(fields) - len(args) - len(kwargs))
Expand All @@ -62,6 +61,16 @@ def get_or_create(self, *args, **kwargs) -> HasDatafile:
log.info(f"File not found, creating '{self.model.__name__}' object")
return self.model(*args, **kwargs)

def all(self) -> Iterator[HasDatafile]:
root = Path(inspect.getfile(self.model)).parent
pattern = str(root / self.model.Meta.datafile_pattern)
splatted = pattern.format(self=Splats())
log.info(f'Finding files matching pattern: {splatted}')
for filename in iglob(splatted):
log.debug(f'Found matching path: {filename}')
results = parse(pattern, filename)
yield self.get(*results.named.values())

def filter(self, **query):
for item in self.all():
match = True
Expand All @@ -70,12 +79,3 @@ def filter(self, **query):
match = False
if match:
yield item


class HasDatafile(Protocol):
datafile: Mapper


class Splats:
def __getattr__(self, name):
return '*'
12 changes: 6 additions & 6 deletions datafiles/tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ def manager():
model = create_model(MyClass, pattern='files/{self.foo}.yml')
return Manager(model)

def describe_all():
@patch('datafiles.mapper.Mapper.exists', False)
def when_no_files_exist(expect, manager):
items = list(manager.all())
expect(items) == []

def describe_get_or_none():
@patch('datafiles.mapper.Mapper.load')
@patch('datafiles.mapper.Mapper.exists', True)
Expand Down Expand Up @@ -55,6 +49,12 @@ def when_file_missing(mock_save, expect, manager):
expect(manager.get_or_create(foo=1, bar=2)) == MyClass(foo=1, bar=2)
expect(mock_save.called) == True

def describe_all():
@patch('datafiles.mapper.Mapper.exists', False)
def when_no_files_exist(expect, manager):
items = list(manager.all())
expect(items) == []

def describe_filter():
@patch('datafiles.mapper.Mapper.exists', False)
def when_no_files_exist(expect, manager):
Expand Down
46 changes: 23 additions & 23 deletions docs/api/manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,6 @@ class MyModel:

Many of the following examples are also shown in this [Jupyter Notebook](https://github.com/jacebrowning/datafiles/blob/develop/notebooks/manager_api.ipynb).

# `all()`

Iterate over all objects matching the pattern:

```python
>>> generator = MyModel.objects.all()
>>> list(generator)
[]
```

```python
>>> m1 = MyModel('foo')
>>> m2 = MyModel('bar', 42)
```

```python
>>> for m in MyModel.objects.all():
... print(m)
...
MyModel(my_key='foo' my_value=0)
MyModel(my_key='bar', my_value=42)
```

# `get_or_none()`

Instantiate an object from an existing file or return `None` if no matching file exists:
Expand Down Expand Up @@ -73,6 +50,29 @@ MyModel(my_key='foo', my_value=42)
MyModel(my_key='bar', my_value=0)
```

# `all()`

Iterate over all objects matching the pattern:

```python
>>> generator = MyModel.objects.all()
>>> list(generator)
[]
```

```python
>>> m1 = MyModel('foo')
>>> m2 = MyModel('bar', 42)
```

```python
>>> for m in MyModel.objects.all():
... print(m)
...
MyModel(my_key='foo' my_value=0)
MyModel(my_key='bar', my_value=42)
```

# `filter()`

Iterate all objects matching the pattern with additional required attribute values:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]

name = "datafiles"
version = "0.6b2"
version = "0.6b3"
description = "File-based ORM for dataclasses."

license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_orm_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Bar:
expect(bar.nested.value) == 2


@pytest.mark.xfail(reason='https://github.com/jacebrowning/datafiles/issues/139')
@pytest.mark.xfail(reason='https://github.com/jacebrowning/datafiles/issues/147')
def test_values_are_filled_from_disk(expect):
InventoryItem.objects.get_or_create(42, "Things", 0.99)

Expand Down

0 comments on commit e8fc6fd

Please sign in to comment.