Ejemplo n.º 1
0
def fix_yaml_loader():
    """Ensure that any string read by yaml is represented as unicode."""
    from yaml import Loader, SafeLoader

    def construct_yaml_str(self, node):
        # Override the default string handling function
        # to always return unicode objects
        return self.construct_scalar(node)

    Loader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
    SafeLoader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
Ejemplo n.º 2
0
def fix_yaml_loader():
    """Ensure that any string read by yaml is represented as unicode."""
    from yaml import Loader, SafeLoader

    def construct_yaml_str(self, node):
        # Override the default string handling function
        # to always return unicode objects
        return self.construct_scalar(node)

    Loader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
    SafeLoader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
Ejemplo n.º 3
0
    def _load_user_settings(self):
        """Load user settings from config file."""
        successful = _ensure_file(self.config_file)

        if not successful:
            LOG.error("Unable to load user config file.")
            return

        self.subscriptions = []

        # Python 2/PyPy shim, per
        # https://stackoverflow.com/questions/2890146/how-to-force-pyyaml-to-load-strings-as-unicode-objects
        # Override the default string handling function to always return unicode objects.
        def construct_yaml_str(self, node):
            """Override to force PyYAML to handle unicode on Python 2."""
            return self.construct_scalar(node)

        SafeLoader.add_constructor("tag:yaml.org,2002:python/unicode", construct_yaml_str)

        with open(self.config_file, "r") as stream:
            LOG.debug("Opening config file to retrieve settings.")
            yaml_settings = yaml.safe_load(stream)

        pretty_settings = yaml.dump(yaml_settings, width=1, indent=4)
        LOG.debug("Settings retrieved from user config file: %s", pretty_settings)

        if yaml_settings is not None:

            # Update self.settings, but only currently valid settings.
            for name, value in yaml_settings.items():
                if name == "subscriptions":
                    pass
                elif name not in self.settings:
                    LOG.debug("Setting %s is not a valid setting, ignoring.", name)
                else:
                    self.settings[name] = value

            fail_count = 0
            for i, yaml_sub in enumerate(yaml_settings.get("subscriptions", [])):
                sub = Subscription.Subscription.parse_from_user_yaml(yaml_sub, self.settings)

                if sub is None:
                    LOG.debug("Unable to parse user YAML for sub # %s - something is wrong.",
                              i + 1)
                    fail_count += 1
                    continue

                self.subscriptions.append(sub)

            if fail_count > 0:
                LOG.error("Some subscriptions from config file couldn't be parsed - check logs.")

        return True
Ejemplo n.º 4
0
Archivo: dynsh.py Proyecto: me-tod/dds
def getSchema():
    def construct_yaml_str(self, node):
        return self.construct_scalar(node)

    Loader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
    SafeLoader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)

    SCHEMA_DIR = getattr(settings, "STATIC_ROOT", None)
    SCHEMA_FILE = SCHEMA_DIR + 'schema.yaml'

    stream = open(SCHEMA_FILE, 'r')
    schema = yaml.load(stream)
    return schema
Ejemplo n.º 5
0
def yaml_load_unicode(stream):
    import yaml
    from yaml import Loader, SafeLoader

    def construct_yaml_str(self, node):
        # Override the default string handling function
        # to always return unicode objects
        return self.construct_scalar(node)

    Loader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
    SafeLoader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)

    return yaml.load(stream)
Ejemplo n.º 6
0
def fix_yaml_loader():
    """确保读出的yaml文件内容
       可以被unicode编码
    """
    from yaml import Loader, SafeLoader

    def construct_yaml_str(self, node):
        # Override the default string handling function
        # to always return unicode objects
        return self.construct_scalar(node)

    Loader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
    SafeLoader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
Ejemplo n.º 7
0
def addOrderedDictToYamlInterpreter():
    _mapping_tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG

    def dict_representer(dumper, data):
        return dumper.represent_dict(data.iteritems())

    def dict_constructor(loader, node):
        return OrderedDict(loader.construct_pairs(node))

    SafeDumper.add_representer(OrderedDict, dict_representer)
    SafeLoader.add_constructor(_mapping_tag, dict_constructor)

    SafeDumper.add_representer(str, SafeRepresenter.represent_str)
Ejemplo n.º 8
0
    def _handle_quirks(self):
        if self._unicode_quirk:
            self._logger.debug('Enabling unicode quirk')

            def construct_yaml_str(self, node):
                try:
                    rawdata = b''.join([chr(ord(x)) for x in self.construct_scalar(node)])
                    return rawdata.decode('utf8')
                except ValueError:
                    # apparently sometimes the data is already correctly encoded
                    return self.construct_scalar(node)

            Loader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
            SafeLoader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
Ejemplo n.º 9
0
    def _handle_quirks(self):
        if self._unicode_quirk:
            self._logger.debug('Enabling unicode quirk')

            def construct_yaml_str(self, node):
                try:
                    rawdata = b''.join([chr(ord(x)) for x in self.construct_scalar(node)])
                    return rawdata.decode('utf8')
                except ValueError:
                    # apparently sometimes the data is already correctly encoded
                    return self.construct_scalar(node)

            Loader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
            SafeLoader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
Ejemplo n.º 10
0
    def __init__(self, filename):
        # Make sure pyyaml always returns unicode
        def construct_yaml_str(self, node):
            return self.construct_scalar(node)

        Loader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
        SafeLoader.add_constructor(u'tag:yaml.org,2002:str',
                                   construct_yaml_str)

        with open(filename, 'r') as f:
            try:
                self.data = yaml.safe_load(f)
            except yaml.YAMLError as e:
                raise e
Ejemplo n.º 11
0
    def _do_loads(self, s, *args, **kwargs):
        import yaml
        from yaml import Loader, SafeLoader

        # Force Unicode string output according to this SO question
        # 2890146/how-to-force-pyyaml-to-load-strings-as-unicode-objects
        def construct_yaml_str(self, node):
            # Override the default string handling function
            # to always return unicode objects
            return self.construct_scalar(node)

        _STR_TAG = 'tag:yaml.org,2002:str'
        Loader.add_constructor(_STR_TAG, construct_yaml_str)
        SafeLoader.add_constructor(_STR_TAG, construct_yaml_str)

        # TODO: optionally utilize C acceleration if available
        return yaml.load(s, *args, **kwargs)
def get_config_yaml( path_config=os.path.dirname(os.path.realpath(__file__)) + "/../config.d/config.yaml", name_config="openstack"):
    import yaml
    from yaml import Loader, SafeLoader

    def construct_yaml_str(self, node):
        # Override the default string handling function 
        # to always return unicode objects
        return self.construct_scalar(node)

    Loader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
    SafeLoader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)

    f = open(path_config)
    # use safe_load instead load
    dataMap = yaml.load(f)[name_config]
    f.close()
    return dataMap
Ejemplo n.º 13
0
from path import Path as path

# https://stackoverflow.com/questions/2890146/how-to-force-pyyaml-to-load-strings-as-unicode-objects
from yaml import Loader, SafeLoader


def construct_yaml_str(self, node):
    """
    Override the default string handling function
    to always return unicode objects
    """
    return self.construct_scalar(node)


Loader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
SafeLoader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)

# SERVICE_VARIANT specifies name of the variant used, which decides what YAML
# configuration files are read during startup.
SERVICE_VARIANT = os.environ.get('SERVICE_VARIANT', None)

# CONFIG_ROOT specifies the directory where the YAML configuration
# files are expected to be found. If not specified, use the project
# directory.
CONFIG_ROOT = path(os.environ.get('CONFIG_ROOT', ENV_ROOT))

# CONFIG_PREFIX specifies the prefix of the YAML configuration files,
# based on the service variant. If no variant is use, don't use a
# prefix.
CONFIG_PREFIX = SERVICE_VARIANT + "." if SERVICE_VARIANT else ""
Ejemplo n.º 14
0
import os

from path import path

# https://stackoverflow.com/questions/2890146/how-to-force-pyyaml-to-load-strings-as-unicode-objects
from yaml import Loader, SafeLoader


def construct_yaml_str(self, node):
    """
    Override the default string handling function
    to always return unicode objects
    """
    return self.construct_scalar(node)
Loader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
SafeLoader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)

# SERVICE_VARIANT specifies name of the variant used, which decides what YAML
# configuration files are read during startup.
SERVICE_VARIANT = os.environ.get('SERVICE_VARIANT', None)

# CONFIG_ROOT specifies the directory where the YAML configuration
# files are expected to be found. If not specified, use the project
# directory.
CONFIG_ROOT = path(os.environ.get('CONFIG_ROOT', ENV_ROOT))

# CONFIG_PREFIX specifies the prefix of the YAML configuration files,
# based on the service variant. If no variant is use, don't use a
# prefix.
CONFIG_PREFIX = SERVICE_VARIANT + "." if SERVICE_VARIANT else ""
Ejemplo n.º 15
0
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals

from yaml import safe_load, safe_dump, SafeLoader


SafeLoader.add_constructor('tag:yaml.org,2002:python/unicode', SafeLoader.construct_yaml_str)


def yaml_load(stream):
    """Loads a dictionary from a stream"""
    return safe_load(stream)


def yaml_dump(data, stream=None):
    """Dumps an object to a YAML string"""
    return safe_dump(data, stream=stream, default_flow_style=False)
Ejemplo n.º 16
0
The files must be either in ``INI`` format or in ``YAML`` format, in
which case, it must end in ``.yaml`` or ``.yml``.
"""

import sys
import os
import configparser
from logging.config import fileConfig, dictConfig
from pathlib import Path
import yaml
from yaml import SafeLoader as sf

_here = Path(__file__).parent
_config_files = [_here / 'defaults.ini', '/etc/ega/conf.ini']

sf.add_constructor('tag:yaml.org,2002:python/tuple',
                   lambda self, node: tuple(sf.construct_sequence(self, node)))


class Configuration(configparser.ConfigParser):
    """Configuration from config_files or environment variables or config server (e.g. Spring Cloud Config)."""

    log_conf = None

    def _load_conf(self, args=None, encoding='utf-8'):
        """Load a configuration file from `args`."""
        # Finding the --conf file
        try:
            conf_file = Path(args[args.index('--conf') + 1]).expanduser()
            if conf_file not in _config_files:
                _config_files.append(conf_file)
                print(f"Overriding configuration settings with {conf_file}",
Ejemplo n.º 17
0
from .game import GameStateModel, NobodyKnewResult
from .loader import GameStateLoader, SpecialField

from yaml import SafeLoader, SafeDumper

SafeLoader.add_constructor(u'!double', SpecialField.doubleJeopardyConstructor)
SafeLoader.add_constructor(u'!image', SpecialField.imageAnswerConstructor)

doubleAndImageConstructor = SpecialField.makeDoubleJeopardyAndConstructor(SpecialField.imageAnswerConstructor)
SafeLoader.add_constructor(u'!double*image', doubleAndImageConstructor)
Ejemplo n.º 18
0
            node=node, value=new_value)
        raise YAMLError(msg)
    return new_value


def _constructor_envfile_variables(loader, node):
    """
    Extracts the environment variable from the node's value.
    :param yaml.Loader loader: the yaml loader
    :param node: the current node in the yaml
    :return: value read from file pointed to by environment variable
    """
    raw_value = loader.construct_scalar(node)
    filepath = os.environ.get(raw_value)
    try:
        with open(filepath, "r") as fd:
            new_value = fd.read()
    except (TypeError, IOError) as e:
        msg = "Cannot construct value from {node}: {path}".format(
            node=node, path=filepath)
        raise YAMLError(msg) from e
    else:
        return new_value


TAG_ENV = "!ENV"
TAG_ENVFILE = "!ENVFILE"

_safe_loader.add_constructor(TAG_ENV, _constructor_env_variables)
_safe_loader.add_constructor(TAG_ENVFILE, _constructor_envfile_variables)
Ejemplo n.º 19
0
def addNobodyKnewResultToYamlInterpreter():
    SafeDumper.add_representer(
        NobodyKnewResult,
        lambda dumper, y: dumper.represent_scalar("!nobody", ""))
    SafeLoader.add_constructor("!nobody", lambda x, y: NobodyKnewResult())