stacosys/run.py

123 lines
3.6 KiB
Python
Raw Normal View History

#!/usr/bin/python
# -*- coding: UTF-8 -*-
2021-01-03 18:24:16 +01:00
import sys
import os
2018-10-27 18:10:28 +02:00
import argparse
import logging
2021-01-10 21:03:55 +01:00
from stacosys.conf.config import Config, ConfigParameter
2021-07-17 09:44:58 +02:00
from stacosys.db import database
2021-01-03 18:24:16 +01:00
from stacosys.core.rss import Rss
from stacosys.core.mailer import Mailer
from stacosys.interface import app
2021-01-28 19:00:54 +01:00
from stacosys.interface import api
from stacosys.interface import form
2021-01-03 18:24:16 +01:00
from stacosys.interface import scheduler
2018-09-15 13:38:03 +02:00
# configure logging
def configure_logging(level):
root_logger = logging.getLogger()
root_logger.setLevel(level)
ch = logging.StreamHandler()
ch.setLevel(level)
# create formatter
2021-01-02 12:36:42 +01:00
formatter = logging.Formatter("[%(asctime)s] %(name)s %(levelname)s %(message)s")
2018-09-15 13:38:03 +02:00
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
root_logger.addHandler(ch)
2018-01-20 10:03:41 +01:00
2018-09-15 13:38:03 +02:00
def stacosys_server(config_pathname):
2018-09-15 13:38:03 +02:00
# configure logging
logger = logging.getLogger(__name__)
configure_logging(logging.INFO)
2021-01-02 12:36:42 +01:00
logging.getLogger("werkzeug").level = logging.WARNING
logging.getLogger("apscheduler.executors").level = logging.WARNING
2018-09-15 13:38:03 +02:00
# check config file exists
if not os.path.isfile(config_pathname):
logger.error(f"Configuration file '{config_pathname}' not found.")
sys.exit(1)
# initialize config
conf = Config.load(config_pathname)
2021-01-28 19:00:54 +01:00
logger.info(conf.__repr__())
2021-02-08 18:35:46 +01:00
# check database file exists (prevents from creating a fresh db)
db_pathname = conf.get(ConfigParameter.DB_SQLITE_FILE)
if not os.path.isfile(db_pathname):
logger.error(f"Database file '{db_pathname}' not found.")
sys.exit(1)
2018-09-15 13:38:03 +02:00
# initialize database
2021-01-02 12:36:42 +01:00
db = database.Database()
db.setup(db_pathname)
2018-09-15 13:38:03 +02:00
2021-01-02 12:36:42 +01:00
logger.info("Start Stacosys application")
2018-09-15 13:38:03 +02:00
2018-09-15 15:02:06 +02:00
# generate RSS for all sites
2021-01-03 18:24:16 +01:00
rss = Rss(
2021-01-10 21:03:55 +01:00
conf.get(ConfigParameter.LANG),
conf.get(ConfigParameter.RSS_FILE),
conf.get(ConfigParameter.RSS_PROTO),
conf.get(ConfigParameter.SITE_NAME),
conf.get(ConfigParameter.SITE_URL),
2021-01-03 18:24:16 +01:00
)
2021-01-10 21:03:55 +01:00
rss.generate()
2021-01-03 18:24:16 +01:00
# configure mailer
mailer = Mailer(
2021-01-10 21:03:55 +01:00
conf.get(ConfigParameter.IMAP_HOST),
conf.get_int(ConfigParameter.IMAP_PORT),
conf.get_bool(ConfigParameter.IMAP_SSL),
conf.get(ConfigParameter.IMAP_LOGIN),
conf.get(ConfigParameter.IMAP_PASSWORD),
conf.get(ConfigParameter.SMTP_HOST),
conf.get_int(ConfigParameter.SMTP_PORT),
conf.get_bool(ConfigParameter.SMTP_STARTTLS),
2021-01-28 19:00:54 +01:00
conf.get_bool(ConfigParameter.SMTP_SSL),
2021-01-10 21:03:55 +01:00
conf.get(ConfigParameter.SMTP_LOGIN),
conf.get(ConfigParameter.SMTP_PASSWORD),
2021-02-08 18:35:46 +01:00
conf.get(ConfigParameter.SITE_ADMIN_EMAIL)
2021-01-03 18:24:16 +01:00
)
2018-09-15 15:02:06 +02:00
2021-02-08 18:35:46 +01:00
# configure mailer logger
mail_handler = mailer.get_error_handler()
logger.addHandler(mail_handler)
app.logger.addHandler(mail_handler)
2021-01-03 18:24:16 +01:00
# configure scheduler
scheduler.configure(
2021-01-10 21:03:55 +01:00
conf.get_int(ConfigParameter.IMAP_POLLING),
conf.get_int(ConfigParameter.COMMENT_POLLING),
conf.get(ConfigParameter.LANG),
conf.get(ConfigParameter.SITE_NAME),
conf.get(ConfigParameter.SITE_TOKEN),
conf.get(ConfigParameter.SITE_ADMIN_EMAIL),
2021-01-03 18:24:16 +01:00
mailer,
rss,
)
2018-09-15 15:02:06 +02:00
2021-01-10 21:03:55 +01:00
# inject config parameters into flask
app.config.update(SITE_TOKEN=conf.get(ConfigParameter.SITE_TOKEN))
2021-01-28 19:00:54 +01:00
logger.info(f"start interfaces {api} {form}")
2021-01-10 21:03:55 +01:00
2021-01-03 18:24:16 +01:00
# start Flask
2018-09-15 13:38:03 +02:00
app.run(
2021-01-10 21:03:55 +01:00
host=conf.get(ConfigParameter.HTTP_HOST),
port=conf.get(ConfigParameter.HTTP_PORT),
2018-09-15 13:38:03 +02:00
debug=False,
use_reloader=False,
)
2021-01-02 12:36:42 +01:00
if __name__ == "__main__":
2018-10-27 18:10:28 +02:00
parser = argparse.ArgumentParser()
2021-01-02 12:36:42 +01:00
parser.add_argument("config", help="config path name")
2018-10-27 18:10:28 +02:00
args = parser.parse_args()
stacosys_server(args.config)