summaryrefslogtreecommitdiff
path: root/optional/plussettingstodb.py
blob: 25a948fc4adec3dcbcf19da0fc346b2888713280 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import os, re, config, sys, mysql.connector
from colorama import Fore, Style, init
from mysql.connector import errorcode
from config import SQL_HOST as MYhost
from config import SQL_PORT as MYport
from config import SQL_USER as MYuser
from config import SQL_PASS as MYpass
from config import SQL_DATABASE as MYbase
from config import vplusfile

section = '^\[([\w]*)\]$'
settings = '^(\w*) = (\w*)$'

TABLES = {}
TABLES['vplus'] = (
    "CREATE TABLE `vplus` ("
    "  `id` int NOT NULL AUTO_INCREMENT,"
    "  `section` varchar(75) DEFAULT NULL,"
    "  `enabled` varchar(75) DEFAULT NULL,"
    "  `option1` varchar(75) DEFAULT NULL,"
    "  `settings` varchar(75) DEFAULT NULL,"
    "  PRIMARY KEY (`id`)"
    ") ENGINE=InnoDB")

def mydbconnect():
    global mydb
    mydb = mysql.connector.connect(
        host=MYhost,
        user=MYuser,
        password=MYpass,
        database=MYbase,
        port=MYport,
        )
    try:
        if mydb.is_connected():
            db_Info = mydb.get_server_info()
            print(Fore.GREEN + "Connected to MySQL database... MySQL Server version ", db_Info + Style.RESET_ALL)
    except mysql.connector.Error as err:
        print(Fore.RED + err + 'From MySQL database' + Style.RESET_ALL)

mydbconnect()

def maketable():
    mycursor = mydb.cursor()
    for table_name in TABLES:
        table_description = TABLES[table_name]
        try:
            print(Fore.GREEN + "Creating table {}: ".format(table_name), end='')
            mycursor.execute(table_description)
        except mysql.connector.Error as err:
            if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
                print(Fore.RED + "already exists. Droping table and trying again." + Style.RESET_ALL)
                sql = "DROP TABLE vplus"
                mycursor.execute(sql)
                maketable()
            else:
                print(Fore.RED + err.msg + Style.RESET_ALL)
        else:
            print(Fore.GREEN + "OK" + Style.RESET_ALL)
    mycursor.close()

def mainloop():
    try:
        plusconf = open(vplusfile, mode="r")
        Lines = plusconf.readlines()
        print(Fore.GREEN + "Adding Valheim Plus settings to table vplus" + Style.RESET_ALL)
        mycursor = mydb.cursor()
        section1 = "ValheimPlus"
        enabled1 = 0
        option = 0
        setting = 0
        for line in Lines:
            insert = 0
            if(re.search(section, line)):
                section1 = re.search(section, line).group(1)
                insert = 0
                enabled1 = 0
                option = 0
                setting = 0
            if(re.search(settings, line)):
                what = re.search(settings, line).group(1)
                setting = re.search(settings, line).group(2)
                insert = 1
                if what == "enabled":
                    enabled1 = setting
                else:
                    option = what
            if insert == 1:
                sql = """INSERT INTO vplus (section, enabled, option1, settings) VALUES ('%s', '%s', '%s', '%s')""" % (section1, enabled1, option, setting)
                mycursor.execute(sql)
                mydb.commit()
        plusconf.close()
        mycursor.close()
        print(Fore.GREEN + "Done" + Style.RESET_ALL)
    except IOError:
        print(Fore.RED + 'Could not read Valheim Plus config file. Please check config.py' + Style.RESET_ALL)

maketable()
mainloop()
mydb.close()
exit()