Python YAML
Library for adding Python code in YAML processing
Experimental - This library exists to try out ideas that enhance and make simpler the management and creation of YAML files. In particular, large YAML files, often seen when using Home Assistant Lovelace front-end.
Requirements
- Python 3.6 (or higher)
Description
Usage documentation is below. More examples beyond what is here is in the
example directory or in the unit tests in test/test_pyaml.py.
This lib is distinguished from other templating languages in that
indentation, crucial in YAML, is preserved on include, eval, and exec
This uses python's eval and exec functions. Google about security concerns
around the use of those. Since this software is not accessing "unaudited" code the
security risk of using eval and exec is viewed as low. Never accept/use
Python code without inspecting the code.
Installation
$ pip install pyaml-processor
Overview
pyaml reads a YAML file and runs the tagged code inside the YAML file. It
supports three processing tags: eval to run code, exec to load code, and include to include other files in the context of the current file. All three
processors are aware of YAML indenting requirements.
Eval
eval is triggered in a YAML file using the tags @% to open an eval and %@
to close an eval. Anything in between the two tags is passed to the Python eval
function for processing. Whatever is returned from the eval is inserted into
the YAML stream. The starting character position of the opening tag is used as
the indent level prepended to everything returned.
For the examples in this section assume that the following Python code
is in the module resources.py and that file contains the following:
from random import randrange
_PATH = "/local/cards/"
def resources(module, module_type):
version = f"?v={randrange(1000000)}"
# This works to, the lib can handle lists, dicts, etc as return values:
# return [{'url': f"{_PATH}/{module}{version}", "type": module_type}]
return f"url: {_PATH}/{module}{version}\ntype: {module_type}"
Example 1:
@+ from resources import resources +@
resources:
- @% resources("layout-card", "module") %@
- @% resources("card-mod", "module") %@
Processing with pyaml results in:
resources:
- url: /local/cards//layout-card?v=238120
type: module
- url: /local/cards//card-mod?v=885753
type: module
Notice that the indentation is preserved from the position on the line where
the eval was invoked.
Note that the space around the start and end tags is optional.
Exec
exec is triggered in a YAML file using the tags @% to open an eval and %@
to close an exec. Anything in between the two tags is passed to the Python exec
function for processing. Whatever is returned from the exec is NOT inserted into
the YAML stream. The code inside the exec tags is dedented meaning
common leading whitespace on each line is removed.
Example 2:
@+
def markdown_card(label):
return \
f"""type: markdown
style: |
ha-card {{background: purple}}
content: |
## {label}"""
+@
title: My awesome Lovelace config
views:
- title: Home
cards:
- @%markdown_card("Kitchen")%@
- @%markdown_card("Living room")%@
Processing with pyaml results in:
title: My awesome Lovelace config
views:
- title: Home
cards:
- type: markdown
style: |
ha-card {background: purple}
content: |
## Kitchen
- type: markdown
style: |
ha-card {background: purple}
content: |
## Living room
Note: any type of Python code may exist between the tags, however, it is likely more maintainable to put code, such the code in the example above, into it's own Python module.
Include
Includes the contents of the file into the YAML stream. The included file
may contain eval and exec blocks. Include is trigged using the same
open and closing tag of @@.
The advantage of using pyaml include over the include processing from PyYAML
is that pyaml preserves indentation.
For example if example3_include.yaml contains:
- zoo: tiger
- moo: cow
And the following YAML file:
big_pets:
@@include some_file.yaml@@
Processing with pyaml results in:
big_pets:
- zoo: tiger
- moo: cow
Running
There are two programs available to try out the library. In the
example directory there is a Python script called simple. This takes
a file name as a single parameter and writes the converted output to
standard out. The input file is a YAML file. While in the example
directory you could, for instance, type ./simple example1.yaml
to see the output of the first example in this README.
The second program is called pyaml is in the bin directory.
It's a slightly more featured. Run it with --help for additional details.
Development
This project uses poetry for development dependencies. Installation instructions are on their website.
To get started developing:
git clone https://github.com/gwww/pyaml.git
cd pyaml
poetry install
poetry shell # Or activate the created virtual environment
pytest # to ensure everything installed properly
There is a Makefile in the root directory as well. The make command
followed by one of the targets in the Makefile can be used. If you don't
have or wish to use make the Makefile serves as examples of common
commands that can be run.
Metadata
Release files for pyaml-processor 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyaml-processor-0.4.0.tar.gz | 7.6 kB | Without provenance | Details for pyaml-processor-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyaml_processor-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for pyaml_processor-0.4.0-py3-none-any.whl |
Total release size: 14.9 kB
Release files / pyaml-processor-0.4.0.tar.gz
| Download URL | pyaml-processor-0.4.0.tar.gz |
|---|---|
| Size | 7.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
408f91c557774cbc739054ceb17aff20c0a92c7265ed2cab126265140ad4831c
|
|
BLAKE2b-256 checksum How to use checksums |
111dc0bcb2070527d39c488504bc994ba043c8d6c84de51f01efc7f3b0a2139f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.0.9 CPython/3.7.7 Darwin/19.5.0
|
Release files / pyaml_processor-0.4.0-py3-none-any.whl
| Download URL | pyaml_processor-0.4.0-py3-none-any.whl |
|---|---|
| Size | 7.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
977296a82f76d203081aa60270b1d7b7998fe13772be1e82b4ae65de32ec4830
|
|
BLAKE2b-256 checksum How to use checksums |
adf6a1f36a1db60f378398ffe8786ed7a51f6d778bf8ed408ea3a209c390a056
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.0.9 CPython/3.7.7 Darwin/19.5.0
|
Security for pyaml-processor 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.