<commit_before>"""Environment variable parser."""

import json
import logging
import os
import warnings
from typing import Any, Dict, Iterator, Mapping, NamedTuple, Sequence, Set

from .file_loaders import load_from_filepath_or_content
from .utils import update_nested


logger = logging.getLogger(__name__)

EnvSetting = NamedTuple('EnvSetting', [('name', str), ('value', Mapping[str, Any])])


class EnvParser:
    r"""Environment variable parser.

    Args:
        prefix: Only environment variables which start with this string
            (case insensitive) are considered.
        split_char: Character to split variables at. Note that if prefix
            is given, the variable name must also be seperated from the base
            with this character.
        implicit_depth: Maximumum depth of implicitely nested environment
            variables to consider. If set, the first `implicit_depth`
            occurrences of a single `split_char` character will be considered
            nested settings boundaries. Note that if a file is given, the
            maximum depth does not apply as the definition is clear.
        settings_file_suffix: Suffix to identify an environment variable as a
            settings file.

            >>> env_parser=EnvParser(prefix='A', split_char='_', setting_file_suffix='SF')
            >>> env_parser.settings_file_env_var
            'A_SF'

        exclude: Environment variables to exclude. Note that the settings file
            constructed from ``settings_file_suffix`` is excluded in any case.

    Attributes:
        settings_file_env_var: Name of the settings file environment variable.
            Is constructed automatically.

    Examples:
        >>> env_parser = EnvParser(prefix='THIS_EXAMPLE', implicit_depth=1)
        >>>
        >>> with os.open('settings.toml', 'w') as f:
        ...     f.write('[testgroup]\nother_var = 345')
        >>> os.environ['THIS_EXAMPLE_TESTGROUP_TESTVAR'] = 27
        >>> os.environ['THIS_EXAMPLE_SETTINGS_FILE'] = './settings.toml'
        >>>
        >>> result_dict = env_parser.parse()
        >>> result_dict
        {'testgroup': {'testvar': 27, 'othervar': 345}}

    """

    def __init__(self,
                 prefix: str = 'APP_SETTINGS',
                 split_char: str = '_',
                 implicit_depth: int = 0,
                 max_depth=None,
                 settings_file_suffix: str = 'SETTINGS_FILE',
                 exclude: Sequence[str] = ()) -> None:
        """Initialize object."""
        self.settings_file_suffix = str(settings_file_suffix)
        if max_depth is not None:
            # Use warnings module as logging probably isn't configured yet at this stage of the app.
            warnings.warn('`max_depth` is deprecated and will be removed '
                          'in next release. Please use `implicit_depth` instead.')
            self.implicit_depth = int(max_depth)
        else:
            self.implicit_depth = int(implicit_depth)
        self.split_char = split_char
        self.prefix = prefix
        self.exclude = exclude

    @property
    def exclude(self) -> Set[str]:
        """Return excluded environment variables."""
        exclude = self._exclude.union({self.settings_file_env_var})
        return set(s.lower() for s in exclude)

    @exclude.setter
    def exclude(self, exclude: Sequence = ()) -> None:
        """Set excluded environment variables."""
        if isinstance(exclude, str):
            exclude = (exclude,)
        self._exclude = set(exclude)<commit_msg>Remove max depth argument<commit_after>"""Environment variable parser."""

import json
import logging
import os
from typing import Any, Dict, Iterator, Mapping, NamedTuple, Sequence, Set

from .file_loaders import load_from_filepath_or_content
from .utils import update_nested


logger = logging.getLogger(__name__)

EnvSetting = NamedTuple('EnvSetting', [('name', str), ('value', Mapping[str, Any])])


class EnvParser:
    r"""Environment variable parser.

    Args:
        prefix: Only environment variables which start with this string
            (case insensitive) are considered.
        split_char: Character to split variables at. Note that if prefix
            is given, the variable name must also be seperated from the base
            with this character.
        implicit_depth: Maximumum depth of implicitely nested environment
            variables to consider. If set, the first `implicit_depth`
            occurrences of a single `split_char` character will be considered
            nested settings boundaries. Note that if a file is given, the
            maximum depth does not apply as the definition is clear.
        settings_file_suffix: Suffix to identify an environment variable as a
            settings file.

            >>> env_parser=EnvParser(prefix='A', split_char='_', setting_file_suffix='SF')
            >>> env_parser.settings_file_env_var
            'A_SF'

        exclude: Environment variables to exclude. Note that the settings file
            constructed from ``settings_file_suffix`` is excluded in any case.

    Attributes:
        settings_file_env_var: Name of the settings file environment variable.
            Is constructed automatically.

    Examples:
        >>> env_parser = EnvParser(prefix='THIS_EXAMPLE', implicit_depth=1)
        >>>
        >>> with os.open('settings.toml', 'w') as f:
        ...     f.write('[testgroup]\nother_var = 345')
        >>> os.environ['THIS_EXAMPLE_TESTGROUP_TESTVAR'] = 27
        >>> os.environ['THIS_EXAMPLE_SETTINGS_FILE'] = './settings.toml'
        >>>
        >>> result_dict = env_parser.parse()
        >>> result_dict
        {'testgroup': {'testvar': 27, 'othervar': 345}}

    """

    def __init__(self,
                 prefix: str = 'APP_SETTINGS',
                 split_char: str = '_',
                 implicit_depth: int = 0,
                 settings_file_suffix: str = 'SETTINGS_FILE',
                 exclude: Sequence[str] = ()) -> None:
        """Initialize object."""
        self.settings_file_suffix = str(settings_file_suffix)
        self.implicit_depth = int(implicit_depth)
        self.split_char = split_char
        self.prefix = prefix
        self.exclude = exclude

    @property
    def exclude(self) -> Set[str]:
        """Return excluded environment variables."""
        exclude = self._exclude.union({self.settings_file_env_var})
        return set(s.lower() for s in exclude)

    @exclude.setter
    def exclude(self, exclude: Sequence = ()) -> None:
        """Set excluded environment variables."""
        if isinstance(exclude, str):
            exclude = (exclude,)
        self._exclude = set(exclude)
{'fraction_non_alphanumeric': 0.09518126212143817, 'fraction_numerical': 0.003580486349395793, 'mean_word_length': 2.519160104986877, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 69, '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': '6690984', 'n_tokens_mistral': 1801, 'n_tokens_neox': 1718, 'n_words': 552}