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
|
import logging
|
||||||
from flask import Flask
|
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 import database
|
||||||
from stacosys.core.rss import Rss
|
from stacosys.core.rss import Rss
|
||||||
from stacosys.core.mailer import Mailer
|
from stacosys.core.mailer import Mailer
|
||||||
|
@ -33,7 +33,7 @@ def configure_logging(level):
|
||||||
|
|
||||||
def stacosys_server(config_pathname):
|
def stacosys_server(config_pathname):
|
||||||
|
|
||||||
conf = config.Config.load(config_pathname)
|
conf = Config.load(config_pathname)
|
||||||
|
|
||||||
# configure logging
|
# configure logging
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
@ -43,43 +43,45 @@ def stacosys_server(config_pathname):
|
||||||
|
|
||||||
# initialize database
|
# initialize database
|
||||||
db = database.Database()
|
db = database.Database()
|
||||||
db.setup(conf.get(config.DB_URL))
|
db.setup(conf.get(Parameter.DB_URL))
|
||||||
|
|
||||||
logger.info("Start Stacosys application")
|
logger.info("Start Stacosys application")
|
||||||
|
|
||||||
# generate RSS for all sites
|
# generate RSS for all sites
|
||||||
rss = Rss(
|
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()
|
rss.generate_all()
|
||||||
|
|
||||||
# configure mailer
|
# configure mailer
|
||||||
mailer = Mailer(
|
mailer = Mailer(
|
||||||
conf.get(config.IMAP_HOST),
|
conf.get(Parameter.IMAP_HOST),
|
||||||
conf.get_int(config.IMAP_PORT),
|
conf.get_int(Parameter.IMAP_PORT),
|
||||||
conf.get_bool(config.IMAP_SSL),
|
conf.get_bool(Parameter.IMAP_SSL),
|
||||||
conf.get(config.IMAP_LOGIN),
|
conf.get(Parameter.IMAP_LOGIN),
|
||||||
conf.get(config.IMAP_PASSWORD),
|
conf.get(Parameter.IMAP_PASSWORD),
|
||||||
conf.get(config.SMTP_HOST),
|
conf.get(Parameter.SMTP_HOST),
|
||||||
conf.get_int(config.SMTP_PORT),
|
conf.get_int(Parameter.SMTP_PORT),
|
||||||
conf.get_bool(config.SMTP_STARTTLS),
|
conf.get_bool(Parameter.SMTP_STARTTLS),
|
||||||
conf.get(config.SMTP_LOGIN),
|
conf.get(Parameter.SMTP_LOGIN),
|
||||||
conf.get(config.SMTP_PASSWORD),
|
conf.get(Parameter.SMTP_PASSWORD),
|
||||||
)
|
)
|
||||||
|
|
||||||
# configure scheduler
|
# configure scheduler
|
||||||
scheduler.configure(
|
scheduler.configure(
|
||||||
conf.get_int(config.IMAP_POLLING),
|
conf.get_int(Parameter.IMAP_POLLING),
|
||||||
conf.get_int(config.COMMENT_POLLING),
|
conf.get_int(Parameter.COMMENT_POLLING),
|
||||||
conf.get(config.LANG),
|
conf.get(Parameter.LANG),
|
||||||
mailer,
|
mailer,
|
||||||
rss,
|
rss,
|
||||||
)
|
)
|
||||||
|
|
||||||
# start Flask
|
# start Flask
|
||||||
app.run(
|
app.run(
|
||||||
host=conf.get(config.HTTP_HOST),
|
host=conf.get(Parameter.HTTP_HOST),
|
||||||
port=conf.get(config.HTTP_PORT),
|
port=conf.get(Parameter.HTTP_PORT),
|
||||||
debug=False,
|
debug=False,
|
||||||
use_reloader=False,
|
use_reloader=False,
|
||||||
)
|
)
|
||||||
|
|
|
@ -3,37 +3,36 @@
|
||||||
|
|
||||||
import profig
|
import profig
|
||||||
|
|
||||||
# constants
|
|
||||||
FLASK_APP = "flask.app"
|
|
||||||
|
|
||||||
DB_URL = "main.db_url"
|
class Parameter:
|
||||||
DB_BACKUP_JSON_FILE = "main.db_backup_json_file"
|
DB_URL = "main.db_url"
|
||||||
LANG = "main.lang"
|
DB_BACKUP_JSON_FILE = "main.db_backup_json_file"
|
||||||
COMMENT_POLLING = "main.newcomment_polling"
|
LANG = "main.lang"
|
||||||
|
COMMENT_POLLING = "main.newcomment_polling"
|
||||||
|
|
||||||
HTTP_HOST = "http.host"
|
HTTP_HOST = "http.host"
|
||||||
HTTP_PORT = "http.port"
|
HTTP_PORT = "http.port"
|
||||||
|
|
||||||
RSS_PROTO = "rss.proto"
|
RSS_PROTO = "rss.proto"
|
||||||
RSS_FILE = "rss.file"
|
RSS_FILE = "rss.file"
|
||||||
|
|
||||||
IMAP_POLLING = "imap.polling"
|
IMAP_POLLING = "imap.polling"
|
||||||
IMAP_SSL = "imap.ssl"
|
IMAP_SSL = "imap.ssl"
|
||||||
IMAP_HOST = "imap.host"
|
IMAP_HOST = "imap.host"
|
||||||
IMAP_PORT = "imap.port"
|
IMAP_PORT = "imap.port"
|
||||||
IMAP_LOGIN = "imap.login"
|
IMAP_LOGIN = "imap.login"
|
||||||
IMAP_PASSWORD = "imap.password"
|
IMAP_PASSWORD = "imap.password"
|
||||||
|
|
||||||
SMTP_STARTTLS = "smtp.starttls"
|
SMTP_STARTTLS = "smtp.starttls"
|
||||||
SMTP_HOST = "smtp.host"
|
SMTP_HOST = "smtp.host"
|
||||||
SMTP_PORT = "smtp.port"
|
SMTP_PORT = "smtp.port"
|
||||||
SMTP_LOGIN = "smtp.login"
|
SMTP_LOGIN = "smtp.login"
|
||||||
SMTP_PASSWORD = "smtp.password"
|
SMTP_PASSWORD = "smtp.password"
|
||||||
|
|
||||||
SITE_NAME = "site.name"
|
SITE_NAME = "site.name"
|
||||||
SITE_URL = "site.url"
|
SITE_URL = "site.url"
|
||||||
SITE_TOKEN = "site.token"
|
SITE_TOKEN = "site.token"
|
||||||
SITE_ADMIN_EMAIL = "site.admin_email"
|
SITE_ADMIN_EMAIL = "site.admin_email"
|
||||||
|
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
# -*- coding: UTF-8 -*-
|
# -*- coding: UTF-8 -*-
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import stacosys.conf.config as config
|
from stacosys.conf.config import Config, Parameter
|
||||||
|
|
||||||
EXPECTED_DB_URL = "sqlite:///db.sqlite"
|
EXPECTED_DB_URL = "sqlite:///db.sqlite"
|
||||||
EXPECTED_HTTP_PORT = 8080
|
EXPECTED_HTTP_PORT = 8080
|
||||||
|
@ -12,38 +12,38 @@ EXPECTED_IMAP_LOGIN = "user"
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def conf():
|
def conf():
|
||||||
conf = config.Config()
|
conf = Config()
|
||||||
conf.put(config.DB_URL, EXPECTED_DB_URL)
|
conf.put(Parameter.DB_URL, EXPECTED_DB_URL)
|
||||||
conf.put(config.HTTP_PORT, EXPECTED_HTTP_PORT)
|
conf.put(Parameter.HTTP_PORT, EXPECTED_HTTP_PORT)
|
||||||
conf.put(config.IMAP_PORT, EXPECTED_IMAP_PORT)
|
conf.put(Parameter.IMAP_PORT, EXPECTED_IMAP_PORT)
|
||||||
conf.put(config.SMTP_STARTTLS, "yes")
|
conf.put(Parameter.SMTP_STARTTLS, "yes")
|
||||||
conf.put(config.IMAP_SSL, "false")
|
conf.put(Parameter.IMAP_SSL, "false")
|
||||||
return conf
|
return conf
|
||||||
|
|
||||||
|
|
||||||
def test_exists(conf):
|
def test_exists(conf):
|
||||||
assert conf is not None
|
assert conf is not None
|
||||||
assert conf.exists(config.DB_URL)
|
assert conf.exists(Parameter.DB_URL)
|
||||||
assert not conf.exists(config.IMAP_HOST)
|
assert not conf.exists(Parameter.IMAP_HOST)
|
||||||
|
|
||||||
|
|
||||||
def test_get(conf):
|
def test_get(conf):
|
||||||
assert conf is not None
|
assert conf is not None
|
||||||
assert conf.get(config.DB_URL) == EXPECTED_DB_URL
|
assert conf.get(Parameter.DB_URL) == EXPECTED_DB_URL
|
||||||
assert conf.get(config.HTTP_PORT) == EXPECTED_HTTP_PORT
|
assert conf.get(Parameter.HTTP_PORT) == EXPECTED_HTTP_PORT
|
||||||
assert conf.get(config.HTTP_HOST) is None
|
assert conf.get(Parameter.HTTP_HOST) is None
|
||||||
assert conf.get(config.HTTP_PORT) == EXPECTED_HTTP_PORT
|
assert conf.get(Parameter.HTTP_PORT) == EXPECTED_HTTP_PORT
|
||||||
assert conf.get(config.IMAP_PORT) == EXPECTED_IMAP_PORT
|
assert conf.get(Parameter.IMAP_PORT) == EXPECTED_IMAP_PORT
|
||||||
assert conf.get_int(config.IMAP_PORT) == int(EXPECTED_IMAP_PORT)
|
assert conf.get_int(Parameter.IMAP_PORT) == int(EXPECTED_IMAP_PORT)
|
||||||
try:
|
try:
|
||||||
conf.get_int(config.HTTP_PORT)
|
conf.get_int(Parameter.HTTP_PORT)
|
||||||
assert False
|
assert False
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
assert conf.get_bool(config.SMTP_STARTTLS)
|
assert conf.get_bool(Parameter.SMTP_STARTTLS)
|
||||||
assert not conf.get_bool(config.IMAP_SSL)
|
assert not conf.get_bool(Parameter.IMAP_SSL)
|
||||||
try:
|
try:
|
||||||
conf.get_bool(config.DB_URL)
|
conf.get_bool(Parameter.DB_URL)
|
||||||
assert False
|
assert False
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
@ -51,7 +51,7 @@ def test_get(conf):
|
||||||
|
|
||||||
def test_put(conf):
|
def test_put(conf):
|
||||||
assert conf is not None
|
assert conf is not None
|
||||||
assert not conf.exists(config.IMAP_LOGIN)
|
assert not conf.exists(Parameter.IMAP_LOGIN)
|
||||||
conf.put(config.IMAP_LOGIN, EXPECTED_IMAP_LOGIN)
|
conf.put(Parameter.IMAP_LOGIN, EXPECTED_IMAP_LOGIN)
|
||||||
assert conf.exists(config.IMAP_LOGIN)
|
assert conf.exists(Parameter.IMAP_LOGIN)
|
||||||
assert conf.get(config.IMAP_LOGIN) == EXPECTED_IMAP_LOGIN
|
assert conf.get(Parameter.IMAP_LOGIN) == EXPECTED_IMAP_LOGIN
|
||||||
|
|
Loading…
Add table
Reference in a new issue