imaginaryfriend/src/config.py

54 lines
1.6 KiB
Python
Raw Normal View History

2016-12-06 18:52:51 +01:00
# Config
2016-11-12 16:36:23 +01:00
import configparser
2017-04-14 15:50:54 +02:00
import os
2016-11-12 16:36:23 +01:00
2016-12-15 21:24:29 +01:00
encoding = 'utf-8'
2016-11-27 16:34:21 +01:00
sections = {
2017-04-14 15:50:54 +02:00
'bot': ['token', 'name', 'anchors', 'purge_interval', 'default_chance', 'spam_stickers'],
2016-12-09 19:37:55 +01:00
'grammar': ['chain_length', 'separator', 'stop_word', 'end_sentence', 'all'],
2016-11-27 16:34:21 +01:00
'logging': ['level'],
2016-12-03 16:08:43 +01:00
'updates': ['mode'],
2016-12-20 19:31:17 +01:00
'media_checker': ['lifetime', 'messages'],
2016-12-31 08:20:00 +01:00
'redis': ['host', 'port', 'db']
2016-11-27 16:34:21 +01:00
}
def getlist(self, section, option, type=str):
return list(map(lambda o: type(o), config.get(section, option).split(',')))
configparser.ConfigParser.getlist = getlist
2017-04-18 22:40:40 +02:00
root_config = 'resources/main.cfg'
user_config = os.getenv('CONFIG_PATH', 'cfg/main.plain.cfg')
2016-11-12 16:36:23 +01:00
config = configparser.ConfigParser()
2017-04-18 22:40:40 +02:00
config.read(root_config, encoding=encoding)
config.read(user_config, encoding=encoding)
2016-11-27 16:34:21 +01:00
for section, options in sections.items():
if not config.has_section(section):
raise ValueError("Config is not valid! Section '{}' is missing!".format(section))
for option in options:
if not config.has_option(section, option):
2016-12-05 18:53:41 +01:00
raise ValueError("Config is not valid! Option '{}' in section '{}' is missing!".format(option, section))
# IOC
2016-12-06 18:52:51 +01:00
from src.redis_c import Redis
2016-12-05 18:53:41 +01:00
redis = Redis(config)
2016-12-06 18:52:51 +01:00
2016-12-28 19:01:37 +01:00
from src.tokenizer import Tokenizer
tokenizer = Tokenizer()
2016-12-09 20:45:50 +01:00
2016-12-14 21:19:46 +01:00
from src.repository import *
trigram_repository = TrigramRepository()
chance_repository = ChanceRepository()
media_repository = MediaRepository()
2016-12-18 16:55:32 +01:00
job_repository = JobRepository()
2016-12-14 21:19:46 +01:00
2016-12-28 19:01:37 +01:00
from src.service import *
2016-12-27 19:23:49 +01:00
data_learner = DataLearner()
reply_generator = ReplyGenerator()
2016-12-05 18:53:41 +01:00
media_checker = MediaUniquenessChecker()
chat_purge_queue = ChatPurgeQueue()