diff --git a/src/zinolib/config/models.py b/src/zinolib/config/models.py new file mode 100644 index 0000000..0074dd2 --- /dev/null +++ b/src/zinolib/config/models.py @@ -0,0 +1,20 @@ +from dataclasses import dataclass +from typing import Optional + + +@dataclass +class UserConfig: + username: str + password: str + + +@dataclass +class ServerV1Config: + server: str + port: int = 8001 + + +@dataclass +class Options: + autoremove: bool = False + timeout: int = 30 diff --git a/src/zinolib/config/zino1.py b/src/zinolib/config/zino1.py new file mode 100644 index 0000000..8da7ce9 --- /dev/null +++ b/src/zinolib/config/zino1.py @@ -0,0 +1,48 @@ +from dataclasses import dataclass + +from . import tcl +from .models import UserConfig, ServerV1Config, Options + + +@dataclass +class ZinoV1Config(UserConfig, ServerV1Config, Options): + """ + How to use:: + + Make a config-class from the tcl-config stored on disk:: + + > config = ZinoV1Config.from_tcl() + + Get the actual user and Zino1 password and update the config-object:: + + > config.set_userauth(actual_username, actual_password) + """ + + @staticmethod + def _parse_tcl(config_dict, section): + fixed_dict = tcl.normalize(config_dict) + connection = fixed_dict["connections"][section] + options = fixed_dict["globals"] + connection['password'] = connection.pop("secret") + return connection, options + + @classmethod + def from_tcl(cls, section="default"): + config_dict = tcl.parse_tcl_config() + connection, options = cls._parse_tcl(config_dict, section) + return cls(**connection, **options) + + def set_userauth(self, username, password): + self.username = username + self.password = password + + def update_from_args(self, args): + """ + Assumes argparse-style args namespace object + + arg-names not found in the config-object are ignored. + """ + for arg in vars(args): + value = getattr(args, arg, None) + if arg in vars(self): + setattr(self, arg, value)