import re
OLDFILE = r"C:\Users\evil\Documents\Phonexies\Saves\Saves17.phxsav"
NEWFILE = r"C:\Users\evil\Documents\Phonexies\Saves\Saves18.phxsav"
OUTPUTFILENAME = r"C:\Users\evil\Documents\Phonexies\Saves\Saves123.phxsav"
def match_event_names(text_data):
pattern = r"^\[([^\]]+)\]"
matches = re.findall(pattern, text_data)
for m in matches:
return m
return None
def stateful_parse(filename):
events_dict = {}
with open(filename) as f:
current_event = None
for line in f.readlines():
if len(line.strip()) == 0:
continue
event_name = match_event_names(line)
if event_name is not None:
current_event = {}
events_dict[event_name] = current_event
else:
lineparts = line.split('=')
current_event[lineparts[0]] = lineparts[1]
return events_dict
def update_newfile_with_oldvalues(oldevents, newevents, ported_events, outputfilename):
with open(outputfilename, 'w') as f:
for event_name, event_data in newevents.items():
f.write(f"[{event_name}]\n\n")
if event_name in ported_events:
oldevent = oldevents[event_name]
for data_key, data_value in event_data.items():
tweaked_value = oldevent.get(data_key, data_value)
f.write(f"{data_key}={tweaked_value}")
f.write('\n')
def main():
oldfile_events = stateful_parse(OLDFILE)
newfile_events = stateful_parse(NEWFILE)
ported_events = set([x for x in newfile_events.keys()]).intersection(oldfile_events.keys())
update_newfile_with_oldvalues(oldfile_events, newfile_events, ported_events, OUTPUTFILENAME)
if __name__ == '__main__':
main()