-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfigIni.py
96 lines (84 loc) · 3.18 KB
/
configIni.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
""" docstring """
from configparser import ConfigParser
import os
CONFIG_FILE_NAME = 'headlightControls.ini'
SECTIONS = [
'Toggle headlights',
'Flash headlights',
'Headlights on',
'Headlights off',
'rFactor Toggle',
]
MISC_VALUES = {
'pit_limiter': '1', # 1: flash headlights when pit limiter on
'pit_lane': '1', # 1: flash headlights when in pit lane
'flash_on_time': '20', # Overtake lane flash on time
'flash_off_time': '20', # Overtake lane flash off time
'pit_flash_on_time': '200', # Pit lane flash on time
'pit_flash_off_time': '200', # Pit lane flash off time
'default_to_on': '0', # Headlights on normally, driver can turn them off
'on_automatically': '0', # Headlights on when:
# 0 Driver turns them on
# 1 At least one other driver has them on
# 2 More than one other driver has them on
# 3 At least half of the other drivers have them on
# 4 All the other drivers have them on
'controller_file': '%ProgramFiles(x86)%/Steam/steamapps/common/rFactor 2/Userdata/player/controller.json',
'flash_count': '4'
}
class Config:
""" docstring """
def __init__(self):
# instantiate
self.config = ConfigParser(interpolation=None)
# set default values
for _section in SECTIONS:
self.set(_section, 'Controller', 'Not yet selected')
self.set(_section, 'Control', '0')
self.set('rFactor Toggle', 'Controller', 'keyboard')
self.set('rFactor Toggle', 'Control', 'DIK_H')
for _val, default in MISC_VALUES.items():
self.set('miscellaneous', _val, default)
# if there is an existing file parse values over those
if os.path.isfile(CONFIG_FILE_NAME):
self.config.read(CONFIG_FILE_NAME)
else:
self.write()
self.config.read(CONFIG_FILE_NAME)
def set(self, _section, _val, _value):
""" update existing value """
if not self.config.has_section(_section):
self.config.add_section(_section)
self.config.set(_section, _val, _value)
def get(self, _section, _val):
""" get a config value """
try:
# get existing value
return self.config.get(_section, _val)
except BaseException: # pylint: disable=bare-except
# No such section in file
self.set(_section, _val, '')
return None
def get_controller_file(self):
# Special case - expand the path
return os.path.normpath(
os.path.expandvars(
self.config.get('miscellaneous', 'controller_file')))
def write(self):
""" save to a file """
with open(CONFIG_FILE_NAME, 'w') as configfile:
self.config.write(configfile)
if __name__ == "__main__":
# Test
_CONFIG_O = Config()
SECTION = 'headlight controls'
VAL = 'controller'
VAL = _CONFIG_O.get(SECTION, VAL)
if VAL:
if VAL == 'controller':
print('%s/%s: %s' % (SECTION, VAL, VAL))
else:
print('%s/%s: %d' % (SECTION, VAL, VAL))
else:
print('%s/%s: <None>' % (SECTION, VAL))
_CONFIG_O.write()