"""Module level configuration.

Fuel allows module-wide configuration values to be set using a YAML_
configuration file and `environment variables`_. Environment variables
override the configuration file which in its turn overrides the defaults.

The configuration is read from ``~/.fuelrc`` if it exists. A custom
configuration file can be used by setting the ``FUEL_CONFIG`` environment
variable. A configuration file is of the form:

.. code-block:: yaml

   data_path: /home/user/datasets

Which could be overwritten by using environment variables:

.. code-block:: bash

   $ FUEL_DATA_PATH=/home/users/other_datasets python

This data path is a sequence of paths separated by an os-specific
delimiter (':' for Linux and OSX, ';' for Windows).

If a setting is not configured and does not provide a default, a
:class:`~.ConfigurationError` is raised when it is
accessed.

Configuration values can be accessed as attributes of
:const:`fuel.config`.

    >>> from fuel import config
    >>> print(config.data_path) # doctest: +SKIP
    '~/datasets'

The following configurations are supported:

.. option:: data_path

   The path where dataset files are stored. Can also be set using the
   environment variable ``FUEL_DATA_PATH``. Expected to be a sequence
   of paths separated by an os-specific delimiter (':' for Linux and
   OSX, ';' for Windows).


.. todo::

   Implement this.

.. option:: floatX

   The default :class:`~numpy.dtype` to use for floating point numbers. The
   default value is ``float64``. A lower value can save memory.

.. option:: extra_downloaders

   A list of package names which, like fuel.downloaders, define an
   `all_downloaders` attribute listing available downloaders. By default,
   an empty list.

.. option:: extra_converters

   A list of package names which, like fuel.converters, define an
   `all_converters` attribute listing available converters. By default,
   an empty list.

.. _YAML: http://yaml.org/
.. _environment variables:
   https://en.wikipedia.org/wiki/Environment_variable

"""
import logging
import os

import yaml

from .exceptions import ConfigurationError

logger = logging.getLogger(__name__)

NOT_SET = object()


class Configuration(object):
    def __init__(self):
        self.config = {}

    def load_yaml(self):
        if 'FUEL_CONFIG' in os.environ:
            yaml_file = os.environ['FUEL_CONFIG']
        else:
            yaml_file = os.path.expanduser('~/.fuelrc')
        if os.path.isfile(yaml_file):
            with open(yaml_file) as f:
                for key, value in yaml.safe_load(f).items():
                    if key not in self.config:
                        raise ValueError("Unrecognized config in YAML: {}"
                                         .format(key))
                    self.config[key]['yaml'] = value

    def __getattr__(self, key):
        if key == 'config' or key not in self.config:
            raise AttributeError
        config_setting = self.config[key]
        if 'value' in config_setting:
            value = config_setting['value']
        elif ('env_var' in config_setting and
              config_setting['env_var'] in os.environ):
            value = os.environ[config_setting['env_var']]
        elif 'yaml' in config_setting:
            value = config_setting['yaml']
        elif 'default' in config_setting:
            value = config_setting['default']
        else:
            raise ConfigurationError("Configuration not set and no default "
                                     "provided: {}.".format(key))
        return config_setting['type'](value)

    def __setattr__(self, key, value):
        if key != 'config' and key in self.config:
            self.config[key]['value'] = value
        else:
            super(Configuration, self).__setattr__(key, value)

    def add_config(self, key, type_, default=NOT_SET, env_var=None):
        """Add a configuration setting.

        Parameters
        ----------
        key : str
            The name of the configuration setting. This must be a valid
            Python attribute name i.e. alphanumeric with underscores.
        type : function
            A function such as ``float``, ``int`` or ``str`` which takes
            the configuration value and returns an object of the correct
            type.  Note that the values retrieved from environment
            variables are always strings, while those retrieved from the
            YAML file might already be parsed. Hence, the function provided
            here must accept both types of input.
        default : object, optional
            The default configuration to return if not set. By default none
            is set and an error is raised instead.
        env_var : str, optional
            The environment variable name that holds this configuration
            value. If not given, this configuration can only be set in the
            YAML configuration file.

        """
        self.config[key] = {'type': type_}
        if env_var is not None:
            self.config[key]['env_var'] = env_var
        if default is not NOT_SET:
            self.config[key]['default'] = default

config = Configuration()

# Define configuration options
config.add_config('data_path', type_=str, env_var='FUEL_DATA_PATH')
config.add_config('default_seed', type_=int, default=1)
config.add_config('extra_downloaders', type_=list, default=[])
config.add_config('extra_converters', type_=list, default=[])

# Default to Theano's floatX if possible
try:
    from theano import config as theano_config
    default_floatX = theano_config.floatX
except Exception:
    default_floatX = 'float64'
config.add_config('floatX', type_=str, env_var='FUEL_FLOATX',
                  default=default_floatX)

config.load_yaml()

@@ -2060,16 +2060,27 @@
 ort os%0A%0A
+import six%0A
 import y
@@ -2187,16 +2187,400 @@
 ect()%0A%0A%0A
+def extra_downloader_converter(value):%0A    %22%22%22Parses extra_%7Bdownloader,converter%7D arguments.%0A%0A    Parameters%0A    ----------%0A    value : iterable or str%0A        If the value is a string, it is split into a list using spaces%0A        as delimitors. Otherwise, it is returned as is.%0A%0A    %22%22%22%0A    if isinstance(value, six.string_types):%0A        value = value.split(%22 %22)%0A    return value%0A%0A%0A
 class Co
@@ -5770,86 +5770,233 @@
 pe_=
-list, default=%5B%5D)%0Aconfig.add_config('extra_converters', type_=list, default=%5B%5D
+extra_downloader_converter,%0A                  default=%5B%5D, env_var='FUEL_EXTRA_DOWNLOADERS')%0Aconfig.add_config('extra_converters', type_=extra_downloader_converter,%0A                  default=%5B%5D, env_var='FUEL_EXTRA_CONVERTERS'
 )%0A%0A#

Add dedicated environment variables to extra downloaders/converters