Move configuration code to its own module

This commit is contained in:
Vladan Popovic 2020-09-06 05:18:27 +02:00
parent eee471e9a7
commit 189681c5fa
3 changed files with 97 additions and 89 deletions

View file

@ -1,66 +1,11 @@
"""
A module containing all console script functions.
"""
import argparse
import asyncio
import logging
import logging.config
from logging import Logger
from typing import Any, Dict, Tuple
import os
import yaml
from chweb.collector import Collector, Producer
from chweb.consumer import Consumer
from chweb.models import Config
def configure(name) -> Tuple[Config, Logger]:
"""
Gets the configuration and creates a Pydantic model from the parsed YAML.
"""
parser = argparse.ArgumentParser(
description='Website availibility checker.')
parser.add_argument('-c', '--config', type=str,
default="/etc/checker.yaml",
help=('The yaml config file. '
'Defaults to /etc/checker.yaml'))
args = parser.parse_args()
with open(args.config, 'r') as conf_file:
conf = yaml.load(conf_file, Loader=yaml.FullLoader)
logging.config.dictConfig(conf['logging'])
config = create_config(conf)
logger = logging.getLogger("chweb.{}".format(name))
return (config, logger)
def create_config(conf: Dict[str, Any]):
kafka_servers_env = os.getenv('KAFKA_SERVERS')
if kafka_servers_env is not None:
kafka_servers = kafka_servers_env.split(',')
kafka_topic = os.getenv('KAFKA_TOPIC')
pg_db = os.getenv('POSTGRES_DB')
pg_host = os.getenv('POSTGRES_HOST')
pg_port = os.getenv('POSTGRES_PORT')
pg_user = os.getenv('POSTGRES_USER')
pg_pass = os.getenv('POSTGRES_PASS')
config = Config(**conf)
config.kafka.servers = (kafka_servers if kafka_servers_env
else config.kafka.servers)
config.kafka.topic = kafka_topic or config.kafka.topic
config.postgres.dbhost = pg_host or config.postgres.dbhost
config.postgres.dbname = pg_db or config.postgres.dbname
config.postgres.dbport = (int(pg_port) if pg_port is not None
else config.postgres.dbport)
config.postgres.dbuser = pg_user or config.postgres.dbuser
config.postgres.dbpass = pg_pass or config.postgres.dbpass
return config
from chweb.config import configure
def collect():

60
src/chweb/config.py Normal file
View file

@ -0,0 +1,60 @@
"""
A module containing all console script functions.
"""
import argparse
import logging
import logging.config
from logging import Logger
from typing import Any, Dict, Tuple
import os
import yaml
from chweb.models import Config
def configure(name) -> Tuple[Config, Logger]:
"""
Gets the configuration and creates a Pydantic model from the parsed YAML.
"""
parser = argparse.ArgumentParser(
description='Website availibility checker.')
parser.add_argument('-c', '--config', type=str,
default="/etc/checker.yaml",
help=('The yaml config file. '
'Defaults to /etc/checker.yaml'))
args = parser.parse_args()
with open(args.config, 'r') as conf_file:
conf = yaml.load(conf_file, Loader=yaml.FullLoader)
logging.config.dictConfig(conf['logging'])
config = create_config(conf)
logger = logging.getLogger("chweb.{}".format(name))
return (config, logger)
def create_config(conf: Dict[str, Any]):
kafka_servers_env = os.getenv('KAFKA_SERVERS')
if kafka_servers_env is not None:
kafka_servers = kafka_servers_env.split(',')
kafka_topic = os.getenv('KAFKA_TOPIC')
pg_db = os.getenv('POSTGRES_DB')
pg_host = os.getenv('POSTGRES_HOST')
pg_port = os.getenv('POSTGRES_PORT')
pg_user = os.getenv('POSTGRES_USER')
pg_pass = os.getenv('POSTGRES_PASS')
config = Config(**conf)
config.kafka.servers = (kafka_servers if kafka_servers_env
else config.kafka.servers)
config.kafka.topic = kafka_topic or config.kafka.topic
config.postgres.dbhost = pg_host or config.postgres.dbhost
config.postgres.dbname = pg_db or config.postgres.dbname
config.postgres.dbport = (int(pg_port) if pg_port is not None
else config.postgres.dbport)
config.postgres.dbuser = pg_user or config.postgres.dbuser
config.postgres.dbpass = pg_pass or config.postgres.dbpass
return config

View file

@ -1,54 +1,57 @@
import asyncio
import pytest
from chweb.cmd import create_config
from chweb.config import create_config
from chweb.models import Check
@pytest.fixture()
def config():
config_dict = {
'kafka': {
'servers': ["localhost:9992"],
'topic': "sample",
},
'postgres': {
'dbhost': "localhost",
'dbport': 5432,
'dbname': "chweb",
'dbuser': "vladan",
'dbpass': "",
},
'sites': [{
'url': "https://example.com",
'regex': "aaaaaaaaaaaaa",
'check_interval': 8,
'kafka': {
'servers': ["localhost:9992"],
'topic': "sample",
},
]
'postgres': {
'dbhost': "localhost",
'dbport': 5432,
'dbname': "chweb",
'dbuser': "vladan",
'dbpass': "",
},
'sites': [{
'url': "https://example.com",
'regex': "aaaaaaaaaaaaa",
'check_interval': 8,
},
]
}
return create_config(config_dict)
@pytest.fixture
def config_invalid():
config_dict = {
'kafka': {
'servers': ["localhost:9992"],
'topic': "sample",
},
'postgres': {
'dbhost': "localhost",
'dbport': 5432,
'dbname': "chweb",
'dbuser': "vladan",
'dbpass': "",
},
'sites': [{
'url': "https://dsadakjhkjsahkjh.com",
'regex': "domain",
'check_interval': 5,
'kafka': {
'servers': ["localhost:9992"],
'topic': "sample",
},
]
'postgres': {
'dbhost': "localhost",
'dbport': 5432,
'dbname': "chweb",
'dbuser': "vladan",
'dbpass': "",
},
'sites': [{
'url': "https://dsadakjhkjsahkjh.com",
'regex': "domain",
'check_interval': 5,
},
]
}
return create_config(config_dict)
@pytest.fixture
def check():
return Check(