mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-11-04 18:07:44 +03:00 
			
		
		
		
	Update from_env()
This commit is contained in:
		
							parent
							
								
									91b9c6814a
								
							
						
					
					
						commit
						ba86005ae3
					
				
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| 
						 | 
				
			
			@ -1392,18 +1392,24 @@ cdef class ConfigurationOption(Provider):
 | 
			
		|||
            current_config = {}
 | 
			
		||||
        self.override(merge_dicts(current_config, options))
 | 
			
		||||
 | 
			
		||||
    def from_env(self, name, default=None):
 | 
			
		||||
    def from_env(self, name, default=UNDEFINED):
 | 
			
		||||
        """Load configuration value from the environment variable.
 | 
			
		||||
 | 
			
		||||
        :param name: Name of the environment variable.
 | 
			
		||||
        :type name: str
 | 
			
		||||
 | 
			
		||||
        :param default: Default value that is used if environment variable does not exist.
 | 
			
		||||
        :type default: str
 | 
			
		||||
        :type default: object
 | 
			
		||||
 | 
			
		||||
        :rtype: None
 | 
			
		||||
        """
 | 
			
		||||
        value = os.getenv(name, default)
 | 
			
		||||
        value = os.environ.get(name, default)
 | 
			
		||||
 | 
			
		||||
        if value is UNDEFINED:
 | 
			
		||||
            if self._is_strict_mode_enabled():
 | 
			
		||||
                raise ValueError('Environment variable "{0}" is undefined'.format(name))
 | 
			
		||||
            value = None
 | 
			
		||||
 | 
			
		||||
        self.override(value)
 | 
			
		||||
 | 
			
		||||
    def _is_strict_mode_enabled(self):
 | 
			
		||||
| 
						 | 
				
			
			@ -1691,18 +1697,24 @@ cdef class Configuration(Object):
 | 
			
		|||
            current_config = {}
 | 
			
		||||
        self.override(merge_dicts(current_config, options))
 | 
			
		||||
 | 
			
		||||
    def from_env(self, name, default=None):
 | 
			
		||||
    def from_env(self, name, default=UNDEFINED):
 | 
			
		||||
        """Load configuration value from the environment variable.
 | 
			
		||||
 | 
			
		||||
        :param name: Name of the environment variable.
 | 
			
		||||
        :type name: str
 | 
			
		||||
 | 
			
		||||
        :param default: Default value that is used if environment variable does not exist.
 | 
			
		||||
        :type default: str
 | 
			
		||||
        :type default: object
 | 
			
		||||
 | 
			
		||||
        :rtype: None
 | 
			
		||||
        """
 | 
			
		||||
        value = os.getenv(name, default)
 | 
			
		||||
        value = os.environ.get(name, default)
 | 
			
		||||
 | 
			
		||||
        if value is UNDEFINED:
 | 
			
		||||
            if self.__strict:
 | 
			
		||||
                raise ValueError('Environment variable "{0}" is undefined'.format(name))
 | 
			
		||||
            value = None
 | 
			
		||||
 | 
			
		||||
        self.override(value)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -745,6 +745,34 @@ class ConfigFromEnvTests(unittest.TestCase):
 | 
			
		|||
        self.config.from_env('UNDEFINED_ENV', 'default-value')
 | 
			
		||||
        self.assertEqual(self.config(), 'default-value')
 | 
			
		||||
 | 
			
		||||
    def test_undefined_in_strict_mode(self):
 | 
			
		||||
        self.config = providers.Configuration(strict=True)
 | 
			
		||||
        with self.assertRaises(ValueError):
 | 
			
		||||
            self.config.from_env('UNDEFINED_ENV')
 | 
			
		||||
 | 
			
		||||
    def test_option_undefined_in_strict_mode(self):
 | 
			
		||||
        self.config = providers.Configuration(strict=True)
 | 
			
		||||
        with self.assertRaises(ValueError):
 | 
			
		||||
            self.config.option.from_env('UNDEFINED_ENV')
 | 
			
		||||
 | 
			
		||||
    def test_undefined_in_strict_mode_with_default(self):
 | 
			
		||||
        self.config = providers.Configuration(strict=True)
 | 
			
		||||
        self.config.from_env('UNDEFINED_ENV', 'default-value')
 | 
			
		||||
        self.assertEqual(self.config(), 'default-value')
 | 
			
		||||
 | 
			
		||||
    def test_option_undefined_in_strict_mode_with_default(self):
 | 
			
		||||
        self.config = providers.Configuration(strict=True)
 | 
			
		||||
        self.config.option.from_env('UNDEFINED_ENV', 'default-value')
 | 
			
		||||
        self.assertEqual(self.config.option(), 'default-value')
 | 
			
		||||
 | 
			
		||||
    def test_default_none(self):
 | 
			
		||||
        self.config.from_env('UNDEFINED_ENV')
 | 
			
		||||
        self.assertIsNone(self.config())
 | 
			
		||||
 | 
			
		||||
    def test_option_default_none(self):
 | 
			
		||||
        self.config.option.from_env('UNDEFINED_ENV')
 | 
			
		||||
        self.assertIsNone(self.config.option())
 | 
			
		||||
 | 
			
		||||
    def test_with_children(self):
 | 
			
		||||
        self.config.section1.value1.from_env('CONFIG_TEST_ENV')
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user