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

Add model manager methods to get all objects #116

Merged
merged 5 commits into from
May 21, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 0.3 (unreleased)

- Added ORM method: `all()`
- Added ORM method: `get_or_none()`.
- Added ORM method: `get_or_create()`.

Expand Down
43 changes: 0 additions & 43 deletions datafiles/builders.py

This file was deleted.

44 changes: 44 additions & 0 deletions datafiles/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Configuration defaults for each model."""

from __future__ import annotations

from contextlib import suppress
from dataclasses import dataclass
from typing import TYPE_CHECKING, Dict, Optional


if TYPE_CHECKING:
from .converters import Converter


@dataclass
class Meta:
datafile_attrs: Optional[Dict[str, Converter]] = None
datafile_pattern: Optional[str] = None

datafile_manual: bool = False
datafile_defaults: bool = False
datafile_auto_load: bool = True
datafile_auto_save: bool = True
datafile_auto_attr: bool = False


def load(obj) -> Meta:
meta = Meta()

with suppress(AttributeError):
meta.datafile_attrs = obj.Meta.datafile_attrs
with suppress(AttributeError):
meta.datafile_pattern = obj.Meta.datafile_pattern
with suppress(AttributeError):
meta.datafile_manual = obj.Meta.datafile_manual
with suppress(AttributeError):
meta.datafile_defaults = obj.Meta.datafile_defaults
with suppress(AttributeError):
meta.datafile_auto_load = obj.Meta.datafile_auto_load
with suppress(AttributeError):
meta.datafile_auto_save = obj.Meta.datafile_auto_save
with suppress(AttributeError):
meta.datafile_auto_attr = obj.Meta.datafile_auto_attr

return meta
22 changes: 14 additions & 8 deletions datafiles/decorators.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
from __future__ import annotations

import dataclasses
from pathlib import Path
from typing import Dict, Optional
from typing import TYPE_CHECKING, Dict, Optional

from .config import Meta
from .models import create_model


from .converters import Converter
from .models import ModelMeta, create_model
if TYPE_CHECKING:
from .converters import Converter


def datafile(
pattern: str,
attrs: Optional[Dict[str, Converter]] = None,
manual: bool = ModelMeta.datafile_manual,
defaults: bool = ModelMeta.datafile_defaults,
auto_load: bool = ModelMeta.datafile_auto_load,
auto_save: bool = ModelMeta.datafile_auto_save,
auto_attr: bool = ModelMeta.datafile_auto_attr,
manual: bool = Meta.datafile_manual,
defaults: bool = Meta.datafile_defaults,
auto_load: bool = Meta.datafile_auto_load,
auto_save: bool = Meta.datafile_auto_save,
auto_attr: bool = Meta.datafile_auto_attr,
):
"""Synchronize a data class to the specified path."""

Expand Down
4 changes: 2 additions & 2 deletions datafiles/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import log

from . import settings
from .builders import build_datafile
from .mappers import create_mapper


LOAD_BEFORE_METHODS = ['__getattribute__', '__getitem__', '__iter__']
Expand Down Expand Up @@ -65,7 +65,7 @@ def apply(instance, datafile):
setattr(instance, attr_name, attr)
else:
continue
attr.datafile = build_datafile(attr, root=datafile)
attr.datafile = create_mapper(attr, root=datafile)
apply(attr, datafile)


Expand Down