YAML: add !include_named_dir and ! include_list_dir (#2054)
* add include_dir constructor for yaml parsing * changed to allow for flat and name based directory including * fixed ci errors * changed flat to listpull/2067/head
parent
96b73684eb
commit
954b56475e
|
@ -3,6 +3,7 @@ import logging
|
|||
import os
|
||||
from collections import OrderedDict
|
||||
|
||||
import glob
|
||||
import yaml
|
||||
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
|
@ -44,6 +45,22 @@ def _include_yaml(loader, node):
|
|||
return load_yaml(fname)
|
||||
|
||||
|
||||
def _include_dir_named_yaml(loader, node):
|
||||
"""Load multiple files from dir."""
|
||||
mapping = OrderedDict()
|
||||
files = os.path.join(os.path.dirname(loader.name), node.value, '*.yaml')
|
||||
for fname in glob.glob(files):
|
||||
filename = os.path.splitext(os.path.basename(fname))[0]
|
||||
mapping[filename] = load_yaml(fname)
|
||||
return mapping
|
||||
|
||||
|
||||
def _include_dir_list_yaml(loader, node):
|
||||
"""Load multiple files from dir."""
|
||||
files = os.path.join(os.path.dirname(loader.name), node.value, '*.yaml')
|
||||
return [load_yaml(f) for f in glob.glob(files)]
|
||||
|
||||
|
||||
def _ordered_dict(loader, node):
|
||||
"""Load YAML mappings into an ordered dict to preserve key order."""
|
||||
loader.flatten_mapping(node)
|
||||
|
@ -84,3 +101,5 @@ yaml.SafeLoader.add_constructor('!include', _include_yaml)
|
|||
yaml.SafeLoader.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
|
||||
_ordered_dict)
|
||||
yaml.SafeLoader.add_constructor('!env_var', _env_var_yaml)
|
||||
yaml.SafeLoader.add_constructor('!include_dir_list', _include_dir_list_yaml)
|
||||
yaml.SafeLoader.add_constructor('!include_dir_named', _include_dir_named_yaml)
|
||||
|
|
Loading…
Reference in New Issue