-
Notifications
You must be signed in to change notification settings - Fork 6
/
chopro2html.py
94 lines (72 loc) · 2 KB
/
chopro2html.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""chopro2html.py
Convert ChoPro/Chordpro to HTML
Usage:
chopro2html <chopro_file>
Example:
chopro2html twinkle_twinkle_little_star.chopro > twinkle.html
"""
import getopt
import sys
VERSION = '0.2.0'
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def main(argv=None):
if argv is None:
argv = sys.argv
OPTS = {
'str' : 'hvl',
'list' : [
'help',
'version',
'lyrics',
],
}
try:
try:
progname = argv[0]
opts, args = getopt.getopt(argv[1:], OPTS['str'], OPTS['list'])
except getopt.error as msg:
raise Usage(msg)
LYRICS_MODE = False
# process options
for o, a in opts:
if o in ('-h', '--help'):
print(__doc__)
sys.exit(0)
elif o in ('-v', '--version'):
print(VERSION)
sys.exit(0)
elif o in ('-l', '--lyrics'):
LYRICS_MODE = True
else:
raise Usage('Unrecognized option: %s' % o)
if len(args) == 0:
raise Usage('Missing expected input: ChoPro file path')
else:
chopro_file_path = args[0]
f = open(chopro_file_path, 'r')
chopro = f.read()
f.close()
if LYRICS_MODE:
lyrics = chopro2lyrics(chopro)
print(lyrics)
else:
html = chopro2html(chopro)
print(html)
except Usage as err:
print(err.msg, file=sys.stderr)
print('for help use --help', file=sys.stderr)
return 3.14159
def chopro2html(chopro_text):
from chopro.core import ChoPro
chopro = ChoPro(chopro_text)
html = chopro.get_html()
return html
def chopro2lyrics(chopro_text):
from chopro.core import ChoPro
chopro = ChoPro(chopro_text)
lyrics = chopro.get_lyrics()
return lyrics
if __name__ == '__main__':
main()