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 str = re
.sub ('\\\\textstyle([^;]+);',
129 '\\\\property Lyrics . textstyle = \\1', str)
130 str = re
.sub ('\\\\key([^;]+);', '\\\\accidentals \\1;', str)
135 ((0,1,20), conv
, 'deprecated \\textstyle, new \key syntax'))
140 str = re
.sub ('\\\\musical_pitch', '\\\\musicalpitch',str)
141 str = 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 str = re
.sub ('\\\\accidentals', '\\\\keysignature',str)
160 str = re
.sub ('specialaccidentals *= *1', 'keyoctaviation = 0',str)
161 str = 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 str = re
.sub ('\\\\melodic', '\\\\notes',str)
183 conversions
.append ((1,0,3), conv
, '\\melodic -> \\notes')
187 str = re
.sub ('default_paper *=', '',str)
188 str = re
.sub ('default_midi *=', '',x
)
192 conversions
.append ((1,0,4), conv
, 'default_{paper,midi}')
196 str = re
.sub ('ChoireStaff', 'ChoirStaff',str)
197 str = 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 str = re
.sub ('\\\\lyric', '\\\\lyrics',str)
219 conversions
.append ((1,0,7), conv
, '\\lyric -> \\lyrics')
223 str = re
.sub ('\\\\\\[/3+', '\\\\times 2/3 { ',str)
224 str = re
.sub ('\\[/3+', '\\\\times 2/3 { [',str)
225 str = re
.sub ('\\\\\\[([0-9/]+)', '\\\\times \\1 {',str)
226 str = re
.sub ('\\[([0-9/]+)', '\\\\times \\1 { [',str)
227 str = re
.sub ('\\\\\\]([0-9/]+)', '}', str)
228 str = re
.sub ('\\\\\\]', '}',str)
229 str = 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 str = re
.sub ('<([^>~]+)~([^>]*)>','<\\1 \\2> ~', str)
248 conversions
.append ((1,0,13), conv
, '<a ~ b> c -> <a b> ~ c')
252 str = re
.sub ('<\\[','[<', str)
253 str = re
.sub ('\\]>','>]', str)
257 conversions
.append ((1,0,14), conv
, '<[a b> <a b]>c -> [<a b> <a b>]')
262 str = re
.sub ('\\\\type','\\\\context', str)
263 str = 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 str = re
.sub ('SkipBars','skipBars', str)
283 str = re
.sub ('fontsize','fontSize', str)
284 str = re
.sub ('midi_instrument','midiInstrument', x
)
288 conversions
.append ((1,0,19), conv
,
289 'fontsize -> fontSize, midi_instrument -> midiInstrument, SkipBars -> skipBars')
294 str = re
.sub ('tieydirection','tieVerticalDirection', str)
295 str = re
.sub ('slurydirection','slurVerticalDirection', str)
296 str = re
.sub ('ydirection','verticalDirection', x
)
300 conversions
.append ((1,0,20), conv
,
301 '{,tie,slur}ydirection -> {v,tieV,slurV}erticalDirection')
306 str = re
.sub ('hshift','horizontalNoteShift', str)
310 conversions
.append ((1,0,21), conv
,
311 'hshift -> horizontalNoteShift')
316 str = re
.sub ('\\\\grouping[^;]*;','', str)
320 conversions
.append ((1,1,52), conv
,
321 'deprecate \\grouping')
326 str = re
.sub ('\\\\wheel','\\\\coda', str)
330 conversions
.append ((1,1,55), conv
,
335 str = re
.sub ('keyoctaviation','keyOctaviation', str)
336 str = re
.sub ('slurdash','slurDash', str)
340 conversions
.append ((1,1,65), conv
,
341 'slurdash -> slurDash, keyoctaviation -> keyOctaviation')
345 str = re
.sub ('\\\\repeat *\"?semi\"?','\\\\repeat "volta"', str)
349 conversions
.append ((1,1,66), conv
,
355 str = re
.sub ('\"?beamAuto\"? *= *\"?0?\"?','noAutoBeaming = "1"', str)
359 conversions
.append ((1,1,67), conv
,
360 'beamAuto -> noAutoBeaming')
364 str = re
.sub ('automaticMelismas', 'automaticMelismata', str)
368 conversions
.append ((1,2,0), conv
,
369 'automaticMelismas -> automaticMelismata')
373 str = re
.sub ('dynamicDir\\b', 'dynamicDirection', str)
377 conversions
.append ((1,2,1), conv
,
378 'dynamicDir -> dynamicDirection')
382 str = re
.sub ('\\\\cadenza *0 *;', '\\\\cadenzaOff', str)
383 str = re
.sub ('\\\\cadenza *1 *;', '\\\\cadenzaOn', str)
387 conversions
.append ((1,3,4), conv
,
388 '\\cadenza -> \cadenza{On|Off}')
392 str = re
.sub ('beamAuto([^=]+)= *"([0-9]+)/([0-9]+)" *;',
393 'beamAuto\\1 = #(make-moment \\2 \\3)',
397 conversions
.append ((1,3,5), conv
, 'beamAuto moment properties')
401 str = re
.sub ('stemStyle',
406 conversions
.append ((1,3,17), conv
, 'stemStyle -> flagStyle')
410 return re
.sub ('staffLineLeading'
413 conversions
.append ((1,3,18), conv
, "staffLineLeading -> staffSpace")
415 ############################
418 def get_conversions (from_version
, to_version
):
419 def version_b (v
, f
= from_version
, t
= to_version
):
420 return version_cmp (v
[0], f
) > 0 and version_cmp (v
[0], t
) <= 0
421 return filter (version_b
, conversions
)
424 def latest_version ():
425 return conversions
[-1][0]
427 def do_conversion (infile
, from_version
, outfile
, to_version
):
429 conv_list
= get_conversions (from_version
, to_version
)
431 sys
.stderr
.write ('Applying conversions: ')
436 sys
.stderr
.write (tup_to_str (x
[0]) + ', ')
438 last_conversion
= x
[0]
440 except FatalConversionError
:
441 sys
.stderr
.write ('Error while converting; I won\'t convert any further')
444 sys
.stderr
.write ('\n')
445 new_ver
= '\\\\version \"%s\"' % tup_to_str (last_conversion
)
446 if re
.search (mudela_version_re_str
, str):
447 str = re
.sub (mudela_version_re_str
,new_ver
, str)
449 str = new_ver
+ '\n' + str
453 return last_conversion
455 class UnknownVersion
:
458 def do_one_file (infile_name
):
459 sys
.stderr
.write ('Processing `%s\' ... '% infile_name
)
462 outfile_name
= infile_name
+ '.NEW'
463 elif __main__
.outfile_name
:
464 outfile_name
= __main__
.outfile_name
466 if __main__
.from_version
:
467 from_version
= __main__
.from_version
469 guess
= guess_mudela_version (infile_name
)
471 raise UnknownVersion()
472 from_version
= str_to_tuple (guess
)
474 if __main__
.to_version
:
475 to_version
= __main__
.to_version
477 to_version
= latest_version ()
481 infile
= open (infile_name
,'r')
486 outfile
= open (outfile_name
, 'w')
490 touched
= do_conversion (infile
, from_version
, outfile
, to_version
)
498 if __main__
.edit
and touched
:
500 os
.remove(infile_name
+ '~')
503 os
.rename (infile_name
, infile_name
+ '~')
504 os
.rename (infile_name
+ '.NEW', infile_name
)
506 sys
.stderr
.write ('\n')
514 (options
, files
) = getopt
.getopt (
515 sys
.argv
[1:], 'o:f:t:seh', ['version', 'output', 'show-rules', 'help', 'edit', 'from=', 'to'])
520 if o
== '--help' or o
== '-h':
523 if o
== '--version' or o
== '-v':
526 elif o
== '--from' or o
=='-f':
527 from_version
= str_to_tuple (a
)
528 elif o
== '--to' or o
=='-t':
529 to_version
= str_to_tuple (a
)
530 elif o
== '--edit' or o
== '-e':
532 elif o
== '--show-rules' or o
== '-s':
533 show_rules (sys
.stdout
)
535 elif o
== '--output' or o
== '-o':
547 except UnknownVersion
: