def setUp(self): Pconf.clear() self.TEST_FILE_PATH = "test" self.TEST_FILE_RESULT = { "file": "result", "overlapping": "file", "deep": {"stillhere": "stillhere", "overlapping": "file"}, } self.TEST_ENV_RESULT = { "env": "result", "overlapping": "env", "deep": {"overlapping": "env"}, }
def setUp(self): Pconf.clear() IntegrationBase.result = { 'tuple': (123, 'string'), 'int': 123, 'float': 1.23, 'list': ['list1', 'list2', {'dict-in-list': 'value'}], 'complex': (1+2j), 'bool': True, 'key': 'value', 'boolstring': 'false', 'string-with-specials': 'Test!@#$%^&*()-_=+[]{};:,<.>/?\\\'"\\`~', 'dict': {'dict': 'value', 'list-in-dict': ['nested-list1', 'nested-list2']} }
def setUp(self): Pconf.clear() self.TEST_FILE_PATH = 'test' self.TEST_FILE_RESULT = { 'file': 'result', 'overlapping': 'file', 'deep': { 'stillhere': 'stillhere', 'overlapping': 'file' } } self.TEST_ENV_RESULT = { 'env': 'result', 'overlapping': 'env', 'deep': { 'overlapping': 'env' } }
def setUp(self): Pconf.clear() IntegrationBase.result = { "tuple": (123, "string"), "int": 123, "float": 1.23, "list": ["list1", "list2", { "dict-in-list": "value" }], "complex": (1 + 2j), "bool": True, "key": "value", "boolstring": "false", "string-with-specials": "Test!@#$%^&*()-_=+[]{};:,<.>/?\\'\"`~", "dict": { "dict": "value", "list-in-dict": ["nested-list1", "nested-list2"] }, "secret": "secret", }
def _init(self, logger): """Read the configuration data from files and profile""" if not self.is_init: if self.pconf_obj: self.pconf_obj.clear() Pconf.clear() Pconf.env() dir = self.get_dir() env_name = self.profile if (len(self.profile)) else os.getenv( 'PY_ENV', 'production') logger.debug('Configuration dir %s with env %s', dir, env_name) if path.isdir(dir): env_file = '' if env_name == 'production': env_file = '%s/production_config.json' % dir elif env_name == 'development': env_file = '%s/development_conf.json' % dir elif env_name == 'integration': env_file = '%s/integration_config.json' % dir elif env_name == 'test': env_file = '%s/test_config.json' % dir elif env_name == 'test_e2e': env_file = '%s/test_e2e_config.json' % dir else: raise ConfigLoadError('Unknown configuration profile: %s' % env_name) if not os.path.exists(env_file): raise ConfigLoadError('Missing configuration file: %s' % env_file) Pconf.file(env_file, encoding='json') base_cfg_file = '%s/config.json' % dir if not os.path.exists(base_cfg_file): raise ConfigLoadError('Missing configuration file: %s' % base_cfg_file) Pconf.file('%s/config.json' % dir, encoding='json') else: raise ConfigLoadError('Missing directory: %s' % dir) self.is_init = True self.pconf_obj = Pconf.get()
def setUp(self): Pconf.clear()
def test_clear(self): Pconf.defaults(TEST_OVERRIDES) self.assertEqual(Pconf.get(), TEST_OVERRIDES) Pconf.clear() self.assertEqual(Pconf.get(), {})
def _read_configure(config_filepath, return_empty=False) -> dict: import os import pprint from pconf import Pconf from ioflow.configure.get_configure_path_from_argv import get_configure_path_from_argv from ioflow.configure.read_configure import find_best_file_candidate, guess_configure_file_type # set return_empty to True for not read config from env # which can prevent unexpected result # e.g. './configure.json' is not for this app, but for other using if return_empty: return {} config_fileprefix = os.path.splitext(os.path.split(config_filepath)[-1])[0] default_configure_candidate = [ ".".join([config_fileprefix, ext]) for ext in ["yaml", "yml", "json"] ] builtin_configure_candidate = [ ".".join(["./builtin_configure", ext]) for ext in ["yaml", "yml", "json"] ] default_configure = find_best_file_candidate(default_configure_candidate) builtin_configure = find_best_file_candidate(builtin_configure_candidate) active_configure_file = get_configure_path_from_argv() if not active_configure_file: active_configure_file = os.getenv("_DEFAULT_CONFIG_FILE", default_configure) builtin_configure_file = os.getenv("_BUILTIN_CONFIG_FILE", builtin_configure) # Note: this is a safeguard, before using any Pconf function, do execute this # In case former Pconf usage influence current usage # which will lead to a hidden and wired bug Pconf.clear() # disable read configure from environment # Pconf.env() active_configure_file_abs_path = os.path.realpath(active_configure_file) if not os.path.exists(active_configure_file): msg = "default configure file is not found! CWD: {}; activate_config: {}; builtin_configure: {}".format( os.getcwd(), active_configure_file, builtin_configure_file) print(msg) raise ValueError(msg) else: print(">>> Using configure read from file: {}".format( active_configure_file_abs_path)) file_encoding = guess_configure_file_type(active_configure_file_abs_path) Pconf.file(active_configure_file, file_encoding) # try loading builtin configure file if builtin_configure_file and os.path.exists(builtin_configure_file): print( "loading builtin configure from {}".format(builtin_configure_file)) file_encoding = guess_configure_file_type(builtin_configure_file) Pconf.file(builtin_configure_file, encoding=file_encoding) else: print(">>> builtin configure file is not found!") # Get all the config values parsed from the sources config = Pconf.get() # NOTE: clean Pconf for later brand new use Pconf.clear() print("++" * 8, "configure", "++" * 8) pprint.pprint(config) return config