import os
from exporters.exceptions import ConfigurationError


class BasePipelineItem(object):

    base_supported_options = {}
    supported_options = {}

    def __new__(cls, options):
        obj = object.__new__(cls)
        object.__init__(obj, options)
        obj.supported_options = cls.base_supported_options.copy()
        obj.supported_options.update(cls.supported_options)
        return obj

    def __init__(self, options):
        self.options = options.get('options', {})
        self.check_options()

    def check_options(self):
        for option_name, option_spec in self.supported_options.iteritems():
            option_value = self.read_option(option_name)
            if option_value and not isinstance(option_value, option_spec['type']):
                raise ConfigurationError('Option %s should be type: %s' %
                                         (option_name, option_spec['type']))
            if 'default' in option_spec:
                continue
            if 'env_fallback' in option_spec and not option_value:
                if not os.environ.get(option_spec['env_fallback']):
                    raise ConfigurationError('No env variable for {}. Expecting {}'
                                             .format(option_name, option_spec['env_fallback']))
            elif not option_value:
                raise ConfigurationError('Missing value for option %s' % option_name)

    def read_option(self, option_name, default=None):
        if option_name in self.options:
            return self.options.get(option_name)
        env_name = self.supported_options.get(option_name, {}).get('env_fallback')
        if env_name:
            return os.environ.get(env_name)
        return self.supported_options.get(option_name, {}).get('default', default)

@@ -1023,20 +1023,16 @@
 pec and 
-not 
 option_v
@@ -1027,32 +1027,40 @@
 and option_value
+ is None
 :%0A              
@@ -1163,44 +1163,62 @@
 or('
-No env variable for %7B%7D. Expecting %7B%7D
+Missing value for option %7B%7D. (tried also: %7B%7D from env)
 '%0A  
@@ -1328,20 +1328,16 @@
    elif 
-not 
 option_v
@@ -1340,16 +1340,24 @@
 on_value
+ in None
 :%0A      

Fix checks.