refactor config constants
This commit is contained in:
parent
cae77c2d05
commit
e85e53a48e
3 changed files with 68 additions and 67 deletions
40
run.py
40
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,
|
||||
)
|
||||
|
|
|
@ -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"
|
||||
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"
|
||||
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"
|
||||
|
||||
|
||||
class Config:
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue