From dfdb5bdb28e9594284354013298735dc5f5a4dbd Mon Sep 17 00:00:00 2001 From: Yax <1949284+kianby@users.noreply.github.com> Date: Sun, 10 Jan 2021 15:36:29 +0100 Subject: [PATCH] refactor config constants --- run.py | 40 ++++++++++++++------------- stacosys/conf/config.py | 61 ++++++++++++++++++++--------------------- tests/test_config.py | 46 +++++++++++++++---------------- 3 files changed, 74 insertions(+), 73 deletions(-) diff --git a/run.py b/run.py index efa984d..40ee59e 100644 --- a/run.py +++ b/run.py @@ -7,7 +7,7 @@ import argparse import logging from flask import Flask -import stacosys.conf.config as config +from stacosys.conf.config import Config, Parameter from stacosys.core import database from stacosys.core.rss import Rss from stacosys.core.mailer import Mailer @@ -33,7 +33,7 @@ def configure_logging(level): def stacosys_server(config_pathname): - conf = config.Config.load(config_pathname) + conf = Config.load(config_pathname) # configure logging logger = logging.getLogger(__name__) @@ -43,43 +43,45 @@ def stacosys_server(config_pathname): # initialize database db = database.Database() - db.setup(conf.get(config.DB_URL)) + db.setup(conf.get(Parameter.DB_URL)) logger.info("Start Stacosys application") # generate RSS for all sites rss = Rss( - conf.get(config.LANG), conf.get(config.RSS_FILE), conf.get(config.RSS_PROTO) + conf.get(Parameter.LANG), + conf.get(Parameter.RSS_FILE), + conf.get(Parameter.RSS_PROTO), ) rss.generate_all() # configure mailer mailer = Mailer( - conf.get(config.IMAP_HOST), - conf.get_int(config.IMAP_PORT), - conf.get_bool(config.IMAP_SSL), - conf.get(config.IMAP_LOGIN), - conf.get(config.IMAP_PASSWORD), - conf.get(config.SMTP_HOST), - conf.get_int(config.SMTP_PORT), - conf.get_bool(config.SMTP_STARTTLS), - conf.get(config.SMTP_LOGIN), - conf.get(config.SMTP_PASSWORD), + conf.get(Parameter.IMAP_HOST), + conf.get_int(Parameter.IMAP_PORT), + conf.get_bool(Parameter.IMAP_SSL), + conf.get(Parameter.IMAP_LOGIN), + conf.get(Parameter.IMAP_PASSWORD), + conf.get(Parameter.SMTP_HOST), + conf.get_int(Parameter.SMTP_PORT), + conf.get_bool(Parameter.SMTP_STARTTLS), + conf.get(Parameter.SMTP_LOGIN), + conf.get(Parameter.SMTP_PASSWORD), ) # configure scheduler scheduler.configure( - conf.get_int(config.IMAP_POLLING), - conf.get_int(config.COMMENT_POLLING), - conf.get(config.LANG), + conf.get_int(Parameter.IMAP_POLLING), + conf.get_int(Parameter.COMMENT_POLLING), + conf.get(Parameter.LANG), mailer, rss, ) # start Flask app.run( - host=conf.get(config.HTTP_HOST), - port=conf.get(config.HTTP_PORT), + host=conf.get(Parameter.HTTP_HOST), + port=conf.get(Parameter.HTTP_PORT), debug=False, use_reloader=False, ) diff --git a/stacosys/conf/config.py b/stacosys/conf/config.py index 15a0ed2..219d09c 100644 --- a/stacosys/conf/config.py +++ b/stacosys/conf/config.py @@ -3,37 +3,36 @@ import profig -# constants -FLASK_APP = "flask.app" - -DB_URL = "main.db_url" -DB_BACKUP_JSON_FILE = "main.db_backup_json_file" -LANG = "main.lang" -COMMENT_POLLING = "main.newcomment_polling" - -HTTP_HOST = "http.host" -HTTP_PORT = "http.port" - -RSS_PROTO = "rss.proto" -RSS_FILE = "rss.file" - -IMAP_POLLING = "imap.polling" -IMAP_SSL = "imap.ssl" -IMAP_HOST = "imap.host" -IMAP_PORT = "imap.port" -IMAP_LOGIN = "imap.login" -IMAP_PASSWORD = "imap.password" - -SMTP_STARTTLS = "smtp.starttls" -SMTP_HOST = "smtp.host" -SMTP_PORT = "smtp.port" -SMTP_LOGIN = "smtp.login" -SMTP_PASSWORD = "smtp.password" - -SITE_NAME = "site.name" -SITE_URL = "site.url" -SITE_TOKEN = "site.token" -SITE_ADMIN_EMAIL = "site.admin_email" + +class Parameter: + DB_URL = "main.db_url" + DB_BACKUP_JSON_FILE = "main.db_backup_json_file" + LANG = "main.lang" + COMMENT_POLLING = "main.newcomment_polling" + + HTTP_HOST = "http.host" + HTTP_PORT = "http.port" + + RSS_PROTO = "rss.proto" + RSS_FILE = "rss.file" + + IMAP_POLLING = "imap.polling" + IMAP_SSL = "imap.ssl" + IMAP_HOST = "imap.host" + IMAP_PORT = "imap.port" + IMAP_LOGIN = "imap.login" + IMAP_PASSWORD = "imap.password" + + SMTP_STARTTLS = "smtp.starttls" + SMTP_HOST = "smtp.host" + SMTP_PORT = "smtp.port" + SMTP_LOGIN = "smtp.login" + SMTP_PASSWORD = "smtp.password" + + SITE_NAME = "site.name" + SITE_URL = "site.url" + SITE_TOKEN = "site.token" + SITE_ADMIN_EMAIL = "site.admin_email" class Config: diff --git a/tests/test_config.py b/tests/test_config.py index 4b6d6f0..2e5452f 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -2,7 +2,7 @@ # -*- coding: UTF-8 -*- import pytest -import stacosys.conf.config as config +from stacosys.conf.config import Config, Parameter EXPECTED_DB_URL = "sqlite:///db.sqlite" EXPECTED_HTTP_PORT = 8080 @@ -12,38 +12,38 @@ EXPECTED_IMAP_LOGIN = "user" @pytest.fixture def conf(): - conf = config.Config() - conf.put(config.DB_URL, EXPECTED_DB_URL) - conf.put(config.HTTP_PORT, EXPECTED_HTTP_PORT) - conf.put(config.IMAP_PORT, EXPECTED_IMAP_PORT) - conf.put(config.SMTP_STARTTLS, "yes") - conf.put(config.IMAP_SSL, "false") + conf = Config() + conf.put(Parameter.DB_URL, EXPECTED_DB_URL) + conf.put(Parameter.HTTP_PORT, EXPECTED_HTTP_PORT) + conf.put(Parameter.IMAP_PORT, EXPECTED_IMAP_PORT) + conf.put(Parameter.SMTP_STARTTLS, "yes") + conf.put(Parameter.IMAP_SSL, "false") return conf def test_exists(conf): assert conf is not None - assert conf.exists(config.DB_URL) - assert not conf.exists(config.IMAP_HOST) + assert conf.exists(Parameter.DB_URL) + assert not conf.exists(Parameter.IMAP_HOST) def test_get(conf): assert conf is not None - assert conf.get(config.DB_URL) == EXPECTED_DB_URL - assert conf.get(config.HTTP_PORT) == EXPECTED_HTTP_PORT - assert conf.get(config.HTTP_HOST) is None - assert conf.get(config.HTTP_PORT) == EXPECTED_HTTP_PORT - assert conf.get(config.IMAP_PORT) == EXPECTED_IMAP_PORT - assert conf.get_int(config.IMAP_PORT) == int(EXPECTED_IMAP_PORT) + assert conf.get(Parameter.DB_URL) == EXPECTED_DB_URL + assert conf.get(Parameter.HTTP_PORT) == EXPECTED_HTTP_PORT + assert conf.get(Parameter.HTTP_HOST) is None + assert conf.get(Parameter.HTTP_PORT) == EXPECTED_HTTP_PORT + assert conf.get(Parameter.IMAP_PORT) == EXPECTED_IMAP_PORT + assert conf.get_int(Parameter.IMAP_PORT) == int(EXPECTED_IMAP_PORT) try: - conf.get_int(config.HTTP_PORT) + conf.get_int(Parameter.HTTP_PORT) assert False except: pass - assert conf.get_bool(config.SMTP_STARTTLS) - assert not conf.get_bool(config.IMAP_SSL) + assert conf.get_bool(Parameter.SMTP_STARTTLS) + assert not conf.get_bool(Parameter.IMAP_SSL) try: - conf.get_bool(config.DB_URL) + conf.get_bool(Parameter.DB_URL) assert False except: pass @@ -51,7 +51,7 @@ def test_get(conf): def test_put(conf): assert conf is not None - assert not conf.exists(config.IMAP_LOGIN) - conf.put(config.IMAP_LOGIN, EXPECTED_IMAP_LOGIN) - assert conf.exists(config.IMAP_LOGIN) - assert conf.get(config.IMAP_LOGIN) == EXPECTED_IMAP_LOGIN + assert not conf.exists(Parameter.IMAP_LOGIN) + conf.put(Parameter.IMAP_LOGIN, EXPECTED_IMAP_LOGIN) + assert conf.exists(Parameter.IMAP_LOGIN) + assert conf.get(Parameter.IMAP_LOGIN) == EXPECTED_IMAP_LOGIN