def test__load_parser_options_from_env(self): env = { 'REDISPARSER__ENDPOINT': 'go.deep', 'REDISPARSER__HOST': 'my-host', 'REDISPARSER__PORT': '66', } res = ConfigLoader.load_parser_options_from_env(RedisParser, env) assert res == {'endpoint': 'go.deep', 'host': 'my-host', 'port': 66} env = { 'ENVIRONMENTPARSER__SCOPE': 'deep', } res = ConfigLoader.load_parser_options_from_env(EnvironmentParser, env) assert res == {'scope': 'deep'}
def handle(self, **options): key = options['key'] configure = ConfigLoader.from_env(suppress_logs=True, silent=True) uwsgi_cfg = configure.get(key) if not uwsgi_cfg: self.stderr.write( f'Parsing error: uWSGI config was not found by the key "{key}"' ) return if options['print']: self.stdout.write('*' * 80 + '\n') self.stdout.write('uWSGI CONFIG'.center(80)) self.stdout.write('*' * 80 + '\n') write_uwsgi_ini_cfg(self.stdout, uwsgi_cfg) self.stdout.write('*' * 80 + '\n') self.stdout.flush() cfg_file = options['file'] with open(cfg_file, 'w') as file: write_uwsgi_ini_cfg(file, uwsgi_cfg) os.execvp('uwsgi', ('--ini', cfg_file))
def test__import_parsers(self): parsers = list( ConfigLoader.import_parsers([ 'EnvironmentParser', 'django_docker_helpers.config.backends.YamlParser' ])) assert parsers == [EnvironmentParser, YamlParser]
def test__from_env(self): env = { 'CONFIG__PARSERS': 'EnvironmentParser,RedisParser,YamlParser', 'ENVIRONMENTPARSER__SCOPE': 'nested', 'YAMLPARSER__CONFIG': './tests/data/config.yml', 'REDISPARSER__HOST': 'wtf.test', 'NESTED__VARIABLE': 'i_am_here', } loader = ConfigLoader.from_env(env=env) assert [type(p) for p in loader.parsers ] == [EnvironmentParser, RedisParser, YamlParser] assert loader.get( 'variable') == 'i_am_here', 'Ensure env copied from ConfigLoader' with pytest.raises(Exception): loader.get('nothing.here') loader = ConfigLoader.from_env(env=env, silent=True) assert loader.get('nothing.here', True) is True loader = ConfigLoader.from_env(parser_modules=['EnvironmentParser'], env={}) assert loader.parsers
def loader(): env = {'PROJECT__DEBUG': 'false'} parsers = [ EnvironmentParser(scope='project', env=env), MPTConsulParser(host=CONSUL_HOST, port=CONSUL_PORT, scope='project'), ConsulParser('my/service/config.yml', host=CONSUL_HOST, port=CONSUL_PORT), MPTRedisParser(host=REDIS_HOST, port=REDIS_PORT, scope='project'), RedisParser('my/conf/service/config.yml', host=REDIS_HOST, port=REDIS_PORT), YamlParser(config='./tests/data/config.yml', scope='project'), ] return ConfigLoader(parsers=parsers)
def test__priority(self, loader: ConfigLoader, store_mpt_consul_config, store_mpt_redis_config, store_consul_config, store_redis_config): assert loader.get('debug') == 'false', 'Ensure value is taken from env' assert loader.get( 'debug', coerce_type=bool ) is False, 'Ensure value is coercing properly for env' assert loader.get( 'variable', coerce_type=int) == 2, 'Ensure consul MPT backend attached' assert loader.get( 'i.am.redis', coerce_type=bool) is True, 'Ensure redis MPT backend attached' assert loader.get( 'some.variable', coerce_type=int) == 2, 'Ensure consul backend attached' assert loader.get( 'some.brutal', coerce_type=int) == 666, 'Ensure redis backend attached'
def test__config_read_queue(self, loader: ConfigLoader, store_mpt_consul_config, store_mpt_redis_config, store_consul_config, store_redis_config): loader.get('some.variable') loader.get('some.brutal') loader.get('debug') loader.get('i.am.redis') loader.get('variable') loader.get('name') loader.get('nothing.here', 'very long string lol') loader.get('secret') assert loader.config_read_queue assert '\033' in ''.join(loader.format_config_read_queue(color=True)) assert '\033' not in ''.join( loader.format_config_read_queue(color=False)) loader.print_config_read_queue(color=True)
def test__from_env__raises_on_empty_values(self): with pytest.raises(ValueError): ConfigLoader.from_env([], {})
def test__default(self, loader: ConfigLoader): sentinel = object() assert loader.get('nonexi', default=sentinel) is sentinel
def test__availability(self, loader: ConfigLoader): assert loader.get('name') == 'wroom-wroom'
def test__get_with_required(self, loader: ConfigLoader): assert loader.get('some.variable', required=True) with pytest.raises(exceptions.RequiredValueIsEmpty): loader.get('some.nonexistent_var', required=True)
def test__config_read_queue(self, loader: ConfigLoader, store_mpt_consul_config, store_mpt_redis_config, store_consul_config, store_redis_config): loader.get('some.variable') loader.get('some.brutal') loader.get('debug', coerce_type=bool) loader.get('i.am.redis') loader.get('variable') loader.get('name') loader.get('nothing.here', 'very long string lol') loader.get('secret') loader.get('something.long', list(range(100))) loader.get('something.long.q', '=' * 80) assert loader.config_read_queue assert '\033[0m' in loader.format_config_read_queue(use_color=True) assert '\033[0m' not in loader.format_config_read_queue( use_color=False) loader.print_config_read_queue(use_color=True)
FILE_UPLOAD_TEMP_DIR = TEMP_DIR # for TemporaryUploadedFile MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media') STATIC_ROOT = os.path.join(PROJECT_PATH, 'static') USERMEDIA_ROOT = os.path.join(PROJECT_PATH, 'usermedia') # ------ for path in (STATIC_ROOT, MEDIA_ROOT, USERMEDIA_ROOT, TEMP_DIR): if not os.path.exists(path): os.makedirs(path, mode=0o755, exist_ok=True) # ★★★★★★★★★★★★★★★ CONFIG LOADER ★★★★★★★★★★★★★★★ # yml_conf = os.path.join(PROJECT_PATH, 'config', os.environ.get('DJANGO_CONFIG_FILE_NAME', 'dev.yml')) os.environ.setdefault('YAMLPARSER__CONFIG', yml_conf) configure = ConfigLoader.from_env(suppress_logs=True, silent=True) # ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ # DEBUG = configure('debug', False) if configure('security', False): SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') SECURE_SSL_REDIRECT = True SESSION_COOKIE_SECURE = True CSRF_COOKIE_SECURE = True # --------------- SECRET SETTINGS --------------- SECRET_KEY = configure('common.secret_key', 'secret') if SECRET_KEY == 'secret': warnings.warn('SECRET_KEY is not assigned! Production unsafe!')