snapclass 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- snapclass-0.1.0/LICENSE +21 -0
- snapclass-0.1.0/PKG-INFO +123 -0
- snapclass-0.1.0/README.md +94 -0
- snapclass-0.1.0/pyproject.toml +59 -0
- snapclass-0.1.0/setup.cfg +4 -0
- snapclass-0.1.0/src/snapclass/__init__.py +42 -0
- snapclass-0.1.0/src/snapclass/collections.py +133 -0
- snapclass-0.1.0/src/snapclass/containers.py +11 -0
- snapclass-0.1.0/src/snapclass/decorators.py +5 -0
- snapclass-0.1.0/src/snapclass/formatters.py +430 -0
- snapclass-0.1.0/src/snapclass/fresh.py +58 -0
- snapclass-0.1.0/src/snapclass/hooks.py +123 -0
- snapclass-0.1.0/src/snapclass/paths.py +47 -0
- snapclass-0.1.0/src/snapclass/plugins.py +65 -0
- snapclass-0.1.0/src/snapclass/py.typed +1 -0
- snapclass-0.1.0/src/snapclass/schemas.py +2598 -0
- snapclass-0.1.0/src/snapclass/serializers.py +512 -0
- snapclass-0.1.0/src/snapclass/sessions.py +4 -0
- snapclass-0.1.0/src/snapclass/sidecar.py +300 -0
- snapclass-0.1.0/src/snapclass/snapshots.py +17 -0
- snapclass-0.1.0/src/snapclass/stash.py +256 -0
- snapclass-0.1.0/src/snapclass/types.py +34 -0
- snapclass-0.1.0/src/snapclass.egg-info/PKG-INFO +123 -0
- snapclass-0.1.0/src/snapclass.egg-info/SOURCES.txt +45 -0
- snapclass-0.1.0/src/snapclass.egg-info/dependency_links.txt +1 -0
- snapclass-0.1.0/src/snapclass.egg-info/requires.txt +9 -0
- snapclass-0.1.0/src/snapclass.egg-info/top_level.txt +1 -0
- snapclass-0.1.0/tests/test_behavior_contracts.py +152 -0
- snapclass-0.1.0/tests/test_collection_patterns.py +352 -0
- snapclass-0.1.0/tests/test_conversion_and_writes.py +614 -0
- snapclass-0.1.0/tests/test_everlast_inspired_workflows.py +282 -0
- snapclass-0.1.0/tests/test_example_app_workflows.py +155 -0
- snapclass-0.1.0/tests/test_formatters.py +367 -0
- snapclass-0.1.0/tests/test_fresh.py +112 -0
- snapclass-0.1.0/tests/test_lorebubble_fixture_port.py +312 -0
- snapclass-0.1.0/tests/test_model_meta.py +342 -0
- snapclass-0.1.0/tests/test_module_relative_patterns.py +88 -0
- snapclass-0.1.0/tests/test_mypy_plugin.py +189 -0
- snapclass-0.1.0/tests/test_p0_app_surfaces.py +863 -0
- snapclass-0.1.0/tests/test_plunkylib_fixture_port.py +267 -0
- snapclass-0.1.0/tests/test_sessions_and_frozen.py +492 -0
- snapclass-0.1.0/tests/test_sidecar.py +420 -0
- snapclass-0.1.0/tests/test_snapclass_public_api.py +352 -0
- snapclass-0.1.0/tests/test_snapshot_magic_and_serializers.py +947 -0
- snapclass-0.1.0/tests/test_stash_binding_and_api.py +805 -0
- snapclass-0.1.0/tests/test_sync.py +278 -0
- snapclass-0.1.0/tests/test_unknown_fields.py +380 -0
snapclass-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mattie Casper
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
snapclass-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: snapclass
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Human-readable file persistence for Python dataclasses.
|
|
5
|
+
Author: Mattie Casper
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Repository, https://github.com/Mattie/snapclass
|
|
8
|
+
Project-URL: Issues, https://github.com/Mattie/snapclass/issues
|
|
9
|
+
Keywords: dataclasses,persistence,serialization,yaml,toml
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
17
|
+
Classifier: Typing :: Typed
|
|
18
|
+
Requires-Python: >=3.11
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
License-File: LICENSE
|
|
21
|
+
Requires-Dist: json5<1,>=0.9
|
|
22
|
+
Requires-Dist: ruamel.yaml<0.20,>=0.18
|
|
23
|
+
Requires-Dist: tomlkit<0.16,>=0.10
|
|
24
|
+
Provides-Extra: test
|
|
25
|
+
Requires-Dist: pytest>=8; extra == "test"
|
|
26
|
+
Provides-Extra: typing
|
|
27
|
+
Requires-Dist: mypy<3,>=1.11; extra == "typing"
|
|
28
|
+
Dynamic: license-file
|
|
29
|
+
|
|
30
|
+
# snapclass
|
|
31
|
+
|
|
32
|
+
Human-readable file persistence for Python dataclasses, an adaptation of the cool [`datafiles` project](https://github.com/jacebrowning/datafiles).
|
|
33
|
+
|
|
34
|
+
`snapclass` is a small persistence layer built around Python dataclasses.
|
|
35
|
+
Decorate a dataclass, give it a path pattern, and its instances can save and
|
|
36
|
+
load themselves as readable YAML, JSON, TOML, or text.
|
|
37
|
+
|
|
38
|
+
It is built for the stuff that belongs in a repo or project folder: prompts,
|
|
39
|
+
configs, fixtures, lightweight app state, and little durable objects with names.
|
|
40
|
+
|
|
41
|
+
You can use a `Stash` to pick a different location for the
|
|
42
|
+
serialized file (with env overrides) and have special format rules when you
|
|
43
|
+
need. You can also include a `sidecar` when you have a doc or binary you want to save next to it.
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
## ORM is a snap!
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from snapclass import snapclass
|
|
51
|
+
|
|
52
|
+
@snapclass("{self.name}.yml")
|
|
53
|
+
class Note:
|
|
54
|
+
name: str
|
|
55
|
+
title: str
|
|
56
|
+
body: str = ""
|
|
57
|
+
|
|
58
|
+
mynote = Note(
|
|
59
|
+
"first_note",
|
|
60
|
+
"Today I used snapclass!",
|
|
61
|
+
"It was my first day of snapclass. My note was saved to YAML for me!",
|
|
62
|
+
)
|
|
63
|
+
mynote.snapshot.save()
|
|
64
|
+
|
|
65
|
+
# Load it back later with the same name.
|
|
66
|
+
same_note = Note.snapshots.get("first_note")
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from snapclass import snapclass, Stash, Fresh
|
|
71
|
+
|
|
72
|
+
# Create a default location with an environment override.
|
|
73
|
+
runsloc = Stash("./runs", env="RUNS_DIR")
|
|
74
|
+
|
|
75
|
+
@snapclass("{self.name}.yml", stash=runsloc)
|
|
76
|
+
class RunData:
|
|
77
|
+
name: str
|
|
78
|
+
# shortcuts for common boilerplate factory code
|
|
79
|
+
metrics: dict[str, float] = Fresh.Dict
|
|
80
|
+
|
|
81
|
+
RunData("baseline", {"accuracy": 0.98, "loss": 0.04}).snapshot.save()
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from snapclass import snapclass, Stash, sidecar
|
|
86
|
+
|
|
87
|
+
@snapclass
|
|
88
|
+
class Style:
|
|
89
|
+
voice: str
|
|
90
|
+
temperature: float
|
|
91
|
+
|
|
92
|
+
# Locations can be nested with stashes.
|
|
93
|
+
app = Stash("./myapp", env="MYAPP_DATA")
|
|
94
|
+
articles = app / "articles"
|
|
95
|
+
|
|
96
|
+
@snapclass("{self.slug}/article.yml", stash=articles)
|
|
97
|
+
class Article:
|
|
98
|
+
slug: str
|
|
99
|
+
title: str
|
|
100
|
+
style: Style
|
|
101
|
+
body: str = sidecar.text("{self.slug}.md")
|
|
102
|
+
|
|
103
|
+
article = Article(
|
|
104
|
+
"dusk-court",
|
|
105
|
+
"Dusk Court",
|
|
106
|
+
Style("warm", 0.4),
|
|
107
|
+
body="# Dusk Court\n\nBe brief, warm, and useful.\n",
|
|
108
|
+
)
|
|
109
|
+
loaded = Article.snapshots.get("dusk-court")
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## FAQ
|
|
113
|
+
|
|
114
|
+
### Why use `snapclass` over `datafiles`?
|
|
115
|
+
|
|
116
|
+
`datafiles` is great and you should absolutely use it for your app or script! I love it so much and use it in project after project. After years of use, I've run into a few limitations-- like issues when multiple modules needed different `datafiles` behavior in one process (because much of the behavior control is global). I designed `snapclass` to isolate some of that via stashes and added some extra features I liked along the way.
|
|
117
|
+
|
|
118
|
+
## License
|
|
119
|
+
|
|
120
|
+
snapclass is licensed under the MIT License. See [LICENSE](LICENSE).
|
|
121
|
+
|
|
122
|
+
Much of snapclass's core behavior, along with portions of its implementation
|
|
123
|
+
and test suite, is adapted from the wonderful [`datafiles`](https://github.com/jacebrowning/datafiles) project by [Jace Browning](https://github.com/jacebrowning). See [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md).
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# snapclass
|
|
2
|
+
|
|
3
|
+
Human-readable file persistence for Python dataclasses, an adaptation of the cool [`datafiles` project](https://github.com/jacebrowning/datafiles).
|
|
4
|
+
|
|
5
|
+
`snapclass` is a small persistence layer built around Python dataclasses.
|
|
6
|
+
Decorate a dataclass, give it a path pattern, and its instances can save and
|
|
7
|
+
load themselves as readable YAML, JSON, TOML, or text.
|
|
8
|
+
|
|
9
|
+
It is built for the stuff that belongs in a repo or project folder: prompts,
|
|
10
|
+
configs, fixtures, lightweight app state, and little durable objects with names.
|
|
11
|
+
|
|
12
|
+
You can use a `Stash` to pick a different location for the
|
|
13
|
+
serialized file (with env overrides) and have special format rules when you
|
|
14
|
+
need. You can also include a `sidecar` when you have a doc or binary you want to save next to it.
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
## ORM is a snap!
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
from snapclass import snapclass
|
|
22
|
+
|
|
23
|
+
@snapclass("{self.name}.yml")
|
|
24
|
+
class Note:
|
|
25
|
+
name: str
|
|
26
|
+
title: str
|
|
27
|
+
body: str = ""
|
|
28
|
+
|
|
29
|
+
mynote = Note(
|
|
30
|
+
"first_note",
|
|
31
|
+
"Today I used snapclass!",
|
|
32
|
+
"It was my first day of snapclass. My note was saved to YAML for me!",
|
|
33
|
+
)
|
|
34
|
+
mynote.snapshot.save()
|
|
35
|
+
|
|
36
|
+
# Load it back later with the same name.
|
|
37
|
+
same_note = Note.snapshots.get("first_note")
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from snapclass import snapclass, Stash, Fresh
|
|
42
|
+
|
|
43
|
+
# Create a default location with an environment override.
|
|
44
|
+
runsloc = Stash("./runs", env="RUNS_DIR")
|
|
45
|
+
|
|
46
|
+
@snapclass("{self.name}.yml", stash=runsloc)
|
|
47
|
+
class RunData:
|
|
48
|
+
name: str
|
|
49
|
+
# shortcuts for common boilerplate factory code
|
|
50
|
+
metrics: dict[str, float] = Fresh.Dict
|
|
51
|
+
|
|
52
|
+
RunData("baseline", {"accuracy": 0.98, "loss": 0.04}).snapshot.save()
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from snapclass import snapclass, Stash, sidecar
|
|
57
|
+
|
|
58
|
+
@snapclass
|
|
59
|
+
class Style:
|
|
60
|
+
voice: str
|
|
61
|
+
temperature: float
|
|
62
|
+
|
|
63
|
+
# Locations can be nested with stashes.
|
|
64
|
+
app = Stash("./myapp", env="MYAPP_DATA")
|
|
65
|
+
articles = app / "articles"
|
|
66
|
+
|
|
67
|
+
@snapclass("{self.slug}/article.yml", stash=articles)
|
|
68
|
+
class Article:
|
|
69
|
+
slug: str
|
|
70
|
+
title: str
|
|
71
|
+
style: Style
|
|
72
|
+
body: str = sidecar.text("{self.slug}.md")
|
|
73
|
+
|
|
74
|
+
article = Article(
|
|
75
|
+
"dusk-court",
|
|
76
|
+
"Dusk Court",
|
|
77
|
+
Style("warm", 0.4),
|
|
78
|
+
body="# Dusk Court\n\nBe brief, warm, and useful.\n",
|
|
79
|
+
)
|
|
80
|
+
loaded = Article.snapshots.get("dusk-court")
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## FAQ
|
|
84
|
+
|
|
85
|
+
### Why use `snapclass` over `datafiles`?
|
|
86
|
+
|
|
87
|
+
`datafiles` is great and you should absolutely use it for your app or script! I love it so much and use it in project after project. After years of use, I've run into a few limitations-- like issues when multiple modules needed different `datafiles` behavior in one process (because much of the behavior control is global). I designed `snapclass` to isolate some of that via stashes and added some extra features I liked along the way.
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
snapclass is licensed under the MIT License. See [LICENSE](LICENSE).
|
|
92
|
+
|
|
93
|
+
Much of snapclass's core behavior, along with portions of its implementation
|
|
94
|
+
and test suite, is adapted from the wonderful [`datafiles`](https://github.com/jacebrowning/datafiles) project by [Jace Browning](https://github.com/jacebrowning). See [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md).
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "snapclass"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Human-readable file persistence for Python dataclasses."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [
|
|
14
|
+
{ name = "Mattie Casper" },
|
|
15
|
+
]
|
|
16
|
+
keywords = [
|
|
17
|
+
"dataclasses",
|
|
18
|
+
"persistence",
|
|
19
|
+
"serialization",
|
|
20
|
+
"yaml",
|
|
21
|
+
"toml",
|
|
22
|
+
]
|
|
23
|
+
classifiers = [
|
|
24
|
+
"Development Status :: 3 - Alpha",
|
|
25
|
+
"Intended Audience :: Developers",
|
|
26
|
+
"Programming Language :: Python :: 3",
|
|
27
|
+
"Programming Language :: Python :: 3.11",
|
|
28
|
+
"Programming Language :: Python :: 3.12",
|
|
29
|
+
"Programming Language :: Python :: 3.13",
|
|
30
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
31
|
+
"Typing :: Typed",
|
|
32
|
+
]
|
|
33
|
+
dependencies = [
|
|
34
|
+
"json5>=0.9,<1",
|
|
35
|
+
"ruamel.yaml>=0.18,<0.20",
|
|
36
|
+
"tomlkit>=0.10,<0.16",
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
[project.optional-dependencies]
|
|
40
|
+
test = [
|
|
41
|
+
"pytest>=8",
|
|
42
|
+
]
|
|
43
|
+
typing = [
|
|
44
|
+
"mypy>=1.11,<3",
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
[project.urls]
|
|
48
|
+
Repository = "https://github.com/Mattie/snapclass"
|
|
49
|
+
Issues = "https://github.com/Mattie/snapclass/issues"
|
|
50
|
+
|
|
51
|
+
[tool.pytest.ini_options]
|
|
52
|
+
testpaths = ["tests"]
|
|
53
|
+
pythonpath = ["src"]
|
|
54
|
+
|
|
55
|
+
[tool.setuptools.packages.find]
|
|
56
|
+
where = ["src"]
|
|
57
|
+
|
|
58
|
+
[tool.setuptools.package-data]
|
|
59
|
+
snapclass = ["py.typed"]
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
from dataclasses import field
|
|
2
|
+
|
|
3
|
+
from . import serializers, formatters, hooks, plugins, sessions, sidecar, types
|
|
4
|
+
from .schemas import (
|
|
5
|
+
Collection,
|
|
6
|
+
Config,
|
|
7
|
+
SnapclassError,
|
|
8
|
+
Missing,
|
|
9
|
+
Model,
|
|
10
|
+
Snapshot,
|
|
11
|
+
auto,
|
|
12
|
+
create_model,
|
|
13
|
+
snapclass,
|
|
14
|
+
frozen,
|
|
15
|
+
sync,
|
|
16
|
+
)
|
|
17
|
+
from .fresh import Fresh
|
|
18
|
+
from .stash import Stash
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"Fresh",
|
|
22
|
+
"Missing",
|
|
23
|
+
"SnapclassError",
|
|
24
|
+
"Collection",
|
|
25
|
+
"Config",
|
|
26
|
+
"Model",
|
|
27
|
+
"Snapshot",
|
|
28
|
+
"Stash",
|
|
29
|
+
"auto",
|
|
30
|
+
"serializers",
|
|
31
|
+
"create_model",
|
|
32
|
+
"snapclass",
|
|
33
|
+
"field",
|
|
34
|
+
"formatters",
|
|
35
|
+
"frozen",
|
|
36
|
+
"hooks",
|
|
37
|
+
"plugins",
|
|
38
|
+
"sessions",
|
|
39
|
+
"sidecar",
|
|
40
|
+
"sync",
|
|
41
|
+
"types",
|
|
42
|
+
]
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import dataclasses
|
|
4
|
+
import os
|
|
5
|
+
from collections.abc import Iterator
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from . import sessions
|
|
9
|
+
from .stash import Stash
|
|
10
|
+
|
|
11
|
+
_missing_argument = object()
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class CollectionDescriptor:
|
|
15
|
+
def __get__(self, obj: object, cls: type) -> "Collection":
|
|
16
|
+
return Collection(cls)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class Collection:
|
|
20
|
+
def __init__(self, cls: type, stash: Stash | None = None) -> None:
|
|
21
|
+
self.model = cls
|
|
22
|
+
self._stash = stash
|
|
23
|
+
|
|
24
|
+
def __call__(self, stash: Stash | str | os.PathLike[str]) -> "Collection":
|
|
25
|
+
from .schemas import _coerce_stash
|
|
26
|
+
|
|
27
|
+
return Collection(self.model, _coerce_stash(stash))
|
|
28
|
+
|
|
29
|
+
def get(self, *args: Any, **kwargs: Any) -> Any:
|
|
30
|
+
from .schemas import _attach_snapshot
|
|
31
|
+
|
|
32
|
+
__tracebackhide__ = sessions.HIDDEN_TRACEBACK
|
|
33
|
+
instance = self._empty_instance(*args, **kwargs)
|
|
34
|
+
_attach_snapshot(instance, self.model.__snapclass_config__, self._stash)
|
|
35
|
+
instance.snapshot.load(_initial=True)
|
|
36
|
+
return instance
|
|
37
|
+
|
|
38
|
+
def get_or_none(self, *args: Any, **kwargs: Any) -> Any | None:
|
|
39
|
+
__tracebackhide__ = sessions.HIDDEN_TRACEBACK
|
|
40
|
+
try:
|
|
41
|
+
return self.get(*args, **kwargs)
|
|
42
|
+
except FileNotFoundError:
|
|
43
|
+
return None
|
|
44
|
+
|
|
45
|
+
def get_or_create(self, *args: Any, **kwargs: Any) -> Any:
|
|
46
|
+
from .schemas import _attach_snapshot, _write_lock_for
|
|
47
|
+
|
|
48
|
+
__tracebackhide__ = sessions.HIDDEN_TRACEBACK
|
|
49
|
+
instance = self._empty_instance(*args, **kwargs, include_defaults=True)
|
|
50
|
+
_attach_snapshot(instance, self.model.__snapclass_config__, self._stash)
|
|
51
|
+
with _write_lock_for(instance.snapshot._require_path()):
|
|
52
|
+
if instance.snapshot.exists:
|
|
53
|
+
instance.snapshot.load(_initial=True)
|
|
54
|
+
else:
|
|
55
|
+
instance.snapshot.save()
|
|
56
|
+
return instance
|
|
57
|
+
|
|
58
|
+
def all(self, *, _exclude: str = "") -> Iterator[Any]:
|
|
59
|
+
from .schemas import _PatternMatcher, _attach_snapshot, _has_path_value
|
|
60
|
+
|
|
61
|
+
__tracebackhide__ = sessions.HIDDEN_TRACEBACK
|
|
62
|
+
if not self.model.__snapclass_config__.pattern:
|
|
63
|
+
raise RuntimeError("'pattern' must be set")
|
|
64
|
+
try:
|
|
65
|
+
matcher = _PatternMatcher(self.model, self._stash)
|
|
66
|
+
except ValueError as exc:
|
|
67
|
+
raise ValueError(
|
|
68
|
+
f"Unable to scan {self.model.__name__}: {exc}"
|
|
69
|
+
) from exc
|
|
70
|
+
for path in matcher.iter_candidates():
|
|
71
|
+
if path.is_dir():
|
|
72
|
+
continue
|
|
73
|
+
values = matcher.values_from(path)
|
|
74
|
+
if _exclude and values and str(values[0]).startswith(_exclude):
|
|
75
|
+
continue
|
|
76
|
+
if matcher.has_recursive_wildcard or _has_path_value(values):
|
|
77
|
+
instance = self._empty_instance(*values)
|
|
78
|
+
_attach_snapshot(instance, self.model.__snapclass_config__, self._stash)
|
|
79
|
+
instance.snapshot.load(path, _initial=True)
|
|
80
|
+
yield instance
|
|
81
|
+
else:
|
|
82
|
+
yield self.get(*values)
|
|
83
|
+
|
|
84
|
+
def filter(self, *, _exclude: str = "", **query: Any) -> Iterator[Any]:
|
|
85
|
+
from .schemas import _lookup
|
|
86
|
+
|
|
87
|
+
__tracebackhide__ = sessions.HIDDEN_TRACEBACK
|
|
88
|
+
for item in self.all(_exclude=_exclude):
|
|
89
|
+
if all(_lookup(item, key) == value for key, value in query.items()):
|
|
90
|
+
yield item
|
|
91
|
+
|
|
92
|
+
def _empty_instance(self, *args: Any, include_defaults: bool = False, **kwargs: Any) -> Any:
|
|
93
|
+
from .schemas import (
|
|
94
|
+
Missing,
|
|
95
|
+
_coerce,
|
|
96
|
+
_field_default,
|
|
97
|
+
_is_missing,
|
|
98
|
+
_placeholder_for,
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
instance = self.model.__new__(self.model)
|
|
102
|
+
fields = [f for f in dataclasses.fields(self.model) if f.init]
|
|
103
|
+
hints = self.model.__snapclass_config__.type_hints
|
|
104
|
+
pattern = self.model.__snapclass_config__.pattern or ""
|
|
105
|
+
arg_iter = iter(args)
|
|
106
|
+
for field in fields:
|
|
107
|
+
try:
|
|
108
|
+
value = next(arg_iter)
|
|
109
|
+
except StopIteration:
|
|
110
|
+
value = kwargs.get(field.name, _missing_argument)
|
|
111
|
+
if value is _missing_argument:
|
|
112
|
+
default = _field_default(field)
|
|
113
|
+
if include_defaults:
|
|
114
|
+
value = default
|
|
115
|
+
elif not _is_missing(default):
|
|
116
|
+
value = default
|
|
117
|
+
elif _placeholder_for(field.name) in pattern:
|
|
118
|
+
value = default
|
|
119
|
+
if _is_missing(value):
|
|
120
|
+
raise TypeError(
|
|
121
|
+
"Collection.get() missing required placeholder field "
|
|
122
|
+
f"argument: '{field.name}'"
|
|
123
|
+
)
|
|
124
|
+
else:
|
|
125
|
+
value = Missing
|
|
126
|
+
elif _placeholder_for(field.name) in pattern:
|
|
127
|
+
value = _coerce(value, hints.get(field.name))
|
|
128
|
+
object.__setattr__(instance, field.name, value)
|
|
129
|
+
object.__setattr__(instance, "_snapclass_initializing", False)
|
|
130
|
+
return instance
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
__all__ = ["Collection", "CollectionDescriptor"]
|