scjson 0.3.1__tar.gz → 0.3.2__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 (36) hide show
  1. {scjson-0.3.1/scjson.egg-info → scjson-0.3.2}/PKG-INFO +1 -1
  2. {scjson-0.3.1 → scjson-0.3.2}/pyproject.toml +1 -1
  3. {scjson-0.3.1 → scjson-0.3.2}/scjson/cli.py +17 -7
  4. {scjson-0.3.1 → scjson-0.3.2}/scjson/jinja_gen.py +121 -13
  5. {scjson-0.3.1 → scjson-0.3.2/scjson.egg-info}/PKG-INFO +1 -1
  6. {scjson-0.3.1 → scjson-0.3.2}/LEGAL.md +0 -0
  7. {scjson-0.3.1 → scjson-0.3.2}/LICENSE +0 -0
  8. {scjson-0.3.1 → scjson-0.3.2}/MANIFEST.in +0 -0
  9. {scjson-0.3.1 → scjson-0.3.2}/README.md +0 -0
  10. {scjson-0.3.1 → scjson-0.3.2}/scjson/CaseStyle.py +0 -0
  11. {scjson-0.3.1 → scjson-0.3.2}/scjson/SCXMLDocumentHandler.py +0 -0
  12. {scjson-0.3.1 → scjson-0.3.2}/scjson/__init__.py +0 -0
  13. {scjson-0.3.1 → scjson-0.3.2}/scjson/__main__.py +0 -0
  14. {scjson-0.3.1 → scjson-0.3.2}/scjson/activation.py +0 -0
  15. {scjson-0.3.1 → scjson-0.3.2}/scjson/context.py +0 -0
  16. {scjson-0.3.1 → scjson-0.3.2}/scjson/dataclasses/__init__.py +0 -0
  17. {scjson-0.3.1 → scjson-0.3.2}/scjson/dataclasses/generated.py +0 -0
  18. {scjson-0.3.1 → scjson-0.3.2}/scjson/dataclasses_strict/__init__.py +0 -0
  19. {scjson-0.3.1 → scjson-0.3.2}/scjson/dataclasses_strict/generated.py +0 -0
  20. {scjson-0.3.1 → scjson-0.3.2}/scjson/engine.py +0 -0
  21. {scjson-0.3.1 → scjson-0.3.2}/scjson/events.py +0 -0
  22. {scjson-0.3.1 → scjson-0.3.2}/scjson/json_stream.py +0 -0
  23. {scjson-0.3.1 → scjson-0.3.2}/scjson/pydantic/__init__.py +0 -0
  24. {scjson-0.3.1 → scjson-0.3.2}/scjson/pydantic/generated.py +0 -0
  25. {scjson-0.3.1 → scjson-0.3.2}/scjson/pydantic_strict/__init__.py +0 -0
  26. {scjson-0.3.1 → scjson-0.3.2}/scjson/pydantic_strict/generated.py +0 -0
  27. {scjson-0.3.1 → scjson-0.3.2}/scjson.egg-info/SOURCES.txt +0 -0
  28. {scjson-0.3.1 → scjson-0.3.2}/scjson.egg-info/dependency_links.txt +0 -0
  29. {scjson-0.3.1 → scjson-0.3.2}/scjson.egg-info/entry_points.txt +0 -0
  30. {scjson-0.3.1 → scjson-0.3.2}/scjson.egg-info/requires.txt +0 -0
  31. {scjson-0.3.1 → scjson-0.3.2}/scjson.egg-info/top_level.txt +0 -0
  32. {scjson-0.3.1 → scjson-0.3.2}/setup.cfg +0 -0
  33. {scjson-0.3.1 → scjson-0.3.2}/tests/__pycache__/test_cli.cpython-310-pytest-8.4.1.pyc +0 -0
  34. {scjson-0.3.1 → scjson-0.3.2}/tests/test_cli.py +0 -0
  35. {scjson-0.3.1 → scjson-0.3.2}/tests/test_engine.py +0 -0
  36. {scjson-0.3.1 → scjson-0.3.2}/tests/test_json_stream.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: scjson
3
- Version: 0.3.1
3
+ Version: 0.3.2
4
4
  Summary: A JSON-based serialization of SCXML (State Chart XML) for modern tooling, interoperability, and education.
5
5
  Author-email: Ira Abbott <ira@mycloud.blue>
6
6
  License-Expression: BSD-1-Clause
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "scjson"
3
- version = "0.3.1"
3
+ version = "0.3.2"
4
4
  description = "A JSON-based serialization of SCXML (State Chart XML) for modern tooling, interoperability, and education."
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.8"
@@ -217,18 +217,28 @@ def typescript(output: Path | None):
217
217
  print(f"Convert Scjson type for typescript - Path: {output}")
218
218
  Gen = JinjaGenPydantic(output=output)
219
219
  base_dir = os.path.abspath(output)
220
- interfaces = Gen.interfaces
221
- schemas = Gen.schemas
222
- all_arrays = Gen.all_arrays
223
220
  os.makedirs(base_dir, exist_ok=True)
224
221
  is_runtime = True
225
222
  file_name = "scjsonProps.ts"
226
223
  file_description = "Properties runtime file for scjson types"
227
224
  Gen.render_to_file(file_name, "scjson_props.ts.jinja2", locals())
228
- is_runtime = False
229
- file_name = "scjsonProps.d.ts"
230
- file_description = "Properties definition file for scjson types"
231
- Gen.render_to_file(f"types/{file_name}", "scjson_props.ts.jinja2", locals())
225
+ #is_runtime = False
226
+ #file_name = "scjsonProps.d.ts"
227
+ #file_description = "Properties definition file for scjson types"
228
+ #Gen.render_to_file(f"types/{file_name}", "scjson_props.ts.jinja2", locals())
229
+
230
+
231
+ @main.command(help="Create Rust type files for scjson")
232
+ @click.option("--output", "-o", type=click.Path(path_type=Path), help="Output file base.")
233
+ def rust(output: Path | None):
234
+ """Create Rust structs and enums for scjson."""
235
+ print(f"Convert Scjson type for rust - Path: {output}")
236
+ Gen = JinjaGenPydantic(output=output, lang="rust")
237
+ base_dir = os.path.abspath(output)
238
+ os.makedirs(base_dir, exist_ok=True)
239
+ file_name = "scjson_props.rs"
240
+ file_description = "Properties file for scjson types"
241
+ Gen.render_to_file(file_name, "scjson_props.rs.jinja2", locals())
232
242
 
233
243
 
234
244
  @main.command(help="Export scjson.schema.json")
@@ -3,6 +3,7 @@ import os
3
3
  from pathlib import Path
4
4
  import importlib
5
5
  import inspect
6
+ import textwrap
6
7
  from typing import List
7
8
  from enum import Enum
8
9
  from pydantic import BaseModel
@@ -18,18 +19,22 @@ from .CaseStyle import (
18
19
 
19
20
  class JinjaGenPydantic(object):
20
21
  """A class to render pydntic models using jinja2 templates."""
21
- def __init__( self,
22
- template_path: str = "",
23
- input: str="Scxml",
24
- output: str = "scjson",
25
- module: str = "scjson.pydantic"
26
- ):
22
+
23
+ def __init__(
24
+ self,
25
+ template_path: str = "",
26
+ input: str = "Scxml",
27
+ output: str = "scjson",
28
+ module: str = "scjson.pydantic",
29
+ lang: str = "typescript",
30
+ ):
27
31
  """Initilize with optional config."""
28
32
  my_path = os.path.join(Path(__file__).parent, "templates")
29
- self.template_path = template_path or my_path
33
+ self.template_path = template_path or my_path
30
34
  self.output = output
31
35
  self.input = input
32
36
  self.module_name = module
37
+ self.lang = lang
33
38
  self.interfaces = {}
34
39
  self.schema = {}
35
40
  self.schemas = {}
@@ -49,12 +54,24 @@ class JinjaGenPydantic(object):
49
54
  self.env.globals.update(type=type)
50
55
  self.env.globals.update(dir=dir)
51
56
  self.env.globals.update(str=str)
52
- self.env.globals.update( get_field_default=JinjaGenPydantic._get_default_value,
53
- get_field_type=JinjaGenPydantic._get_field_type,
54
- get_schema_types=JinjaGenPydantic._get_schema_types,
55
- list_join=JinjaGenPydantic._list_join,
56
- is_field_enum=JinjaGenPydantic._is_field_enum,
57
- )
57
+ self.env.globals.update(textwrap=textwrap)
58
+ if self.lang == "rust":
59
+ self.env.globals.update(
60
+ get_field_default=JinjaGenPydantic._get_rust_default_value,
61
+ get_field_type=JinjaGenPydantic._get_rust_field_type,
62
+ )
63
+ else:
64
+ self.env.globals.update(
65
+ get_field_default=JinjaGenPydantic._get_default_value,
66
+ get_field_type=JinjaGenPydantic._get_field_type,
67
+ )
68
+ self.env.globals.update(
69
+ get_schema_types=JinjaGenPydantic._get_schema_types,
70
+ list_join=JinjaGenPydantic._list_join,
71
+ is_field_enum=JinjaGenPydantic._is_field_enum,
72
+ first_enum=JinjaGenPydantic._first_enum_member,
73
+ rust_ident=JinjaGenPydantic._rust_ident,
74
+ )
58
75
  self.env.globals.update( to_camel=to_camel,
59
76
  to_pascal=to_pascal,
60
77
  to_snake=to_snake,
@@ -168,6 +185,97 @@ class JinjaGenPydantic(object):
168
185
  # None -> null, integer -> number, else ->
169
186
  return ret_val
170
187
 
188
+ def _get_rust_default_value(prop: dict, defs: dict | None = None) -> str:
189
+ """Return a Rust expression for the property's default value."""
190
+
191
+ def get_fallback(prop: dict) -> str:
192
+ type_name = prop.get("type")
193
+ mapping = {
194
+ "string": 'String::new()',
195
+ "integer": "0",
196
+ "number": "0.0",
197
+ "boolean": "false",
198
+ "array": "Vec::new()",
199
+ "object": "Map::new()",
200
+ }
201
+ return mapping.get(type_name, "Value::Null")
202
+
203
+ ret_val = "Value::Null"
204
+ if "type" in prop:
205
+ if "default" in prop:
206
+ if prop["type"] == "string":
207
+ ret_val = f'"{prop["default"]}".to_string()'
208
+ elif prop["type"] == "boolean":
209
+ ret_val = str(prop["default"]).lower()
210
+ else:
211
+ ret_val = str(prop["default"])
212
+ else:
213
+ ret_val = get_fallback(prop)
214
+ elif "$ref" in prop:
215
+ ref_name = prop["$ref"].split("/")[-1]
216
+ if "default" in prop:
217
+ ret_val = f'{ref_name}Props::{to_pascal(prop["default"])}'
218
+ elif defs and ref_name in defs and "enum" in defs[ref_name]:
219
+ first = defs[ref_name]["enum"][0]
220
+ ret_val = f'{ref_name}Props::{to_pascal(first)}'
221
+ else:
222
+ ret_val = f'default_{ref_name.lower()}()'
223
+ if "anyOf" in prop:
224
+ has_null = any(opt.get("type") == "null" for opt in prop["anyOf"])
225
+ ret_val = "None" if has_null else "Value::Null"
226
+ return ret_val
227
+
228
+ def _get_rust_field_type(prop: dict | str) -> str:
229
+ """Return the Rust type for a schema property."""
230
+
231
+ def xlate(prop: dict | str) -> str:
232
+ if "type" in prop:
233
+ if prop["type"] == "array":
234
+ return f"Vec<{xlate(prop['items'])}>"
235
+ else:
236
+ dtype = prop["type"]
237
+ elif "$ref" in prop:
238
+ dtype = prop["$ref"].split("/")[-1] + "Props"
239
+ else:
240
+ dtype = str(prop)
241
+ return (
242
+ "String" if dtype == "string" else
243
+ "i64" if dtype == "integer" else
244
+ "f64" if dtype == "number" else
245
+ "bool" if dtype == "boolean" else
246
+ "Vec<Value>" if dtype == "array" else
247
+ "Map<String, Value>" if dtype in ["object", "{}"] else
248
+ dtype
249
+ )
250
+
251
+ ret_val = "None"
252
+ if "type" in prop or "$ref" in prop:
253
+ ret_val = xlate(prop)
254
+ elif "anyOf" in prop:
255
+ types = [xlate(t) for t in prop["anyOf"] if t.get("type") != "null"]
256
+ if len(types) == 1:
257
+ ret_val = f"Option<{types[0]}>"
258
+ else:
259
+ ret_val = "Value"
260
+ return ret_val
261
+
262
+ def _first_enum_member(enum_cls: Enum) -> str:
263
+ """Return the first member name of an Enum class."""
264
+ return next(iter(enum_cls.__members__.keys()))
265
+
266
+ def _rust_ident(name: str) -> str:
267
+ """Escape Rust keywords for identifiers."""
268
+ keywords = {
269
+ "as", "break", "const", "continue", "crate", "else", "enum", "extern",
270
+ "false", "fn", "for", "if", "impl", "in", "let", "loop", "match",
271
+ "mod", "move", "mut", "pub", "ref", "return", "self", "Self",
272
+ "static", "struct", "super", "trait", "true", "type", "unsafe",
273
+ "use", "where", "while", "async", "await", "dyn", "abstract",
274
+ "become", "box", "do", "final", "macro", "override", "priv",
275
+ "typeof", "unsized", "virtual", "yield", "try", "union",
276
+ }
277
+ return f"r#{name}" if name in keywords else name
278
+
171
279
  def _get_schema_types(schema: dict, name: str = "") -> List[str]:
172
280
  """Template helper to return the reference type from teh schema."""
173
281
  t_list = [f'{name}'] if name else []
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: scjson
3
- Version: 0.3.1
3
+ Version: 0.3.2
4
4
  Summary: A JSON-based serialization of SCXML (State Chart XML) for modern tooling, interoperability, and education.
5
5
  Author-email: Ira Abbott <ira@mycloud.blue>
6
6
  License-Expression: BSD-1-Clause
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes