cimhub-core 2.0.0a0__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.
- cimhub_core-2.0.0a0/PKG-INFO +104 -0
- cimhub_core-2.0.0a0/README.md +83 -0
- cimhub_core-2.0.0a0/pyproject.toml +54 -0
- cimhub_core-2.0.0a0/setup.cfg +4 -0
- cimhub_core-2.0.0a0/src/cimhub_core/__init__.py +54 -0
- cimhub_core-2.0.0a0/src/cimhub_core/base.py +162 -0
- cimhub_core-2.0.0a0/src/cimhub_core/connectivity/__init__.py +32 -0
- cimhub_core-2.0.0a0/src/cimhub_core/connectivity/base_voltage.py +215 -0
- cimhub_core-2.0.0a0/src/cimhub_core/connectivity/lookup.py +24 -0
- cimhub_core-2.0.0a0/src/cimhub_core/connectivity/nodes.py +58 -0
- cimhub_core-2.0.0a0/src/cimhub_core/connectivity/terminals.py +83 -0
- cimhub_core-2.0.0a0/src/cimhub_core/domains.py +71 -0
- cimhub_core-2.0.0a0/src/cimhub_core/get_utils/__init__.py +5 -0
- cimhub_core-2.0.0a0/src/cimhub_core/get_utils/get_base_voltage.py +23 -0
- cimhub_core-2.0.0a0/src/cimhub_core/get_utils/get_substation.py +21 -0
- cimhub_core-2.0.0a0/src/cimhub_core/get_utils/get_voltage_level.py +25 -0
- cimhub_core-2.0.0a0/src/cimhub_core/get_utils/mermaid.py +275 -0
- cimhub_core-2.0.0a0/src/cimhub_core/graph_model.py +176 -0
- cimhub_core-2.0.0a0/src/cimhub_core/mcp/__init__.py +37 -0
- cimhub_core-2.0.0a0/src/cimhub_core/mcp/connection.py +249 -0
- cimhub_core-2.0.0a0/src/cimhub_core/mcp/dual_transport_service.py +442 -0
- cimhub_core-2.0.0a0/src/cimhub_core/mcp/jsonrpc.py +129 -0
- cimhub_core-2.0.0a0/src/cimhub_core/mcp/topics.py +35 -0
- cimhub_core-2.0.0a0/src/cimhub_core/registry.py +70 -0
- cimhub_core-2.0.0a0/src/cimhub_core/scaffold.py +531 -0
- cimhub_core-2.0.0a0/src/cimhub_core/serialization.py +513 -0
- cimhub_core-2.0.0a0/src/cimhub_core/transformations/__init__.py +3 -0
- cimhub_core-2.0.0a0/src/cimhub_core/transformations/lines/__init__.py +7 -0
- cimhub_core-2.0.0a0/src/cimhub_core/transformations/lines/conductor_distance_to_wire_positions.py +291 -0
- cimhub_core-2.0.0a0/src/cimhub_core/transformations/lines/wire_position_to_wire_spacing.py +35 -0
- cimhub_core-2.0.0a0/src/cimhub_core/transformations/loads/__init__.py +13 -0
- cimhub_core-2.0.0a0/src/cimhub_core/transformations/loads/load_response.py +150 -0
- cimhub_core-2.0.0a0/src/cimhub_core/transformations/transformers/__init__.py +64 -0
- cimhub_core-2.0.0a0/src/cimhub_core/transformations/transformers/impedance.py +333 -0
- cimhub_core-2.0.0a0/src/cimhub_core/transformations/transformers/per_unit.py +280 -0
- cimhub_core-2.0.0a0/src/cimhub_core/transformations/transformers/test_data.py +391 -0
- cimhub_core-2.0.0a0/src/cimhub_core.egg-info/PKG-INFO +104 -0
- cimhub_core-2.0.0a0/src/cimhub_core.egg-info/SOURCES.txt +39 -0
- cimhub_core-2.0.0a0/src/cimhub_core.egg-info/dependency_links.txt +1 -0
- cimhub_core-2.0.0a0/src/cimhub_core.egg-info/requires.txt +2 -0
- cimhub_core-2.0.0a0/src/cimhub_core.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cimhub_core
|
|
3
|
+
Version: 2.0.0a0
|
|
4
|
+
Summary: CIMHub Core Library — shared foundation for all CIMHub converter packages
|
|
5
|
+
Author-email: Pacific Northwest National Laboratory <gridappsd@pnnl.gov>
|
|
6
|
+
License: BSD-2-Clause
|
|
7
|
+
Project-URL: Homepage, https://github.com/PNNL-CIM-Tools/CIMHub_2_0
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/PNNL-CIM-Tools/CIMHub_2_0/issues
|
|
9
|
+
Project-URL: Documentation, https://pnnl-cim-tools.github.io/CIMHub_2_0/cimhub_core/
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Environment :: Console
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
16
|
+
Classifier: Topic :: Scientific/Engineering
|
|
17
|
+
Requires-Python: >=3.10
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
Requires-Dist: cim-graph==0.5.0a3
|
|
20
|
+
Requires-Dist: linkml-runtime~=1.9.5
|
|
21
|
+
|
|
22
|
+
# cimhub_core
|
|
23
|
+
|
|
24
|
+
Shared foundation for all CIMHub converter packages. Provides the base classes, data model, and utilities that every spoke in the hub-and-spoke architecture depends on.
|
|
25
|
+
|
|
26
|
+
## What's in here
|
|
27
|
+
|
|
28
|
+
| Module | Purpose |
|
|
29
|
+
|---|---|
|
|
30
|
+
| `base.py` | ABCs: `FormatReader`, `FormatWriter`, `FormatImporter`, `FormatExporter` |
|
|
31
|
+
| `graph_model.py` | `GraphModel` — typed in-memory store for LinkML dataclasses |
|
|
32
|
+
| `registry.py` | `ConverterRegistry` — decorator-based dispatch for import/export functions |
|
|
33
|
+
| `scaffold.py` | Code generator that scaffolds a new converter package from a template |
|
|
34
|
+
| `serialization.py` | Shared serialization helpers (CSV, YAML round-trips) |
|
|
35
|
+
| `connectivity/` | Topology helpers (bus lookups, connectivity node resolution) |
|
|
36
|
+
| `transformations/` | CIM-to-CIM geometry transforms (wire spacings, coordinate systems) |
|
|
37
|
+
| `get_utils/` | `get_or_create` helpers for common CIM objects (BaseVoltage, Substation, etc.) |
|
|
38
|
+
| `mcp/` | MCP server tools for LLM-assisted model introspection |
|
|
39
|
+
|
|
40
|
+
## Architecture
|
|
41
|
+
|
|
42
|
+
Every CIMHub converter follows the same four-layer pipeline:
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
LinkML YAML schemas → Generated dataclasses → GraphModel → FormatReader / FormatWriter
|
|
46
|
+
↕
|
|
47
|
+
FormatImporter / FormatExporter
|
|
48
|
+
(native ↔ CIM conversion)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
`cimhub_core` owns the bottom two layers (GraphModel + ABCs). Each spoke package owns its own LinkML schema, generated dataclasses, and converter implementations.
|
|
52
|
+
|
|
53
|
+
## GraphModel
|
|
54
|
+
|
|
55
|
+
The central data container. A sorted dictionary keyed by class then by identifier.
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from cimhub_core import GraphModel
|
|
59
|
+
|
|
60
|
+
model = GraphModel()
|
|
61
|
+
model.add_to_graph(obj)
|
|
62
|
+
|
|
63
|
+
# Retrieve all objects of a class (sorted by identifier)
|
|
64
|
+
buses = model.list_by_class(schema.BusRecord)
|
|
65
|
+
|
|
66
|
+
# Search by attribute value
|
|
67
|
+
gen = model.find_by_attribute(schema.GeneratorRecord, "bus_number", 101)
|
|
68
|
+
|
|
69
|
+
# Counts and membership
|
|
70
|
+
model.count(schema.BusRecord)
|
|
71
|
+
schema.BusRecord in model
|
|
72
|
+
len(model)
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Converter ABCs
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
from cimhub_core import FormatImporter, FormatExporter
|
|
79
|
+
|
|
80
|
+
class MyImporter(FormatImporter):
|
|
81
|
+
def to_cim(self, source, **kwargs):
|
|
82
|
+
...
|
|
83
|
+
|
|
84
|
+
class MyExporter(FormatExporter):
|
|
85
|
+
def to_format(self, cim_model, **kwargs):
|
|
86
|
+
...
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
See `cimhub_core/src/cimhub_core/development/CONVERTER_API.md` for the full contract.
|
|
90
|
+
|
|
91
|
+
## Development Docs
|
|
92
|
+
|
|
93
|
+
The `development/` directory contains the canonical references for all converter authors:
|
|
94
|
+
|
|
95
|
+
| Doc | What it covers |
|
|
96
|
+
|---|---|
|
|
97
|
+
| `CONVERTER_API.md` | Public API every converter must expose |
|
|
98
|
+
| `READER_WRITER_GUIDE.md` | `FormatReader` / `FormatWriter` implementation guide |
|
|
99
|
+
| `LINKML_TEMPLATE.md` | How to write LinkML schemas for native formats |
|
|
100
|
+
| `UNITS.md` | `CIMUnit` usage — never manually multiply by 1e3/1e6 |
|
|
101
|
+
| `STYLE_GUIDE.md` | Coding rules (KISS → DRY → Readable → Maintainable) |
|
|
102
|
+
| `ARCHITECTURE.md` | Full sub-package architecture diagram |
|
|
103
|
+
| `TEST_FIXTURE_ARCHITECTURE.md` | 4-tier test strategy |
|
|
104
|
+
| `CONDUCTOR_GEOMETRY_ANTI_PATTERNS.md` | Line/cable geometry export rules |
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# cimhub_core
|
|
2
|
+
|
|
3
|
+
Shared foundation for all CIMHub converter packages. Provides the base classes, data model, and utilities that every spoke in the hub-and-spoke architecture depends on.
|
|
4
|
+
|
|
5
|
+
## What's in here
|
|
6
|
+
|
|
7
|
+
| Module | Purpose |
|
|
8
|
+
|---|---|
|
|
9
|
+
| `base.py` | ABCs: `FormatReader`, `FormatWriter`, `FormatImporter`, `FormatExporter` |
|
|
10
|
+
| `graph_model.py` | `GraphModel` — typed in-memory store for LinkML dataclasses |
|
|
11
|
+
| `registry.py` | `ConverterRegistry` — decorator-based dispatch for import/export functions |
|
|
12
|
+
| `scaffold.py` | Code generator that scaffolds a new converter package from a template |
|
|
13
|
+
| `serialization.py` | Shared serialization helpers (CSV, YAML round-trips) |
|
|
14
|
+
| `connectivity/` | Topology helpers (bus lookups, connectivity node resolution) |
|
|
15
|
+
| `transformations/` | CIM-to-CIM geometry transforms (wire spacings, coordinate systems) |
|
|
16
|
+
| `get_utils/` | `get_or_create` helpers for common CIM objects (BaseVoltage, Substation, etc.) |
|
|
17
|
+
| `mcp/` | MCP server tools for LLM-assisted model introspection |
|
|
18
|
+
|
|
19
|
+
## Architecture
|
|
20
|
+
|
|
21
|
+
Every CIMHub converter follows the same four-layer pipeline:
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
LinkML YAML schemas → Generated dataclasses → GraphModel → FormatReader / FormatWriter
|
|
25
|
+
↕
|
|
26
|
+
FormatImporter / FormatExporter
|
|
27
|
+
(native ↔ CIM conversion)
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
`cimhub_core` owns the bottom two layers (GraphModel + ABCs). Each spoke package owns its own LinkML schema, generated dataclasses, and converter implementations.
|
|
31
|
+
|
|
32
|
+
## GraphModel
|
|
33
|
+
|
|
34
|
+
The central data container. A sorted dictionary keyed by class then by identifier.
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
from cimhub_core import GraphModel
|
|
38
|
+
|
|
39
|
+
model = GraphModel()
|
|
40
|
+
model.add_to_graph(obj)
|
|
41
|
+
|
|
42
|
+
# Retrieve all objects of a class (sorted by identifier)
|
|
43
|
+
buses = model.list_by_class(schema.BusRecord)
|
|
44
|
+
|
|
45
|
+
# Search by attribute value
|
|
46
|
+
gen = model.find_by_attribute(schema.GeneratorRecord, "bus_number", 101)
|
|
47
|
+
|
|
48
|
+
# Counts and membership
|
|
49
|
+
model.count(schema.BusRecord)
|
|
50
|
+
schema.BusRecord in model
|
|
51
|
+
len(model)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Converter ABCs
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from cimhub_core import FormatImporter, FormatExporter
|
|
58
|
+
|
|
59
|
+
class MyImporter(FormatImporter):
|
|
60
|
+
def to_cim(self, source, **kwargs):
|
|
61
|
+
...
|
|
62
|
+
|
|
63
|
+
class MyExporter(FormatExporter):
|
|
64
|
+
def to_format(self, cim_model, **kwargs):
|
|
65
|
+
...
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
See `cimhub_core/src/cimhub_core/development/CONVERTER_API.md` for the full contract.
|
|
69
|
+
|
|
70
|
+
## Development Docs
|
|
71
|
+
|
|
72
|
+
The `development/` directory contains the canonical references for all converter authors:
|
|
73
|
+
|
|
74
|
+
| Doc | What it covers |
|
|
75
|
+
|---|---|
|
|
76
|
+
| `CONVERTER_API.md` | Public API every converter must expose |
|
|
77
|
+
| `READER_WRITER_GUIDE.md` | `FormatReader` / `FormatWriter` implementation guide |
|
|
78
|
+
| `LINKML_TEMPLATE.md` | How to write LinkML schemas for native formats |
|
|
79
|
+
| `UNITS.md` | `CIMUnit` usage — never manually multiply by 1e3/1e6 |
|
|
80
|
+
| `STYLE_GUIDE.md` | Coding rules (KISS → DRY → Readable → Maintainable) |
|
|
81
|
+
| `ARCHITECTURE.md` | Full sub-package architecture diagram |
|
|
82
|
+
| `TEST_FIXTURE_ARCHITECTURE.md` | 4-tier test strategy |
|
|
83
|
+
| `CONDUCTOR_GEOMETRY_ANTI_PATTERNS.md` | Line/cable geometry export rules |
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools~=80.9"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "cimhub_core"
|
|
7
|
+
description = "CIMHub Core Library — shared foundation for all CIMHub converter packages"
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
version = "2.0.0a0"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { text = "BSD-2-Clause" }
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Pacific Northwest National Laboratory", email = "gridappsd@pnnl.gov" }
|
|
14
|
+
]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
"Environment :: Console",
|
|
19
|
+
"Development Status :: 3 - Alpha",
|
|
20
|
+
"Intended Audience :: Science/Research",
|
|
21
|
+
"License :: OSI Approved :: BSD License",
|
|
22
|
+
"Topic :: Scientific/Engineering",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
dependencies = [
|
|
26
|
+
"cim-graph ==0.5.0a3",
|
|
27
|
+
"linkml-runtime ~=1.9.5"
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
"Homepage" = "https://github.com/PNNL-CIM-Tools/CIMHub_2_0"
|
|
32
|
+
"Bug Tracker" = "https://github.com/PNNL-CIM-Tools/CIMHub_2_0/issues"
|
|
33
|
+
"Documentation" = "https://pnnl-cim-tools.github.io/CIMHub_2_0/cimhub_core/"
|
|
34
|
+
|
|
35
|
+
[tool.pytest.ini_options]
|
|
36
|
+
testpaths = ["tests"]
|
|
37
|
+
addopts = "-ra -q"
|
|
38
|
+
|
|
39
|
+
[tool.coverage.report]
|
|
40
|
+
skip_empty = true
|
|
41
|
+
|
|
42
|
+
[tool.mypy]
|
|
43
|
+
strict = false
|
|
44
|
+
ignore_missing_imports = false
|
|
45
|
+
disallow_untyped_defs = false
|
|
46
|
+
warn_unused_ignores = true
|
|
47
|
+
warn_unreachable = true
|
|
48
|
+
|
|
49
|
+
[tool.setuptools]
|
|
50
|
+
packages = { find = { where = ["src"] } }
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
[tool.setuptools.package-data]
|
|
54
|
+
"*" = ["*.json", "*.yaml"]
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""CIMHub core — shared foundation for all CIMHub sub-packages.
|
|
2
|
+
|
|
3
|
+
Public API:
|
|
4
|
+
GraphModel — sorted-dictionary data store for LinkML dataclasses
|
|
5
|
+
ConverterRegistry — decorator-based schema-to-function dispatcher
|
|
6
|
+
FormatReader — ABC for parsing native files into dataclasses
|
|
7
|
+
FormatWriter — ABC for writing dataclasses to native files
|
|
8
|
+
FormatImporter — ABC for native format → CIM conversion (to_cim)
|
|
9
|
+
FormatExporter — ABC for CIM → native format conversion (to_format)
|
|
10
|
+
See development/CONVERTER_API.md for the contract.
|
|
11
|
+
scaffold_package — generate directory tree for a new sub-package
|
|
12
|
+
get_mermaid — render a LinkML dataclass instance/class as a mermaid diagram
|
|
13
|
+
download_mermaid — render a mermaid diagram to an image via mermaid.ink
|
|
14
|
+
|
|
15
|
+
Serialization utilities (for FormatWriter implementations):
|
|
16
|
+
AnnotationSerializer — automatic serialization driven by LinkML annotations
|
|
17
|
+
get_schema_annotations — load annotations from YAML schema at runtime
|
|
18
|
+
get_field_order — get field names in annotated order
|
|
19
|
+
get_native_name — resolve native format name for a field
|
|
20
|
+
format_by_spec — format a value using a format specification
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
__version__ = "2.0.0a0"
|
|
24
|
+
|
|
25
|
+
from cimhub_core.base import FormatExporter, FormatImporter, FormatReader, FormatWriter
|
|
26
|
+
from cimhub_core.get_utils import download_mermaid, get_mermaid
|
|
27
|
+
from cimhub_core.graph_model import GraphModel
|
|
28
|
+
from cimhub_core.registry import ConverterRegistry
|
|
29
|
+
from cimhub_core.scaffold import scaffold_package
|
|
30
|
+
from cimhub_core.serialization import (
|
|
31
|
+
AnnotationSerializer,
|
|
32
|
+
format_by_spec,
|
|
33
|
+
get_field_order,
|
|
34
|
+
get_native_name,
|
|
35
|
+
get_schema_annotations,
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
__all__ = [
|
|
39
|
+
"GraphModel",
|
|
40
|
+
"ConverterRegistry",
|
|
41
|
+
"FormatReader",
|
|
42
|
+
"FormatWriter",
|
|
43
|
+
"FormatImporter",
|
|
44
|
+
"FormatExporter",
|
|
45
|
+
"scaffold_package",
|
|
46
|
+
"get_mermaid",
|
|
47
|
+
"download_mermaid",
|
|
48
|
+
# Serialization utilities
|
|
49
|
+
"AnnotationSerializer",
|
|
50
|
+
"get_schema_annotations",
|
|
51
|
+
"get_field_order",
|
|
52
|
+
"get_native_name",
|
|
53
|
+
"format_by_spec",
|
|
54
|
+
]
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"""Abstract base classes for format-specific I/O and conversion.
|
|
2
|
+
|
|
3
|
+
Every CIMHub sub-package implements one or more of these interfaces to
|
|
4
|
+
bridge its native file format with LinkML-generated dataclasses and the
|
|
5
|
+
CIM graph model.
|
|
6
|
+
|
|
7
|
+
Serialization layer (file ↔ LinkML objects):
|
|
8
|
+
FormatReader — parse native files into dataclass instances
|
|
9
|
+
FormatWriter — write dataclass instances back to native files
|
|
10
|
+
|
|
11
|
+
Conversion layer (LinkML objects ↔ CIM graph):
|
|
12
|
+
FormatImporter — native format → CIM graph
|
|
13
|
+
FormatExporter — CIM graph → native format
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
from abc import ABC, abstractmethod
|
|
19
|
+
from typing import Any, Optional, Type
|
|
20
|
+
|
|
21
|
+
__all__ = ["FormatReader", "FormatWriter", "FormatImporter", "FormatExporter"]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
# ---------------------------------------------------------------------------
|
|
25
|
+
# Serialization
|
|
26
|
+
# ---------------------------------------------------------------------------
|
|
27
|
+
|
|
28
|
+
class FormatReader(ABC):
|
|
29
|
+
"""Parse native format files into LinkML dataclass instances."""
|
|
30
|
+
|
|
31
|
+
@abstractmethod
|
|
32
|
+
def read(self, source: str, schema_class: Type) -> list:
|
|
33
|
+
"""Read *source* and return a list of *schema_class* instances.
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
source: Path to file or directory containing format-specific data.
|
|
37
|
+
schema_class: The LinkML-generated dataclass type to instantiate.
|
|
38
|
+
|
|
39
|
+
Returns:
|
|
40
|
+
List of populated dataclass instances.
|
|
41
|
+
"""
|
|
42
|
+
...
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class FormatWriter(ABC):
|
|
46
|
+
"""Write LinkML dataclass instances to native format files."""
|
|
47
|
+
|
|
48
|
+
@abstractmethod
|
|
49
|
+
def write(self, objects: list, destination: str) -> None:
|
|
50
|
+
"""Write *objects* to *destination* in the native format.
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
objects: List of LinkML-generated dataclass instances.
|
|
54
|
+
destination: Path to output file or directory.
|
|
55
|
+
"""
|
|
56
|
+
...
|
|
57
|
+
|
|
58
|
+
@abstractmethod
|
|
59
|
+
def serialize(self, obj) -> str:
|
|
60
|
+
"""Convert a single dataclass instance to its native format string.
|
|
61
|
+
|
|
62
|
+
Returns:
|
|
63
|
+
The native representation (DSS command, RAW record, GLM block, etc.).
|
|
64
|
+
"""
|
|
65
|
+
...
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
# ---------------------------------------------------------------------------
|
|
69
|
+
# Conversion
|
|
70
|
+
# ---------------------------------------------------------------------------
|
|
71
|
+
|
|
72
|
+
class FormatImporter(ABC):
|
|
73
|
+
"""Read a native format, convert to CIM, and return a populated graph model.
|
|
74
|
+
|
|
75
|
+
This is the canonical contract for every CIMHub importer. Subclasses declare
|
|
76
|
+
the concrete cimgraph model type they produce (``model_cls``) and the
|
|
77
|
+
``FormatReader`` they parse with (``reader_cls``), then implement
|
|
78
|
+
:meth:`to_cim` to drive the per-record conversion loop. The base class
|
|
79
|
+
provides :meth:`make_network` and :meth:`make_reader` so subclasses never
|
|
80
|
+
repeat the create-or-reuse boilerplate.
|
|
81
|
+
|
|
82
|
+
Each format also exposes a thin convenience function — ``<fmt>_to_cim`` —
|
|
83
|
+
that delegates to ``to_cim`` so users get a one-liner::
|
|
84
|
+
|
|
85
|
+
feeder = dss_to_cim("Master.dss") # convenience
|
|
86
|
+
feeder = DssImporter().to_cim("Master.dss") # equivalent
|
|
87
|
+
|
|
88
|
+
See ``cimhub_core/development/CONVERTER_API.md`` for the full spec.
|
|
89
|
+
"""
|
|
90
|
+
|
|
91
|
+
#: Concrete cimgraph GraphModel subclass this importer populates
|
|
92
|
+
#: (e.g. ``FeederModel``, ``NodeBreakerModel``, ``BusBranchModel``).
|
|
93
|
+
model_cls: type
|
|
94
|
+
|
|
95
|
+
#: ``FormatReader`` subclass used to parse the native source. May be ``None``
|
|
96
|
+
#: for formats whose ``source`` is an in-memory object (e.g. GDM).
|
|
97
|
+
reader_cls: Optional[type] = None
|
|
98
|
+
|
|
99
|
+
@abstractmethod
|
|
100
|
+
def to_cim(self, source: Any, *, network=None, reader=None):
|
|
101
|
+
"""Convert *source* to CIM and return the populated graph model.
|
|
102
|
+
|
|
103
|
+
Args:
|
|
104
|
+
source: Path to a file/directory (``str`` or ``Path``), or an
|
|
105
|
+
in-memory native object for formats without a file representation.
|
|
106
|
+
network: Existing graph model to populate. If ``None``, a new
|
|
107
|
+
``model_cls`` is created via :meth:`make_network`.
|
|
108
|
+
reader: Existing ``FormatReader`` to parse with. If ``None``, a new
|
|
109
|
+
``reader_cls`` is created via :meth:`make_reader`. Ignored by
|
|
110
|
+
formats whose ``source`` is already in memory.
|
|
111
|
+
|
|
112
|
+
Returns:
|
|
113
|
+
A populated cimgraph ``GraphModel`` subclass (never raw records).
|
|
114
|
+
"""
|
|
115
|
+
...
|
|
116
|
+
|
|
117
|
+
def make_network(self, network=None):
|
|
118
|
+
"""Return *network* if given, else a fresh empty ``self.model_cls``."""
|
|
119
|
+
if network is not None:
|
|
120
|
+
return network
|
|
121
|
+
from cimgraph.databases import XMLFile
|
|
122
|
+
return self.model_cls(connection=XMLFile(""), container=None)
|
|
123
|
+
|
|
124
|
+
def make_reader(self, reader=None):
|
|
125
|
+
"""Return *reader* if given, else a fresh ``self.reader_cls`` instance."""
|
|
126
|
+
if reader is not None:
|
|
127
|
+
return reader
|
|
128
|
+
if self.reader_cls is None:
|
|
129
|
+
raise TypeError(
|
|
130
|
+
f"{type(self).__name__} has no reader_cls; "
|
|
131
|
+
f"its source is consumed in memory"
|
|
132
|
+
)
|
|
133
|
+
return self.reader_cls()
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
class FormatExporter(ABC):
|
|
137
|
+
"""Traverse a CIM graph model and emit a native format.
|
|
138
|
+
|
|
139
|
+
Mirror of :class:`FormatImporter`. The first argument is always the CIM
|
|
140
|
+
``network`` (never a file path); the second is the ``destination`` (output
|
|
141
|
+
path, or an in-memory sink for formats like GDM). The return value is the
|
|
142
|
+
native artifact (a commands dict, an in-memory model, DataFrames) or
|
|
143
|
+
``None`` when the exporter only writes files — documented per format.
|
|
144
|
+
|
|
145
|
+
Each format exposes a thin ``cim_to_<fmt>`` convenience function delegating
|
|
146
|
+
to :meth:`to_format`. See ``CONVERTER_API.md`` for the full spec.
|
|
147
|
+
"""
|
|
148
|
+
|
|
149
|
+
@abstractmethod
|
|
150
|
+
def to_format(self, network, destination: Any, **opts):
|
|
151
|
+
"""Convert CIM objects in *network* and write/return the native format.
|
|
152
|
+
|
|
153
|
+
Args:
|
|
154
|
+
network: A cimgraph ``GraphModel`` containing CIM objects.
|
|
155
|
+
destination: Output file/directory path, or an in-memory target for
|
|
156
|
+
formats without a file representation.
|
|
157
|
+
**opts: Format-specific options (e.g. ``system_base_mva``).
|
|
158
|
+
|
|
159
|
+
Returns:
|
|
160
|
+
The native artifact, or ``None`` if the exporter only writes files.
|
|
161
|
+
"""
|
|
162
|
+
...
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""Shared connectivity helpers for CIM converters.
|
|
2
|
+
|
|
3
|
+
Terminal/ConnectivityNode wiring is identical across every cimhub importer:
|
|
4
|
+
a Terminal links a piece of ConductingEquipment to a ConnectivityNode, and CIM
|
|
5
|
+
requires *both* directions of every association to be populated
|
|
6
|
+
(``terminal.ConductingEquipment``/``equipment.Terminals`` and
|
|
7
|
+
``terminal.ConnectivityNode``/``connectivity_node.Terminals``). Exporters read the
|
|
8
|
+
back-references, so a converter that sets only the forward ref produces a graph that
|
|
9
|
+
looks wired but breaks downstream.
|
|
10
|
+
|
|
11
|
+
These helpers centralize that wiring. They are profile-agnostic: the CIM classes are
|
|
12
|
+
resolved at runtime from ``network.cim`` (the active data profile on the GraphModel),
|
|
13
|
+
so the same helper works for any profile (cim17v40, cimhub_2026, ...). The top-level
|
|
14
|
+
``import ... as cim`` is for typing/IDE only and is never used at runtime.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from cimhub_core.connectivity.base_voltage import (
|
|
18
|
+
get_or_create_base_voltage,
|
|
19
|
+
propagate_base_voltages,
|
|
20
|
+
)
|
|
21
|
+
from cimhub_core.connectivity.lookup import find_by_name
|
|
22
|
+
from cimhub_core.connectivity.nodes import get_or_create_connectivity_node
|
|
23
|
+
from cimhub_core.connectivity.terminals import attach_terminal_to_node, link_terminal
|
|
24
|
+
|
|
25
|
+
__all__ = [
|
|
26
|
+
"link_terminal",
|
|
27
|
+
"attach_terminal_to_node",
|
|
28
|
+
"get_or_create_connectivity_node",
|
|
29
|
+
"find_by_name",
|
|
30
|
+
"propagate_base_voltages",
|
|
31
|
+
"get_or_create_base_voltage",
|
|
32
|
+
]
|