Skip to content

Instantly share code, notes, and snippets.

@tuokri
Created February 11, 2020 14:51
Show Gist options
  • Save tuokri/22c1bc897513ddce2c26605923f556da to your computer and use it in GitHub Desktop.
Save tuokri/22c1bc897513ddce2c26605923f556da to your computer and use it in GitHub Desktop.
UDK (RS2: Vietnam) Config Parser
import configparser
import os
class MultiValueConfigDict(dict):
def __setitem__(self, key, value):
if key in self and isinstance(value, list):
self[key].extend(value)
else:
super().__setitem__(key, value)
class MultiValueParser(configparser.ConfigParser):
@staticmethod
def getlist(value):
return value.split(os.sep)
def _write_section(self, fp, section_name, section_items, delimiter):
fp.write("[{}]\n".format(section_name))
for key, value in section_items:
value = self._interpolation.before_write(self, section_name, key,
value)
if value is not None or not self._allow_no_value:
if str(value).count("\n") > 1:
# Multi value case.
split = str(value).split("\n")
first = split[0]
rest = split[1:]
first = f"{delimiter}{first}\n{key}{delimiter}"
rest = f"\n{key}{delimiter}".join(rest)
value = first + rest
else:
value = delimiter + str(value).replace("\n", "\n\t")
else:
value = ""
fp.write("{}{}\n".format(key, value))
fp.write("\n")
def main():
cg = MultiValueParser(
strict=False,
dict_type=MultiValueConfigDict,
converters={"list": MultiValueParser.getlist},
comment_prefixes="@",
)
cg.optionxform = str
cg.read("ROEngine.ini")
cg["URL"]["Map"] = "WTF"
with open("ROEngine2.ini", "w") as c:
cg.write(c, space_around_delimiters=False)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment