Change config to use dataclasses

This means that we depend on python 3.7 now. But it gives an easier
possibility to distinguish between required and optional parameters. It
also allows to pecify the type of the option drectly and access it via
the dataclasses fields.

This might fix #2
This commit is contained in:
Jan Losinski 2019-02-05 21:16:48 +01:00
parent a37468ce90
commit fdac477d09
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG Key ID: AF7958914F414B08
1 changed files with 28 additions and 35 deletions

View File

@ -1,36 +1,29 @@
import os import dataclasses
import configparser import configparser
import os
from logbook import Logger from logbook import Logger
logger = Logger(__name__) logger = Logger(__name__)
@dataclasses.dataclass
class Config: class Config:
known_values = ["wallabag_host", "db_uri", "client_id", "client_secret", "domain", "smtp_from", "smtp_host", wallabag_host: str
"smtp_port", "smtp_user", "smtp_passwd", "tag", "refresh_grace", "consume_interval", db_uri: str
"interface_host", "interface_port"] client_id: str
required_values = ["wallabag_host", "db_uri", "client_id", "client_secret", "domain", "smtp_from", "smtp_host", client_secret: str
"smtp_port", "smtp_user", "smtp_passwd"] domain: str
smtp_from: str
def __init__(self, wallabag_host, db_uri, client_id, client_secret, domain, smtp_from, smtp_host, smtp_port, smtp_host: str
smtp_user, smtp_passwd, tag='kindle', refresh_grace=120, consume_interval=30, smtp_port: int
interface_host="127.0.0.1", interface_port=8080): smtp_user: str
self.wallabag_host = wallabag_host smtp_passwd: str
self.db_uri = db_uri tag: str = "kindle"
self.client_id = client_id refresh_grace: int = 120
self.client_secret = client_secret consume_interval: int = 30
self.domain = domain interface_host: str = "127.0.0.1"
self.smtp_from = smtp_from interface_port: int = 8080
self.smtp_host = smtp_host
self.smtp_port = smtp_port
self.smtp_user = smtp_user
self.smtp_passwd = smtp_passwd
self.tag = tag
self.refresh_grace = refresh_grace
self.consume_interval = consume_interval
self.interface_host = interface_host
self.interface_port = interface_port
@staticmethod @staticmethod
def from_file(filename): def from_file(filename):
@ -51,12 +44,12 @@ class Config:
tmp = {} tmp = {}
missing = [] missing = []
for key in Config.known_values: for field in dataclasses.fields(Config):
if key in dflt: if field.name in dflt:
tmp[key] = dflt[key] tmp[field.name] = field.type(dflt[field.name])
else: else:
if key in Config.required_values: if field.default is dataclasses.MISSING:
missing.append(key) missing.append(field.name)
if 0 != len(missing): if 0 != len(missing):
logger.warn("Config file {filename} does not contain configs for: {lst}", filename=filename, logger.warn("Config file {filename} does not contain configs for: {lst}", filename=filename,
@ -70,12 +63,12 @@ class Config:
logger.info("Read config from environment") logger.info("Read config from environment")
tmp = {} tmp = {}
missing = [] missing = []
for key in Config.known_values: for field in dataclasses.fields(Config):
if key.upper() in os.environ: if field.name.upper() in os.environ:
tmp[key] = os.environ[key.upper()] tmp[field.name] = field.type(os.environ[field.name.upper()])
else: else:
if key in Config.required_values: if field.default is dataclasses.MISSING:
missing.append(key.upper()) missing.append(field.name.upper())
if 0 != len(missing): if 0 != len(missing):
logger.warn("Environment config does not contain configs for: {lst}", lst=", ".join(missing)) logger.warn("Environment config does not contain configs for: {lst}", lst=", ".join(missing))