-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||