Reputation: 475
I have these two functions. The first one gets a file path ensures the file exists.
The second function is intended to check that a table exists in a toml file and then ensure three values exist and are not None
or ''
I am wondering how I can make it completely fail if any the table default
or any of the three values don't exist.
def get_source_path(env_var: str, default_path: str) -> str:
final_path = Path(os.getenv(env_var, default_path)).expanduser().resolve()
if not final_path.is_file():
raise FileNotFoundError(f'File {final_path} does not exist')
return final_path
def check_toml():
toml_dict = tomlkit.loads(Path(get_source_path('CONFIG_PATH', const.D_CONFIG_PATH)).read_text())
if dd := toml_dict.get('default'):
for key in 'conf_path', 'prefix', 'suffix':
if not dd.get(key):
print(f'Expected key "{key}" is missing')
else:
print('Toml file missing default table')
return toml_dict
Basically, I want the second function to fail and tell either default is missing or one of the three values are missing - otherwise return toml_dict
.
The toml file looks like
[default]
conf_path = "d"
prefix = "p"
suffix = "s"
Edit;
I think my best bet may be to raise a keyerror. But still thinking on best how.
Upvotes: 2
Views: 166