Created
July 28, 2023 06:14
-
-
Save mouseos/4b40fe7a0ec31caf78ee8ea872b7031b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pprint | |
import re | |
def optimod_to_dict(input_string): | |
lines = input_string.strip().split('\n') | |
data_dict = {} | |
current_key = None | |
for line in lines: | |
if line.startswith('OptimodVersion'): | |
data_dict['OptimodVersion'] = line.split( | |
'=')[1].strip().replace("<", "").replace(">", "") | |
elif line.startswith('Preset Name'): | |
preset_info = line.split(' ') | |
data_dict['Preset Name'] = { | |
'Name': preset_info[1].strip().replace("<", "").replace(">", "").replace("Name=", ""), | |
'size': preset_info[2].strip().replace("size=", ""), | |
} | |
elif line.startswith('Factory Preset Name'): | |
data_dict['Factory Preset Name'] = line.strip().replace( | |
"<", "").replace(">", "").replace("Factory Preset Name=", "") | |
elif line.startswith('Preset Type'): | |
data_dict['Preset Type'] = line.split( | |
'=')[1].strip().replace("<", "").replace(">", "") | |
elif line.startswith('Preset File Name'): | |
data_dict['Preset File Name'] = line.split( | |
'=')[1].strip().replace("<", "").replace(">", "") | |
elif 'String' in line: | |
key_name = line.split(":")[1].strip().replace( | |
"<", "").replace(">", "").replace("String", "") | |
if ("RATIO" in key_name): | |
line = line.split("<") | |
line2 = line[2].split(";") | |
data_dict[key_name] = {'value1': line2[0].replace(">", ""), | |
'value2': (line2[1].replace("D:", "").replace(";", "")), | |
'type': 'String', | |
} | |
else: | |
line = line.split(":") | |
data_dict[key_name] = {'value1': re.sub("<|>.*", "", line[2]), | |
'value2': line[3].replace(";", ""), | |
'type': 'String', | |
} | |
elif 'Int' in line: | |
line = line.split(":") | |
key_name = line[1].strip().replace( | |
"<", "").replace(">", "").replace("Int", "") | |
data_dict[key_name] = {'value1': int(re.sub("<|>.*|;D|;", "", line[2])), | |
'value2': int(line[3].replace(";", "")), | |
'type': 'Int', | |
} | |
elif 'Cent' in line: | |
line = line.split(":") | |
key_name = line[1].strip().replace( | |
"<", "").replace(">", "").replace("Cent", "") | |
data_dict[key_name] = {'value1': int(re.sub("<|>.*|;D|;", "", line[2])), | |
'value2': int(line[3].replace(";", "")), | |
'type': 'Cent', | |
} | |
return data_dict | |
def dict_to_optimod(data_dict): | |
lines = [] | |
# Adding the fixed header lines | |
lines.append(f"OptimodVersion=<{data_dict['OptimodVersion']}>") | |
lines.append( | |
f"Preset Name=<{data_dict['Preset Name']['Name']}> size={data_dict['Preset Name']['size']}") | |
lines.append(f"Factory Preset Name=<{data_dict['Factory Preset Name']}>") | |
lines.append(f"Preset Type= <{data_dict['Preset Type']}>") | |
lines.append(f"Preset File Name= <{data_dict['Preset File Name']}>") | |
# Adding the parameter lines from the dictionary | |
for key, value in data_dict.items(): | |
if key not in ['OptimodVersion', 'Preset Name', 'Factory Preset Name', 'Preset Type', 'Preset File Name']: | |
if value['type'] == 'String': | |
if 'RATIO' in key: | |
line = f"C:<{key}>{value['type']}:<{value['value1']}>;D:{value['value2']};" | |
else: | |
line = f"C:<{key}>{value['type']}:<{value['value1']}>;D:{value['value2']};" | |
else: | |
line = f"C:<{key}>{value['type']}:{value['value1']};D:{value['value2']};" | |
lines.append(line) | |
lines.append("End Preset<end>\n") | |
return '\n'.join(lines) | |
# ファイルからデータを読み込む | |
def read_data_from_file(filename): | |
with open(filename, 'r') as file: | |
data_string = file.read() | |
return data_string | |
# データをファイルに書き込む | |
def write_data_to_file(filename, data_string): | |
with open(filename, 'w') as file: | |
file.write(data_string) | |
# ファイルからデータを読み込んで辞書型にパース | |
data_string = read_data_from_file('ROCK-SMOOTH.orbf') | |
# | |
data_dict = optimod_to_dict(data_string) | |
print((data_dict)) | |
print(dict_to_optimod(data_dict)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment