mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-11-04 09:57:37 +03:00 
			
		
		
		
	Add Configuration.from_ini() method
This commit is contained in:
		
							parent
							
								
									3a0c746430
								
							
						
					
					
						commit
						87bb5ff97d
					
				| 
						 | 
				
			
			@ -7,6 +7,10 @@ that were made in every particular version.
 | 
			
		|||
From version 0.7.6 *Dependency Injector* framework strictly 
 | 
			
		||||
follows `Semantic versioning`_
 | 
			
		||||
 | 
			
		||||
Development version
 | 
			
		||||
-------------------
 | 
			
		||||
- Add ``Configuration.from_ini()`` method to load configuration from ini file.
 | 
			
		||||
 | 
			
		||||
3.17.1
 | 
			
		||||
------
 | 
			
		||||
- Fix ``DynamicContainer`` deep-copying bug.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| 
						 | 
				
			
			@ -21,6 +21,10 @@ else:
 | 
			
		|||
    else:
 | 
			
		||||
        _is_coroutine_marker = True
 | 
			
		||||
 | 
			
		||||
try:
 | 
			
		||||
    import ConfigParser as iniconfigparser
 | 
			
		||||
except ImportError:
 | 
			
		||||
    import configparser as iniconfigparser
 | 
			
		||||
 | 
			
		||||
from .errors import (
 | 
			
		||||
    Error,
 | 
			
		||||
| 
						 | 
				
			
			@ -1155,6 +1159,23 @@ cdef class Configuration(Object):
 | 
			
		|||
        """
 | 
			
		||||
        self.override(value)
 | 
			
		||||
 | 
			
		||||
    def from_ini(self, filepath):
 | 
			
		||||
        """Load configuration from ini file.
 | 
			
		||||
 | 
			
		||||
        :param filepath: Path to the configuration file.
 | 
			
		||||
        :type filepath: str
 | 
			
		||||
 | 
			
		||||
        :rtype: None
 | 
			
		||||
        """
 | 
			
		||||
        parser = iniconfigparser.ConfigParser()
 | 
			
		||||
        parser.read([filepath])
 | 
			
		||||
 | 
			
		||||
        config = {}
 | 
			
		||||
        for section in parser.sections():
 | 
			
		||||
            config[section] = dict(parser[section])
 | 
			
		||||
 | 
			
		||||
        self.override(config)
 | 
			
		||||
 | 
			
		||||
    def _create_children(self, value):
 | 
			
		||||
        children = dict()
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,8 @@
 | 
			
		|||
"""Dependency injector config providers unit tests."""
 | 
			
		||||
 | 
			
		||||
import os
 | 
			
		||||
import tempfile
 | 
			
		||||
 | 
			
		||||
import unittest2 as unittest
 | 
			
		||||
 | 
			
		||||
from dependency_injector import containers, providers
 | 
			
		||||
| 
						 | 
				
			
			@ -246,3 +249,32 @@ class ConfigLinkingTests(unittest.TestCase):
 | 
			
		|||
        self.assertEqual(services.config(), {'value': 'services2'})
 | 
			
		||||
        self.assertEqual(services.config.value(), 'services2')
 | 
			
		||||
        self.assertEqual(services.value_getter(), 'services2')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ConfigFromIniTests(unittest.TestCase):
 | 
			
		||||
 | 
			
		||||
    def setUp(self):
 | 
			
		||||
        self.config = providers.Configuration(name='config')
 | 
			
		||||
        _, self.config_file = tempfile.mkstemp()
 | 
			
		||||
 | 
			
		||||
        with open(self.config_file, 'w') as config_file:
 | 
			
		||||
            config_file.write(
 | 
			
		||||
                '[section1]\n'
 | 
			
		||||
                'value1=1\n'
 | 
			
		||||
                '\n'
 | 
			
		||||
                '[section2]\n'
 | 
			
		||||
                'value2=2\n'
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
    def tearDown(self):
 | 
			
		||||
        del self.config
 | 
			
		||||
        os.unlink(self.config_file)
 | 
			
		||||
 | 
			
		||||
    def test(self):
 | 
			
		||||
        self.config.from_ini(self.config_file)
 | 
			
		||||
 | 
			
		||||
        self.assertEqual(self.config(), {'section1': {'value1': '1'}, 'section2': {'value2': '2'}})
 | 
			
		||||
        self.assertEqual(self.config.section1(), {'value1': '1'})
 | 
			
		||||
        self.assertEqual(self.config.section1.value1(), '1')
 | 
			
		||||
        self.assertEqual(self.config.section2(), {'value2': '2'})
 | 
			
		||||
        self.assertEqual(self.config.section2.value2(), '2')
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user