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'
27 mudela_version_re_str = '\\\\version *\"(.*)\"'
28 mudela_version_re = re.compile(mudela_version_re_str)
31 return '%s version %s' %(program_name, version);
34 sys.stderr.write (program_id () + '\n')
43 print 'can\'t open file: ' + f + '\n'
47 print 'gulped empty file: ' + f + '\n'
52 return tuple (map (atoi, split (s,'.')))
55 return join (map (lambda x: '%s' % x, list (t)), '.')
57 def version_cmp (t1, t2):
64 def guess_mudela_version(filename):
65 s = gulp_file (filename)
66 m = mudela_version_re.search (s)
74 ("Usage: %s [OPTION]... [FILE]...\n"
75 + "Try to convert to newer mudela-versions\n"
77 + " -h, --help print this help\n"
78 + ' -e, --edit in place edit\n'
79 + ' -f, --from=VERSION start from version\n'
80 + ' -s, --show-rules print out all rules.\n'
81 + ' -t, --to=VERSION target version\n') % program_name)
84 class FatalConversionError:
89 def show_rules (file):
91 file.write ('%s: %s\n' % (tup_to_str (x[0]), x[2]))
93 ############################
95 if 1: # need new a namespace
99 if re.search ('\\\\octave', x):
103 sys.stderr.write ('\nNot smart enough to convert \\octave')
104 raise FatalConversionError()
109 ((0,1,19), conv, 'deprecated \\octave; can\'t convert automatically'))
112 if 1: # need new a namespace
116 x = re.sub ('\\\\textstyle([^;]+);',
117 '\\\\property Lyrics . textstyle = \\1', x)
118 x = re.sub ('\\\\key([^;]+);', '\\\\accidentals \\1;', x)
124 ((0,1,20), conv, 'deprecated \\textstyle, new \key syntax'))
131 x = re.sub ('\\\\musical_pitch',
132 '\\\\musicalpitch',x)
133 x = re.sub ('\\\\meter',
140 ((0,1,21), conv, '\\musical_pitch -> \\musicalpitch, '+
141 '\\meter -> \\time'))
148 ((1,0,0), conv, '0.1.21 -> 1.0.0 '))
155 x = re.sub ('\\\\accidentals',
156 '\\\\keysignature',x)
157 x = re.sub ('specialaccidentals *= *1',
158 'keyoctaviation = 0',x)
159 x = re.sub ('specialaccidentals *= *0',
160 'keyoctaviation = 1',x)
166 ((1,0,1), conv, '\\accidentals -> \\keysignature, ' +
167 'specialaccidentals -> keyoctaviation'))
173 if re.search ('\\\\header', x):
177 sys.stderr.write ('\nNot smart enough to convert to new \\header format')
180 conversions.append ((1,0,2), conv, '\\header { key = concat + with + operator }')
186 x = re.sub ('\\\\melodic', '\\\\notes',x)
190 conversions.append ((1,0,3), conv, '\\melodic -> \\notes')
196 x = re.sub ('default_paper *=', '',x)
197 x = re.sub ('default_midi *=', '',x)
201 conversions.append ((1,0,4), conv, 'default_{paper,midi}')
207 x = re.sub ('ChoireStaff', 'ChoirStaff',x)
208 x = re.sub ('\\output', 'output = ',x)
212 conversions.append ((1,0,5), conv, 'ChoireStaff -> ChoirStaff')
219 found = re.search ('[a-zA-Z]+ = *\\translator',x)
223 sys.stderr.write ('\nNot smart enough to \\translator syntax')
224 raise FatalConversionError()
227 conversions.append ((1,0,6), conv, 'foo = \\translator {\\type .. } ->\\translator {\\type ..; foo; }')
234 x = re.sub ('\\\\lyric', '\\\\lyrics',x)
238 conversions.append ((1,0,7), conv, '\\lyric -> \\lyrics')
244 x = re.sub ('\\\\\\[/3+', '\\\\times 2/3 { ',x)
245 x = re.sub ('\\[/3+', '\\\\times 2/3 { [',x)
246 x = re.sub ('\\\\\\[([0-9/]+)', '\\\\times \\1 {',x)
247 x = re.sub ('\\[([0-9/]+)', '\\\\times \\1 { [',x)
248 x = re.sub ('\\\\\\]([0-9/]+)', '}', x)
249 x = re.sub ('\\\\\\]', '}',x)
250 x = re.sub ('\\]([0-9/]+)', '] }', x)
255 conversions.append ((1,0,10), conv, '[2/3 ]1/1 -> \\times 2/3 ')
260 conversions.append ((1,0,12), conv, 'Chord syntax stuff')
267 x = re.sub ('<([^>~]+)~([^>]*)>','<\\1 \\2> ~', x)
271 conversions.append ((1,0,13), conv, '<a ~ b> c -> <a b> ~ c')
277 x = re.sub ('<\\[','[<', x)
278 x = re.sub ('\\]>','>]', x)
282 conversions.append ((1,0,14), conv, '<[a b> <a b]>c -> [<a b> <a b>]')
289 x = re.sub ('\\\\type','\\\\context', x)
293 conversions.append ((1,0,16), conv, '\\type -> \\context')
297 ############################
300 def get_conversions (from_version, to_version):
301 def version_b (v, f = from_version, t = to_version):
302 return version_cmp (v[0], f) > 0 and version_cmp (v[0], t) <= 0
303 return filter (version_b, conversions)
306 def latest_version ():
307 return conversions[-1][0]
309 def do_conversion (infile, from_version, outfile, to_version):
311 conv_list = get_conversions (from_version, to_version)
313 sys.stderr.write ('Applying conversions: ')
314 lines = infile.readlines();
318 sys.stderr.write (tup_to_str (x[0]) + ', ')
320 last_conversion = x[0]
323 except FatalConversionError:
324 sys.stderr.write ('Error while converting; I won\'t convert any further')
328 x = re.sub (mudela_version_re_str, '\\\\version \"%s\"' % tup_to_str (last_conversion), x)
331 class UnknownVersion:
334 def do_one_file (infile_name):
335 sys.stderr.write ('Processing `%s\' ... '% infile_name)
338 outfile_name = infile_name + '.NEW'
339 elif __main__.outfile_name:
340 outfile_name = __main__.outfile_name
342 if __main__.from_version:
343 from_version = __main__.from_version
345 guess = guess_mudela_version (infile_name)
347 raise UnknownVersion()
348 from_version = str_to_tuple (guess)
350 if __main__.to_version:
351 to_version = __main__.to_version
353 to_version = latest_version ()
357 infile = open (infile_name,'r')
362 outfile = open (outfile_name, 'w')
367 do_conversion (infile, from_version, outfile, to_version)
377 os.remove(infile_name + '~')
380 os.rename (infile_name, infile_name + '~')
381 os.rename (infile_name + '.NEW', infile_name)
383 sys.stderr.write ('\n')
392 (options, files) = getopt.getopt (
393 sys.argv[1:], 'f:t:seh', ['show-rules', 'help', 'edit', 'from', 'to'])
398 if o== '--help' or o == '-h':
400 elif o== '--from' or o=='-f':
401 from_version = str_to_tuple (a)
402 elif o== '--to' or o=='-t':
403 to_version = str_to_tuple (a)
404 elif o== '--edit' or o == '-e':
406 elif o== '--show-rules' or o == '-s':
407 show_rules (sys.stdout)
419 except UnknownVersion: