Module flavio.io.yaml
Functions for working with YAML files.
Functions
def dict_constructor(loader, node)-
Expand source code
def dict_constructor(loader, node): return OrderedDict(loader.construct_pairs(node)) def load_include(*args, **kwargs)-
Expand source code
def load_include(*args, **kwargs): return yaml.load(*args, Loader=SafeIncludeLoader, **kwargs) def represent_dict_order(self, data)-
Expand source code
def represent_dict_order(self, data): return self.represent_mapping('tag:yaml.org,2002:map', data.items())
Classes
class SafeIncludeLoader (stream)-
Expand source code
class SafeIncludeLoader(yaml.SafeLoader): """PyYAML loader supporting the `!include` and `!include_merge_list` constructors.""" def __init__(self, stream): try: self._root = os.path.dirname(stream.name) except AttributeError: # this happens when `stream` is a string self._root = None super().__init__(stream) def include(self, node): file = self.construct_scalar(node) filename = self._get_filename(file) if filename is None and self._root is None: raise ValueError("'{}' cannot be included. If the YAML input is a string, '!include' only supports absolute paths.".format(file)) with open(filename, 'r') as f: return yaml.load(f, self.__class__) def include_merge_list(self, node): if self._root is None: warnings.warn("If the YAML input is a string, '!include_merge_list' only supports absolute paths.") files = self.construct_sequence(node) a = [] for file in files: filename = self._get_filename(file) if filename is None: a.append(file) else: try: with open(filename, 'r') as f: a += yaml.load(f, self.__class__) except OSError as e: if e.errno in [errno.ENOENT, errno.EINVAL, errno.EISDIR]: # ENOENT: FileNotFoundError # EINVAL: Invalid argument # EISDIR: IsADirectoryError a.append(file) else: raise return a def _get_filename(self, file): try: if os.path.isabs(file): return file elif self._root is not None: return os.path.join(self._root, file) else: return None except TypeError: return NonePyYAML loader supporting the
!includeand!include_merge_listconstructors.Initialize the scanner.
Ancestors
- yaml.loader.SafeLoader
- yaml.reader.Reader
- yaml.scanner.Scanner
- yaml.parser.Parser
- yaml.composer.Composer
- yaml.constructor.SafeConstructor
- yaml.constructor.BaseConstructor
- yaml.resolver.Resolver
- yaml.resolver.BaseResolver
Class variables
var yaml_constructors
Methods
def include(self, node)-
Expand source code
def include(self, node): file = self.construct_scalar(node) filename = self._get_filename(file) if filename is None and self._root is None: raise ValueError("'{}' cannot be included. If the YAML input is a string, '!include' only supports absolute paths.".format(file)) with open(filename, 'r') as f: return yaml.load(f, self.__class__) def include_merge_list(self, node)-
Expand source code
def include_merge_list(self, node): if self._root is None: warnings.warn("If the YAML input is a string, '!include_merge_list' only supports absolute paths.") files = self.construct_sequence(node) a = [] for file in files: filename = self._get_filename(file) if filename is None: a.append(file) else: try: with open(filename, 'r') as f: a += yaml.load(f, self.__class__) except OSError as e: if e.errno in [errno.ENOENT, errno.EINVAL, errno.EISDIR]: # ENOENT: FileNotFoundError # EINVAL: Invalid argument # EISDIR: IsADirectoryError a.append(file) else: raise return a