tanbro / pyyaml-include

yaml include other yaml
https://pypi.org/project/pyyaml-include/
GNU General Public License v3.0
77 stars 20 forks source link

Multiple YAML documents in a single file #10

Closed elvis2 closed 4 years ago

elvis2 commented 4 years ago

Is there support forr !include a single yaml file that has three yaml documents? A new document in YAML starts with:

Example

---
name: doc-a

---
name: doc-b

---
name: doc-c

This would be extremely useful if it is not supported. I read through the docs but did not see that this feature is supported.

If it is supported, could you provide an example?

tanbro commented 4 years ago

Thanks for the nice advice!

I think the tag can be used in YAML multiple documents.

eg:

# -*- coding: utf-8 -*-

from __future__ import absolute_import, print_function, unicode_literals
from typing import Iterable

import unittest

from textwrap import dedent

import yaml

from yamlinclude import YamlIncludeConstructor

YAML1 = {'name': '1'}
YAML2 = {'name': '2'}

class MultiLoaderTestCase(unittest.TestCase):

    constructor = YamlIncludeConstructor()

    def setUp(self):
        yaml.add_constructor('!include', self.constructor)

    def test_full_load_all_yaml(self):
        txt = dedent('''
        ---
        file1: !include tests/data/include.d/1.yaml

        ---
        file2: !include tests/data/include.d/2.yaml

        ''')

        iterable = yaml.full_load_all(txt)
        for i, data in enumerate(iterable):
            if i == 0:
                self.assertEqual(data, {'file1': YAML1})
            elif i == 1:
                self.assertEqual(data, {'file2': YAML2})
            else:
                raise RuntimeError()

This tag/constructor woks in yaml.load_all

the testcase was add in commit f972a5e

elvis2 commented 4 years ago

Thank you for the example.

What version of yamlinclude are you using in the example?

tanbro commented 4 years ago

The testcase can run on statble version :-)