-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileRoutines.py
More file actions
123 lines (100 loc) · 4.09 KB
/
Copy pathFileRoutines.py
File metadata and controls
123 lines (100 loc) · 4.09 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
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
import configparser
from io import StringIO
import GlobalVars
from GlobalVars import Settings
import inspect
import re
def findreplaceline(Find,value,lines):
for i, line in enumerate(lines):
# Check if the line starts with "OutSim Opts"
if line.startswith(str(Find)):
# Update the line
lines[i] = str(str(Find)+" "+str(value))
# Exit the loop
break
from configparser import ConfigParser
class CaseSensitiveConfigParser(ConfigParser):
def optionxform(self, optionstr):
return optionstr
def readconfig(filename):
default_config = {}
for class_name, class_obj in vars(Settings).items():
if not class_name.startswith("__"):
default_config[class_name] = {}
# Iterate through the variables in each nested class
for variable_name, variable_value in vars(class_obj).items():
if not variable_name.startswith("__"):
default_config[class_name][variable_name] = variable_value
print(default_config)
config = CaseSensitiveConfigParser()
config_file = 'configs/' + filename
try:
config.read(config_file)
except configparser.Error as e:
GlobalVars.InternalVars.InternalConfigFileError = f'Error reading config file: {e}'
for section, options in default_config.items():
if not config.has_section(section):
config.add_section(section)
for option, value in options.items():
if not config.has_option(section, option):
config.set(section, option, str(value))
config_string = StringIO()
config.write(config_string)
config_str = config_string.getvalue()
print(config_str)
#options = config.items('Settings')
for section in config.sections():
for option, value in config.items(section):
if section in vars(Settings):
try:
setattr(vars(Settings)[section], option, float(value))
except ValueError:
setattr(vars(Settings)[section], option, value)
#GlobalVars.Settings.__dict__[option]=value
#GlobalVars.Settings.LFSSteerAngle = 77
# print(GlobalVars.Settings.LFSSteerAngle)
def writeconfig(filename):
#global config
config_file = 'configs/' + filename
config = CaseSensitiveConfigParser()
default_config = {}
for class_name, class_obj in vars(Settings).items():
if not class_name.startswith("__"):
default_config[class_name] = {}
# Iterate through the variables in each nested class
for variable_name, variable_value in vars(class_obj).items():
if not variable_name.startswith("__"):
default_config[class_name][variable_name] = variable_value
#print(dir(Settings))
# print("222",default_config)
for section, options in default_config.items():
if not config.has_section(section):
config.add_section(section)
for option, value in options.items():
config.set(section, option, str(value))
#print(option, " ", str(value))
config_string = StringIO()
config.write(config_string)
config_str = config_string.getvalue()
print(config_str)
with open(config_file, 'w') as f:
config.write(f)
def patchLFScfg():
try:
with open(Settings.Main.LFS_cfg_location, "r") as file:
# Read the contents of the file into memory and split by lines
lines = file.read().split("\n")
findreplaceline("OutSim Mode",2,lines)
findreplaceline("OutSim Delay", 0, lines)
findreplaceline("OutSim IP", "127.0.0.1", lines)
findreplaceline("OutSim Port", 30000, lines)
findreplaceline("OutSim ID", 0, lines)
findreplaceline("OutSim Opts", 80, lines)
# Rejoin the lines
contents = "\n".join(lines)
# Open the file for writing
with open(Settings.Main.LFS_cfg_location, "w") as file:
# Write the new contents to the file
file.write(contents)
except Exception:
print("LFS cfg not patched!")