gr-mcp/grc/blocks/json_config.block.yml

59 lines
1.8 KiB
YAML

id: json_config
label: JSON Config
flags: [ show_id, python ]
parameters:
- id: config_file
label: Config File
dtype: file_open
default: ""
- id: schema_file
label: Config Schema
dtype: file_open
default: ""
# Not my favorite thing because it doesn't explicitly close the file, but
# it should be okay. The garbage collector will take care of it.
value: "${ json.load(open(config_file)) }"
asserts:
- ${ schema_file == "" or validate(json.load(open(config_file)), json.load(open(schema_file))) is None }
templates:
imports: |-
import json
from jsonschema import validate
# Note that yaml makes it really hard to insert spaces at the beginning of the line.
# The "\" char below is used to escape the space at the beginning of a line.
var_make: "with open(${config_file}) as fid:\n\
\ self.${id} = ${id} = json.load(fid)\n\
self.${id}_schema = ${id}_schema = ${schema_file}\n\
if ${id}_schema:\n\
\ with open(${id}_schema) as fid:\n\
\ validate(${id}, json.load(fid))"
documentation: |-
This block represents a json config file that is read in as a dictionary.
The values can be used directly when instantiating blocks. For example,
Sample Rate: yaml_config["samp_rate"]
Optionally, a json schema can be specified to validate the configuration.
For example, you could have a json file that contains:
{
"samp_rate": 1e6,
}
And a schema that contains
{
"type": "object",
"properties": {
"samp_rate": {"type": "number", "exclusiveMinimum": 0}
}
}
If the id of this block is json_config_0, then you can access the samp rate
in other blocks as json_config_0["samp_rate"]
file_format: 1