-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreadJSONfile.py
50 lines (42 loc) · 1.48 KB
/
readJSONfile.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
import json
def read_file(filepath):
try:
with open(filepath, 'r') as f:
return f.read()
except FileNotFoundError:
pass
return None # File not found
class Json:
dict = dict()
def __init__(self, filepath):
self.json_str = read_file(filepath)
if self.json_str:
try:
self.dict = json.loads(self.json_str)
except json.JSONDecodeError:
pass
def get_item(self, item_name):
for __section, items in self.dict.items():
if item_name in items:
value = items[item_name]
return value
return None
if __name__ == "__main__":
# Test
from configIni import Config
from pyDirectInputKeySend.directInputKeySend import rfKeycodeToDIK
_CONFIG_O = Config()
_controller_file = _CONFIG_O.get_controller_file()
_JSON_O = Json(_controller_file)
headlight_control = _JSON_O.get_item("Control - Headlights")
if headlight_control:
print(F'headlight_control: {headlight_control}')
keycode = rfKeycodeToDIK(headlight_control[1])
rf2headlights_keycode = _CONFIG_O.get('rFactor Toggle', 'Control')
if keycode == rf2headlights_keycode:
print(F'Match: {keycode}')
else:
print(
F"Doesn't match: rF2 keycode {keycode} : rf2headlights keycode {rf2headlights_keycode}")
else:
print(F'"Control - Headlights" not in {_controller_file}')