-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
186 lines (162 loc) · 6.49 KB
/
config.py
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from __future__ import print_function
import os
import utils
import const
"""
The module for contains and for setup/configuration functions.
This module contain file names, directory names and game name.
"""
config_list = [] # list of dictionaries {'key':'string', 'value':'string', 'comment':'string'}
keys_down_list = []
keys_up_list = []
mods_list = []
config_dict = {} # clean config_from_file (no comments, just keys and values), flat dictionary
keys_down_dict = {}
keys_up_dict = {}
mods_paths = [] # list of dictionaries (name, fullpath)
def do_list_from_file(filepath): # returns a list of dicts
# load to list
raw_list = []
file = open(filepath)
for line in file:
raw_list.append(line)
file.close()
# make list with separate comments
dict_list = []
for line in raw_list:
new_line = {}
comment = None
key = None
value = None
divided_line = line.split(const.CONFIG_SEPS[1], 1)
if len(divided_line) > 1:
comment = divided_line[1].strip() # this is comment
if len(divided_line[0].split(const.CONFIG_SEPS[0], 1)) > 1:
key = divided_line[0].split(const.CONFIG_SEPS[0], 1)[0].strip()
value = divided_line[0].split(const.CONFIG_SEPS[0], 1)[1].strip()
else:
if len(divided_line[0].split(const.CONFIG_SEPS[0], 1)) > 1:
key = divided_line[0].split(const.CONFIG_SEPS[0], 1)[0].strip()
value = divided_line[0].split(const.CONFIG_SEPS[0], 1)[1].strip()
if key is not None:
new_line.update({'key': key})
if value is not None:
new_line.update({'value': value})
if comment is not None:
new_line.update({'comment': comment})
dict_list.append(new_line)
return dict_list
def do_file_from_list(filepath, list): # returns None
print('bede robic plik')
lines_to_write = []
for line in list:
print(line)
line_to_write = ''
if 'key' in line:
line_to_write += (line['key'] + const.CONFIG_SEPS[0] + ' ')
if 'value' in line:
line_to_write += line['value']
if 'comment' in line:
if 'key' in line:
line_to_write += (' ' + const.CONFIG_SEPS[1] + ' ' + line['comment'])
else:
line_to_write += (const.CONFIG_SEPS[1] + ' ' + line['comment'])
line_to_write += os.linesep
lines_to_write.append(line_to_write)
file = open(filepath, 'w')
file.writelines(lines_to_write)
file.close()
def do_dict_from_list(list): # returns dictionary
dictionary = {}
for d in list:
if 'key' in d:
dictionary.update({d['key']: d['value']})
# convert to ints and tuples
for key in dictionary:
if dictionary[key].isdigit():
dictionary[key] = int(dictionary[key])
elif ',' in dictionary[key]:
dictionary[key] = map(int, dictionary[key].split(','))
elif '.' in dictionary[key]:
dictionary[key] = float(dictionary[key])
return dictionary
def upgrade_dict_from_list(dict, list): # returns None
for d in list:
if 'key' in d:
dict.update({d['key']: d['value']})
# convert to ints and tuples
for key in dict:
if dict[key].isdigit():
dict[key] = int(dict[key])
elif ',' in dict[key]:
dict[key] = map(int, dict[key].split(','))
def upgrade_list_from_dict(list, dict): # returns None
for key, value in dict.items():
for line in list:
if 'key' in line:
if key == line['key']:
if ',' in str(value):
line['value'] = str(value)[1:-1]
else:
line['value'] = str(value)
def do_mods(_): # returns list of paths
mods = []
mods.append({'name': const.VANILLA, 'dir': 'all'}) # sneaky vanilla game adding
for line in _:
if 'key' in line:
mods.append({'name': line['key'], 'dir': line['value']})
# now make list of fullpaths
paths = []
for line in mods:
path = const.MODS + os.sep + line['name']
if line['dir'] != 'all':
for name in const.DIR_NAMES:
if name == line['dir']:
# print( SUB_DIR_TREE[DIR_NAMES[name].upper()] )
path += os.sep + const.SUB_DIR_TREE[const.DIR_NAMES[name].upper()]
paths.append(path)
print(paths)
# now make list of all fullpaths!
all_paths = []
for path in paths:
for tup in os.walk(path):
all_paths.append(tup[0])
print(all_paths)
return all_paths
def setup(): # returns None
# create directory if needed
for dir in const.BASIC_DIR_TREE:
if utils.check_if_directory_exists(dir) == False:
utils.create_directory(dir)
# create files if needed
if utils.check_if_file_exists(const.CONFIG_FILE) == False:
# utils.create_file(const.CONFIG_FILE)
do_file_from_list(const.CONFIG_FILE, const.DEFAULT_CONFIG_FILE)
if utils.check_if_file_exists(const.KEYS_DOWN_FILE) == False:
utils.create_file(const.KEYS_DOWN_FILE)
do_file_from_list(const.KEYS_DOWN_FILE, const.DEFAULT_KEYS_DOWN_FILE)
if utils.check_if_file_exists(const.KEYS_UP_FILE) == False:
utils.create_file(const.KEYS_UP_FILE)
do_file_from_list(const.KEYS_UP_FILE, const.DEFAULT_KEYS_UP_FILE)
if utils.check_if_file_exists(const.MODS_FILE) == False:
utils.create_file(const.MODS_FILE)
do_file_from_list(const.MODS_FILE, const.DEFAULT_MODS_FILE)
def load_config():
global config_list, keys_down_list, keys_up_list, mods_list, config_dict, keys_down_dict,keys_up_dict, mods_paths
config_list = do_list_from_file(const.CONFIG_FILE)
keys_down_list = do_list_from_file(const.KEYS_DOWN_FILE)
keys_up_list = do_list_from_file(const.KEYS_UP_FILE)
mods_list = do_list_from_file(const.MODS_FILE)
config_dict = do_dict_from_list(config_list)
keys_down_dict = do_dict_from_list(keys_down_list)
keys_up_dict = do_dict_from_list(keys_up_list)
mods_paths = do_mods(mods_list)
def save_config():
upgrade_list_from_dict(config_list, config_dict)
upgrade_list_from_dict(keys_down_list, keys_down_dict)
upgrade_list_from_dict(keys_up_list, keys_up_dict)
do_file_from_list(const.CONFIG_FILE, config_list)
do_file_from_list(const.KEYS_DOWN_FILE, keys_down_list)
do_file_from_list(const.KEYS_UP_FILE, keys_up_list)
if __name__ == '__main__':
setup()