blivechat/config.py

67 lines
1.7 KiB
Python
Raw Normal View History

2020-02-03 16:18:21 +08:00
# -*- coding: utf-8 -*-
import configparser
import logging
import os
from typing import *
logger = logging.getLogger(__name__)
CONFIG_PATH = os.path.join('data', 'config.ini')
_config: Optional['AppConfig'] = None
def init():
if reload():
return
logger.warning('Using default config')
global _config
_config = AppConfig()
2020-02-03 16:18:21 +08:00
def reload():
config = AppConfig()
if not config.load(CONFIG_PATH):
return False
global _config
_config = config
return True
2020-02-03 16:18:21 +08:00
def get_config():
return _config
class AppConfig:
def __init__(self):
self.database_url = 'sqlite:///data/database.db'
2020-02-06 17:39:56 +08:00
self.enable_translate = True
self.allow_translate_rooms = {}
2020-08-22 18:38:52 +08:00
self.tornado_xheaders = False
2020-08-30 17:46:04 +08:00
self.loader_url = ''
2020-02-03 16:18:21 +08:00
def load(self, path):
try:
2020-08-22 18:38:52 +08:00
config = configparser.ConfigParser()
config.read(path)
2020-02-03 16:18:21 +08:00
app_section = config['app']
self.database_url = app_section['database_url']
2020-02-06 17:39:56 +08:00
self.enable_translate = app_section.getboolean('enable_translate')
2020-08-22 18:38:52 +08:00
2020-08-30 17:46:04 +08:00
allow_translate_rooms = app_section['allow_translate_rooms']
if allow_translate_rooms == '':
self.allow_translate_rooms = {}
else:
allow_translate_rooms = allow_translate_rooms.split(',')
self.allow_translate_rooms = set(map(lambda id_: int(id_.strip()), allow_translate_rooms))
2020-08-22 18:38:52 +08:00
self.tornado_xheaders = app_section.getboolean('tornado_xheaders')
2020-08-30 17:46:04 +08:00
self.loader_url = app_section['loader_url']
2020-08-22 18:38:52 +08:00
2020-02-03 16:18:21 +08:00
except (KeyError, ValueError):
logger.exception('Failed to load config:')
return False
return True