Mercurial > hgrepos > Python > libs > ConfigMix
view mixconfig/yaml.py @ 2:9981a68040b6
An INI-style configuration file parser
| author | Franz Glasner <f.glasner@feldmann-mg.com> |
|---|---|
| date | Tue, 08 Mar 2016 15:40:37 +0100 |
| parents | e4c63b4f1568 |
| children | bedc4f95b9e9 |
line wrap: on
line source
# -*- coding: utf-8 -*- r"""Simple wrapper for yaml to support all-unicode strings when loading configuration files. """ from __future__ import division, print_function, absolute_import import yaml __all__ = ["safe_load", "safe_load_all", "load", "load_all"] class ConfigLoader(yaml.Loader): """A YAML loader, which makes all !!str strings to Unicode. Standard PyYAML does this only in the non-ASCII case. """ def construct_yaml_str(self, node): return self.construct_scalar(node) ConfigLoader.add_constructor( "tag:yaml.org,2002:str", ConfigLoader.construct_yaml_str) class ConfigSafeLoader(yaml.SafeLoader): """A safe YAML loader, which makes all !!str strings to Unicode. Standard PyYAML does this only in the non-ASCII case. """ def construct_yaml_str(self, node): return self.construct_scalar(node) ConfigSafeLoader.add_constructor( "tag:yaml.org,2002:str", ConfigSafeLoader.construct_yaml_str) def load(stream, Loader=ConfigLoader): return yaml.load(stream, Loader) def load_all(stream, Loader=ConfigLoader): return yaml.load_all(stream, Loader) def safe_load(stream): return yaml.load(stream, Loader=ConfigSafeLoader) def safe_load_all(stream): return yaml.load_all(stream, Loader=ConfigSafeLoader)
