Extending PyYAML with a custom constructor for including YAML files within YAML files
Project description
pyyaml-include
An extending constructor of PyYAML, which including YAML files within YAML files.
Install
pip install pyyaml-include
Usage
Consider we have such YAML files:
├── 0.yaml
└── include.d
├── 1.yaml
└── 2.yaml
-
1.yaml's content:name: "1"
-
2.yaml's content:name: "2"
To include 1.yaml, 2.yaml in 0.yaml, we shall first add YamlIncludeConstructor to PyYAML's loader, then write !include tag in 0.yaml and load it:
import yaml
from yamlinclude import YamlIncludeConstructor
YamlIncludeConstructor.add_to_loader_class(yaml.FullLoader, base_dir='/your/conf/dir')
with open('0.yaml') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
print(data)
Include files by name
-
On top level:
If
0.yamlwas:!include include.d/1.yaml
We'll get:
{"name": "1"}
-
In mapping:
If
0.yamlwas:file1: !include include.d/1.yaml file2: !include include.d/2.yaml
We'll get:
file1: name: "1" file2: name: "2"
-
In sequence:
If
0.yamlwas:files: - !include include.d/1.yaml - !include include.d/2.yaml
We'll get:
files: - name: "1" - name: "2"
ℹ Note:
File name can be either absolute (like
/usr/conf/1.5/Make.yml) or relative (like../../cfg/img.yml).
Include files by wildcards
File name can contain shell-style wildcards. Data loaded from the file(s) found by wildcards will be set in a sequence.
If 0.yaml was:
files: !include include.d/*.yaml
We'll get:
files:
- name: "1"
- name: "2"
ℹ Note:
- For
Python>=3.5, ifrecursiveargument of!includeYAML tag istrue, the pattern“**”will match any files and zero or more directories and subdirectories.- Using the
“**”pattern in large directory trees may consume an inordinate amount of time because of recursive search.
In order to enable recursive argument, we shall write the !include tag in Mapping or Sequence mode:
-
Arguments in
Sequencemode:!include [tests/data/include.d/**/*.yaml, true]
-
Arguments in
Mappingmode:!include {pathname: tests/data/include.d/**/*.yaml, recursive: true}
Authors
liu xue yan liu_xue_yan@foxmail.com
Changelog
1.1
Date: 2019-03-18
- Change:
- Update PyYAML to 5.*
- Argument
loader_classofYamlIncludeConstructor.add_to_loader_class()renamed and required(former:loader_cls, default=None)
1.0.4
Date: 2019-01-07
-
Change:
- rename:
TAG==>DEFAULT_TAG_NAME - add:
encodingargument
- rename:
-
Fix:
- A wrong logging text format
-
Misc:
- add:
.pylintrc
- add:
1.0.3
Date: 2018-12-04
-
New Feature:
- Add
base_dirargument
- Add
-
Misc:
- Add some new unit-test
- Add Python3.7 in CircleCI
1.0.2
Date: 2018-07-11
-
Add:
encodingargument
-
Bug fix:
- encoding error if non-ascii characters on non-utf8 os.
1.0.1
Date: 2018-07-03
-
Add:
-
Old Python2.6 and new Python3.7 compatibilities
-
class method
add_to_loader_classA class method to add the constructor itself into YAML loader class
-
Sphinx docs
-
-
Change:
-
Rename module file
include.pytoconstructor.py -
Rename class data member
DEFAULT_TAGtoTAG
-
1.0
Date: 2018-06-08
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyyaml-include-1.1.tar.gz.
File metadata
- Download URL: pyyaml-include-1.1.tar.gz
- Upload date:
- Size: 34.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb2cba0443fb581b1b70f40b0c17ab55c90b6153f4e62bd545955cf83f4a6bd2
|
|
| MD5 |
3207d28799415f4cad16146fb3d4ee35
|
|
| BLAKE2b-256 |
b8022ade173f15d576d883448c6454efab735ab98d39dcb13043d0de15d9e427
|
File details
Details for the file pyyaml_include-1.1-py2.py3-none-any.whl.
File metadata
- Download URL: pyyaml_include-1.1-py2.py3-none-any.whl
- Upload date:
- Size: 18.4 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
42aea0b46d792347d2b31a5ced21ef6937d49ac0f4c4177c6a06a39ccdb12e5a
|
|
| MD5 |
d824140ac2ad933982c61e47669f88ba
|
|
| BLAKE2b-256 |
5467c1ea9129d04ff4298266912aa3d2ca999ceab1e1c396a88763d0b0b15b10
|