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'
17 version = '@TOPLEVEL_VERSION@'
27 mudela_version_re_str = '\\\\version *\"(.*)\"'
28 mudela_version_re = re.compile(mudela_version_re_str)
31 return '%s (GNU LilyPond) %s' %(program_name, version);
34 sys.stderr.write (program_id () + '\n')
38 r"""Usage: %s [OPTION]... [FILE]...
39 Try to convert to newer mudela-versions. The version number of the
40 input is guessed by default from \version directive
43 -h, --help print this help
44 -e, --edit in place edit
45 -f, --from=VERSION start from version
46 -s, --show-rules print all rules.
47 -t, --to=VERSION target version
48 --version print program version
50 Report bugs to bugs-gnu-music@gnu.org
59 sys.stdout.write (r"""%s
61 This is free software. It is covered by the GNU General Public
62 License, and you are welcome to change it and/or distribute copies of
63 it under certain conditions. invoke as `%s --warranty' for more
66 """ % (program_id() , program_name))
75 print 'can\'t open file: ' + f + '\n'
79 print 'gulped empty file: ' + f + '\n'
84 return tuple (map (atoi, split (s,'.')))
87 return join (map (lambda x: '%s' % x, list (t)), '.')
89 def version_cmp (t1, t2):
95 def guess_mudela_version(filename):
96 s = gulp_file (filename)
97 m = mudela_version_re.search (s)
103 class FatalConversionError:
108 def show_rules (file):
109 for x in conversions:
110 file.write ('%s: %s\n' % (tup_to_str (x[0]), x[2]))
112 ############################
114 if 1: # need new a namespace
116 if re.search ('\\\\octave', str):
117 sys.stderr.write ('\nNot smart enough to convert \\octave')
118 raise FatalConversionError()
123 ((0,1,19), conv, 'deprecated \\octave; can\'t convert automatically'))
126 if 1: # need new a namespace
128 x = re.sub ('\\\\textstyle([^;]+);',
129 '\\\\property Lyrics . textstyle = \\1', str)
130 x = re.sub ('\\\\key([^;]+);', '\\\\accidentals \\1;', str)
135 ((0,1,20), conv, 'deprecated \\textstyle, new \key syntax'))
140 x = re.sub ('\\\\musical_pitch', '\\\\musicalpitch',str)
141 x = re.sub ('\\\\meter', '\\\\time',str)
146 ((0,1,21), conv, '\\musical_pitch -> \\musicalpitch, '+
147 '\\meter -> \\time'))
154 ((1,0,0), conv, '0.1.21 -> 1.0.0 '))
159 x = re.sub ('\\\\accidentals', '\\\\keysignature',str)
160 x = re.sub ('specialaccidentals *= *1', 'keyoctaviation = 0',str)
161 x = re.sub ('specialaccidentals *= *0', 'keyoctaviation = 1',str)
166 ((1,0,1), conv, '\\accidentals -> \\keysignature, ' +
167 'specialaccidentals -> keyoctaviation'))
171 if re.search ('\\\\header', lines):
172 sys.stderr.write ('\nNot smart enough to convert to new \\header format')
175 conversions.append ((1,0,2), conv, '\\header { key = concat + with + operator }')
179 x = re.sub ('\\\\melodic', '\\\\notes',str)
183 conversions.append ((1,0,3), conv, '\\melodic -> \\notes')
187 x = re.sub ('default_paper *=', '',str)
188 x = re.sub ('default_midi *=', '',x)
192 conversions.append ((1,0,4), conv, 'default_{paper,midi}')
196 x = re.sub ('ChoireStaff', 'ChoirStaff',str)
197 x = re.sub ('\\output', 'output = ',str)
201 conversions.append ((1,0,5), conv, 'ChoireStaff -> ChoirStaff')
205 if re.search ('[a-zA-Z]+ = *\\translator',str):
206 sys.stderr.write ('\nNot smart enough to change \\translator syntax')
207 raise FatalConversionError()
210 conversions.append ((1,0,6), conv, 'foo = \\translator {\\type .. } ->\\translator {\\type ..; foo; }')
215 x = re.sub ('\\\\lyric', '\\\\lyrics',str)
219 conversions.append ((1,0,7), conv, '\\lyric -> \\lyrics')
223 x = re.sub ('\\\\\\[/3+', '\\\\times 2/3 { ',str)
224 x = re.sub ('\\[/3+', '\\\\times 2/3 { [',str)
225 x = re.sub ('\\\\\\[([0-9/]+)', '\\\\times \\1 {',str)
226 x = re.sub ('\\[([0-9/]+)', '\\\\times \\1 { [',str)
227 x = re.sub ('\\\\\\]([0-9/]+)', '}', str)
228 x = re.sub ('\\\\\\]', '}',str)
229 x = re.sub ('\\]([0-9/]+)', '] }', str)
232 conversions.append ((1,0,10), conv, '[2/3 ]1/1 -> \\times 2/3 ')
237 conversions.append ((1,0,12), conv, 'Chord syntax stuff')
244 x = re.sub ('<([^>~]+)~([^>]*)>','<\\1 \\2> ~', str)
248 conversions.append ((1,0,13), conv, '<a ~ b> c -> <a b> ~ c')
252 x = re.sub ('<\\[','[<', str)
253 x = re.sub ('\\]>','>]', str)
257 conversions.append ((1,0,14), conv, '<[a b> <a b]>c -> [<a b> <a b>]')
262 x = re.sub ('\\\\type','\\\\context', str)
263 x = re.sub ('textstyle','textStyle', str)
267 conversions.append ((1,0,16), conv, '\\type -> \\context, textstyle -> textStyle')
272 if re.search ('\\\\repeat',str):
273 sys.stderr.write ('\nNot smart enough to convert \\repeat')
274 raise FatalConversionError()
277 conversions.append ((1,0,18), conv,
278 '\\repeat NUM Music Alternative -> \\repeat FOLDSTR Music Alternative')
282 x = re.sub ('SkipBars','skipBars', str)
283 x = re.sub ('fontsize','fontSize', str)
284 x = re.sub ('midi_instrument','midiInstrument', x)
288 conversions.append ((1,0,19), conv,
289 'fontsize -> fontSize, midi_instrument -> midiInstrument, SkipBars -> skipBars')
294 x = re.sub ('tieydirection','tieVerticalDirection', str)
295 x = re.sub ('slurydirection','slurVerticalDirection', str)
296 x = re.sub ('ydirection','verticalDirection', x)
300 conversions.append ((1,0,20), conv,
301 '{,tie,slur}ydirection -> {v,tieV,slurV}erticalDirection')
306 x = re.sub ('hshift','horizontalNoteShift', str)
310 conversions.append ((1,0,21), conv,
311 'hshift -> horizontalNoteShift')
316 x = re.sub ('\\\\grouping[^;]*;','', str)
320 conversions.append ((1,1,52), conv,
321 'deprecate \\grouping')
326 x = re.sub ('\\\\wheel','\\\\coda', str)
330 conversions.append ((1,1,55), conv,
335 x = re.sub ('keyoctaviation','keyOctaviation', str)
336 x = re.sub ('slurdash','slurDash', str)
340 conversions.append ((1,1,65), conv,
341 'slurdash -> slurDash, keyoctaviation -> keyOctaviation')
345 x = re.sub ('\\\\repeat *\"?semi\"?','\\\\repeat "volta"', str)
349 conversions.append ((1,1,66), conv,
355 x = re.sub ('\"?beamAuto\"? *= *\"?0?\"?','noAutoBeaming = "1"', str)
359 conversions.append ((1,1,67), conv,
360 'beamAuto -> noAutoBeaming')
364 x = re.sub ('automaticMelismas', 'automaticMelismata', str)
368 conversions.append ((1,2,0), conv,
369 'automaticMelismas -> automaticMelismata')
373 x = re.sub ('dynamicDir', 'dynamicDirection', str)
377 conversions.append ((1,2,1), conv,
378 'dynamicDir -> dynamicDirection')
380 ############################
383 def get_conversions (from_version, to_version):
384 def version_b (v, f = from_version, t = to_version):
385 return version_cmp (v[0], f) > 0 and version_cmp (v[0], t) <= 0
386 return filter (version_b, conversions)
389 def latest_version ():
390 return conversions[-1][0]
392 def do_conversion (infile, from_version, outfile, to_version):
394 conv_list = get_conversions (from_version, to_version)
396 sys.stderr.write ('Applying conversions: ')
397 str = infile.read (-1)
401 sys.stderr.write (tup_to_str (x[0]) + ', ')
403 last_conversion = x[0]
405 except FatalConversionError:
406 sys.stderr.write ('Error while converting; I won\'t convert any further')
409 sys.stderr.write ('\n')
410 new_ver = '\\\\version \"%s\"' % tup_to_str (last_conversion)
411 if re.search (mudela_version_re_str):
412 str = re.sub (mudela_version_re_str,new_ver , str)
414 str = new_ver + '\n' + str
418 class UnknownVersion:
421 def do_one_file (infile_name):
422 sys.stderr.write ('Processing `%s\' ... '% infile_name)
425 outfile_name = infile_name + '.NEW'
426 elif __main__.outfile_name:
427 outfile_name = __main__.outfile_name
429 if __main__.from_version:
430 from_version = __main__.from_version
432 guess = guess_mudela_version (infile_name)
434 raise UnknownVersion()
435 from_version = str_to_tuple (guess)
437 if __main__.to_version:
438 to_version = __main__.to_version
440 to_version = latest_version ()
444 infile = open (infile_name,'r')
449 outfile = open (outfile_name, 'w')
454 do_conversion (infile, from_version, outfile, to_version)
464 os.remove(infile_name + '~')
467 os.rename (infile_name, infile_name + '~')
468 os.rename (infile_name + '.NEW', infile_name)
470 sys.stderr.write ('\n')
478 (options, files) = getopt.getopt (
479 sys.argv[1:], 'o:f:t:seh', ['version', 'output', 'show-rules', 'help', 'edit', 'from=', 'to'])
484 if o== '--help' or o == '-h':
487 if o == '--version' or o == '-v':
490 elif o== '--from' or o=='-f':
491 from_version = str_to_tuple (a)
492 elif o== '--to' or o=='-t':
493 to_version = str_to_tuple (a)
494 elif o== '--edit' or o == '-e':
496 elif o== '--show-rules' or o == '-s':
497 show_rules (sys.stdout)
499 elif o == '--output' or o == '-o':
511 except UnknownVersion: