2016-04-19 17:40:59 +00:00
|
|
|
import os
|
|
|
|
import configparser
|
|
|
|
|
2016-09-02 15:45:10 +00:00
|
|
|
class config:
|
2016-04-19 17:40:59 +00:00
|
|
|
# Check if config.ini exists and load/generate it
|
2016-08-17 14:41:05 +00:00
|
|
|
def __init__(self, file):
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
2016-11-17 18:13:06 +00:00
|
|
|
Initialize a config file object
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-11-17 18:13:06 +00:00
|
|
|
:param file: file name
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
2016-09-02 10:41:19 +00:00
|
|
|
self.config = configparser.ConfigParser()
|
|
|
|
self.default = True
|
2016-08-17 14:41:05 +00:00
|
|
|
self.fileName = file
|
2016-04-19 17:40:59 +00:00
|
|
|
if os.path.isfile(self.fileName):
|
|
|
|
# config.ini found, load it
|
|
|
|
self.config.read(self.fileName)
|
|
|
|
self.default = False
|
|
|
|
else:
|
|
|
|
# config.ini not found, generate a default one
|
|
|
|
self.generateDefaultConfig()
|
|
|
|
self.default = True
|
|
|
|
|
|
|
|
|
|
|
|
# Check if config.ini has all needed the keys
|
|
|
|
def checkConfig(self):
|
|
|
|
"""
|
2016-11-17 18:13:06 +00:00
|
|
|
Check is the config file has all required keys
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-11-17 18:13:06 +00:00
|
|
|
:return: True if valid, False if not valid
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
# Try to get all the required keys
|
|
|
|
self.config.get("db","host")
|
|
|
|
self.config.get("db","username")
|
|
|
|
self.config.get("db","password")
|
|
|
|
self.config.get("db","database")
|
2016-05-31 20:49:30 +00:00
|
|
|
self.config.get("db","workers")
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-11-15 19:36:29 +00:00
|
|
|
self.config.get("redis","host")
|
|
|
|
self.config.get("redis","port")
|
|
|
|
self.config.get("redis","database")
|
|
|
|
self.config.get("redis","password")
|
|
|
|
|
2016-04-19 17:40:59 +00:00
|
|
|
self.config.get("server","port")
|
2016-09-04 14:07:10 +00:00
|
|
|
self.config.get("server","threads")
|
2016-06-04 10:44:54 +00:00
|
|
|
self.config.get("server","gzip")
|
|
|
|
self.config.get("server","gziplevel")
|
|
|
|
self.config.get("server","cikey")
|
|
|
|
|
2017-01-16 20:24:15 +00:00
|
|
|
self.config.get("cheesegull", "apiurl")
|
|
|
|
self.config.get("cheesegull", "apikey")
|
2016-10-15 21:21:17 +00:00
|
|
|
|
2016-06-04 10:44:54 +00:00
|
|
|
self.config.get("debug","enable")
|
|
|
|
self.config.get("debug","packets")
|
|
|
|
self.config.get("debug","time")
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-06-15 17:01:00 +00:00
|
|
|
self.config.get("sentry","enable")
|
2016-07-14 13:38:28 +00:00
|
|
|
self.config.get("sentry","banchodns")
|
|
|
|
self.config.get("sentry","ircdns")
|
2016-06-15 17:01:00 +00:00
|
|
|
|
2016-05-19 20:53:09 +00:00
|
|
|
self.config.get("discord","enable")
|
|
|
|
self.config.get("discord","boturl")
|
2016-06-02 17:22:02 +00:00
|
|
|
self.config.get("discord","devgroup")
|
2016-07-14 10:37:07 +00:00
|
|
|
|
2016-10-06 21:06:59 +00:00
|
|
|
self.config.get("datadog", "enable")
|
|
|
|
self.config.get("datadog", "apikey")
|
|
|
|
self.config.get("datadog", "appkey")
|
|
|
|
|
2016-07-14 10:37:07 +00:00
|
|
|
self.config.get("irc","enable")
|
|
|
|
self.config.get("irc","port")
|
2016-09-04 10:36:21 +00:00
|
|
|
self.config.get("irc","hostname")
|
2016-07-14 10:37:07 +00:00
|
|
|
|
|
|
|
self.config.get("localize","enable")
|
|
|
|
self.config.get("localize","ipapiurl")
|
2016-04-19 17:40:59 +00:00
|
|
|
return True
|
2016-12-26 09:33:05 +00:00
|
|
|
except configparser.Error:
|
2016-04-19 17:40:59 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
def generateDefaultConfig(self):
|
2016-09-02 10:41:19 +00:00
|
|
|
"""
|
2016-11-17 18:13:06 +00:00
|
|
|
Write a default config file to disk
|
|
|
|
|
|
|
|
:return:
|
2016-09-02 10:41:19 +00:00
|
|
|
"""
|
2016-04-19 17:40:59 +00:00
|
|
|
# Open config.ini in write mode
|
|
|
|
f = open(self.fileName, "w")
|
|
|
|
|
|
|
|
# Set keys to config object
|
|
|
|
self.config.add_section("db")
|
|
|
|
self.config.set("db", "host", "localhost")
|
|
|
|
self.config.set("db", "username", "root")
|
|
|
|
self.config.set("db", "password", "")
|
|
|
|
self.config.set("db", "database", "ripple")
|
2016-05-31 20:49:30 +00:00
|
|
|
self.config.set("db", "workers", "4")
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-11-15 19:36:29 +00:00
|
|
|
self.config.add_section("redis")
|
|
|
|
self.config.set("redis", "host", "localhost")
|
|
|
|
self.config.set("redis", "port", "6379")
|
|
|
|
self.config.set("redis", "database", "0")
|
|
|
|
self.config.set("redis", "password", "")
|
|
|
|
|
2016-04-19 17:40:59 +00:00
|
|
|
self.config.add_section("server")
|
|
|
|
self.config.set("server", "port", "5001")
|
2016-09-04 14:07:10 +00:00
|
|
|
self.config.set("server", "threads", "16")
|
2016-06-04 10:44:54 +00:00
|
|
|
self.config.set("server", "gzip", "1")
|
|
|
|
self.config.set("server", "gziplevel", "6")
|
|
|
|
self.config.set("server", "cikey", "changeme")
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2017-01-16 20:24:15 +00:00
|
|
|
self.config.add_section("cheesegull")
|
|
|
|
self.config.set("cheesegull", "apiurl", "http://cheesegu.ll/api")
|
|
|
|
self.config.set("cheesegull", "apikey", "")
|
2016-10-15 21:21:17 +00:00
|
|
|
|
2016-06-04 10:44:54 +00:00
|
|
|
self.config.add_section("debug")
|
|
|
|
self.config.set("debug", "enable", "0")
|
|
|
|
self.config.set("debug", "packets", "0")
|
|
|
|
self.config.set("debug", "time", "0")
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-06-15 17:01:00 +00:00
|
|
|
self.config.add_section("sentry")
|
|
|
|
self.config.set("sentry", "enable", "0")
|
2016-07-14 13:38:28 +00:00
|
|
|
self.config.set("sentry", "banchodns", "")
|
|
|
|
self.config.set("sentry", "ircdns", "")
|
2016-06-15 17:01:00 +00:00
|
|
|
|
2016-05-19 20:53:09 +00:00
|
|
|
self.config.add_section("discord")
|
2016-06-04 10:44:54 +00:00
|
|
|
self.config.set("discord", "enable", "0")
|
2016-05-19 20:53:09 +00:00
|
|
|
self.config.set("discord", "boturl", "")
|
2016-06-02 17:22:02 +00:00
|
|
|
self.config.set("discord", "devgroup", "")
|
2016-05-31 20:49:30 +00:00
|
|
|
|
2016-10-06 21:06:59 +00:00
|
|
|
self.config.add_section("datadog")
|
2016-12-07 20:15:55 +00:00
|
|
|
self.config.set("datadog", "enable", "0")
|
|
|
|
self.config.set("datadog", "apikey", "")
|
|
|
|
self.config.set("datadog", "appkey", "")
|
2016-10-06 21:06:59 +00:00
|
|
|
|
2016-07-14 10:37:07 +00:00
|
|
|
self.config.add_section("irc")
|
|
|
|
self.config.set("irc", "enable", "1")
|
|
|
|
self.config.set("irc", "port", "6667")
|
2016-09-04 10:36:21 +00:00
|
|
|
self.config.set("irc", "hostname", "ripple")
|
2016-07-14 10:37:07 +00:00
|
|
|
|
|
|
|
self.config.add_section("localize")
|
|
|
|
self.config.set("localize", "enable", "1")
|
|
|
|
self.config.set("localize", "ipapiurl", "http://ip.zxq.co")
|
|
|
|
|
2016-04-19 17:40:59 +00:00
|
|
|
# Write ini to file and close
|
|
|
|
self.config.write(f)
|
|
|
|
f.close()
|