diff --git a/stacosys/conf/config.py b/stacosys/conf/config.py index a4742d3..15a0ed2 100644 --- a/stacosys/conf/config.py +++ b/stacosys/conf/config.py @@ -4,59 +4,62 @@ import profig # constants -FLASK_APP = 'flask.app' +FLASK_APP = "flask.app" -DB_URL = 'main.db_url' -DB_FILE = 'main.db_file' -LANG = 'main.lang' -COMMENT_POLLING = 'main.newcomment_polling' +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' +HTTP_HOST = "http.host" +HTTP_PORT = "http.port" -RSS_PROTO = 'rss.proto' -RSS_FILE = 'rss.file' +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' +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' +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' +SITE_NAME = "site.name" +SITE_URL = "site.url" +SITE_TOKEN = "site.token" +SITE_ADMIN_EMAIL = "site.admin_email" -# variable -params = dict() +class Config: + def __init__(self): + self._params = dict() -def initialize(config_pathname, flask_app): - cfg = profig.Config(config_pathname) - cfg.sync() - params.update(cfg) - params.update({FLASK_APP: flask_app}) + @classmethod + def load(cls, config_pathname): + cfg = profig.Config(config_pathname) + cfg.sync() + config = cls() + config._params.update(cfg) + return config + def exists(self, key): + return key in self._params -def get(key): - return params[key] + def get(self, key): + return self._params[key] if key in self._params else None + def put(self, key, value): + self._params[key] = value -def get_int(key): - return int(params[key]) + def get_int(self, key): + return int(self._params[key]) + def get_bool(self, key): + return self._params[key].lower() in ("yes", "true") -def get_bool(key): - return params[key].lower() in ('yes', 'true', '1') - - -def flaskapp(): - return params[FLASK_APP] diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..4b6d6f0 --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,57 @@ +#!/usr/bin/python +# -*- coding: UTF-8 -*- + +import pytest +import stacosys.conf.config as config + +EXPECTED_DB_URL = "sqlite:///db.sqlite" +EXPECTED_HTTP_PORT = 8080 +EXPECTED_IMAP_PORT = "5000" +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") + return conf + + +def test_exists(conf): + assert conf is not None + assert conf.exists(config.DB_URL) + assert not conf.exists(config.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) + try: + conf.get_int(config.HTTP_PORT) + assert False + except: + pass + assert conf.get_bool(config.SMTP_STARTTLS) + assert not conf.get_bool(config.IMAP_SSL) + try: + conf.get_bool(config.DB_URL) + assert False + except: + pass + + +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