Skip to main content

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

Project description

pyyaml-include

CircleCI PyPI PyPI - Format PyPI - Status PyPI - Python Version PyPI - Implementation

Extending PyYAML with a custom constructor for 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

yaml.add_constructor('!include', YamlIncludeConstructor())

with open('0.yaml') as f:
    data = yaml.load(f)

print(data)

Include files by name

  • On top level:

    If 0.yaml was:

    !include include.d/1.yaml
    

    We'll get:

    {"name": "1"}
    
  • In mapping:

    If 0.yaml was:

    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.yaml was:

    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/src/Python-1.5/Makefile) or relative (like ../../img/banner.gif).

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, 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 arugment, we shall write the !include tag in Mapping or Sequence mode:

  • Arguments in Sequence mode:

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

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

Authors

liu xue yan liu_xue_yan@foxmail.com

Changelog

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.0.0.4.tar.gz (22.2 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.0.0.4-py2.py3-none-any.whl (3.6 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

File hashes

Hashes for pyyaml-include-1.0.0.4.tar.gz
Algorithm Hash digest
SHA256 64ccfbfcb665a73dd40a960247087af2fab90e112afd4b7cf8a41b7c79071aea
MD5 f02b5605926883711d3e2a72af189959
BLAKE2b-256 6ae1f425c13bad76a020b06252aacf95e2815042358e225ffd00b0424467d941

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyyaml_include-1.0.0.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4445c32f7d1ede5eb68a95d18325846b7d7ee77ed055077a95099496ec3c4852
MD5 cefcc3398e91b004451288938338069f
BLAKE2b-256 55a20e234e41f7707b060a303573a0741b61f737c645b9978cf7feee2c99e69c

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