def test_search_path(self): ''' ``search_path`` should be taken from ``lookup_options`` ''' with patch('config_resolver.core.Config') as mck: get_config('world', 'hello', lookup_options={ 'search_path': 'testdata:testdata/a:testdata/b' }) mck.assert_called_with( 'hello', 'world', filename='config.ini', require_load=False, search_path='testdata:testdata/a:testdata/b', version=None)
def test_ignore_handler(self): ''' Version 5 will accept a new "handler" argument. This should be accepted, but ignored in version 4. ''' with patch('config_resolver.core.Config') as mck: get_config('foo', 'bar', lookup_options={}, handler='dummy_handler') get_config('foo', 'bar', lookup_options={}, handler='dummy_handler') mck.assert_called_with( 'bar', 'foo', filename='config.ini', require_load=False, search_path=None, version=None)
def _config_section(self, section_name: str) -> Optional[Dict[str, Any]]: config_result = get_config('records_mover', 'bluelabs') cfg = config_result.config if section_name in cfg: return cfg[section_name] else: return None
def load_config(filename="app.ini", lookup_options=[]): """ load app config """ return get_config( app_name="pizstrip", filename=filename, lookup_options=lookup_options ).config
def test_return_value(self): with patch('config_resolver.core.Config') as mck: cfg, meta = get_config('world', 'hello') self.assertEqual(cfg, mck()) self.assertEqual(meta.loaded_files, mck()._loaded_files) self.assertEqual(meta.active_path, mck()._active_path)
def _infer_scratch_s3_url( self, boto3_session: Optional['boto3.session.Session']) -> Optional[str]: if "SCRATCH_S3_URL" in os.environ: return os.environ["SCRATCH_S3_URL"] config_result = get_config('records_mover', 'bluelabs') cfg = config_result.config if 'aws' in cfg: aws_cfg = cfg['aws'] s3_scratch_url: Optional[str] = aws_cfg.get('s3_scratch_url') if s3_scratch_url is not None: return s3_scratch_url else: s3_scratch_url_prefix: Optional[str] =\ aws_cfg.get('s3_scratch_url_appended_with_iam_username') if s3_scratch_url_prefix is not None: if boto3_session is None: logger.warning( 'Cannot generate S3 scratch URL with IAM username, ' 'as I have no IAM username') return None return self._append_aws_username_to_bucket( s3_scratch_url_prefix, boto3_session) else: logger.debug('No S3 scratch bucket config found') return None else: logger.debug('No config ini file found') return None
def test_secured_config(self): with patch('config_resolver.core.SecuredConfig') as mck: cfg_b = get_config('world', 'hello', lookup_options={ 'secure': True }) mck.assert_called_with('hello', 'world', filename='config.ini', require_load=False, search_path=None, version=None)
def test_no_warning(self): """ If we receive a call via "get_config" we should *not* raise a warning """ with catch_warnings(record=True) as warnings: cfg, meta = get_config('world', 'hello') my_warnings = [wrn for wrn in warnings if 'get_config' in str(wrn)] self.assertEqual( len(my_warnings), 0, "We should not have seen a warning from this call")
def test_new_default_filename(self): ''' In config_resolver 5 we will switch to "config.ini" from "app.ini". We want the transition-layer to continue working as usual ''' with patch('config_resolver.core.Config') as mck: cfg_b = get_config('world', 'hello') mck.assert_called_with('hello', 'world', filename='config.ini', require_load=False, search_path=None, version=None)
def test_filename(self): ''' ``filename`` should be taken from ``lookup_options`` ''' with patch('config_resolver.core.Config') as mck: cfg_b = get_config('world', 'hello', filename='test.ini') mck.assert_called_with('hello', 'world', filename='test.ini', require_load=False, search_path=None, version=None) with patch('config_resolver.core.Config') as mck: cfg_b = get_config('world', 'hello', lookup_options={ 'filename': 'test.ini' }) mck.assert_called_with('hello', 'world', filename='test.ini', require_load=False, search_path=None, version=None)
def _infer_session_type() -> str: if 'RECORDS_MOVER_SESSION_TYPE' in os.environ: return os.environ['RECORDS_MOVER_SESSION_TYPE'] config_result = get_config('records_mover', 'bluelabs') cfg = config_result.config if 'session' in cfg: session_cfg = cfg['session'] session_type: Optional[str] = session_cfg.get('session_type') if session_type is not None: logger.info(f"Using session_type={session_type} from config file") return session_type if 'AIRFLOW__CORE__EXECUTOR' in os.environ: # Guess based on an env variable sometimes set by Airflow return 'airflow' return 'env'
import logging from config_resolver import get_config logging.basicConfig(level=logging.DEBUG) cfg = get_config("bird_feeder", "acmecorp").config print(cfg.get('section', 'var'))
import logging from config_resolver import get_config logging.basicConfig(level=logging.DEBUG) cfg = get_config("bird_feeder", "acmecorp", {"version": "1.0"}).config print(cfg.get('section', 'var'))
import logging from config_resolver import get_config logging.basicConfig(level=logging.DEBUG) cfg = get_config("bird_feeder", "acmecorp", {"secure": True}).config print(cfg.get('section', 'var'))