wodson 1.0.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.
wodson-1.0.0/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
wodson-1.0.0/PKG-INFO ADDED
@@ -0,0 +1,108 @@
1
+ Metadata-Version: 2.4
2
+ Name: wodson
3
+ Version: 1.0.0
4
+ Summary: ASAM ODS tools collection — ATFX reader, HTTP server, and more (experimental)
5
+ Keywords: ASAM,ODS,ATFX,measurement data,protobuf
6
+ Author: Andreas K
7
+ Author-email: Andreas K <totonga@gmail.com>
8
+ License-Expression: Apache-2.0
9
+ License-File: LICENSE
10
+ Classifier: Development Status :: 2 - Pre-Alpha
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Intended Audience :: Science/Research
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.14
16
+ Classifier: Topic :: Scientific/Engineering
17
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
18
+ Requires-Dist: numpy>=2.0
19
+ Requires-Dist: odsbox[oidc]>=1.2.0
20
+ Requires-Python: >=3.14
21
+ Project-URL: Homepage, https://github.com/totonga/asamatfx
22
+ Project-URL: Repository, https://github.com/totonga/asamatfx
23
+ Project-URL: Issues, https://github.com/totonga/asamatfx/issues
24
+ Project-URL: Changelog, https://github.com/totonga/asamatfx/releases
25
+ Description-Content-Type: text/markdown
26
+
27
+ # wodson
28
+
29
+ A collection of ASAM ODS tools.
30
+
31
+ ## Features
32
+
33
+ ### Experimental ATFX parser
34
+
35
+ This is an experimental ATFX reader. Target is not correctness or completeness, but a working prototype that can read some real-world ATFX files and be used as a base for further experiments. It currently supports:
36
+
37
+ - Parses ATFX XML files (ASAM ODS 5.1, 5.3, 6.2 schemas)
38
+ - Reads optional external binary `.dat` files (all standard typespecs)
39
+ - Loads everything into an in-memory SQLite database
40
+ - Exposes `ods.Model` (application model) and `ods.DataMatrices` (data-read)
41
+ - Embeddable Python library (`AtfxStore`)
42
+ - Standalone HTTP server compatible with `odsbox.ConI` (`AtfxServer`)
43
+ - CLI: `uv run wodson atfx serve --file path/to/file.atfx`
44
+
45
+ ## Requirements
46
+
47
+ - Python ≥ 3.14
48
+ - [`odsbox`](https://pypi.org/project/odsbox/) ≥ 1.2.0
49
+ - [`numpy`](https://numpy.org/) ≥ 2.0
50
+
51
+ ## Installation
52
+
53
+ ```bash
54
+ pip install wodson
55
+ # or with uv:
56
+ uv add wodson
57
+ ```
58
+
59
+ ## Quick Start
60
+
61
+ See [docs/atfx.md](docs/atfx.md) for usage examples (embedded library, HTTP server, CLI).
62
+
63
+ ## Project Layout
64
+
65
+ ```
66
+ src/wodson/
67
+ __init__.py Package root
68
+ _cli.py CLI entry point (wodson atfx serve …)
69
+ atfx/
70
+ __init__.py Public API (AtfxStore, AtfxServer, AtfxSession, …)
71
+ _cli.py atfx subcommand logic
72
+ base_model/ ASAM ODS base model protobuf JSON files
73
+ ODSBaseModel_asam37.protobuf.json
74
+ _atfx_store.py AtfxStore — parses + loads ATFX into SQLite
75
+ _server.py AtfxServer — ASAM ODS HTTP server
76
+ _session.py AtfxSession — in-process requests adapter
77
+ _base_model.py Loads ODSBaseModel JSON → ods.BaseModel
78
+ _model_builder.py Builds ods.Model from <application_model> XML
79
+ _instance_parser.py Parses <instance_data> (inline + external)
80
+ _binary_reader.py Reads external .dat binary files via numpy
81
+ _db.py SQLite schema creation + instance loading
82
+ _data_read.py SelectStatement → SQL → DataMatrices
83
+ _xml_utils.py Namespace-aware XML element lookup helpers
84
+ _naming.py ODS name → SQLite identifier utilities
85
+ docs/
86
+ atfx.md ATFX reader quick-start guide
87
+ USAGE.md Full end-user usage guide
88
+ spec/ ASAM schema files and base model JSON
89
+ tests/ pytest test suite
90
+ ```
91
+
92
+ ## Development
93
+
94
+ ```bash
95
+ # Install with dev dependencies
96
+ uv sync
97
+
98
+ # Run tests
99
+ uv run pytest tests/ -v
100
+
101
+ # Lint
102
+ uv run ruff check src/ tests/
103
+
104
+ # Type check
105
+ uv run mypy --strict src/
106
+ ```
107
+
108
+ See [docs/USAGE.md](docs/USAGE.md) for detailed end-user documentation.
wodson-1.0.0/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # wodson
2
+
3
+ A collection of ASAM ODS tools.
4
+
5
+ ## Features
6
+
7
+ ### Experimental ATFX parser
8
+
9
+ This is an experimental ATFX reader. Target is not correctness or completeness, but a working prototype that can read some real-world ATFX files and be used as a base for further experiments. It currently supports:
10
+
11
+ - Parses ATFX XML files (ASAM ODS 5.1, 5.3, 6.2 schemas)
12
+ - Reads optional external binary `.dat` files (all standard typespecs)
13
+ - Loads everything into an in-memory SQLite database
14
+ - Exposes `ods.Model` (application model) and `ods.DataMatrices` (data-read)
15
+ - Embeddable Python library (`AtfxStore`)
16
+ - Standalone HTTP server compatible with `odsbox.ConI` (`AtfxServer`)
17
+ - CLI: `uv run wodson atfx serve --file path/to/file.atfx`
18
+
19
+ ## Requirements
20
+
21
+ - Python ≥ 3.14
22
+ - [`odsbox`](https://pypi.org/project/odsbox/) ≥ 1.2.0
23
+ - [`numpy`](https://numpy.org/) ≥ 2.0
24
+
25
+ ## Installation
26
+
27
+ ```bash
28
+ pip install wodson
29
+ # or with uv:
30
+ uv add wodson
31
+ ```
32
+
33
+ ## Quick Start
34
+
35
+ See [docs/atfx.md](docs/atfx.md) for usage examples (embedded library, HTTP server, CLI).
36
+
37
+ ## Project Layout
38
+
39
+ ```
40
+ src/wodson/
41
+ __init__.py Package root
42
+ _cli.py CLI entry point (wodson atfx serve …)
43
+ atfx/
44
+ __init__.py Public API (AtfxStore, AtfxServer, AtfxSession, …)
45
+ _cli.py atfx subcommand logic
46
+ base_model/ ASAM ODS base model protobuf JSON files
47
+ ODSBaseModel_asam37.protobuf.json
48
+ _atfx_store.py AtfxStore — parses + loads ATFX into SQLite
49
+ _server.py AtfxServer — ASAM ODS HTTP server
50
+ _session.py AtfxSession — in-process requests adapter
51
+ _base_model.py Loads ODSBaseModel JSON → ods.BaseModel
52
+ _model_builder.py Builds ods.Model from <application_model> XML
53
+ _instance_parser.py Parses <instance_data> (inline + external)
54
+ _binary_reader.py Reads external .dat binary files via numpy
55
+ _db.py SQLite schema creation + instance loading
56
+ _data_read.py SelectStatement → SQL → DataMatrices
57
+ _xml_utils.py Namespace-aware XML element lookup helpers
58
+ _naming.py ODS name → SQLite identifier utilities
59
+ docs/
60
+ atfx.md ATFX reader quick-start guide
61
+ USAGE.md Full end-user usage guide
62
+ spec/ ASAM schema files and base model JSON
63
+ tests/ pytest test suite
64
+ ```
65
+
66
+ ## Development
67
+
68
+ ```bash
69
+ # Install with dev dependencies
70
+ uv sync
71
+
72
+ # Run tests
73
+ uv run pytest tests/ -v
74
+
75
+ # Lint
76
+ uv run ruff check src/ tests/
77
+
78
+ # Type check
79
+ uv run mypy --strict src/
80
+ ```
81
+
82
+ See [docs/USAGE.md](docs/USAGE.md) for detailed end-user documentation.
@@ -0,0 +1,96 @@
1
+ [project]
2
+ name = "wodson"
3
+ version = "1.0.0"
4
+ description = "ASAM ODS tools collection — ATFX reader, HTTP server, and more (experimental)"
5
+ readme = "README.md"
6
+ license = "Apache-2.0"
7
+ license-files = ["LICENSE"]
8
+ requires-python = ">=3.14"
9
+ authors = [
10
+ { name = "Andreas K", email = "totonga@gmail.com" },
11
+ ]
12
+ keywords = ["ASAM", "ODS", "ATFX", "measurement data", "protobuf"]
13
+ classifiers = [
14
+ "Development Status :: 2 - Pre-Alpha",
15
+ "Intended Audience :: Developers",
16
+ "Intended Audience :: Science/Research",
17
+ "Operating System :: OS Independent",
18
+ "Programming Language :: Python :: 3",
19
+ "Programming Language :: Python :: 3.14",
20
+ "Topic :: Scientific/Engineering",
21
+ "Topic :: Software Development :: Libraries :: Python Modules",
22
+ ]
23
+ dependencies = [
24
+ "numpy>=2.0",
25
+ "odsbox[oidc]>=1.2.0",
26
+ ]
27
+
28
+ [project.urls]
29
+ Homepage = "https://github.com/totonga/asamatfx"
30
+ Repository = "https://github.com/totonga/asamatfx"
31
+ Issues = "https://github.com/totonga/asamatfx/issues"
32
+ Changelog = "https://github.com/totonga/asamatfx/releases"
33
+
34
+ [project.scripts]
35
+ wodson = "wodson._cli:main"
36
+
37
+ [build-system]
38
+ requires = ["uv_build>=0.6,<0.12"]
39
+ build-backend = "uv_build"
40
+
41
+ [dependency-groups]
42
+ dev = [
43
+ "mypy>=1.20.2",
44
+ "pytest>=9.0.3",
45
+ "pytest-cov>=7.1.0",
46
+ "python-semantic-release>=9.0.0",
47
+ "ruff>=0.15.12",
48
+ "types-protobuf>=7.34.1.20260503",
49
+ "types-requests>=2.33.0.20260503",
50
+ ]
51
+
52
+ [tool.mypy]
53
+ strict = true
54
+ python_version = "3.14"
55
+ warn_return_any = true
56
+ plugins = []
57
+
58
+ [[tool.mypy.overrides]]
59
+ module = ["tests.*"]
60
+ disallow_untyped_defs = false
61
+
62
+ [tool.ruff]
63
+ target-version = "py314"
64
+ line-length = 120
65
+
66
+ [tool.ruff.lint]
67
+ select = ["E", "F", "I", "UP", "ANN"]
68
+ ignore = ["ANN401"]
69
+
70
+ [tool.ruff.lint.per-file-ignores]
71
+ "tests/**" = ["ANN"]
72
+
73
+ [tool.pytest.ini_options]
74
+ pythonpath = ["src"]
75
+ testpaths = ["tests"]
76
+ markers = [
77
+ "integration: tests that require a live ASAM ODS server",
78
+ "devtest: tests that require locally available spec files (docs/spec/, not checked in) — excluded from CI",
79
+ ]
80
+
81
+ [tool.setuptools.package-data]
82
+ wodson = ["py.typed"]
83
+ "wodson.atfx" = ["base_model/*"]
84
+
85
+ [tool.semantic_release]
86
+ version_toml = ["pyproject.toml:project.version"]
87
+ version_variables = ["src/wodson/__init__.py:__version__"]
88
+ tag_format = "v{version}"
89
+ build_command = "uv build"
90
+
91
+ [tool.semantic_release.branches.main]
92
+ match = "main"
93
+ prerelease = false
94
+
95
+ [tool.semantic_release.changelog]
96
+ exclude_commit_patterns = ["^Merge", "^chore"]
@@ -0,0 +1,3 @@
1
+ """wodson — ASAM ODS tools collection. Use ``wodson.atfx`` for the ATFX reader API."""
2
+
3
+ __version__ = "1.0.0"
@@ -0,0 +1,36 @@
1
+ """Top-level command-line interface for wodson.
2
+
3
+ Usage::
4
+
5
+ uv run wodson atfx serve
6
+ uv run wodson atfx serve --file path/to/file.atfx
7
+ uv run wodson atfx serve --file path/to/file.atfx --host 0.0.0.0 --port 8080
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import argparse
13
+
14
+ import wodson.atfx._cli as _atfx_cli
15
+
16
+
17
+ def _build_parser() -> argparse.ArgumentParser:
18
+ parser = argparse.ArgumentParser(
19
+ prog="wodson",
20
+ description="wodson ASAM ODS command-line tools.",
21
+ )
22
+ sub = parser.add_subparsers(dest="module", required=True)
23
+ _atfx_cli.register_atfx_subparser(sub)
24
+ return parser
25
+
26
+
27
+ def main() -> None:
28
+ """Entry point for the ``wodson`` CLI command."""
29
+ parser = _build_parser()
30
+ args = parser.parse_args()
31
+ if args.module == "atfx":
32
+ _atfx_cli.dispatch(args)
33
+
34
+
35
+ if __name__ == "__main__":
36
+ main()
@@ -0,0 +1,7 @@
1
+ """wodson.atfx — ATFX reader with in-memory SQLite backend and ODS data-read API."""
2
+
3
+ from ._atfx_store import AtfxStore
4
+ from ._server import CONTEXT_VAR_ATFX_FILE, AtfxServer
5
+ from ._session import AtfxSession
6
+
7
+ __all__ = ["AtfxServer", "AtfxSession", "AtfxStore", "CONTEXT_VAR_ATFX_FILE"]
@@ -0,0 +1,142 @@
1
+ """AtfxStore: main public class for loading and querying ATFX files."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import logging
6
+ import sqlite3
7
+ import xml.etree.ElementTree as ET
8
+ from pathlib import Path
9
+
10
+ import odsbox.proto.ods_pb2 as ods
11
+
12
+ from ._base_model import load_base_model
13
+ from ._data_read import data_read
14
+ from ._db import create_schema, load_instances
15
+ from ._instance_parser import parse_instances, resolve_external_component_refs
16
+ from ._model_builder import build_model, detect_ods_version
17
+ from ._xml_utils import _find, _findall, _text
18
+
19
+ _log = logging.getLogger(__name__)
20
+
21
+
22
+ class AtfxStore:
23
+ """Load an ATFX file into an in-memory SQLite database and provide ODS API access.
24
+
25
+ Usage::
26
+
27
+ store = AtfxStore("path/to/file.atfx")
28
+ model = store.model()
29
+ result = store.data_read(select_statement)
30
+ """
31
+
32
+ def __init__(self, file_path: str | Path, base_model_path: Path | None = None) -> None:
33
+ """Initialize the store by parsing and loading the ATFX file.
34
+
35
+ Args:
36
+ file_path: Path to the .atfx file.
37
+ base_model_path: Optional path to the base model JSON. Defaults to shipped version.
38
+ """
39
+ self._file_path = Path(file_path).resolve()
40
+ self._atfx_dir = self._file_path.parent
41
+ _log.info("Loading ATFX file: %s", self._file_path)
42
+
43
+ # Parse XML
44
+ tree = ET.parse(self._file_path) # noqa: S314
45
+ root = tree.getroot()
46
+ _log.debug("XML parsed successfully")
47
+
48
+ # Extract version metadata from the XML
49
+ self._ods_version: str = detect_ods_version(root)
50
+ self._base_model_version: str = _text(root, "base_model_version")
51
+ _log.debug(
52
+ "ODS version: %s, base model version: %s",
53
+ self._ods_version or "(not detected)",
54
+ self._base_model_version or "(not set)",
55
+ )
56
+
57
+ # Load base model
58
+ self._base_model = load_base_model(base_model_path)
59
+
60
+ # Build application model
61
+ self._model = build_model(root, self._base_model)
62
+
63
+ # Parse file map from <files> section
64
+ self._file_map = self._parse_file_map(root)
65
+ if self._file_map:
66
+ _log.debug("%d external file(s) mapped", len(self._file_map))
67
+
68
+ # Parse instances
69
+ instances = parse_instances(root, self._model)
70
+
71
+ # Resolve AoExternalComponent references (third value-reference pattern)
72
+ resolve_external_component_refs(self._model, instances, self._file_map, self._atfx_dir)
73
+
74
+ # Create in-memory SQLite DB
75
+ self._conn = sqlite3.connect(":memory:", check_same_thread=False)
76
+ create_schema(self._conn, self._model)
77
+ load_instances(self._conn, self._model, instances, self._file_map)
78
+ _log.info(
79
+ "AtfxStore ready: %d entities, %d instance groups",
80
+ len(self._model.entities),
81
+ len(instances),
82
+ )
83
+
84
+ def model(self) -> ods.Model:
85
+ """Return the application model as an ods.Model protobuf."""
86
+ return self._model
87
+
88
+ def data_read(self, select_statement: ods.SelectStatement) -> ods.DataMatrices:
89
+ """Execute a SelectStatement and return DataMatrices.
90
+
91
+ This matches the ASAM ODS HTTP API data-read method semantics.
92
+
93
+ Args:
94
+ select_statement: The ODS SelectStatement protobuf.
95
+
96
+ Returns:
97
+ ods.DataMatrices containing the query results.
98
+ """
99
+ return data_read(self._conn, self._model, select_statement)
100
+
101
+ def context_read(self) -> ods.ContextVariables:
102
+ """Return context variables for this ATFX session.
103
+
104
+ Populated variables:
105
+
106
+ * ``ASAM-ODS-VERSION`` -- ODS schema version extracted from the XML
107
+ namespace, e.g. ``"6.1.0"``.
108
+ * ``BASE-MODEL-VERSION`` -- value of ``<base_model_version>`` in the
109
+ ATFX file, e.g. ``"asam35"``.
110
+ """
111
+ ctx = ods.ContextVariables()
112
+ if self._ods_version:
113
+ ctx.variables["ASAM-ODS-VERSION"].string_array.values.append(self._ods_version)
114
+ if self._base_model_version:
115
+ ctx.variables["BASE-MODEL-VERSION"].string_array.values.append(self._base_model_version)
116
+ return ctx
117
+
118
+ def close(self) -> None:
119
+ """Close the SQLite connection."""
120
+ _log.debug("Closing AtfxStore for %s", self._file_path)
121
+ self._conn.close()
122
+
123
+ def __enter__(self) -> AtfxStore:
124
+ return self
125
+
126
+ def __exit__(self, *_args: object) -> None:
127
+ self.close()
128
+
129
+ def _parse_file_map(self, root: ET.Element) -> dict[str, Path]:
130
+ """Parse the <files> section to build identifier -> file path mapping."""
131
+ file_map: dict[str, Path] = {}
132
+ files_el = _find(root, "files")
133
+ if files_el is None:
134
+ return file_map
135
+
136
+ for comp_el in _findall(files_el, "component"):
137
+ identifier = _text(comp_el, "identifier")
138
+ filename = _text(comp_el, "filename")
139
+ if identifier and filename:
140
+ file_map[identifier] = self._atfx_dir / filename
141
+
142
+ return file_map