stapel-attributes 0.1.1__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.
Files changed (77) hide show
  1. stapel_attributes-0.1.1/LICENSE +21 -0
  2. stapel_attributes-0.1.1/PKG-INFO +107 -0
  3. stapel_attributes-0.1.1/README.md +87 -0
  4. stapel_attributes-0.1.1/__init__.py +133 -0
  5. stapel_attributes-0.1.1/base.py +362 -0
  6. stapel_attributes-0.1.1/conf.py +22 -0
  7. stapel_attributes-0.1.1/conftest.py +46 -0
  8. stapel_attributes-0.1.1/errors.py +70 -0
  9. stapel_attributes-0.1.1/exceptions.py +44 -0
  10. stapel_attributes-0.1.1/py.typed +0 -0
  11. stapel_attributes-0.1.1/pyproject.toml +58 -0
  12. stapel_attributes-0.1.1/registry.py +502 -0
  13. stapel_attributes-0.1.1/results.py +120 -0
  14. stapel_attributes-0.1.1/serializers.py +197 -0
  15. stapel_attributes-0.1.1/setup.cfg +4 -0
  16. stapel_attributes-0.1.1/stapel_attributes.egg-info/PKG-INFO +107 -0
  17. stapel_attributes-0.1.1/stapel_attributes.egg-info/SOURCES.txt +142 -0
  18. stapel_attributes-0.1.1/stapel_attributes.egg-info/dependency_links.txt +1 -0
  19. stapel_attributes-0.1.1/stapel_attributes.egg-info/requires.txt +7 -0
  20. stapel_attributes-0.1.1/stapel_attributes.egg-info/top_level.txt +1 -0
  21. stapel_attributes-0.1.1/tests/__init__.py +0 -0
  22. stapel_attributes-0.1.1/tests/sample_types.py +148 -0
  23. stapel_attributes-0.1.1/tests/sample_types_module.py +66 -0
  24. stapel_attributes-0.1.1/tests/test_feature_types.py +975 -0
  25. stapel_attributes-0.1.1/tests/test_public_api.py +98 -0
  26. stapel_attributes-0.1.1/tests/test_registry.py +162 -0
  27. stapel_attributes-0.1.1/tests/test_validation.py +440 -0
  28. stapel_attributes-0.1.1/types/__init__.py +31 -0
  29. stapel_attributes-0.1.1/types/bool/__init__.py +16 -0
  30. stapel_attributes-0.1.1/types/bool/config.py +20 -0
  31. stapel_attributes-0.1.1/types/bool/dao.py +21 -0
  32. stapel_attributes-0.1.1/types/bool/dto.py +19 -0
  33. stapel_attributes-0.1.1/types/bool/type.py +131 -0
  34. stapel_attributes-0.1.1/types/date/__init__.py +16 -0
  35. stapel_attributes-0.1.1/types/date/config.py +27 -0
  36. stapel_attributes-0.1.1/types/date/dao.py +19 -0
  37. stapel_attributes-0.1.1/types/date/dto.py +19 -0
  38. stapel_attributes-0.1.1/types/date/type.py +252 -0
  39. stapel_attributes-0.1.1/types/float/__init__.py +16 -0
  40. stapel_attributes-0.1.1/types/float/config.py +27 -0
  41. stapel_attributes-0.1.1/types/float/dao.py +23 -0
  42. stapel_attributes-0.1.1/types/float/dto.py +19 -0
  43. stapel_attributes-0.1.1/types/float/type.py +180 -0
  44. stapel_attributes-0.1.1/types/header/__init__.py +18 -0
  45. stapel_attributes-0.1.1/types/header/config.py +19 -0
  46. stapel_attributes-0.1.1/types/header/constants.py +3 -0
  47. stapel_attributes-0.1.1/types/header/dao.py +19 -0
  48. stapel_attributes-0.1.1/types/header/dto.py +19 -0
  49. stapel_attributes-0.1.1/types/header/type.py +108 -0
  50. stapel_attributes-0.1.1/types/hex_color/__init__.py +20 -0
  51. stapel_attributes-0.1.1/types/hex_color/config.py +48 -0
  52. stapel_attributes-0.1.1/types/hex_color/constants.py +10 -0
  53. stapel_attributes-0.1.1/types/hex_color/dao.py +26 -0
  54. stapel_attributes-0.1.1/types/hex_color/dto.py +39 -0
  55. stapel_attributes-0.1.1/types/hex_color/type.py +294 -0
  56. stapel_attributes-0.1.1/types/hierarchical_select/__init__.py +23 -0
  57. stapel_attributes-0.1.1/types/hierarchical_select/config.py +59 -0
  58. stapel_attributes-0.1.1/types/hierarchical_select/dao.py +23 -0
  59. stapel_attributes-0.1.1/types/hierarchical_select/dto.py +23 -0
  60. stapel_attributes-0.1.1/types/hierarchical_select/type.py +296 -0
  61. stapel_attributes-0.1.1/types/int/__init__.py +16 -0
  62. stapel_attributes-0.1.1/types/int/config.py +27 -0
  63. stapel_attributes-0.1.1/types/int/dao.py +23 -0
  64. stapel_attributes-0.1.1/types/int/dto.py +19 -0
  65. stapel_attributes-0.1.1/types/int/type.py +161 -0
  66. stapel_attributes-0.1.1/types/select/__init__.py +19 -0
  67. stapel_attributes-0.1.1/types/select/config.py +51 -0
  68. stapel_attributes-0.1.1/types/select/constants.py +4 -0
  69. stapel_attributes-0.1.1/types/select/dao.py +21 -0
  70. stapel_attributes-0.1.1/types/select/dto.py +19 -0
  71. stapel_attributes-0.1.1/types/select/type.py +259 -0
  72. stapel_attributes-0.1.1/types/string/__init__.py +16 -0
  73. stapel_attributes-0.1.1/types/string/config.py +27 -0
  74. stapel_attributes-0.1.1/types/string/dao.py +21 -0
  75. stapel_attributes-0.1.1/types/string/dto.py +19 -0
  76. stapel_attributes-0.1.1/types/string/type.py +153 -0
  77. stapel_attributes-0.1.1/validation.py +731 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Stapel contributors
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.
@@ -0,0 +1,107 @@
1
+ Metadata-Version: 2.4
2
+ Name: stapel-attributes
3
+ Version: 0.1.1
4
+ Summary: Typed attributes engine for the Stapel framework
5
+ License: MIT
6
+ Keywords: django,stapel,attributes
7
+ Classifier: Framework :: Django
8
+ Classifier: Programming Language :: Python :: 3.11
9
+ Classifier: Programming Language :: Python :: 3.12
10
+ Requires-Python: >=3.11
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+ Requires-Dist: stapel-core<0.4,>=0.3.0
14
+ Requires-Dist: djangorestframework>=3.14
15
+ Requires-Dist: djangorestframework-dataclasses>=1.2
16
+ Requires-Dist: drf-polymorphic<3,>=2.1
17
+ Requires-Dist: drf-spectacular>=0.27
18
+ Provides-Extra: all
19
+ Dynamic: license-file
20
+
21
+ # stapel-attributes
22
+
23
+ Typed attributes engine for the [Stapel framework](https://github.com/usestapel) —
24
+ a polymorphic type system for attribute ("feature") configurations: an open
25
+ registry of feature types, Config/DTO/DAO layering per type, polymorphic DRF
26
+ serializers with OpenAPI schemas, and a structured value-validation pipeline.
27
+
28
+ This is an **L1 library**, not a module: it ships no models, migrations,
29
+ views, urls or comm surface. Both `stapel-categories` (attribute schema) and
30
+ `stapel-listings` (attribute values) import it — the code both need
31
+ synchronously lives one layer down, like `stapel-core` itself.
32
+
33
+ Provenance: port of the `categories/feature_types` engine and the
34
+ `ads` validation pipeline from waylot-catalog (see CHANGELOG for what was
35
+ fixed in transit).
36
+
37
+ ## Install
38
+
39
+ ```bash
40
+ pip install stapel-attributes
41
+ ```
42
+
43
+ No `INSTALLED_APPS` entry, no urls — just import it:
44
+
45
+ ```python
46
+ from stapel_attributes import (
47
+ FeatureDef,
48
+ validate_dto,
49
+ normalize_to_dao,
50
+ validate_dto_structured,
51
+ )
52
+
53
+ configs = [
54
+ FeatureDef(slug="mileage", config={"type": "int", "min": 0, "postfix": "km"}),
55
+ FeatureDef(slug="condition", config={"type": "bool"}, mandatory=True),
56
+ ]
57
+ payload = {"mileage": {"type": "int", "value": 120000},
58
+ "condition": {"type": "bool", "value": True}}
59
+
60
+ validate_dto(configs, payload) # raises ValidationError on failure
61
+ dao = normalize_to_dao(configs, payload) # {"mileage": {"type": "int", "value": 120000, "postfix": "km", "order": 0}, ...}
62
+ result = validate_dto_structured(configs, payload) # machine-readable batch result
63
+ ```
64
+
65
+ ## Built-in types
66
+
67
+ `int`, `float`, `string`, `bool`, `hex_color`, `select`, `date`, `header`,
68
+ `hierarchical_select`. Each type is a plugin: a Config dataclass (schema), a
69
+ DTO (client input), a DAO (stored value + display metadata) and a handler
70
+ (`BaseFeatureType[TConfig, TDto, TDao]`).
71
+
72
+ Marketplace-specific types (size grids, convertible units, ...) are **not**
73
+ shipped — hosts register their own via the open registry (see MODULE.md for
74
+ the worked example).
75
+
76
+ ## Settings
77
+
78
+ All configuration lives in the `STAPEL_ATTRIBUTES` namespace (dict setting,
79
+ flat setting, or env var — resolved lazily):
80
+
81
+ | Key | Default | Meaning |
82
+ |---|---|---|
83
+ | `EXTRA_TYPES` | `[]` | Dotted paths of extra feature types, **merged** over the built-ins (each entry: a `BaseFeatureType` subclass or a module that registers types on import). |
84
+
85
+ ## Structured validation
86
+
87
+ Every validation failure carries a machine code end-to-end
88
+ (`ValidationErrorCode`) via `FeatureValidationError` — no message parsing.
89
+ Batch validators return `ValidationBatchResult` rows with `error`,
90
+ `ref_value`, `localizable_error` (an `error.400.feature_*` key) and `params`.
91
+
92
+ ## Extension points
93
+
94
+ See [MODULE.md](MODULE.md) — the agent-facing map of every fork-free seam
95
+ (settings, the type registry, serializer factories, translation-key hooks).
96
+
97
+ ## Development
98
+
99
+ ```bash
100
+ pip install -e . && pip install pytest pytest-django ruff
101
+ ./setup-hooks.sh
102
+ pytest tests/
103
+ ```
104
+
105
+ ## License
106
+
107
+ MIT
@@ -0,0 +1,87 @@
1
+ # stapel-attributes
2
+
3
+ Typed attributes engine for the [Stapel framework](https://github.com/usestapel) —
4
+ a polymorphic type system for attribute ("feature") configurations: an open
5
+ registry of feature types, Config/DTO/DAO layering per type, polymorphic DRF
6
+ serializers with OpenAPI schemas, and a structured value-validation pipeline.
7
+
8
+ This is an **L1 library**, not a module: it ships no models, migrations,
9
+ views, urls or comm surface. Both `stapel-categories` (attribute schema) and
10
+ `stapel-listings` (attribute values) import it — the code both need
11
+ synchronously lives one layer down, like `stapel-core` itself.
12
+
13
+ Provenance: port of the `categories/feature_types` engine and the
14
+ `ads` validation pipeline from waylot-catalog (see CHANGELOG for what was
15
+ fixed in transit).
16
+
17
+ ## Install
18
+
19
+ ```bash
20
+ pip install stapel-attributes
21
+ ```
22
+
23
+ No `INSTALLED_APPS` entry, no urls — just import it:
24
+
25
+ ```python
26
+ from stapel_attributes import (
27
+ FeatureDef,
28
+ validate_dto,
29
+ normalize_to_dao,
30
+ validate_dto_structured,
31
+ )
32
+
33
+ configs = [
34
+ FeatureDef(slug="mileage", config={"type": "int", "min": 0, "postfix": "km"}),
35
+ FeatureDef(slug="condition", config={"type": "bool"}, mandatory=True),
36
+ ]
37
+ payload = {"mileage": {"type": "int", "value": 120000},
38
+ "condition": {"type": "bool", "value": True}}
39
+
40
+ validate_dto(configs, payload) # raises ValidationError on failure
41
+ dao = normalize_to_dao(configs, payload) # {"mileage": {"type": "int", "value": 120000, "postfix": "km", "order": 0}, ...}
42
+ result = validate_dto_structured(configs, payload) # machine-readable batch result
43
+ ```
44
+
45
+ ## Built-in types
46
+
47
+ `int`, `float`, `string`, `bool`, `hex_color`, `select`, `date`, `header`,
48
+ `hierarchical_select`. Each type is a plugin: a Config dataclass (schema), a
49
+ DTO (client input), a DAO (stored value + display metadata) and a handler
50
+ (`BaseFeatureType[TConfig, TDto, TDao]`).
51
+
52
+ Marketplace-specific types (size grids, convertible units, ...) are **not**
53
+ shipped — hosts register their own via the open registry (see MODULE.md for
54
+ the worked example).
55
+
56
+ ## Settings
57
+
58
+ All configuration lives in the `STAPEL_ATTRIBUTES` namespace (dict setting,
59
+ flat setting, or env var — resolved lazily):
60
+
61
+ | Key | Default | Meaning |
62
+ |---|---|---|
63
+ | `EXTRA_TYPES` | `[]` | Dotted paths of extra feature types, **merged** over the built-ins (each entry: a `BaseFeatureType` subclass or a module that registers types on import). |
64
+
65
+ ## Structured validation
66
+
67
+ Every validation failure carries a machine code end-to-end
68
+ (`ValidationErrorCode`) via `FeatureValidationError` — no message parsing.
69
+ Batch validators return `ValidationBatchResult` rows with `error`,
70
+ `ref_value`, `localizable_error` (an `error.400.feature_*` key) and `params`.
71
+
72
+ ## Extension points
73
+
74
+ See [MODULE.md](MODULE.md) — the agent-facing map of every fork-free seam
75
+ (settings, the type registry, serializer factories, translation-key hooks).
76
+
77
+ ## Development
78
+
79
+ ```bash
80
+ pip install -e . && pip install pytest pytest-django ruff
81
+ ./setup-hooks.sh
82
+ pytest tests/
83
+ ```
84
+
85
+ ## License
86
+
87
+ MIT
@@ -0,0 +1,133 @@
1
+ """stapel-attributes — typed attributes engine for the Stapel framework.
2
+
3
+ A polymorphic type system for attribute (feature) configurations: an open
4
+ registry of feature types, Config/DTO/DAO layering per type, polymorphic
5
+ DRF serializers with OpenAPI schemas, and a structured value-validation
6
+ pipeline (DTO -> DAO). Ported from waylot-catalog's ``categories/feature_types``
7
+ engine; see MODULE.md for provenance and extension points.
8
+
9
+ This is an L1 *library*: it has no models, migrations, views, urls or comm
10
+ surface of its own. Both stapel-categories (schema side) and stapel-listings
11
+ (value side) import it.
12
+
13
+ Public API is lazily exported (PEP 562) — importing this package never pulls
14
+ in Django or requires configured settings.
15
+ """
16
+
17
+ __all__ = [
18
+ # Settings
19
+ "attributes_settings",
20
+ # Base abstractions
21
+ "BaseFeatureType",
22
+ "DaoMeta",
23
+ "DictDataclassSerializer",
24
+ "FeatureDef",
25
+ "dataclass_to_dict_no_none",
26
+ # Structured errors & results
27
+ "FeatureValidationError",
28
+ "FeatureValidationResult",
29
+ "ValidationBatchResult",
30
+ "ValidationErrorCode",
31
+ "ValidationStatus",
32
+ # Registry
33
+ "register_feature_type",
34
+ "registered_types",
35
+ "get_feature_type",
36
+ "get_all_feature_types",
37
+ "get_all_type_slugs",
38
+ # Parse / convert
39
+ "parse_config",
40
+ "parse_dto",
41
+ "dao_to_dict",
42
+ "validate_feature_config",
43
+ "validate_feature_dto",
44
+ "dto_to_dao",
45
+ "normalize_feature_dto",
46
+ "get_default_value",
47
+ "format_feature_value",
48
+ "collect_translation_keys_for_feature",
49
+ "collect_all_builtin_translation_keys",
50
+ # Validation pipeline (configs -> DTO -> DAO)
51
+ "coerce_feature_defs",
52
+ "get_feature_slug",
53
+ "build_feature_lookup",
54
+ "validate_dto",
55
+ "normalize_to_dao",
56
+ "validate_dto_structured",
57
+ "validate_configs_structured",
58
+ "validate_dao_structured",
59
+ "validate_description",
60
+ # Polymorphic serializer factories
61
+ "get_feature_config_serializer_class",
62
+ "get_feature_dto_serializer_class",
63
+ "get_feature_dao_serializer_class",
64
+ "get_feature_config_proxy_serializer",
65
+ "get_feature_dto_proxy_serializer",
66
+ "get_feature_dao_proxy_serializer",
67
+ ]
68
+
69
+ # name -> submodule that defines it. Resolution is deferred until first
70
+ # attribute access so that `import stapel_attributes` stays Django-free.
71
+ _LAZY_EXPORTS = {
72
+ "attributes_settings": ".conf",
73
+ # base
74
+ "BaseFeatureType": ".base",
75
+ "DaoMeta": ".base",
76
+ "DictDataclassSerializer": ".base",
77
+ "FeatureDef": ".base",
78
+ "dataclass_to_dict_no_none": ".base",
79
+ # errors & results
80
+ "FeatureValidationError": ".exceptions",
81
+ "FeatureValidationResult": ".results",
82
+ "ValidationBatchResult": ".results",
83
+ "ValidationErrorCode": ".results",
84
+ "ValidationStatus": ".results",
85
+ # registry
86
+ "register_feature_type": ".registry",
87
+ "registered_types": ".registry",
88
+ "get_feature_type": ".registry",
89
+ "get_all_feature_types": ".registry",
90
+ "get_all_type_slugs": ".registry",
91
+ "parse_config": ".registry",
92
+ "parse_dto": ".registry",
93
+ "dao_to_dict": ".registry",
94
+ "validate_feature_config": ".registry",
95
+ "validate_feature_dto": ".registry",
96
+ "dto_to_dao": ".registry",
97
+ "normalize_feature_dto": ".registry",
98
+ "get_default_value": ".registry",
99
+ "format_feature_value": ".registry",
100
+ "collect_translation_keys_for_feature": ".registry",
101
+ "collect_all_builtin_translation_keys": ".registry",
102
+ # validation pipeline
103
+ "coerce_feature_defs": ".validation",
104
+ "get_feature_slug": ".validation",
105
+ "build_feature_lookup": ".validation",
106
+ "validate_dto": ".validation",
107
+ "normalize_to_dao": ".validation",
108
+ "validate_dto_structured": ".validation",
109
+ "validate_configs_structured": ".validation",
110
+ "validate_dao_structured": ".validation",
111
+ "validate_description": ".validation",
112
+ # serializer factories
113
+ "get_feature_config_serializer_class": ".serializers",
114
+ "get_feature_dto_serializer_class": ".serializers",
115
+ "get_feature_dao_serializer_class": ".serializers",
116
+ "get_feature_config_proxy_serializer": ".serializers",
117
+ "get_feature_dto_proxy_serializer": ".serializers",
118
+ "get_feature_dao_proxy_serializer": ".serializers",
119
+ }
120
+
121
+
122
+ def __getattr__(name):
123
+ if name in _LAZY_EXPORTS:
124
+ from importlib import import_module
125
+
126
+ value = getattr(import_module(_LAZY_EXPORTS[name], __name__), name)
127
+ globals()[name] = value # cache for subsequent lookups
128
+ return value
129
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
130
+
131
+
132
+ def __dir__():
133
+ return sorted(set(globals()) | set(__all__))