def test_directiveat_defaults(self): items = [ { "values": ["VNAME", 4, 666, 1222], "template": "@int({},default={})" }, { "values": ["ANAME", 4.1, 666.6, 1.222, 66.6666], "template": "@float({},default={})", }, { "values": ["SNAME", "bb", "ccc"], "template": "@str({},default={})" }, ] for item in items: values = item["values"] template = item["template"] for v in values[1:]: t = template.format(",".join(map(str, values)), v) print("Template, ", t) p = Placeholder.from_string(t) assert p is not None assert p.cast(p.default_value) == v assert len(p.options) == len(values) - 1 print(p.directive._kwargs)
def test_replace_ignore_defaults(self, generic_temp_folder, data): sample_dict = data["data"] placeholders = data["placeholders"] # , _, _, to_be_raplaced_keys = data environment_variables = data["environment_variables"] conf = XConfig.from_dict(sample_dict) rich.print(conf) np.random.seed(66) to_replace = {} to_replace_with_defaults = {} for p in placeholders: _p = Placeholder.from_string(p) if _p.default_value is not None: to_replace_with_defaults[_p.name] = np.random.randint(0, 10) else: to_replace[_p.name] = np.random.randint(0, 10) rich.print("NAME", _p.name, _p.default_value is None) defaults_placeholders_counter = len(conf.available_placeholders()) - len( conf.available_placeholders(ignore_defaults=True) ) assert defaults_placeholders_counter == len(to_replace_with_defaults) if defaults_placeholders_counter > 0: conf_copy_A = conf.copy() conf_copy_A.replace_variables_map(to_replace, replace_defaults=False) conf_copy_B = conf.copy() conf_copy_B.replace_variables_map(to_replace, replace_defaults=True) assert len(conf_copy_A.available_placeholders()) > len( conf_copy_B.available_placeholders() )
def test_env_variables(self, generic_temp_folder, data): sample_dict = data["data"] placeholders = data["placeholders"] # , _, _, to_be_raplaced_keys = data environment_variables = data["environment_variables"] conf = XConfig.from_dict(sample_dict) np.random.seed(66) assert len(conf.available_placeholders()) == len(placeholders) + len( environment_variables ) for envv in environment_variables: pl = Placeholder.from_string(envv) assert ( pl.name not in os.environ ), f"this PYTEST needs no environment variable with name '{pl.name}' " # Load and parse environment variables but nobody set them! conf = XConfig.from_dict(sample_dict, replace_environment_variables=True) assert len(conf.available_placeholders()) == len(placeholders) + len( environment_variables ) # Load and parse environment variables with manual set for envv in environment_variables: pl = Placeholder.from_string(envv) os.environ[pl.name] = str(np.random.uniform(0, 1, (1,))) conf = XConfig.from_dict(sample_dict, replace_environment_variables=True) import rich rich.print(conf) assert len(conf.available_placeholders()) == len(placeholders) for envv in environment_variables: pl = Placeholder.from_string(envv) del os.environ[pl.name]
def replace_variable(self, old_value: str, new_value: str): """Replaces target variables with custom new value :param old_value: value to replace :type old_value: str :param new_value: new key value :type new_value: str """ chunks = self.chunks_as_lists(discard_private_qualifiers=True) for k, v in chunks: p = Placeholder.from_string(v) if p is not None and p.is_valid(): if old_value == p.name: pydash.set_(self, k, p.cast(new_value))
def is_a_placeholder(self, value: any) -> bool: """Checks if value is likely a placeholder :param value: input value to check (e.g. '$hello') :type value: any :return: TRUE if value is a placeholder :rtype: bool """ if not isinstance(value, str): return False placeholder = Placeholder.from_string(value) if placeholder: return placeholder.is_valid() return False
def available_placeholders( self, ignore_defaults: bool = False, ) -> Dict[str, Placeholder]: """Retrieves the available placeholders list :param ignore_defaults: TRUE to ignore placeholders with default :type: ignore_defaults: bool :return: list of found (str,str) pairs :rtype: Tuple[str,str] """ chunks = self.chunks_as_tuples(discard_private_qualifiers=True) placeholders = {} for k, v in chunks: if self.is_a_placeholder(v): key = ".".join(k) placelholder = Placeholder.from_string(v) if ignore_defaults and placelholder.default_value is not None: continue placeholders[key] = placelholder return placeholders
def _deep_parse_for_environ(self, chunks: Sequence[Tuple[Union[str, list], Any]]): """Deep visit of dictionary replacing environment variables if any :param chunks: chunks to visit :type chunks: Sequence[Tuple[Union[str, list], Any]] :raises NotImplementedError: Importer type not found :raises OSError: replace file not found """ env_to_replace = {} for chunk_name, value in chunks: if not isinstance(value, str): continue placeholder = Placeholder.from_string(value) if placeholder is not None: if placeholder.is_valid(): if placeholder.type == PlaceholderType.ENV: if placeholder.name in os.environ: env_to_replace[placeholder.name] = os.environ.get( placeholder.name) self.replace_variables_map(env_to_replace)
def test_replace(self, generic_temp_folder, data): sample_dict = data["data"] placeholders = data["placeholders"] # , _, _, to_be_raplaced_keys = data environment_variables = data["environment_variables"] conf = XConfig.from_dict(sample_dict) np.random.seed(66) to_replace = {} for p in placeholders: _p = Placeholder.from_string(p) to_replace[_p.name] = np.random.randint(0, 10) assert len(conf.available_placeholders()) == len(placeholders) + len( environment_variables ) conf.check_available_placeholders(close_app=False) if len(conf.available_placeholders()) > 0: for name, p in conf.available_placeholders().items(): assert isinstance(name, str) assert isinstance(p, Placeholder) with pytest.raises(SystemExit): conf.check_available_placeholders(close_app=True) conf.replace_variables_map(to_replace) chunks = conf.chunks_as_lists() for key, value in chunks: if not isinstance(value, Box) and not isinstance(value, BoxList): assert value not in to_replace.keys() assert len(conf.available_placeholders()) == len(environment_variables)