def get(self, var, section=None, **kwargs):
        """Retrieve a config var.
        Return environment variable if it exists
        (ie [self.prefix + _] + [section + _] + var)
        otherwise var from config file.

        If both are null and no default is set a ConfigError will be raised,
        otherwise default will be returned.

        :param var: key to look up
        :param default: default value, must be supplied as keyword
        """
        # default is not a specified keyword argument so we can distinguish
        # between a default set to None and no default sets
        if not section and self.section:
            section = self.section
        default = kwargs.get('default', None)
        env_var = "{}{}{}".format(
            _suffix(self.prefix) if self.prefix else '',
            _suffix(alphasnake(section)) if section else '',
            alphasnake(str(var))).upper()
        config = self.config.get(section, {}) if section else self.config
        result = config.get(var, default)
        result = os.getenv(env_var, default=result)
        # no default keyword supplied (and no result)
        #  use is None to allow empty lists etc
        if result is None and 'default' not in kwargs:
            msg = "Could not find '{}'".format(var)
            if section:
                msg = "{} in section '{}'.".format(msg, section)
            msg = "{} Checked environment variable: {}".format(msg, env_var)
            if self.config_file:
                msg = "{} and file: {}".format(msg, self.config_file)
            raise ConfigError(msg)
        return result
Esempio n. 2
0
 def test_dromedory_case(self):
     self.assertEqual('dromedory_case', alphasnake('dromedoryCase'))
     self.assertEqual('dromedory_case', alphasnake('dromedoryCase#'))
     self.assertEqual('dromedory_case', alphasnake('dromedory#Case'))
Esempio n. 3
0
 def test_upper_case(self):
     self.assertEqual('upper', alphasnake('UPPER#'))
     self.assertEqual('upper_upper', alphasnake('UPPER# UPPER'))
Esempio n. 4
0
 def test_camel_case(self):
     self.assertEqual('camel_case', alphasnake('CamelCase'))
     self.assertEqual('camel_case', alphasnake('CamelCase#'))
     self.assertEqual('camel_case', alphasnake('Camel#Case'))
Esempio n. 5
0
 def test_lower_case(self):
     self.assertEqual('lower', alphasnake('lower#'))
     self.assertEqual('lower_lower', alphasnake('lower# lower'))