<commit_before>"""
Dynamic Settings Loader.

Values will be read from the module specified by the DYNACONF_SETTINGS_MODULE
environment variable;
"""
import os
import types
import errno
import importlib
from contextlib import contextmanager
from dynaconf import default_settings
from dynaconf.utils.parse_conf import converters
from dynaconf.conf.exceptions import ImproperlyConfigured
from dynaconf.conf.functional import LazyObject, empty
from dynaconf.loaders import default_loader


ENVIRONMENT_VARIABLE = default_settings.ENVVAR_FOR_DYNACONF


class LazySettings(LazyObject):
    """
    A lazy proxy for either global settings or a custom settings obj
    The user can manually configure settings prior to using them. Otherwise,
    uses the settings module pointed to by DYNACONF_SETTINGS_MODULE.
    """

    def _setup(self, name=None):
        """
        Load the settings module pointed to by the environment variable. This
        is used the first time we need any settings at all, if the user has
        not previously configured the settings manually.
        """
        settings_module = os.environ.get(ENVIRONMENT_VARIABLE)
        if not settings_module:
            desc = ("setting %s" % name) if name else "settings"
            raise ImproperlyConfigured(
                "Requested %s, but settings are not configured. "
                "You must either define the environment variable %s "
                "or call settings.configure() before accessing settings."
                % (desc, ENVIRONMENT_VARIABLE))

        self._wrapped = Settings(settings_module)

    def __getattr__(self, name):
        if self._wrapped is empty:
            self._setup(name)
        return getattr(self._wrapped, name)

    def configure(self, root=None,
                  default_settings=default_settings, **options):
        """
        Called to manually configure the settings. The 'default_settings'
        parameter sets where to retrieve any unspecified values from (its
        argument must support attribute access (__getattr__)).
        """
        if root:
            if root.startswith('/'):
                default_settings.PROJECT_ROOT = root
            else:
                default_settings.PROJECT_ROOT = os.path.realpath(root)

        if self._wrapped is not empty:
            raise RuntimeError('Settings already configured.')

        holder = UserSettingsHolder(default_settings)
        for name, value in options.items():
            setattr(holder, name, value)
        self._wrapped = holder

    @property
    def configured(self):
        """
        Returns True if the settings have already been configured.
        """
        return self._wrapped is not empty

    def __call__(self, *args, **kwargs):
        return self.get(*args, **kwargs)


class BaseSettings(object):
    """
    Common logic for settings whether set by a module or by the user.
    """
    @property
    def store(self):
        if not hasattr(self, '_store'):
            self._store = {
                key: value
                for key, value
                in default_settings.__dict__.items()
                if key.isupper()
            }
        return self._store

    def keys(self):
        return self.store.keys()

    def values(self):
        return self.store.values()

    def get(self, key, default=None, cast=None):
        data = self.store.get(key, default)
        if cast:
            data = converters.get(cast)(data)
        return data

    def __call__(self, *args, **kwargs):
        return self.get(*args, **kwargs)

    def as_bool(self, key):
        return self.get(key, cast='@bool')

    def as_int(self, key):
        return self.get(key, cast='@int')

    def as_float(self, key):
        return self.get(key, cast='@float')

    def as_json(self, key):
        return self.get(key, cast='@json')

    @contextmanager
    def using_namespace(self, namespace):
        try:
            self.namespace(namespace)
            yield
        finally:
            self.namespace()

    def namespace(self, namespace=None):
        namespace = namespace or 'DYNACONF'
        if not isinstance(namespace, basestring):
            raise AttributeError('namespace should be a string')
        if "_" in namespace:
            raise AttributeError('namespace should not contains _')
        self.DYNACONF_NAMESPACE = namespace.upper()
        self.load_from_envvar_namespace(namespace=namespace, silent=False)

    def load_from_envvar_namespace(self, namespace=None, silent=True):
        namespace = namespace or getattr(
            self, 'DYNACONF_NAMESPACE', 'DYNACONF')
        try:
            data = {
                key.partition('_')[-1]: parse_conf_data(data)
                for key, data
                in os.environ.items()
                if key.startswith(namespace)
            }
            for key, value in data.items():
                setattr(self, key, value)
                self.store[key] = value
        except Exception as e:
            if silent:
                return False
            e.message = 'Unable to load config env namespace (%s)' % e.message
            raise


class Settings(BaseSettings):
    def __init__(self, settings_module):
        # store the settings module in case someone later cares
        self.SETTINGS_MODULE = settings_module

        mod = importlib.import_module(self.SETTINGS_MODULE)

        self._explicit_settings = set()
        for setting in dir(mod):
            if setting.isupper():
                setting_value = getattr(mod, setting)
                setattr(self, setting, setting_value)
                self._explicit_settings.add(setting)
                self.store[setting] = setting_value

        if not hasattr(self, 'PROJECT_ROOT'):
            self.PROJECT_ROOT = os.path.realpath(
                os.path.dirname(os.path.abspath(settings_module))
            ) if loaded_from == 'module' else os.getcwd()

        self.execute_loaders()

    def import_from_filename(self, filename, silent=False):
        mod = types.ModuleType('config')<commit_msg>can also load from arbitraty filepath<commit_after>"""
Dynamic Settings Loader.

Values will be read from the module specified by the DYNACONF_SETTINGS_MODULE
environment variable;
"""
import os
import types
import errno
import importlib
from contextlib import contextmanager
from dynaconf import default_settings
from dynaconf.utils.parse_conf import converters
from dynaconf.conf.exceptions import ImproperlyConfigured
from dynaconf.conf.functional import LazyObject, empty
from dynaconf.loaders import default_loader


ENVIRONMENT_VARIABLE = default_settings.ENVVAR_FOR_DYNACONF


class LazySettings(LazyObject):
    """
    A lazy proxy for either global settings or a custom settings obj
    The user can manually configure settings prior to using them. Otherwise,
    uses the settings module pointed to by DYNACONF_SETTINGS_MODULE.
    """

    def _setup(self, name=None):
        """
        Load the settings module pointed to by the environment variable. This
        is used the first time we need any settings at all, if the user has
        not previously configured the settings manually.
        """
        settings_module = os.environ.get(ENVIRONMENT_VARIABLE)
        if not settings_module:
            desc = ("setting %s" % name) if name else "settings"
            raise ImproperlyConfigured(
                "Requested %s, but settings are not configured. "
                "You must either define the environment variable %s "
                "or call settings.configure() before accessing settings."
                % (desc, ENVIRONMENT_VARIABLE))

        self._wrapped = Settings(settings_module)

    def __getattr__(self, name):
        if self._wrapped is empty:
            self._setup(name)
        return getattr(self._wrapped, name)

    def configure(self, root=None,
                  default_settings=default_settings, **options):
        """
        Called to manually configure the settings. The 'default_settings'
        parameter sets where to retrieve any unspecified values from (its
        argument must support attribute access (__getattr__)).
        """
        if root:
            if root.startswith('/'):
                default_settings.PROJECT_ROOT = root
            else:
                default_settings.PROJECT_ROOT = os.path.realpath(root)

        if self._wrapped is not empty:
            raise RuntimeError('Settings already configured.')

        holder = UserSettingsHolder(default_settings)
        for name, value in options.items():
            setattr(holder, name, value)
        self._wrapped = holder

    @property
    def configured(self):
        """
        Returns True if the settings have already been configured.
        """
        return self._wrapped is not empty

    def __call__(self, *args, **kwargs):
        return self.get(*args, **kwargs)


class BaseSettings(object):
    """
    Common logic for settings whether set by a module or by the user.
    """
    @property
    def store(self):
        if not hasattr(self, '_store'):
            self._store = {
                key: value
                for key, value
                in default_settings.__dict__.items()
                if key.isupper()
            }
        return self._store

    def keys(self):
        return self.store.keys()

    def values(self):
        return self.store.values()

    def get(self, key, default=None, cast=None):
        data = self.store.get(key, default)
        if cast:
            data = converters.get(cast)(data)
        return data

    def __call__(self, *args, **kwargs):
        return self.get(*args, **kwargs)

    def as_bool(self, key):
        return self.get(key, cast='@bool')

    def as_int(self, key):
        return self.get(key, cast='@int')

    def as_float(self, key):
        return self.get(key, cast='@float')

    def as_json(self, key):
        return self.get(key, cast='@json')

    @contextmanager
    def using_namespace(self, namespace):
        try:
            self.namespace(namespace)
            yield
        finally:
            self.namespace()

    def namespace(self, namespace=None):
        namespace = namespace or 'DYNACONF'
        if not isinstance(namespace, basestring):
            raise AttributeError('namespace should be a string')
        if "_" in namespace:
            raise AttributeError('namespace should not contains _')
        self.DYNACONF_NAMESPACE = namespace.upper()
        self.execute_loaders(namespace=namespace, silent=False)

    def execute_loaders(self, namespace=None, silent=True):
        loaders = getattr(
            self,
            'LOADERS_FOR_DYNACONF',
            self.get('LOADERS_FOR_DYNACONF')
        )
        for loader_module_name in loaders:
            try:
                loader = importlib.import_module(loader_module_name)
            except ImportError:
                loader = self.import_from_filename(loader_module_name)
            loader.main(self, namespace, silent=False)


class Settings(BaseSettings):

    def __init__(self, settings_module):
        default_loader(self)
        self.SETTINGS_MODULE = settings_module

        try:
            mod = importlib.import_module(settings_module)
            loaded_from = 'module'
        except ImportError:
            mod = self.import_from_filename(settings_module)
            loaded_from = 'filename'

        self._explicit_settings = set()

        for setting in dir(mod):
            if setting.isupper():
                setting_value = getattr(mod, setting)
                setattr(self, setting, setting_value)
                self._explicit_settings.add(setting)
                self.store[setting] = setting_value

        if not hasattr(self, 'PROJECT_ROOT'):
            self.PROJECT_ROOT = os.path.realpath(
                os.path.dirname(os.path.abspath(settings_module))
            ) if loaded_from == 'module' else os.getcwd()

        self.execute_loaders()

    def import_from_filename(self, filename, silent=False):
        mod = types.ModuleType('config')
{'fraction_non_alphanumeric': 0.08358085808580858, 'fraction_numerical': 8.250825082508251e-05, 'mean_word_length': 2.274176121015667, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2183814', 'n_tokens_mistral': 3271, 'n_tokens_neox': 3150, 'n_words': 944}