forked from HotPocketRemix/DarkSoulsItemRandomizer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathini_parser.py
More file actions
37 lines (31 loc) · 1.37 KB
/
ini_parser.py
File metadata and controls
37 lines (31 loc) · 1.37 KB
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
import configparser
def get_values_from_ini(ini_file, section="DEFAULT"):
config = configparser.ConfigParser()
config.read(ini_file)
if section in config: #check if section in ini
return config[section]
else:
return config["DEFAULT"]
def save_ini(ini_file, options):
config = configparser.ConfigParser()
for k,v in vars(options).items():
config["DEFAULT"][k] = str(v) #values must be strings to write
with open(ini_file, 'w') as configfile:
config.write(configfile)
def get_option_value(config, k):
"""
The following parses the values in the ini to produce the option list in the correct format
While I could create a separate list/dict to specify the typing of the data coming in
or by creating a section in the inifile for the typing
This would add extra work to the developer who adds new features which I think is far more annoying
So instead we get this ugly mess of try/excepts to get the typing we actually care about: int -> bool -> string
TODO: Allow type to be specified when calling (not really important at all)
"""
try:
option = config.getint(k, 0)
except ValueError:
try:
option = config.getboolean(k, False)
except ValueError:
option = config.get(k, "No Default Value")
return option