import yaml
|
|
from typing import List, Tuple
|
|
import ptaimport.passwords as pwd
|
|
import ptaimport.data_importers as importers
|
|
|
|
class Config:
|
|
|
|
def __init__( self, yaml_data: str ):
|
|
self.data = yaml.safe_load(yaml_data)
|
|
|
|
|
|
def is_correct( self ) -> Tuple[bool, str]:
|
|
"""
|
|
Verifies that the configuration is correct
|
|
|
|
Returns:
|
|
bool: true if the config is correct
|
|
str: error message to display to the user
|
|
"""
|
|
ok = True
|
|
errors = []
|
|
|
|
if "passwords" not in self.data:
|
|
errors.append('Missing "passwords" section')
|
|
else:
|
|
(pwd_ok, pwd_err) = pwd.verify(
|
|
self.data["passwords"],
|
|
"passwords"
|
|
)
|
|
ok &= pwd_ok
|
|
errors = errors + pwd_err
|
|
|
|
if "imports" not in self.data:
|
|
errors.append('missing "imports" section')
|
|
elif type(self.data["imports"]) is not list:
|
|
errors.append('"imports" section is not a list')
|
|
else:
|
|
for i, importer in enumerate(self.data["imports"]):
|
|
(imp_ok, imp_err) = importers.verify(
|
|
importer,
|
|
f"imports[{i}]"
|
|
)
|
|
ok &= imp_ok
|
|
errors = errors + imp_err
|
|
|
|
return (ok, "\n".join(errors))
|
|
|
|
|
|
def passwords( self ) -> pwd.PasswordManager:
|
|
"""
|
|
Returns the password manager defined in the config
|
|
"""
|
|
return pwd.Manager( self.data["passwords"] )
|
|
|
|
|
|
def data_imports( self ) -> List[importers.DataImporter]:
|
|
"""
|
|
Returns the list of data importers defined in the config
|
|
"""
|
|
ret = []
|
|
for config in self.data["imports"]:
|
|
ret.append( importers.Importer(
|
|
config
|
|
) )
|
|
return ret
|