Skip to content

Commit

Permalink
Make config-object
Browse files Browse the repository at this point in the history
  • Loading branch information
hmpf committed Oct 12, 2023
1 parent 799d4af commit a6c77fd
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/zinolib/config/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from dataclasses import dataclass
from typing import Optional

Check warning on line 2 in src/zinolib/config/models.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/config/models.py#L1-L2

Added lines #L1 - L2 were not covered by tests


@dataclass
class UserConfig:
username: str
password: str

Check warning on line 8 in src/zinolib/config/models.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/config/models.py#L5-L8

Added lines #L5 - L8 were not covered by tests


@dataclass
class ServerV1Config:
server: str
port: int = 8001

Check warning on line 14 in src/zinolib/config/models.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/config/models.py#L11-L14

Added lines #L11 - L14 were not covered by tests


@dataclass
class Options:
autoremove: bool = False
timeout: int = 30

Check warning on line 20 in src/zinolib/config/models.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/config/models.py#L17-L20

Added lines #L17 - L20 were not covered by tests
48 changes: 48 additions & 0 deletions src/zinolib/config/zino1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from dataclasses import dataclass

Check warning on line 1 in src/zinolib/config/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/config/zino1.py#L1

Added line #L1 was not covered by tests

from . import tcl
from .models import UserConfig, ServerV1Config, Options

Check warning on line 4 in src/zinolib/config/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/config/zino1.py#L3-L4

Added lines #L3 - L4 were not covered by tests


@dataclass
class ZinoV1Config(UserConfig, ServerV1Config, Options):

Check warning on line 8 in src/zinolib/config/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/config/zino1.py#L7-L8

Added lines #L7 - L8 were not covered by tests
"""
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

Check warning on line 27 in src/zinolib/config/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/config/zino1.py#L21-L27

Added lines #L21 - L27 were not covered by tests

@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)

Check warning on line 33 in src/zinolib/config/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/config/zino1.py#L29-L33

Added lines #L29 - L33 were not covered by tests

def set_userauth(self, username, password):
self.username = username
self.password = password

Check warning on line 37 in src/zinolib/config/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/config/zino1.py#L35-L37

Added lines #L35 - L37 were not covered by tests

def update_from_args(self, args):

Check warning on line 39 in src/zinolib/config/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/config/zino1.py#L39

Added line #L39 was not covered by tests
"""
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)

Check warning on line 48 in src/zinolib/config/zino1.py

View check run for this annotation

Codecov / codecov/patch

src/zinolib/config/zino1.py#L45-L48

Added lines #L45 - L48 were not covered by tests

0 comments on commit a6c77fd

Please sign in to comment.