-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnrealConfig.py
150 lines (123 loc) · 4.35 KB
/
UnrealConfig.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
"""
UEBuild Tools - Version Information Updater for Unreal Engine
Copyright (C) 2024 IT-Hock
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
# Loads a INI File from unreal
import logging
import re
class UnrealConfig:
"""
A class used to handle Unreal Engine configuration files.
...
Attributes
----------
path : str
the path to the configuration file
config : dict
the configuration data loaded from the file
Methods
-------
load():
Loads the configuration data from the file.
get(section=None, key=None):
Returns the value for a given key in a given section. If no key is provided, returns the entire section. If no section is provided, searches all sections for the key.
set(section, key, value):
Sets the value for a given key in a given section.
save():
Saves the current configuration data back to the file.
"""
def __init__(self, path):
"""
Constructs a new UnrealConfig object.
Parameters
----------
path : str
The path to the configuration file.
"""
self.path = path
self.config = self.load()
def load(self):
"""
Loads the configuration data from the file.
Returns
-------
dict
The configuration data.
"""
with open(self.path, 'r') as f:
lines = f.readlines()
config = {}
for line in lines:
if line == '\n' or line == '' or line == '\r\n':
continue
line = line.strip()
# Regex match key=value
if re.match(r'^.*?=.*?$', line):
key, value = line.split('=', 1)
config[section][key] = value
# Regex match [section]
elif re.match(r'^\[.*]$', line):
section = line[1:-1]
config[section] = {}
else:
logging.error(f"Line: {line}")
raise Exception("Invalid INI File")
return config
def get(self, section=None, key=None):
"""
Returns the value for a given key in a given section. If no key is provided, returns the entire section. If no section is provided, searches all sections for the key.
Parameters
----------
section : str, optional
The section to search in.
key : str, optional
The key to search for.
Returns
-------
str or dict
The value for the key, or the entire section.
"""
if key is None:
return self.config[section]
if section is None:
for section in self.config:
if key in self.config[section]:
return self.config[section][key]
return self.config[section][key]
def set(self, section, key, value):
"""
Sets the value for a given key in a given section.
Parameters
----------
section : str
The section to set the value in.
key : str
The key to set the value for.
value : str
The value to set.
"""
self.config[section][key] = value
def save(self):
"""
Saves the current configuration data back to the file.
"""
with open(self.path, 'w') as f:
for section in self.config:
f.write(f'[{section}]\n')
for key, value in self.config[section].items():
f.write(f'{key}={value}\n')
f.write('\n')
def __str__(self):
return str(self.config)
def __repr__(self):
return str(self.config)