Here's the code to merge mod files in Python. It merges non-conflicting sequences as proven by the previously published can_merge_seq function.
Neither of the functions can handle two mods adding the same content yet. Also, this still needs to be hooked up to PeridexisErrant's code.
import difflib
def do_merge_seq (mod_text, vanilla_text, gen_text):
if vanilla_text == gen_text: #this should happen often
return mod_text
van_mod_match = difflib.SequenceMatcher(None, vanilla_text, mod_text)
van_gen_match = difflib.SequenceMatcher(None, vanilla_text, gen_text)
van_mod_ops = van_mod_match.get_opcodes()
van_gen_ops = van_gen_match.get_opcodes()
output_file_temp = []
cur_v = 0
while cur_v < len(vanilla_text) :
(mod_tag, mod_i1, mod_i2, mod_j1, mod_j2) = van_mod_ops[0]
(gen_tag, gen_i1, gen_i2, gen_j1, gen_j2) = van_gen_ops[0]
#print van_mod_ops[0]
#print van_gen_ops[0]
#print cur_v
if mod_tag == 'equal' and gen_tag == 'equal' :
if mod_i2 < gen_i2:
output_file_temp += vanilla_text[cur_v:mod_i2]
cur_v = mod_i2
van_mod_ops.pop(0)
else:
output_file_temp += vanilla_text[cur_v:gen_i2]
cur_v = gen_i2
van_gen_ops.pop(0)
if mod_i2 == gen_i2 :
van_mod_ops.pop(0)
else:
if mod_tag != 'equal' :
output_file_temp += mod_text[mod_j1:mod_j2]
cur_v = mod_i2
van_mod_ops.pop(0)
if mod_i2 == gen_i2 :
van_gen_ops.pop(0)
elif gen_tag!='equal':
output_file_temp += gen_text[gen_j1:gen_j2]
cur_v = gen_i2
van_gen_ops.pop(0)
if mod_i2 == gen_i2 :
van_mod_ops.pop(0)
#if neither gen_tag nor mod_tag is 'equal', this mod can't be merged
#print (output_file_temp)
if van_mod_ops:
(mod_tag, mod_i1, mod_i2, mod_j1, mod_j2) = van_mod_ops[0]
output_file_temp += mod_text[mod_j1:mod_j2]
if van_gen_ops:
(gen_tag, gen_i1, gen_i2, gen_j1, gen_j2) = van_gen_ops[0]
output_file_temp += gen_text[gen_j1:gen_j2]
return output_file_temp
vanilla_file = "vanilla"
output_file = "vanilla"
print ("----")
print output_file
output_file = do_merge_seq ('anything at all', vanilla_file, output_file)
print output_file
print ("----")
output_file = vanilla_file
print output_file
output_file = do_merge_seq ('nilla', vanilla_file, output_file)
print output_file
output_file = do_merge_seq ('vanill', vanilla_file, output_file)
print ''.join(output_file)
print ("----")
output_file = vanilla_file
print output_file
output_file = do_merge_seq ('vonilla', vanilla_file, output_file)
print output_file
output_file = do_merge_seq ('banana', vanilla_file, output_file)
print ''.join(output_file)
print ("----")
output_file = vanilla_file
print output_file
output_file = do_merge_seq ('banana', vanilla_file, output_file)
print output_file
output_file = do_merge_seq ('vonilla', vanilla_file, output_file)
print ''.join(output_file)
print ("----")
output_file = vanilla_file
print output_file
output_file = do_merge_seq ('banana', vanilla_file, output_file)
print output_file
output_file = do_merge_seq ('vanilla', vanilla_file, output_file)
print ''.join(output_file)