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 Codacy Badge Codacy Badge

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

YamlIncludeConstructor.add_to_loader_class(base_dir='/your/conf/dir')

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.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
  • Bugix:

    • 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.0.3.tar.gz (28.4 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.3-py2.py3-none-any.whl (18.9 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: pyyaml-include-1.0.3.tar.gz
  • Upload date:
  • Size: 28.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for pyyaml-include-1.0.3.tar.gz
Algorithm Hash digest
SHA256 de0fdb84a41353c13073a0decc036b77d2c619a7831f72a616216860c60e17d4
MD5 f1ecb0832a701accda17c9d8691fd1cf
BLAKE2b-256 c4b4852cecfdc79dabec658469b6c75a9689c5c71f4ff55dab6b6db61f6fd475

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyyaml_include-1.0.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 18.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for pyyaml_include-1.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 18b4a54a7a4a0dfe3c58bf8074e5fe4c81cb764feea0c5f4146967aa403ac06a
MD5 888f6ebfa5dda435921a5455b2916654
BLAKE2b-256 1940cdcdd1c5551a1f6e1c449d7e11c1af299f5102e66fc1ac4fd79915a3375d

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