Skip to content

Commit 3e620df

Browse files
authored
Merge pull request #11 from ndsev/release/0.5.0
Release/0.5.0
2 parents 6465042 + 89e8fb2 commit 3e620df

File tree

3 files changed

+72
-50
lines changed

3 files changed

+72
-50
lines changed

README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ zs-yaml input.yaml output.bin
4545
zs-yaml input.bin output.yaml
4646
```
4747

48+
### Programmatic Usage
49+
50+
In addition to the command-line interface, `zs-yaml` provides the conversion functions for use within Python scripts. In addition to the ones available to the CLI, there are also functions for programmatic use, e.g. `yaml_to_pyobj` for deserializing YAML files to Python objects:
51+
52+
```python
53+
from zs_yaml.convert import yaml_to_pyobj
54+
55+
# Convert a YAML file to a Python object
56+
# (instance of the zserio object defined by the schema)
57+
zserio_object = yaml_to_pyobj('input.yaml')
58+
59+
# Use the zserio_object as needed in your application
60+
```
61+
4862
### Notes
4963

5064
- You have to use the exact same order of fields in the YAML as defined by the zserio schema, because zserio expects this.
@@ -220,4 +234,4 @@ Note: We plan to implement automatic source documentation generation in a future
220234

221235
## Reference
222236

223-
This project references the zserio serialization framework. For more information about zserio, visit the [zserio GitHub project](https://github.com/ndsev/zserio).
237+
This project references the zserio serialization framework. For more information about zserio, visit the [zserio GitHub project](https://github.com/ndsev/zserio).

zs_yaml/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
__commit_id__ = '7217bfe'
2-
__version__ = '0.4.0'
2+
__version__ = '0.5.0'

zs_yaml/convert.py

+56-48
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,69 @@
2020

2121
from .yaml_transformer import YamlTransformer
2222

23-
def _json_to_zs_bin(json_input_path, bin_output_path, module_name, type_name):
23+
def _yaml_to_zserio_object(yaml_input_path):
2424
"""
25-
Converts a JSON file to a binary file using Zserio serialization.
25+
Converts a YAML file to a Zserio object.
2626
2727
Args:
28-
json_input_path (str): Path to the input JSON file.
29-
bin_output_path (str): Path to the output binary file.
30-
module_name (str): Name of the Zserio module containing the schema.
31-
type_name (str): Name of the Zserio type to be used for serialization.
28+
yaml_input_path (str): Path to the input YAML file.
29+
30+
Returns:
31+
tuple: A tuple containing (zserio_object, temp_json_path, meta)
3232
3333
Raises:
34-
ValueError: If the specified Zserio type is not found in the module.
34+
ValueError: If schema_module and schema_type are not specified in the _meta
35+
section of the YAML file.
3536
"""
36-
module = importlib.import_module(module_name)
37-
ImportedType = getattr(module, type_name)
37+
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as temp_json_file:
38+
temp_json_path = temp_json_file.name
39+
40+
meta = yaml_to_json(yaml_input_path, temp_json_path)
41+
42+
schema_module = meta.get('schema_module')
43+
schema_type = meta.get('schema_type')
44+
init_args = meta.get('initialization_args', [])
45+
46+
if not schema_module or not schema_type:
47+
raise ValueError("Error: schema_module and schema_type must be specified in the _meta section")
48+
49+
module = importlib.import_module(schema_module)
50+
ImportedType = getattr(module, schema_type)
3851
if ImportedType is None:
39-
raise ValueError(f"Type {type_name} not found in module {module_name}")
40-
zserio_object = zserio.from_json_file(ImportedType, json_input_path)
41-
zserio.serialize_to_file(zserio_object, bin_output_path)
52+
raise ValueError(f"Type {schema_type} not found in module {schema_module}")
53+
54+
zserio_object = zserio.from_json_file(ImportedType, temp_json_path, *init_args)
55+
return zserio_object, temp_json_path, meta
56+
57+
def yaml_to_bin(yaml_input_path, bin_output_path):
58+
"""
59+
Converts a YAML file to a binary file using Zserio serialization.
60+
61+
Args:
62+
yaml_input_path (str): Path to the input YAML file.
63+
bin_output_path (str): Path to the output binary file.
64+
"""
65+
try:
66+
zserio_object, temp_json_path, _ = _yaml_to_zserio_object(yaml_input_path)
67+
zserio.serialize_to_file(zserio_object, bin_output_path)
68+
finally:
69+
os.remove(temp_json_path)
70+
71+
def yaml_to_pyobj(yaml_input_path):
72+
"""
73+
Converts a YAML file to an in-memory Python object using Zserio deserialization.
74+
75+
Args:
76+
yaml_input_path (str): Path to the input YAML file.
77+
78+
Returns:
79+
object: The deserialized Python object.
80+
"""
81+
try:
82+
zserio_object, temp_json_path, _ = _yaml_to_zserio_object(yaml_input_path)
83+
return zserio_object
84+
finally:
85+
os.remove(temp_json_path)
4286

4387
def yaml_to_yaml(yaml_input_path, yaml_output_path=None):
4488
"""
@@ -95,42 +139,6 @@ def json_to_yaml(json_input_path, yaml_output_path):
95139
yaml.safe_dump(data, yaml_file, default_flow_style=False, sort_keys=False)
96140

97141

98-
def yaml_to_bin(yaml_input_path, bin_output_path):
99-
"""
100-
Converts a YAML file to a binary file using Zserio serialization.
101-
102-
Args:
103-
yaml_input_path (str): Path to the input YAML file.
104-
bin_output_path (str): Path to the output binary file.
105-
106-
Raises:
107-
ValueError: If schema_module and schema_type are not specified in the _meta
108-
section of the YAML file.
109-
"""
110-
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as temp_json_file:
111-
temp_json_path = temp_json_file.name
112-
113-
try:
114-
meta = yaml_to_json(yaml_input_path, temp_json_path)
115-
116-
schema_module = meta.get('schema_module')
117-
schema_type = meta.get('schema_type')
118-
init_args = meta.get('initialization_args', [])
119-
120-
if not schema_module or not schema_type:
121-
raise ValueError("Error: schema_module and schema_type must be specified in the _meta section for binary output")
122-
123-
module = importlib.import_module(schema_module)
124-
ImportedType = getattr(module, schema_type)
125-
if ImportedType is None:
126-
raise ValueError(f"Type {schema_type} not found in module {schema_module}")
127-
128-
zserio_object = zserio.from_json_file(ImportedType, temp_json_path, *init_args)
129-
zserio.serialize_to_file(zserio_object, bin_output_path)
130-
finally:
131-
os.remove(temp_json_path)
132-
133-
134142
def bin_to_yaml(bin_input_path, yaml_output_path):
135143
"""
136144
Converts a binary file to a YAML file using Zserio deserialization.

0 commit comments

Comments
 (0)