<commit_before># Copyright Hybrid Logic Ltd.  See LICENSE file for details.
# -*- test-case-name: flocker.node.test.test_config -*-

"""
APIs for parsing and validating configuration.
"""

from __future__ import unicode_literals, absolute_import

import os
import types
import yaml

from twisted.python.filepath import FilePath

from ._model import (
    Application, AttachedVolume, Deployment,
    DockerImage, Node, Port
)


class ConfigurationError(Exception):
    """
    Some part of the supplied configuration was wrong.

    The exception message will include some details about what.
    """


class Configuration(object):
    """
    Validate and parse configurations.
    """
    def __init__(self, lenient=False):
        """
        :param bool lenient: If ``True`` don't complain about certain
            deficiencies in the output of ``flocker-reportstate``, In
            particular https://github.com/ClusterHQ/flocker/issues/289 means
            the mountpoint is unknown.
        """
        self._lenient = lenient

    def _parse_environment_config(self, application_name, config):
        """
        Validate and return an application config's environment variables.

        :param unicode application_name: The name of the application.

        :param dict config: The config of a single ``Application`` instance,
            as extracted from the ``applications`` ``dict`` in
            ``_applications_from_configuration``.

        :raises ConfigurationError: if the ``environment`` element of
            ``config`` is not a ``dict`` or ``dict``-like value.

        :returns: ``None`` if there is no ``environment`` element in the
            config, or the ``dict`` of environment variables if there is.

        """
        invalid_env_message = (
            "Application '{application_name}' has a config error. "
            "'environment' must be a dictionary of key/value pairs. ").format(
                application_name=application_name)

        environment = config.pop('environment', None)
        if environment:
            if not isinstance(environment, dict):
                raise ConfigurationError(
                    invalid_env_message + "Got type '{envtype}'".format(
                        envtype=type(environment).__name__)
                )
            for key, value in environment.iteritems():
                if not isinstance(value, types.StringTypes):
                    raise ConfigurationError(
                        ("Application '{application_name}' has a config "
                         "error. Environment variable '{key}' must be of "
                         "type string or unicode; got '{envtype}'.").format(<commit_msg>comment updated; env vars are returned as frozenset now, not dict<commit_after># Copyright Hybrid Logic Ltd.  See LICENSE file for details.
# -*- test-case-name: flocker.node.test.test_config -*-

"""
APIs for parsing and validating configuration.
"""

from __future__ import unicode_literals, absolute_import

import os
import types
import yaml

from twisted.python.filepath import FilePath

from ._model import (
    Application, AttachedVolume, Deployment,
    DockerImage, Node, Port
)


class ConfigurationError(Exception):
    """
    Some part of the supplied configuration was wrong.

    The exception message will include some details about what.
    """


class Configuration(object):
    """
    Validate and parse configurations.
    """
    def __init__(self, lenient=False):
        """
        :param bool lenient: If ``True`` don't complain about certain
            deficiencies in the output of ``flocker-reportstate``, In
            particular https://github.com/ClusterHQ/flocker/issues/289 means
            the mountpoint is unknown.
        """
        self._lenient = lenient

    def _parse_environment_config(self, application_name, config):
        """
        Validate and return an application config's environment variables.

        :param unicode application_name: The name of the application.

        :param dict config: The config of a single ``Application`` instance,
            as extracted from the ``applications`` ``dict`` in
            ``_applications_from_configuration``.

        :raises ConfigurationError: if the ``environment`` element of
            ``config`` is not a ``dict`` or ``dict``-like value.

        :returns: ``None`` if there is no ``environment`` element in the
            config, or the ``frozenset`` of environment variables if there is.

        """
        invalid_env_message = (
            "Application '{application_name}' has a config error. "
            "'environment' must be a dictionary of key/value pairs. ").format(
                application_name=application_name)

        environment = config.pop('environment', None)
        if environment:
            if not isinstance(environment, dict):
                raise ConfigurationError(
                    invalid_env_message + "Got type '{envtype}'".format(
                        envtype=type(environment).__name__)
                )
            for key, value in environment.iteritems():
                if not isinstance(value, types.StringTypes):
                    raise ConfigurationError(
                        ("Application '{application_name}' has a config "
                         "error. Environment variable '{key}' must be of "
                         "type string or unicode; got '{envtype}'.").format(
{'fraction_non_alphanumeric': 0.09152480322167307, 'fraction_numerical': 0.001098297638660077, 'mean_word_length': 2.3624615384615386, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1967136', 'n_tokens_mistral': 1336, 'n_tokens_neox': 1269, 'n_words': 469}