Process values in YAML files
Project description
YAML Processor
Installation
To install from PyPI, run:
python3 -m pip install yamlprocessor
YAML Modularisation / Include
Allow modularisation of YAML files using a controlled include file mechanism, backed by dividing the original JSON schema file into a set of subschema files.
Consider a YAML file hello.yaml
:
hello:
- location: earth
targets:
- human
- cat
- dog
- location: mars
targets:
- martian
# And so on
And its associated JSON schema file:
{
"properties": {
"hello": {
"items": {
"properties": {
"location": {
"type": "string"
},
"targets": {
"items": {
"type": "string"
},
"minItems": 1,
"type": "array",
"uniqueItems": true
}
},
"required": ["location", "targets"],
"type": "object"
},
"type": "array"
}
},
"required": ["hello"],
"type": "object"
}
We want to modularise the YAML file in this way.
Let's call this hello-root.yaml
:
hello:
- INCLUDE: earth.yaml
- INCLUDE: mars.yaml
Where earth.yaml
contains:
location: earth
targets:
- human
- cat
- dog
And mars.yaml
contains:
location: mars
targets:
- martian
At runtime, we can run the yp-data INFILE OUTFILE
command to process and recombine the YAML files.
To split the schema to support these YAML files, however, we'll
use the yp-schema SCHEMA-FILE CONFIG-FILE
command.
For this command to work, we need to supply it with some settings
to tell it where to split up the schema in the syntax:
{
"OUTPUT-ROOT-SCHEMA-FILENAME": "",
"OUTPUT-SUB-SCHEMA-FILENAME-1": "JMESPATH-1",
/* and so on */
}
Obviously, we must have a root schema output file name.
The rest of the entries are output file names for the subschemas.
The https://jmespath.org/ syntax tells the
yp-schema
command where to split JSON schema into
subschemas. In the example above, we can give use the setting:
{
"hello.schema.json": "",
"hello-location.schema.json": "properties.hello.items"
}
The resulting hello.schema.json
will look like this,
which can be used to validate both hello.yaml
and hello-root.yaml
:
{
"properties": {
"hello": {
"items": {
"oneOf": [
{"$ref": "hello-location.schema.json"},
{
"properties": {
"INCLUDE": {
"type": "string"
},
"QUERY": {
"type": "string"
}
},
"required": ["INCLUDE"],
"type": "string"
}
]
},
"type": "array"
}
},
"required": ["hello"],
"type": "object"
}
The resulting hello-location.schema.json
will look like this
which can be used to validate earth.yaml
and mars.yaml
:
{
"properties": {
"location": {
"type": "string"
},
"targets": {
"items": {
"type": "string"
},
"minItems": 1,
"type": "array",
"uniqueItems": true
}
},
"required": ["location", "targets"],
"type": "object"
}
YAML Modularisation / Include with Query
Consider an example where we want to include only a subset of the data structure from the include file. We can use a JMESPath query to achieve this.
For example, we may have something like this in hello-root.yaml
:
hello:
INCLUDE: planets.yaml
QUERY: "[?type=='rocky'].{location: location, targets: targets}"
Where planets.yaml
contains:
- location: earth
type: rocky
targets:
- human
- cat
- dog
- location: mars
type: rocky
targets:
- martian
- location: jupiter
type: gaseous
targets:
- ...
Running yp-data hello-root.yaml
will return:
hello:
- location: earth
targets:
- human
- cat
- dog
- location: mars
targets:
- martian
YAML String Value Variable Substitution
Process variable substitution syntax for string values in YAML files. Consider:
key: ${SWEET_HOME}/sugar.txt
If SWEET_HOME
is defined in the environment and has a value /home/sweet
,
then running yp-data
on the above will give:
key: /home/sweet/sugar.txt
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for yamlprocessor-0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7baa12a5a7e30fdd2531d203619090f68ca6d8c7b4b6017e3937add7f5c9b4db |
|
MD5 | 69c8fcb17a262fa7e2b821f6ef6d766c |
|
BLAKE2b-256 | eb04f77c45df266456e7638cec563a3979c50634a8cb69c4f715cb7d67fdfd20 |