Skip to content

Commit

Permalink
Make username/password optional in toml config
Browse files Browse the repository at this point in the history
  • Loading branch information
hmpf committed Oct 26, 2023
1 parent 24ddc7f commit d5f23d6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/zinolib/config/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

from pydantic import BaseModel


Expand All @@ -6,6 +8,11 @@ class UserConfig(BaseModel):
password: str


class OptionalUserConfig(BaseModel):
username: Optional[str]
password: Optional[str]


class ServerV1Config(BaseModel):
server: str
port: int = 8001
Expand Down
23 changes: 19 additions & 4 deletions src/zinolib/config/zino1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pydantic import BaseModel

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


def _parse_tcl(config_dict, section):
Expand All @@ -19,13 +19,18 @@ class ZinoV1Config(UserConfig, ServerV1Config, Options):
"""
How to use::
Make a config-class from the tcl-config stored on disk::
Given a legacy tcl config file stored on disk, containing at minimum server,
username and secret::
> config = ZinoV1Config.from_tcl()
Get the actual user and Zino1 secret and update the config-object::
With a toml-file stored on disk, which needs only a server::
> config.set_userauth(actual_username, secret)
> config = ZinoV1Config.from_toml()
Explicitly set the user and Zino1 secret::
> config.set_userauth(username, secret)
Read some command-line arguments via argparse.ArgumentParser and update the
config::
Expand All @@ -34,16 +39,26 @@ class ZinoV1Config(UserConfig, ServerV1Config, Options):
"""
DEFAULT_SECTION: ClassVar = "default"

@classmethod
def get_legacy_class(cls):
return type("ZinoV1LegacyConfig", (UserConfig, ServerV1Config, Options, cls))

@classmethod
def get_class(cls):
return type("ZinoV1Config", (OptionalUserConfig, ServerV1Config, Options, cls))

@classmethod
def from_dict(cls, config_dict, section=DEFAULT_SECTION):
connection = config_dict["connections"][section]
options = config_dict.get("options", {})
classobj = cls.get_class()
return cls(**connection, **options)

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

@classmethod
Expand Down

0 comments on commit d5f23d6

Please sign in to comment.