any2tosca 0.1.0__py3-none-any.whl
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.
- any2tosca/__init__.py +26 -0
- any2tosca/cli.py +371 -0
- any2tosca/converter.py +2060 -0
- any2tosca/openapi.py +142 -0
- any2tosca/redfish.py +177 -0
- any2tosca/yaml_utils.py +83 -0
- any2tosca/yang.py +239 -0
- any2tosca-0.1.0.dist-info/METADATA +419 -0
- any2tosca-0.1.0.dist-info/RECORD +13 -0
- any2tosca-0.1.0.dist-info/WHEEL +5 -0
- any2tosca-0.1.0.dist-info/entry_points.txt +5 -0
- any2tosca-0.1.0.dist-info/licenses/LICENSE +202 -0
- any2tosca-0.1.0.dist-info/top_level.txt +1 -0
any2tosca/__init__.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""
|
|
2
|
+
any2tosca
|
|
3
|
+
----------
|
|
4
|
+
Convert JSON Schema documents to TOSCA data types.
|
|
5
|
+
|
|
6
|
+
Public API::
|
|
7
|
+
|
|
8
|
+
from any2tosca import convert_schema, build_tosca_document
|
|
9
|
+
from any2tosca.yaml_utils import dump_tosca_yaml
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
__author__ = "Chris Lauwers"
|
|
13
|
+
__copyright__ = "Copyright (c) 2022-2026, Ubicity Corp."
|
|
14
|
+
__email__ = "info@ubicity.com"
|
|
15
|
+
|
|
16
|
+
from .converter import Converter, ConversionResult, build_tosca_document, convert_schema
|
|
17
|
+
from .openapi import OpenAPIExtractor
|
|
18
|
+
from .redfish import RedfishNormalizer
|
|
19
|
+
from .yaml_utils import dump_tosca_yaml
|
|
20
|
+
|
|
21
|
+
__all__ = [
|
|
22
|
+
"Converter", "ConversionResult", "convert_schema", "build_tosca_document",
|
|
23
|
+
"OpenAPIExtractor", "RedfishNormalizer",
|
|
24
|
+
"dump_tosca_yaml",
|
|
25
|
+
]
|
|
26
|
+
__version__ = "0.1.0"
|
any2tosca/cli.py
ADDED
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
"""
|
|
2
|
+
cli.py
|
|
3
|
+
------
|
|
4
|
+
Command-line entry points for any2tosca, redfish2tosca, and oas2tosca.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
__author__ = "Chris Lauwers"
|
|
10
|
+
__copyright__ = "Copyright (c) 2022-2026, Ubicity Corp."
|
|
11
|
+
__email__ = "info@ubicity.com"
|
|
12
|
+
|
|
13
|
+
import argparse
|
|
14
|
+
import json
|
|
15
|
+
import re
|
|
16
|
+
import sys
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
from typing import Any
|
|
19
|
+
|
|
20
|
+
import yaml
|
|
21
|
+
|
|
22
|
+
from .converter import ConversionResult, Converter, build_caps_document, build_tosca_document, convert_schema
|
|
23
|
+
from .openapi import OpenAPIExtractor
|
|
24
|
+
from .redfish import RedfishNormalizer
|
|
25
|
+
from .yang import YANGNormalizer
|
|
26
|
+
from .yaml_utils import dump_tosca_yaml
|
|
27
|
+
|
|
28
|
+
__all__ = ["main_json", "main_redfish", "main_oas", "main_yang"]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
# ── Shared helpers ────────────────────────────────────────────────────────────
|
|
32
|
+
|
|
33
|
+
def _make_parser(prog: str, description: str, epilog: str) -> argparse.ArgumentParser:
|
|
34
|
+
parser = argparse.ArgumentParser(
|
|
35
|
+
prog=prog,
|
|
36
|
+
description=description,
|
|
37
|
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
38
|
+
epilog=epilog,
|
|
39
|
+
)
|
|
40
|
+
parser.add_argument(
|
|
41
|
+
"inputs",
|
|
42
|
+
nargs="+",
|
|
43
|
+
metavar="INPUT",
|
|
44
|
+
help="Input file(s) or a directory.",
|
|
45
|
+
)
|
|
46
|
+
parser.add_argument(
|
|
47
|
+
"-o", "--output",
|
|
48
|
+
default=None,
|
|
49
|
+
metavar="OUTPUT_DIR",
|
|
50
|
+
help="Output directory for TOSCA files (default: tosca/).",
|
|
51
|
+
)
|
|
52
|
+
parser.add_argument(
|
|
53
|
+
"-I", "--include-dir",
|
|
54
|
+
action="append",
|
|
55
|
+
default=[],
|
|
56
|
+
metavar="DIR",
|
|
57
|
+
help="Additional directory to search for referenced schema files "
|
|
58
|
+
"(may be given multiple times).",
|
|
59
|
+
)
|
|
60
|
+
parser.add_argument(
|
|
61
|
+
"-v", "--verbose",
|
|
62
|
+
action="store_true",
|
|
63
|
+
help="Print extra detail while processing.",
|
|
64
|
+
)
|
|
65
|
+
return parser
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _collect_input_files(inputs: list[str], extensions: tuple[str, ...]) -> list[Path]:
|
|
69
|
+
files: list[Path] = []
|
|
70
|
+
for inp in inputs:
|
|
71
|
+
p = Path(inp)
|
|
72
|
+
if p.is_dir():
|
|
73
|
+
found = sorted(f for ext in extensions for f in p.glob(f"*{ext}"))
|
|
74
|
+
if not found:
|
|
75
|
+
exts = ", ".join(extensions)
|
|
76
|
+
print(f"Warning: no {exts} files found in '{inp}'", file=sys.stderr)
|
|
77
|
+
files.extend(found)
|
|
78
|
+
elif p.is_file():
|
|
79
|
+
files.append(p)
|
|
80
|
+
else:
|
|
81
|
+
print(f"Warning: '{inp}' is not a file or directory — skipping.", file=sys.stderr)
|
|
82
|
+
return files
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _load_file(path: Path) -> Any:
|
|
86
|
+
"""Load a JSON or YAML file into a Python object."""
|
|
87
|
+
with open(path, "r", encoding="utf-8") as fh:
|
|
88
|
+
if path.suffix in (".yaml", ".yml"):
|
|
89
|
+
return yaml.safe_load(fh)
|
|
90
|
+
return json.load(fh)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _report(result: ConversionResult, verbose: bool, path: Path) -> None:
|
|
94
|
+
label = str(path) if verbose else path.name
|
|
95
|
+
print(f"Processing: {label}")
|
|
96
|
+
total = len(result.data_types) + len(result.node_types)
|
|
97
|
+
if not total:
|
|
98
|
+
print(" ⚠ No convertible types found.")
|
|
99
|
+
else:
|
|
100
|
+
names = list(result.data_types) + list(result.node_types)
|
|
101
|
+
print(f" ✓ {total} type(s): {', '.join(names)}")
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _write_file(result: ConversionResult, input_path: Path, out_dir: Path) -> Path:
|
|
105
|
+
"""Write a single ConversionResult to <out_dir>/<input_stem>.yaml (and companion _caps.yaml)."""
|
|
106
|
+
tosca_doc = build_tosca_document(
|
|
107
|
+
result.data_types,
|
|
108
|
+
result.node_types,
|
|
109
|
+
result.relationship_types or None,
|
|
110
|
+
result.interface_types or None,
|
|
111
|
+
result.imports or None,
|
|
112
|
+
)
|
|
113
|
+
out_path = out_dir / input_path.with_suffix(".yaml").name
|
|
114
|
+
out_path.write_text(dump_tosca_yaml(tosca_doc), encoding="utf-8")
|
|
115
|
+
|
|
116
|
+
if result.capability_types:
|
|
117
|
+
stem = input_path.stem
|
|
118
|
+
# Strip version suffix (e.g. .v1_14_0) to get the base name
|
|
119
|
+
base = re.sub(r"[._]v\d+(?:[._]\d+)*$", "", stem) or stem
|
|
120
|
+
caps_path = out_dir / f"{base}_caps.yaml"
|
|
121
|
+
caps_doc = build_caps_document(result.capability_types)
|
|
122
|
+
caps_path.write_text(dump_tosca_yaml(caps_doc), encoding="utf-8")
|
|
123
|
+
|
|
124
|
+
return out_path
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
# ── any2tosca ────────────────────────────────────────────────────────────────
|
|
128
|
+
|
|
129
|
+
def main_json(argv: list[str] | None = None) -> int:
|
|
130
|
+
"""Convert plain JSON Schema files to TOSCA."""
|
|
131
|
+
parser = _make_parser(
|
|
132
|
+
"any2tosca",
|
|
133
|
+
"Convert JSON Schema files to TOSCA data types (YAML).",
|
|
134
|
+
"Examples:\n"
|
|
135
|
+
" any2tosca my_schema.json\n"
|
|
136
|
+
" any2tosca schemas/ -o output/\n"
|
|
137
|
+
" any2tosca a.json b.json -o output/",
|
|
138
|
+
)
|
|
139
|
+
args = parser.parse_args(argv)
|
|
140
|
+
input_files = _collect_input_files(args.inputs, (".json",))
|
|
141
|
+
|
|
142
|
+
if not input_files:
|
|
143
|
+
print("Error: no JSON Schema files found.", file=sys.stderr)
|
|
144
|
+
return 1
|
|
145
|
+
|
|
146
|
+
out_dir = Path(args.output) if args.output else Path("tosca")
|
|
147
|
+
out_dir.mkdir(parents=True, exist_ok=True)
|
|
148
|
+
search_paths = [Path(d) for d in args.include_dir]
|
|
149
|
+
written = errors = 0
|
|
150
|
+
|
|
151
|
+
for schema_path in input_files:
|
|
152
|
+
try:
|
|
153
|
+
root_schema = _load_file(schema_path)
|
|
154
|
+
except (json.JSONDecodeError, yaml.YAMLError) as exc:
|
|
155
|
+
print(f" ✗ Parse error in {schema_path.name}: {exc}", file=sys.stderr)
|
|
156
|
+
errors += 1
|
|
157
|
+
continue
|
|
158
|
+
except OSError as exc:
|
|
159
|
+
print(f" ✗ File read error: {exc}", file=sys.stderr)
|
|
160
|
+
errors += 1
|
|
161
|
+
continue
|
|
162
|
+
|
|
163
|
+
top_name = root_schema.get("title") or schema_path.stem
|
|
164
|
+
result = convert_schema(root_schema, top_name, base_path=schema_path.parent,
|
|
165
|
+
search_paths=search_paths, source_path=schema_path)
|
|
166
|
+
_report(result, args.verbose, schema_path)
|
|
167
|
+
if not result.data_types and not result.node_types:
|
|
168
|
+
continue
|
|
169
|
+
out_path = _write_file(result, schema_path, out_dir)
|
|
170
|
+
print(f" → {out_path}")
|
|
171
|
+
written += 1
|
|
172
|
+
|
|
173
|
+
if written == 0:
|
|
174
|
+
print("No TOSCA files were written.", file=sys.stderr)
|
|
175
|
+
return 1
|
|
176
|
+
print(f"\n✅ {written} file(s) written to: {out_dir}/")
|
|
177
|
+
if errors:
|
|
178
|
+
print(f"⚠ {errors} file(s) had errors and were skipped.", file=sys.stderr)
|
|
179
|
+
return 2
|
|
180
|
+
return 0
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
# ── redfish2tosca ─────────────────────────────────────────────────────────────
|
|
184
|
+
|
|
185
|
+
def main_redfish(argv: list[str] | None = None) -> int:
|
|
186
|
+
"""Convert Redfish JSON Schema files to TOSCA."""
|
|
187
|
+
parser = _make_parser(
|
|
188
|
+
"redfish2tosca",
|
|
189
|
+
"Convert Redfish JSON Schema files to TOSCA node and data types (YAML).",
|
|
190
|
+
"Examples:\n"
|
|
191
|
+
" redfish2tosca Drive.json\n"
|
|
192
|
+
" redfish2tosca schemas/ -o tosca/",
|
|
193
|
+
)
|
|
194
|
+
args = parser.parse_args(argv)
|
|
195
|
+
input_files = _collect_input_files(args.inputs, (".json",))
|
|
196
|
+
|
|
197
|
+
if not input_files:
|
|
198
|
+
print("Error: no Redfish JSON Schema files found.", file=sys.stderr)
|
|
199
|
+
return 1
|
|
200
|
+
|
|
201
|
+
out_dir = Path(args.output) if args.output else Path("tosca")
|
|
202
|
+
out_dir.mkdir(parents=True, exist_ok=True)
|
|
203
|
+
normalizer = RedfishNormalizer()
|
|
204
|
+
converter = Converter()
|
|
205
|
+
search_paths = [Path(d) for d in args.include_dir]
|
|
206
|
+
written = errors = 0
|
|
207
|
+
|
|
208
|
+
for schema_path in input_files:
|
|
209
|
+
try:
|
|
210
|
+
redfish_schema = _load_file(schema_path)
|
|
211
|
+
except (json.JSONDecodeError, yaml.YAMLError) as exc:
|
|
212
|
+
print(f" ✗ Parse error in {schema_path.name}: {exc}", file=sys.stderr)
|
|
213
|
+
errors += 1
|
|
214
|
+
continue
|
|
215
|
+
except OSError as exc:
|
|
216
|
+
print(f" ✗ File read error: {exc}", file=sys.stderr)
|
|
217
|
+
errors += 1
|
|
218
|
+
continue
|
|
219
|
+
|
|
220
|
+
root_schema, node_type_names = normalizer.normalize(redfish_schema)
|
|
221
|
+
top_name = next(iter(node_type_names), None) or schema_path.stem
|
|
222
|
+
result = converter.convert_schema(root_schema, top_name, node_type_names,
|
|
223
|
+
base_path=schema_path.parent,
|
|
224
|
+
search_paths=search_paths,
|
|
225
|
+
source_path=schema_path)
|
|
226
|
+
_report(result, args.verbose, schema_path)
|
|
227
|
+
if not result.data_types and not result.node_types:
|
|
228
|
+
continue
|
|
229
|
+
out_path = _write_file(result, schema_path, out_dir)
|
|
230
|
+
print(f" → {out_path}")
|
|
231
|
+
written += 1
|
|
232
|
+
|
|
233
|
+
if written == 0:
|
|
234
|
+
print("No TOSCA files were written.", file=sys.stderr)
|
|
235
|
+
return 1
|
|
236
|
+
print(f"\n✅ {written} file(s) written to: {out_dir}/")
|
|
237
|
+
if errors:
|
|
238
|
+
print(f"⚠ {errors} file(s) had errors and were skipped.", file=sys.stderr)
|
|
239
|
+
return 2
|
|
240
|
+
return 0
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
# ── oas2tosca ─────────────────────────────────────────────────────────────────
|
|
244
|
+
|
|
245
|
+
def main_oas(argv: list[str] | None = None) -> int:
|
|
246
|
+
"""Convert OpenAPI documents to TOSCA."""
|
|
247
|
+
parser = _make_parser(
|
|
248
|
+
"oas2tosca",
|
|
249
|
+
"Convert OpenAPI 2.0/3.x documents to TOSCA node and data types (YAML).",
|
|
250
|
+
"Examples:\n"
|
|
251
|
+
" oas2tosca api.yaml\n"
|
|
252
|
+
" oas2tosca api.json -o output/\n"
|
|
253
|
+
" oas2tosca specs/ -o output/",
|
|
254
|
+
)
|
|
255
|
+
args = parser.parse_args(argv)
|
|
256
|
+
input_files = _collect_input_files(args.inputs, (".json", ".yaml", ".yml"))
|
|
257
|
+
|
|
258
|
+
if not input_files:
|
|
259
|
+
print("Error: no OpenAPI files found.", file=sys.stderr)
|
|
260
|
+
return 1
|
|
261
|
+
|
|
262
|
+
out_dir = Path(args.output) if args.output else Path("tosca")
|
|
263
|
+
out_dir.mkdir(parents=True, exist_ok=True)
|
|
264
|
+
extractor = OpenAPIExtractor()
|
|
265
|
+
converter = Converter()
|
|
266
|
+
search_paths = [Path(d) for d in args.include_dir]
|
|
267
|
+
written = errors = 0
|
|
268
|
+
|
|
269
|
+
for api_path in input_files:
|
|
270
|
+
try:
|
|
271
|
+
openapi_doc = _load_file(api_path)
|
|
272
|
+
except (json.JSONDecodeError, yaml.YAMLError) as exc:
|
|
273
|
+
print(f" ✗ Parse error in {api_path.name}: {exc}", file=sys.stderr)
|
|
274
|
+
errors += 1
|
|
275
|
+
continue
|
|
276
|
+
except OSError as exc:
|
|
277
|
+
print(f" ✗ File read error: {exc}", file=sys.stderr)
|
|
278
|
+
errors += 1
|
|
279
|
+
continue
|
|
280
|
+
|
|
281
|
+
root_schema, node_type_names = extractor.extract(openapi_doc)
|
|
282
|
+
top_name = openapi_doc.get("info", {}).get("title") or api_path.stem
|
|
283
|
+
result = converter.convert_schema(root_schema, top_name, node_type_names,
|
|
284
|
+
base_path=api_path.parent,
|
|
285
|
+
search_paths=search_paths,
|
|
286
|
+
source_path=api_path)
|
|
287
|
+
_report(result, args.verbose, api_path)
|
|
288
|
+
if not result.data_types and not result.node_types:
|
|
289
|
+
continue
|
|
290
|
+
out_path = _write_file(result, api_path, out_dir)
|
|
291
|
+
print(f" → {out_path}")
|
|
292
|
+
written += 1
|
|
293
|
+
|
|
294
|
+
if written == 0:
|
|
295
|
+
print("No TOSCA files were written.", file=sys.stderr)
|
|
296
|
+
return 1
|
|
297
|
+
print(f"\n✅ {written} file(s) written to: {out_dir}/")
|
|
298
|
+
if errors:
|
|
299
|
+
print(f"⚠ {errors} file(s) had errors and were skipped.", file=sys.stderr)
|
|
300
|
+
return 2
|
|
301
|
+
return 0
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
# ── yang2tosca ────────────────────────────────────────────────────────────────
|
|
305
|
+
|
|
306
|
+
def main_yang(argv: list[str] | None = None) -> int:
|
|
307
|
+
"""Convert YANG module files to TOSCA."""
|
|
308
|
+
parser = _make_parser(
|
|
309
|
+
"yang2tosca",
|
|
310
|
+
"Convert YANG module files to TOSCA node and data types (YAML).",
|
|
311
|
+
"Examples:\n"
|
|
312
|
+
" yang2tosca server.yang\n"
|
|
313
|
+
" yang2tosca models/ -o tosca/",
|
|
314
|
+
)
|
|
315
|
+
args = parser.parse_args(argv)
|
|
316
|
+
input_files = _collect_input_files(args.inputs, (".yang",))
|
|
317
|
+
|
|
318
|
+
if not input_files:
|
|
319
|
+
print("Error: no YANG files found.", file=sys.stderr)
|
|
320
|
+
return 1
|
|
321
|
+
|
|
322
|
+
out_dir = Path(args.output) if args.output else Path("tosca")
|
|
323
|
+
out_dir.mkdir(parents=True, exist_ok=True)
|
|
324
|
+
normalizer = YANGNormalizer()
|
|
325
|
+
converter = Converter()
|
|
326
|
+
search_paths = [Path(d) for d in args.include_dir]
|
|
327
|
+
written = errors = 0
|
|
328
|
+
|
|
329
|
+
for yang_path in input_files:
|
|
330
|
+
try:
|
|
331
|
+
root_schema, node_type_names = normalizer.normalize(yang_path)
|
|
332
|
+
except Exception as exc:
|
|
333
|
+
print(f" ✗ Parse error in {yang_path.name}: {exc}", file=sys.stderr)
|
|
334
|
+
errors += 1
|
|
335
|
+
continue
|
|
336
|
+
|
|
337
|
+
top_name = normalizer.module_name or yang_path.stem
|
|
338
|
+
result = converter.convert_schema(root_schema, top_name, node_type_names,
|
|
339
|
+
base_path=yang_path.parent,
|
|
340
|
+
search_paths=search_paths,
|
|
341
|
+
source_path=yang_path)
|
|
342
|
+
_report(result, args.verbose, yang_path)
|
|
343
|
+
if not result.data_types and not result.node_types:
|
|
344
|
+
continue
|
|
345
|
+
out_path = _write_file(result, yang_path, out_dir)
|
|
346
|
+
print(f" → {out_path}")
|
|
347
|
+
written += 1
|
|
348
|
+
|
|
349
|
+
if written == 0:
|
|
350
|
+
print("No TOSCA files were written.", file=sys.stderr)
|
|
351
|
+
return 1
|
|
352
|
+
print(f"\n✅ {written} file(s) written to: {out_dir}/")
|
|
353
|
+
if errors:
|
|
354
|
+
print(f"⚠ {errors} file(s) had errors and were skipped.", file=sys.stderr)
|
|
355
|
+
return 2
|
|
356
|
+
return 0
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
# ── Entry point wrappers ──────────────────────────────────────────────────────
|
|
360
|
+
|
|
361
|
+
def cli_entry() -> None:
|
|
362
|
+
sys.exit(main_json())
|
|
363
|
+
|
|
364
|
+
def redfish_entry() -> None:
|
|
365
|
+
sys.exit(main_redfish())
|
|
366
|
+
|
|
367
|
+
def oas_entry() -> None:
|
|
368
|
+
sys.exit(main_oas())
|
|
369
|
+
|
|
370
|
+
def yang_entry() -> None:
|
|
371
|
+
sys.exit(main_yang())
|