3 # convert-mudela.py -- convertor for mudela versions
5 # source file of the GNU LilyPond music typesetter
10 # use -f and -t for -s output
16 program_name
= 'convert-mudela'
29 mudela_version_re_str
='\\\\version *\"\(.*\)\"'
30 mudela_version_re
= regex
.compile(mudela_version_re_str
)
33 return '%s version %s' %(program_name
, version
);
36 sys
.stderr
.write (program_id () + '\n')
45 print 'can\'t open file: ' + f
+ '\n'
49 print 'gulped empty file: ' + f
+ '\n'
55 return tuple (map (atoi
, split (s
,'.')))
58 return join (map (lambda x
: '%s' % x
, list (t
)), '.')
60 def version_cmp (t1
, t2
):
67 def guess_mudela_version(filename
):
68 s
= gulp_file (filename
)
69 if mudela_version_re
.search(s
) <> -1:
70 return mudela_version_re
.group(1)
76 ("Usage: %s [OPTION]... [FILE]...\n"
77 + "Try to convert to newer mudela-versions\n"
79 + " -h, --help print this help\n"
80 + ' -e, --edit in place edit\n'
81 + ' -f, --from=VERSION start from version\n'
82 + ' -s, --show-rules print out all rules.\n'
83 + ' -t, --to=VERSION target version\n') % program_name
)
86 class FatalConversionError
:
91 def show_rules (file):
93 file.write ('%s: %s\n' % (tup_to_str (x
[0]), x
[2]))
95 ############################
97 if 1: # need new a namespace
101 if regex
.search ('\\\\octave', x
) <> -1:
105 sys
.stderr
.write ('\nNot smart enough to convert \\octave\n')
106 raise FatalConversionError()
111 ((0,1,19), conv
, 'deprecated \\octave; can\'t convert automatically'))
114 if 1: # need new a namespace
118 x
= regsub
.gsub ('\\\\textstyle\\([^;]+\\);',
119 '\\\\property Lyrics . textstyle = \\1', x
)
120 x
= regsub
.gsub ('\\\\key\\([^;]+\\);', '\\\\accidentals \\1;', x
)
126 ((0,1,20), conv
, 'deprecated \\textstyle, new \key syntax'))
129 if 1: # need new a namespace
133 x
= regsub
.gsub ('\\\\musical_pitch',
134 '\\\\musicalpitch',x
)
135 x
= regsub
.gsub ('\\\\meter',
142 ((0,1,21), conv
, '\\musical_pitch -> \\musicalpitch, '+
143 '\\meter -> \\time\n'))
145 if 1: # need new a namespace
150 ((1,0,0), conv
, '0.1.21 -> 1.0.0 '))
153 if 1: # need new a namespace
157 x
= regsub
.gsub ('\\\\accidentals',
158 '\\\\keysignature',x
)
159 x
= regsub
.gsub ('specialaccidentals *= *1',
160 'keyoctaviation = 0',x
)
161 x
= regsub
.gsub ('specialaccidentals *= *0',
162 'keyoctaviation = 1',x
)
168 ((1,0,1), conv
, '\\accidentals -> \\keysignature, ' +
169 'specialaccidentals -> keyoctaviation\n'))
175 if regex
.search ('\\\\header', x
) <> -1:
179 sys
.stderr
.write ('\nNot smart enough to convert to new \\header format\n')
182 conversions
.append ((1,0,2), conv
, '\header { key = concat + with + operator }\n')
184 ############################
186 def get_conversions (from_version
, to_version
):
187 def version_b (v
, f
= from_version
, t
= to_version
):
188 return version_cmp (v
[0], f
) > 0 and version_cmp (v
[0], t
) <= 0
189 return filter (version_b
, conversions
)
192 def latest_version ():
193 return conversions
[-1][0]
195 def do_conversion (infile
, from_version
, outfile
, to_version
):
197 conv_list
= get_conversions (from_version
, to_version
)
199 sys
.stderr
.write ('Applying conversions: ')
200 lines
= infile
.readlines();
204 sys
.stderr
.write (tup_to_str (x
[0]) + ', ')
206 last_conversion
= x
[0]
208 sys
.stderr
.write ('\n')
210 except FatalConversionError
:
211 sys
.stderr
.write ('Error while converting; I won\'t convert any further')
215 x
= regsub
.sub (mudela_version_re_str
, '\\version \"%s\"' % tup_to_str (last_conversion
), x
)
218 class UnknownVersion
:
221 def do_one_file (infile_name
):
224 outfile_name
= infile_name
+ '.NEW'
225 elif __main__
.outfile_name
:
226 outfile_name
= __main__
.outfile_name
228 if __main__
.from_version
:
229 from_version
= __main__
.from_version
231 guess
= guess_mudela_version (infile_name
)
233 raise UnknownVersion()
234 from_version
= str_to_tuple (guess
)
236 if __main__
.to_version
:
237 to_version
= __main__
.to_version
239 to_version
= latest_version ()
243 infile
= open (infile_name
,'r')
248 outfile
= open (outfile_name
, 'w')
253 do_conversion (infile
, from_version
, outfile
, to_version
)
256 os
.rename (infile_name
, infile_name
+ '~')
257 os
.rename (infile_name
+ '.NEW', infile_name
)
266 (options
, files
) = getopt
.getopt (
267 sys
.argv
[1:], 'f:t:seh', ['show-rules', 'help', 'edit', 'from', 'to'])
272 if o
== '--help' or o
== '-h':
274 elif o
== '--from' or o
=='-f':
275 from_version
= str_to_tuple (a
)
276 elif o
== '--to' or o
=='-t':
277 to_version
= str_to_tuple (a
)
278 elif o
== '--edit' or o
== '-e':
280 elif o
== '--show-rules' or o
== '-s':
281 show_rules (sys
.stdout
)
293 except UnknownVersion
: