Skip to main content

Extending PyYAML with a custom constructor for including YAML files within YAML files

Project description

pyyaml-include

CircleCI Documentation Status GitHub tag PyPI PyPI - License PyPI - Format PyPI - Status PyPI - Python Version PyPI - Implementation Maintainability Test Coverage

An extending constructor of PyYAML: include YAML files into YAML document.

Install

pip install pyyaml-include

Usage

Consider we have such YAML files:

├── 0.yml
└── include.d
    ├── 1.yml
    └── 2.yml
  • 1.yml 's content:

    name: "1"
    
  • 2.yml 's content:

    name: "2"
    

To include 1.yml, 2.yml in 0.yml, we shall add YamlIncludeConstructor to PyYAML's loader, then add an !include tag in 0.yaml:

import yaml
from yamlinclude import YamlIncludeConstructor

YamlIncludeConstructor.add_to_loader_class(loader_class=yaml.FullLoader, base_dir='/your/conf/dir')

with open('0.yml') as f:
    data = yaml.load(f, Loader=yaml.FullLoader)

print(data)

Mapping

If 0.yml was:

file1: !include include.d/1.yml
file2: !include include.d/2.yml

We'll get:

file1:
  name: "1"
file2:
  name: "2"

Sequence

If 0.yml was:

files:
  - !include include.d/1.yml
  - !include include.d/2.yml

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).

Wildcards

File name can contain shell-style wildcards. Data loaded from the file(s) found by wildcards will be set in a sequence.

That is to say, a list will be returned when including file name contains wildcards. Length of the returned list equals number of matched files:

  • when only 1 file matched, length of list will be 1
  • when there are no files matched, an empty list will be returned

If 0.yml was:

files: !include include.d/*.yml

We'll get:

files:
  - name: "1"
  - name: "2"

Note:

  • For Python>=3.5, if recursive argument of !include YAML tag is true, 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 set it in Mapping or Sequence arguments mode:

  • Arguments in Sequence mode:

    !include [tests/data/include.d/**/*.yml, true]
    
  • Arguments in Mapping mode:

    !include {pathname: tests/data/include.d/**/*.yml, recursive: true}
    

Non YAML files

This extending constructor can now load data from non YAML files, supported file types are:

  • json
  • toml (only available when with toml installed)
  • ini

The constructor read non YAML files by different readers according to a pattern table defined in src/yamlinclude/readers.py.

Default reader table can be replaced by a custom reader_map when call add_to_loader_class.

AUTHORS

Changelog

1.2

Date: 2019-02-03

  • New:

    • non YAML file including
  • Misc:

    • adjust docs
    • add pip and conda configure file of development environment

1.1

Date: 2019-03-18

  • Change:
    • Update PyYAML to 5.*
    • Rename: Argument loader_class of YamlIncludeConstructor.add_to_loader_class() (former: loader_cls)

1.0.4

Date: 2019-01-07

  • Change:

    • rename: TAG ==> DEFAULT_TAG_NAME
    • add: encoding argument
  • Fix:

    • A wrong logging text format
  • Misc:

    • add: .pylintrc

1.0.3

Date: 2018-12-04

  • New Feature:

    • Add base_dir argument
  • Misc:

    • Add some new unit-test
    • Add Python3.7 in CircleCI

1.0.2

Date: 2018-07-11

  • Add:

    • encoding argument
  • 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_class

      A class method to add the constructor itself into YAML loader class

    • Sphinx docs

  • Change:

    • Rename module file include.py to constructor.py

    • Rename class data member DEFAULT_TAG to TAG

1.0

Date: 2018-06-08

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

pyyaml-include-1.2.tar.gz (38.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyyaml_include-1.2-py2.py3-none-any.whl (20.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pyyaml-include-1.2.tar.gz.

File metadata

  • Download URL: pyyaml-include-1.2.tar.gz
  • Upload date:
  • Size: 38.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.1.post20191125 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.7.5

File hashes

Hashes for pyyaml-include-1.2.tar.gz
Algorithm Hash digest
SHA256 2343c4dad744d3ce907ec50683b29b4383c7c967f142275bdad8ed56d4de9d94
MD5 489f9e3158c7db955af5b43a38968d98
BLAKE2b-256 6818e774bf0e7f7cd5e9ca52edbf3cab045ca7504c2b4bc5e00c1f504b8cf34f

See more details on using hashes here.

File details

Details for the file pyyaml_include-1.2-py2.py3-none-any.whl.

File metadata

  • Download URL: pyyaml_include-1.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 20.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.1.post20191125 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.7.5

File hashes

Hashes for pyyaml_include-1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2d4e3843110cf515aaf1568b217b05061f8e9f57cd3b35e8e654c838717796cb
MD5 3a1e3f2882b6519f2e34519d0e31a02c
BLAKE2b-256 6aa7b79b8a329691f5a3405b7a7e42677cc1c88921159baa35f49e088e615029

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page