def from_toml(cls, toml_string: str = None, filename: Union[str, Path] = None, encoding: str = 'utf-8', errors: str = 'strict', **kwargs): """ Transforms a toml string or file into a Box object :param toml_string: string to pass to `toml.load` :param filename: filename to open and pass to `toml.load` :param encoding: File encoding :param errors: How to handle encoding errors :param kwargs: parameters to pass to `Box()` :return: """ box_args = {} for arg in kwargs.copy(): if arg in BOX_PARAMETERS: box_args[arg] = kwargs.pop(arg) data = _from_toml(toml_string=toml_string, filename=filename, encoding=encoding, errors=errors) return cls(data, **box_args)
def from_toml( cls, toml_string: str = None, filename: Union[str, PathLike] = None, key_name: str = "toml", encoding: str = "utf-8", errors: str = "strict", **kwargs, ): """ Transforms a toml string or file into a BoxList object :param toml_string: string to pass to `toml.load` :param filename: filename to open and pass to `toml.load` :param key_name: Specify the name of the key to pull the list from (cannot directly convert from toml) :param encoding: File encoding :param errors: How to handle encoding errors :param kwargs: parameters to pass to `Box()` :return: """ box_args = {} for arg in list(kwargs.keys()): if arg in BOX_PARAMETERS: box_args[arg] = kwargs.pop(arg) data = _from_toml(toml_string=toml_string, filename=filename, encoding=encoding, errors=errors) if key_name not in data: raise BoxError(f"{key_name} was not found.") return cls(data[key_name], **box_args)
def test_bad_from_toml(self): with pytest.raises(BoxError): _from_toml()
def test_from_toml_file(self): out_file = Path(tmp_dir, "toml_test.tml") assert not out_file.exists() out_file.write_text(toml_string) result = _from_toml(filename=out_file) assert result["movies"]["Spaceballs"]["length"] == 96
def test_from_toml(self): result = _from_toml(toml_string) assert result["movies"]["Spaceballs"]["length"] == 96
def test_from_toml(self): result = _from_toml(toml_string) assert result['movies']['Spaceballs']['length'] == 96