-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmscorepluginconverter.py
More file actions
executable file
·91 lines (75 loc) · 2.98 KB
/
mscorepluginconverter.py
File metadata and controls
executable file
·91 lines (75 loc) · 2.98 KB
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
#!/usr/bin/env python3
import sys
import argparse
def get_line_ending(line):
end = '\n'
if line.endswith('\r\n'):
end = '\r\n'
return end
def replace_imports(line):
tokens = line.strip().split()
if len(tokens) < 3:
return line
if tokens[0] == "import":
if tokens[1] in ["MuseScore", "FileIO"] and tokens[2] == "1.0":
tokens[2] = "3.0"
return ' '.join(tokens) + get_line_ending(line)
return line
def replace_props(line):
replacement_list = {
".pos.x ": ".offsetX ",
".pos.x=": ".offsetX=",
".pos.y ": ".offsetY ",
".pos.y=": ".offsetY=",
".setSig(": ".timesig = fraction(",
}
for orig, target in replacement_list.items():
line = line.replace(orig, target)
return line
def replace_enums(line):
val_replace = [
{ "orig": "Element", "target": "Placement", "values": ["ABOVE", "BELOW"] },
{ "orig": "MScore", "target": "Direction", "values": ["UP", "DOWN"] },
{ "orig": "MScore", "target": "DirectionH", "values": ["LEFT", "RIGHT"] },
{ "orig": "MScore", "target": "OrnamentStyle", "values": ["DEFAULT", "BAROQUE"] },
{ "orig": "MScore", "target": "GlissandoStyle", "values": ["CHROMATIC", "WHITE_KEYS", "BLACK_KEYS", "DIATONIC"] },
{ "orig": "NoteHead", "target": "NoteHeadType", "values": ["HEAD_AUTO", "HEAD_WHOLE", "HEAD_HALF", "HEAD_QUARTER", "HEAD_BREVIS", "HEAD_TYPES"] },
{ "orig": "Note", "target": "NoteValueType", "values": ["OFFSET_VAL", "USER_VAL"] },
]
# applied after val_replace
any_replace = [
{ "orig": "TextStyleType", "target": "Tid" },
{ "orig": "NoteHead", "target": "NoteHeadGroup" },
]
ambiguous = ["MScore.AUTO"]
def replace_enum_val(line, orig_name, target_name, val=""):
orig_str = orig_name + '.' + val
target_str = target_name + '.' + val
return line.replace(orig_str, target_str)
for item in val_replace:
orig = item["orig"]
target = item["target"]
for val in item["values"]:
line = replace_enum_val(line, orig, target, val)
for item in any_replace:
line = replace_enum_val(line, item["orig"], item["target"])
for s in ambiguous:
if s in line:
print("Ambiguous replacement:", s)
return line
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Converts MuseScore 2 plugins for usage with MuseScore 3")
parser.add_argument("source", help="Source plugin .qml file")
parser.add_argument("destination", help="Destination .qml file")
args = parser.parse_args()
with open(args.source, newline='') as src:
script = list(src)
if not script:
print("Empty script")
sys.exit(1)
script = map(replace_imports, script)
script = map(replace_props, script)
script = map(replace_enums, script)
with open(args.destination, 'w') as out:
for line in script:
out.write(line)