lilypond-1.3.18
[lilypond.git] / scripts / convert-mudela.py
blob5d91080465d3cd9182e81b2e4bdef4452deee704
1 #!@PYTHON@
3 # convert-mudela.py -- convertor for mudela versions
4 #
5 # source file of the GNU LilyPond music typesetter
6 #
7 # (c) 1998
9 # TODO
10 # use -f and -t for -s output
12 # NEWS
13 # 0.2
14 # - rewrite in python
16 program_name = 'convert-mudela'
17 version = '@TOPLEVEL_VERSION@'
19 import os
20 import sys
21 import __main__
22 import getopt
23 from string import *
24 import re
25 import time
27 mudela_version_re_str = '\\\\version *\"(.*)\"'
28 mudela_version_re = re.compile(mudela_version_re_str)
30 def program_id ():
31 return '%s (GNU LilyPond) %s' %(program_name, version);
33 def identify ():
34 sys.stderr.write (program_id () + '\n')
36 def usage ():
37 sys.stdout.write (
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
42 Options:
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
52 """ % program_name)
55 sys.exit (0)
57 def print_version ():
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
64 information.
66 """ % (program_id() , program_name))
68 def gulp_file(f):
69 try:
70 i = open(f)
71 i.seek (0, 2)
72 n = i.tell ()
73 i.seek (0,0)
74 except:
75 print 'can\'t open file: ' + f + '\n'
76 return ''
77 s = i.read (n)
78 if len (s) <= 0:
79 print 'gulped empty file: ' + f + '\n'
80 i.close ()
81 return s
83 def str_to_tuple (s):
84 return tuple (map (atoi, split (s,'.')))
86 def tup_to_str (t):
87 return join (map (lambda x: '%s' % x, list (t)), '.')
89 def version_cmp (t1, t2):
90 for x in [0,1,2]:
91 if t1[x] - t2[x]:
92 return t1[x] - t2[x]
93 return 0
95 def guess_mudela_version(filename):
96 s = gulp_file (filename)
97 m = mudela_version_re.search (s)
98 if m:
99 return m.group(1)
100 else:
101 return ''
103 class FatalConversionError:
104 pass
106 conversions = []
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
115 def conv (str):
116 if re.search ('\\\\octave', str):
117 sys.stderr.write ('\nNot smart enough to convert \\octave')
118 raise FatalConversionError()
120 return lines
122 conversions.append (
123 ((0,1,19), conv, 'deprecated \\octave; can\'t convert automatically'))
126 if 1: # need new a namespace
127 def conv (str):
128 str = re.sub ('\\\\textstyle([^;]+);',
129 '\\\\property Lyrics . textstyle = \\1', str)
130 str = re.sub ('\\\\key([^;]+);', '\\\\accidentals \\1;', str)
132 return str
134 conversions.append (
135 ((0,1,20), conv, 'deprecated \\textstyle, new \key syntax'))
138 if 1:
139 def conv (str):
140 str = re.sub ('\\\\musical_pitch', '\\\\musicalpitch',str)
141 str = re.sub ('\\\\meter', '\\\\time',str)
143 return str
145 conversions.append (
146 ((0,1,21), conv, '\\musical_pitch -> \\musicalpitch, '+
147 '\\meter -> \\time'))
149 if 1:
150 def conv (str):
151 return lines
153 conversions.append (
154 ((1,0,0), conv, '0.1.21 -> 1.0.0 '))
157 if 1:
158 def conv (str):
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)
163 return str
165 conversions.append (
166 ((1,0,1), conv, '\\accidentals -> \\keysignature, ' +
167 'specialaccidentals -> keyoctaviation'))
169 if 1:
170 def conv(str):
171 if re.search ('\\\\header', lines):
172 sys.stderr.write ('\nNot smart enough to convert to new \\header format')
173 return lines
175 conversions.append ((1,0,2), conv, '\\header { key = concat + with + operator }')
177 if 1:
178 def conv(str):
179 str = re.sub ('\\\\melodic', '\\\\notes',str)
181 return str
183 conversions.append ((1,0,3), conv, '\\melodic -> \\notes')
185 if 1:
186 def conv(str):
187 str = re.sub ('default_paper *=', '',str)
188 str = re.sub ('default_midi *=', '',x)
190 return str
192 conversions.append ((1,0,4), conv, 'default_{paper,midi}')
194 if 1:
195 def conv(str):
196 str = re.sub ('ChoireStaff', 'ChoirStaff',str)
197 str = re.sub ('\\output', 'output = ',str)
199 return str
201 conversions.append ((1,0,5), conv, 'ChoireStaff -> ChoirStaff')
203 if 1:
204 def conv(str):
205 if re.search ('[a-zA-Z]+ = *\\translator',str):
206 sys.stderr.write ('\nNot smart enough to change \\translator syntax')
207 raise FatalConversionError()
208 return str
210 conversions.append ((1,0,6), conv, 'foo = \\translator {\\type .. } ->\\translator {\\type ..; foo; }')
213 if 1:
214 def conv(str):
215 str = re.sub ('\\\\lyric', '\\\\lyrics',str)
217 return str
219 conversions.append ((1,0,7), conv, '\\lyric -> \\lyrics')
221 if 1:
222 def conv(str):
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)
230 return str
232 conversions.append ((1,0,10), conv, '[2/3 ]1/1 -> \\times 2/3 ')
234 if 1:
235 def conv(str):
236 return lines
237 conversions.append ((1,0,12), conv, 'Chord syntax stuff')
240 if 1:
241 def conv(str):
244 str = re.sub ('<([^>~]+)~([^>]*)>','<\\1 \\2> ~', str)
246 return str
248 conversions.append ((1,0,13), conv, '<a ~ b> c -> <a b> ~ c')
250 if 1:
251 def conv(str):
252 str = re.sub ('<\\[','[<', str)
253 str = re.sub ('\\]>','>]', str)
255 return str
257 conversions.append ((1,0,14), conv, '<[a b> <a b]>c -> [<a b> <a b>]')
260 if 1:
261 def conv(str):
262 str = re.sub ('\\\\type','\\\\context', str)
263 str = re.sub ('textstyle','textStyle', str)
265 return str
267 conversions.append ((1,0,16), conv, '\\type -> \\context, textstyle -> textStyle')
270 if 1:
271 def conv(str):
272 if re.search ('\\\\repeat',str):
273 sys.stderr.write ('\nNot smart enough to convert \\repeat')
274 raise FatalConversionError()
275 return str
277 conversions.append ((1,0,18), conv,
278 '\\repeat NUM Music Alternative -> \\repeat FOLDSTR Music Alternative')
280 if 1:
281 def conv(str):
282 str = re.sub ('SkipBars','skipBars', str)
283 str = re.sub ('fontsize','fontSize', str)
284 str = re.sub ('midi_instrument','midiInstrument', x)
286 return str
288 conversions.append ((1,0,19), conv,
289 'fontsize -> fontSize, midi_instrument -> midiInstrument, SkipBars -> skipBars')
292 if 1:
293 def conv(str):
294 str = re.sub ('tieydirection','tieVerticalDirection', str)
295 str = re.sub ('slurydirection','slurVerticalDirection', str)
296 str = re.sub ('ydirection','verticalDirection', x)
298 return str
300 conversions.append ((1,0,20), conv,
301 '{,tie,slur}ydirection -> {v,tieV,slurV}erticalDirection')
304 if 1:
305 def conv(str):
306 str = re.sub ('hshift','horizontalNoteShift', str)
308 return str
310 conversions.append ((1,0,21), conv,
311 'hshift -> horizontalNoteShift')
314 if 1:
315 def conv(str):
316 str = re.sub ('\\\\grouping[^;]*;','', str)
318 return str
320 conversions.append ((1,1,52), conv,
321 'deprecate \\grouping')
324 if 1:
325 def conv(str):
326 str = re.sub ('\\\\wheel','\\\\coda', str)
328 return str
330 conversions.append ((1,1,55), conv,
331 '\\wheel -> \\coda')
333 if 1:
334 def conv(str):
335 str = re.sub ('keyoctaviation','keyOctaviation', str)
336 str = re.sub ('slurdash','slurDash', str)
338 return str
340 conversions.append ((1,1,65), conv,
341 'slurdash -> slurDash, keyoctaviation -> keyOctaviation')
343 if 1:
344 def conv(str):
345 str = re.sub ('\\\\repeat *\"?semi\"?','\\\\repeat "volta"', str)
347 return str
349 conversions.append ((1,1,66), conv,
350 'semi -> volta')
353 if 1:
354 def conv(str):
355 str = re.sub ('\"?beamAuto\"? *= *\"?0?\"?','noAutoBeaming = "1"', str)
357 return str
359 conversions.append ((1,1,67), conv,
360 'beamAuto -> noAutoBeaming')
362 if 1:
363 def conv(str):
364 str = re.sub ('automaticMelismas', 'automaticMelismata', str)
366 return str
368 conversions.append ((1,2,0), conv,
369 'automaticMelismas -> automaticMelismata')
371 if 1:
372 def conv(str):
373 str = re.sub ('dynamicDir\\b', 'dynamicDirection', str)
375 return str
377 conversions.append ((1,2,1), conv,
378 'dynamicDir -> dynamicDirection')
380 if 1:
381 def conv(str):
382 str = re.sub ('\\\\cadenza *0 *;', '\\\\cadenzaOff', str)
383 str = re.sub ('\\\\cadenza *1 *;', '\\\\cadenzaOn', str)
385 return str
387 conversions.append ((1,3,4), conv,
388 '\\cadenza -> \cadenza{On|Off}')
390 if 1:
391 def conv (str):
392 str = re.sub ('beamAuto([^=]+)= *"([0-9]+)/([0-9]+)" *;',
393 'beamAuto\\1 = #(make-moment \\2 \\3)',
394 str)
395 return str
397 conversions.append ((1,3,5), conv, 'beamAuto moment properties')
399 if 1:
400 def conv (str):
401 str = re.sub ('stemStyle',
402 'flagStyle',
403 str)
404 return str
406 conversions.append ((1,3,17), conv, 'stemStyle -> flagStyle')
408 if 1:
409 def conv (str):
410 return re.sub ('staffLineLeading'
411 'staffSpace',
412 str)
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: ')
432 str = infile.read ()
433 last_conversion = ()
434 try:
435 for x in conv_list:
436 sys.stderr.write (tup_to_str (x[0]) + ', ')
437 str = x[1] (str)
438 last_conversion = x[0]
440 except FatalConversionError:
441 sys.stderr.write ('Error while converting; I won\'t convert any further')
443 if last_conversion:
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)
448 else:
449 str = new_ver + '\n' + str
451 outfile.write(str)
453 return last_conversion
455 class UnknownVersion:
456 pass
458 def do_one_file (infile_name):
459 sys.stderr.write ('Processing `%s\' ... '% infile_name)
460 outfile_name = ''
461 if __main__.edit:
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
468 else:
469 guess = guess_mudela_version (infile_name)
470 if not guess:
471 raise UnknownVersion()
472 from_version = str_to_tuple (guess)
474 if __main__.to_version:
475 to_version = __main__.to_version
476 else:
477 to_version = latest_version ()
480 if infile_name:
481 infile = open (infile_name,'r')
482 else:
483 infile = sys.stdin
485 if outfile_name:
486 outfile = open (outfile_name, 'w')
487 else:
488 outfile = sys.stdout
490 touched = do_conversion (infile, from_version, outfile, to_version)
492 if infile_name:
493 infile.close ()
495 if outfile_name:
496 outfile.close ()
498 if __main__.edit and touched:
499 try:
500 os.remove(infile_name + '~')
501 except:
502 pass
503 os.rename (infile_name, infile_name + '~')
504 os.rename (infile_name + '.NEW', infile_name)
506 sys.stderr.write ('\n')
507 sys.stderr.flush ()
509 edit = 0
510 to_version = ()
511 from_version = ()
512 outfile_name = ''
514 (options, files) = getopt.getopt (
515 sys.argv[1:], 'o:f:t:seh', ['version', 'output', 'show-rules', 'help', 'edit', 'from=', 'to'])
517 for opt in options:
518 o = opt[0]
519 a = opt[1]
520 if o== '--help' or o == '-h':
521 usage ()
522 sys.exit (0)
523 if o == '--version' or o == '-v':
524 print_version ()
525 sys.exit (0)
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':
531 edit = 1
532 elif o== '--show-rules' or o == '-s':
533 show_rules (sys.stdout)
534 sys.exit(0)
535 elif o == '--output' or o == '-o':
536 outfile_name = a
537 else:
538 print o
539 raise getopt.error
541 identify ()
542 for f in files:
543 if f == '-':
544 f = ''
545 try:
546 do_one_file (f)
547 except UnknownVersion:
548 pass