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 16, 2023
1 parent 905b347 commit b765b6f
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/zinolib/config/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from pydantic import BaseModel
from typing import Optional


class UserConfig(BaseModel):
username: str
password: str


class ServerV1Config(BaseModel):
server: str
port: str = "8001"


class Options(BaseModel):
autoremove: bool = False
timeout: int = 30
52 changes: 52 additions & 0 deletions src/zinolib/config/zino1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from pydantic import BaseModel

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


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


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 secret and update the config-object::
> config.set_userauth(actual_username, secret)
Read some command-line arguments via argparse.ArgumentParser and update the
config::
> config.update_from_args(args)
"""

@classmethod
def from_tcl(cls, filename=None, section="default"):
config_dict = tcl.parse_tcl_config(filename)
connection, options = _parse_tcl(config_dict, section)
return cls(**connection, **options)

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 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)
File renamed without changes.
File renamed without changes.
74 changes: 74 additions & 0 deletions tests/test_zinolib_config_zino1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import unittest
from argparse import ArgumentParser


from zinolib.config.zino1 import ZinoV1Config, _parse_tcl


class ParseTclTest(unittest.TestCase):

def test_parse_tcl_golden_path(self):
section = "default"
config_dict = {
"default": {
"Port": "8001",
"Secret": "0123456789",
"Server": "example.org",
"Sortby": '"upd-rev"',
"User": "admin",
},
"dev-server": {
"Port": "8001",
"Secret": "0123456789",
"Server": "example.com",
"User": "admin",
},
}
expected_connection = {
"port": "8001",
"password": "0123456789",
"server": "example.org",
"username": "admin",
}
expected_option = {
"sort_by": '"upd-rev"',
}
connection, options = _parse_tcl(config_dict, section)
self.assertEqual(expected_connection, connection)
self.assertEqual(expected_option, options)


class ZinoV1ConfigTest(unittest.TestCase):

example_connection = {
"port": "8001",
"password": "0123456789",
"server": "example.org",
"username": "admin",
}

def manually_create_config(self, connection=None):
connection = connection or self.example_connection
options = {}
return ZinoV1Config(**connection, **options)

def test_manually_create_config(self):
config = self.manually_create_config()
self.assertEqual(config.port, "8001")

def test_set_userauth(self):
config = self.manually_create_config()
self.assertEqual(config.username, "admin")
config.set_userauth('foo', 'barfybarf')
self.assertEqual(config.username, "foo")

def test_update_from_args(self):
parser = ArgumentParser()
parser.add_argument("password")
parser.add_argument("unknown")
args = parser.parse_args(["x", "y"])
config = self.manually_create_config()
self.assertEqual(config.password, "0123456789")
config.update_from_args(args)
self.assertEqual(config.password, "x")
self.assertNotIn("unknown", vars(config))

0 comments on commit b765b6f

Please sign in to comment.