<commit_before># -*- coding: utf-8 -*-
import os
from pathlib import Path
from typing import Any, Mapping, Optional, Type

from eduid_common.config.base import TBaseConfigSubclass
from eduid_common.config.parsers.base import BaseConfigParser

__author__ = 'ft'

from eduid_common.config.parsers.exceptions import ParserException


def load_config(
    typ: Type[TBaseConfigSubclass], ns: str, app_name: str, test_config: Optional[Mapping[str, Any]] = None,
) -> TBaseConfigSubclass:
    """ Figure out where to load configuration from, and do it. """
    parser = _choose_parser(app_name, ns)

    if not parser:
        raise ParserException('Could not find a suitable config parser')

    if test_config:
        return typ(**test_config)

    config = parser.read_configuration()

    return typ(**config)


def _choose_parser(app_name: str, ns: str) -> Optional[BaseConfigParser]:
    """
    Choose a parser to use for this app.

    Do local imports accordingly to not make etcd, yaml etc. mandatory requirements for
    all users of eduid-common.

    :param app_name: Name of the application
    :param ns: Namespace for the application

    :return: Config parser instance
    """
    parser: Optional[BaseConfigParser] = None
    ns = os.environ.get('EDUID_CONFIG_NS', ns)
    app_name = os.environ.get('EDUID_CONFIG_APP_NAME', app_name)
    yaml_file = os.environ.get('EDUID_CONFIG_YAML')
    if yaml_file:
        try:
            from eduid_common.config.parsers.yaml import YamlConfigParser

            parser = YamlConfigParser(path=Path(yaml_file), ns=ns, app_name=app_name)
        except ImportError:
            raise ParserException('YamlConfigParser could not be imported')
    if not parser:
        try:
            from eduid_common.config.parsers.etcd import EtcdConfigParser
        except ImportError:
            raise ParserException('EtcdConfigParser could not be imported')

        parser = EtcdConfigParser(namespace=f'/{ns}/{app_name}')
    return parser<commit_msg>fix etcd parser namespace<commit_after># -*- coding: utf-8 -*-
import os
from pathlib import Path
from typing import Any, Mapping, Optional, Type

from eduid_common.config.base import TBaseConfigSubclass
from eduid_common.config.parsers.base import BaseConfigParser

__author__ = 'ft'

from eduid_common.config.parsers.exceptions import ParserException


def load_config(
    typ: Type[TBaseConfigSubclass], ns: str, app_name: str, test_config: Optional[Mapping[str, Any]] = None,
) -> TBaseConfigSubclass:
    """ Figure out where to load configuration from, and do it. """
    parser = _choose_parser(app_name, ns)

    if not parser:
        raise ParserException('Could not find a suitable config parser')

    if test_config:
        return typ(**test_config)

    config = parser.read_configuration()

    return typ(**config)


def _choose_parser(app_name: str, ns: str) -> Optional[BaseConfigParser]:
    """
    Choose a parser to use for this app.

    Do local imports accordingly to not make etcd, yaml etc. mandatory requirements for
    all users of eduid-common.

    :param app_name: Name of the application
    :param ns: Namespace for the application

    :return: Config parser instance
    """
    parser: Optional[BaseConfigParser] = None
    ns = os.environ.get('EDUID_CONFIG_NS', ns)
    app_name = os.environ.get('EDUID_CONFIG_APP_NAME', app_name)
    yaml_file = os.environ.get('EDUID_CONFIG_YAML')
    if yaml_file:
        try:
            from eduid_common.config.parsers.yaml import YamlConfigParser

            parser = YamlConfigParser(path=Path(yaml_file), ns=ns, app_name=app_name)
        except ImportError:
            raise ParserException('YamlConfigParser could not be imported')
    if not parser:
        try:
            from eduid_common.config.parsers.etcd import EtcdConfigParser
        except ImportError:
            raise ParserException('EtcdConfigParser could not be imported')

        parser = EtcdConfigParser(namespace=ns)
    return parser
{'fraction_non_alphanumeric': 0.0845634225669252, 'fraction_numerical': 0.0005003752814610958, 'mean_word_length': 3.4820627802690582, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, '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': '4729379', 'n_tokens_mistral': 1182, 'n_tokens_neox': 1147, 'n_words': 346}