2016-04-19 17:40:59 +00:00
|
|
|
import os
|
|
|
|
import configparser
|
|
|
|
|
|
|
|
class config:
|
|
|
|
"""
|
|
|
|
config.ini object
|
|
|
|
|
|
|
|
config -- list with ini data
|
|
|
|
default -- if true, we have generated a default config.ini
|
|
|
|
"""
|
|
|
|
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
fileName = "" # config filename
|
|
|
|
default = True
|
|
|
|
|
|
|
|
# Check if config.ini exists and load/generate it
|
|
|
|
def __init__(self, __file):
|
|
|
|
"""
|
|
|
|
Initialize a config object
|
|
|
|
|
|
|
|
__file -- filename
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.fileName = __file
|
|
|
|
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):
|
|
|
|
"""
|
|
|
|
Check if this config has the required keys
|
|
|
|
|
|
|
|
return -- True if valid, False if not
|
|
|
|
"""
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
self.config.get("server","port")
|
2016-06-04 10:44:54 +00:00
|
|
|
self.config.get("server","threads")
|
|
|
|
self.config.get("server","gzip")
|
|
|
|
self.config.get("server","gziplevel")
|
|
|
|
self.config.get("server","localize")
|
|
|
|
self.config.get("server","cikey")
|
|
|
|
|
|
|
|
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")
|
|
|
|
self.config.get("sentry","dns")
|
|
|
|
|
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-04-19 17:40:59 +00:00
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
# Generate a default config.ini
|
|
|
|
def generateDefaultConfig(self):
|
|
|
|
"""Open and set default keys for that config file"""
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
self.config.add_section("server")
|
|
|
|
self.config.set("server", "port", "5001")
|
2016-06-04 10:44:54 +00:00
|
|
|
self.config.set("server", "threads", "16")
|
|
|
|
self.config.set("server", "gzip", "1")
|
|
|
|
self.config.set("server", "gziplevel", "6")
|
|
|
|
self.config.set("server", "localize", "1")
|
|
|
|
self.config.set("server", "cikey", "changeme")
|
2016-04-19 17:40:59 +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")
|
|
|
|
self.config.set("sentry", "dns", "")
|
|
|
|
|
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-04-19 17:40:59 +00:00
|
|
|
# Write ini to file and close
|
|
|
|
self.config.write(f)
|
|
|
|
f.close()
|