mirror of
https://github.com/github/octodns.git
synced 2024-05-11 05:55:00 +00:00
Add support for !include YAML directive
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
#
|
||||
#
|
||||
|
||||
from os.path import dirname, join
|
||||
|
||||
from natsort import natsort_keygen
|
||||
from yaml import SafeDumper, SafeLoader, dump, load
|
||||
from yaml.constructor import ConstructorError
|
||||
@@ -23,7 +25,17 @@ class ContextLoader(SafeLoader):
|
||||
def _construct(self, node):
|
||||
return self._pairs(node)[0]
|
||||
|
||||
def include(self, node):
|
||||
mark = self.get_mark()
|
||||
directory = dirname(mark.name)
|
||||
|
||||
filename = join(directory, self.construct_scalar(node))
|
||||
|
||||
with open(filename, 'r') as fh:
|
||||
return safe_load(fh, self.__class__)
|
||||
|
||||
|
||||
ContextLoader.add_constructor('!include', ContextLoader.include)
|
||||
ContextLoader.add_constructor(
|
||||
ContextLoader.DEFAULT_MAPPING_TAG, ContextLoader._construct
|
||||
)
|
||||
|
||||
5
tests/config/include/array.yaml
Normal file
5
tests/config/include/array.yaml
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
- 14
|
||||
- 15
|
||||
- 16
|
||||
- 72
|
||||
3
tests/config/include/dict.yaml
Normal file
3
tests/config/include/dict.yaml
Normal file
@@ -0,0 +1,3 @@
|
||||
---
|
||||
k: v
|
||||
z: 42
|
||||
1
tests/config/include/empty.yaml
Normal file
1
tests/config/include/empty.yaml
Normal file
@@ -0,0 +1 @@
|
||||
---
|
||||
2
tests/config/include/include-doesnt-exist.yaml
Normal file
2
tests/config/include/include-doesnt-exist.yaml
Normal file
@@ -0,0 +1,2 @@
|
||||
---
|
||||
key: !include does-not-exist.yaml
|
||||
8
tests/config/include/main.yaml
Normal file
8
tests/config/include/main.yaml
Normal file
@@ -0,0 +1,8 @@
|
||||
---
|
||||
included-array: !include array.yaml
|
||||
included-dict: !include dict.yaml
|
||||
included-empty: !include empty.yaml
|
||||
included-nested: !include nested.yaml
|
||||
included-subdir: !include subdir/value.yaml
|
||||
key: value
|
||||
name: main
|
||||
2
tests/config/include/nested.yaml
Normal file
2
tests/config/include/nested.yaml
Normal file
@@ -0,0 +1,2 @@
|
||||
---
|
||||
!include subdir/value.yaml
|
||||
2
tests/config/include/subdir/value.yaml
Normal file
2
tests/config/include/subdir/value.yaml
Normal file
@@ -0,0 +1,2 @@
|
||||
---
|
||||
Hello World!
|
||||
@@ -62,3 +62,27 @@ class TestYaml(TestCase):
|
||||
buf = StringIO()
|
||||
safe_dump({'45a03129': 42, '45a0392a': 43}, buf)
|
||||
self.assertEqual("---\n45a0392a: 43\n45a03129: 42\n", buf.getvalue())
|
||||
|
||||
def test_include(self):
|
||||
with open('tests/config/include/main.yaml') as fh:
|
||||
data = safe_load(fh)
|
||||
self.assertEqual(
|
||||
{
|
||||
'included-array': [14, 15, 16, 72],
|
||||
'included-dict': {'k': 'v', 'z': 42},
|
||||
'included-empty': None,
|
||||
'included-nested': 'Hello World!',
|
||||
'included-subdir': 'Hello World!',
|
||||
'key': 'value',
|
||||
'name': 'main',
|
||||
},
|
||||
data,
|
||||
)
|
||||
|
||||
with open('tests/config/include/include-doesnt-exist.yaml') as fh:
|
||||
with self.assertRaises(FileNotFoundError) as ctx:
|
||||
data = safe_load(fh)
|
||||
self.assertEqual(
|
||||
"[Errno 2] No such file or directory: 'tests/config/include/does-not-exist.yaml'",
|
||||
str(ctx.exception),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user