-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_ultimate.py
executable file
·74 lines (66 loc) · 2.56 KB
/
convert_ultimate.py
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
#!/usr/bin/env python3
"""
Ultimate Converter - Converts tabs from a popular tab website to an intermediate format parseable by `chordpro a2crd`.
Usage:
convert_ultimate.py <path_to_song>
The result is printed to stdout.
"""
import sys
import re
import os
if __name__ == "__main__":
verse_count = 1
last_section = None
re_section_header = re.compile('^\[([^\]]*)\]$')
re_numbered = re.compile('^(.*) [0-9]*$')
re_repeat = re.compile('^(.*) x([0-9]{1,2})$')
re_repeat2 = re.compile('^(.*) ([0-9]{1,2})x$')
with open(sys.argv[1], 'r') as file:
lines = [i.rstrip('\n') for i in file.readlines()]
(song, artist) = os.path.basename(sys.argv[1])[:-4].split(" - ")
print("{t: %s}" % (song))
print("{artist: %s}" % (artist))
unskip_empty_line = True
was_empty_line = False
for i in range(0, len(lines)):
line = lines[i].rstrip()
match = re_section_header.match(line)
if was_empty_line and unskip_empty_line and not match:
unskip_empty_line = False
print("")
if len(line) == 0:
# this line is empty
was_empty_line = True
continue
else:
was_empty_line = False
unskip_empty_line = True
if match:
# this line is a section header
unskip_empty_line = False
if last_section:
print('{end_of_%s}\n' % (last_section.replace('-', '').replace(' ', '_')))
match_numbered = re_numbered.match(match.group(1))
if match_numbered:
last_section = match_numbered.group(1).lower()
else:
last_section = match.group(1).lower()
print('{start_of_%s: %s}' % (last_section.replace('-', '').replace(' ', '_'), "%s %d" % (last_section.capitalize(), verse_count) if last_section == 'verse' else last_section.capitalize()))
if last_section == "verse":
verse_count = verse_count + 1
else:
# this line is a normal line
match = re_repeat.match(line)
if match:
print(match[1].rstrip())
print(f'{{c: x{match[2]}}}')
else:
match = re_repeat2.match(line)
if match:
print(match[1].rstrip())
print(f'{{c: x{match[2]}}}')
else:
if not (len(line) == 0 and i < (len(lines) - 1) and lines[i+1].startswith("[")):
print(line)
if last_section:
print('{end_of_%s}' % (last_section))