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'
28 mudela_version_re_str ='\\\\version *\"\(.*\)\"'
29 mudela_version_re = regex.compile(mudela_version_re_str)
32 return '%s version %s' %(program_name, version);
35 sys.stderr.write (program_id () + '\n')
44 print 'can\'t open file: ' + f + '\n'
48 print 'gulped empty file: ' + f + '\n'
54 return tuple (map (atoi, split (s,'.')))
57 return join (map (lambda x: '%s' % x, list (t)), '.')
59 def version_cmp (t1, t2):
66 def guess_mudela_version(filename):
67 s = gulp_file (filename)
68 if mudela_version_re.search(s) <> -1:
69 return mudela_version_re.group(1)
75 ("Usage: %s [OPTION]... [FILE]...\n"
76 + "Try to convert to newer mudela-versions\n"
78 + " -h, --help print this help\n"
79 + ' -e, --edit in place edit\n'
80 + ' -f, --from=VERSION start from version\n'
81 + ' -s, --show-rules print out all rules.\n'
82 + ' -t, --to=VERSION target version\n') % program_name)
85 class FatalConversionError:
90 def show_rules (file):
92 file.write ('%s: %s\n' % (tup_to_str (x[0]), x[2]))
94 ############################
96 if 1: # need new a namespace
100 if regex.search ('\\\\octave', x) <> -1:
104 sys.stderr.write ('\nNot smart enough to convert \\octave\n')
105 raise FatalConversionError()
110 ((0,1,19), conv, 'deprecated \\octave; can\'t convert automatically'))
113 if 1: # need new a namespace
117 x = regsub.gsub ('\\\\textstyle\\([^;]+\\);',
118 '\\\\property Lyrics . textstyle = \\1', x)
119 x = regsub.gsub ('\\\\key\\([^;]+\\);', '\\\\accidentals \\1;', x)
125 ((0,1,20), conv, 'deprecated \\textstyle, new \key syntax'))
128 if 1: # need new a namespace
132 x = regsub.gsub ('\\\\musical_pitch',
133 '\\\\musicalpitch',x)
134 x = regsub.gsub ('\\\\meter',
141 ((0,1,21), conv, '\\musical_pitch -> \\musicalpitch, '+
142 '\\meter -> \\time\n'))
144 if 1: # need new a namespace
149 ((1,0,0), conv, '0.1.21 -> 1.0.0 '))
152 if 1: # need new a namespace
156 x = regsub.gsub ('\\\\accidentals',
157 '\\\\keysignature',x)
158 x = regsub.gsub ('specialaccidentals *= *1',
159 'keyoctaviation = 0',x)
160 x = regsub.gsub ('specialaccidentals *= *0',
161 'keyoctaviation = 1',x)
167 ((1,0,1), conv, '\\accidentals -> \\keysignature, ' +
168 'specialaccidentals -> keyoctaviation\n'))
174 if regex.search ('\\\\header', x) <> -1:
178 sys.stderr.write ('\nNot smart enough to convert to new \\header format\n')
181 conversions.append ((1,0,2), conv, '\header { key = concat + with + operator }\n')
187 x = regsub.gsub ('\\\\melodic', '\\\\notes',x)
191 conversions.append ((1,0,3), conv, '\melodic -> \notes\n')
196 x = regsub.gsub ('default_paper *=', '',x)
197 x = regsub.gsub ('default_midi *=', '',x)
201 conversions.append ((1,0,4), conv, 'default_{paper,midi}\n')
207 x = regsub.gsub ('ChoireStaff', 'ChoirStaff',x)
208 x = regsub.gsub ('\\output', 'output = ',x)
212 conversions.append ((1,0,5), conv, 'ChoireStaff -> ChoirStaff\n')
219 found = regex.search ('[a-zA-Z]+ = *\\translator',x) <> -1
223 sys.stderr.write ('\nNot smart enough to \\translator syntax\n')
224 raise FatalConversionError()
227 conversions.append ((1,0,6), conv, 'foo = \\translator {\type .. } ->\translator {\type ..; foo; }\n')
228 ############################
230 def get_conversions (from_version, to_version):
231 def version_b (v, f = from_version, t = to_version):
232 return version_cmp (v[0], f) > 0 and version_cmp (v[0], t) <= 0
233 return filter (version_b, conversions)
236 def latest_version ():
237 return conversions[-1][0]
239 def do_conversion (infile, from_version, outfile, to_version):
241 conv_list = get_conversions (from_version, to_version)
243 sys.stderr.write ('Applying conversions: ')
244 lines = infile.readlines();
248 sys.stderr.write (tup_to_str (x[0]) + ', ')
250 last_conversion = x[0]
253 except FatalConversionError:
254 sys.stderr.write ('Error while converting; I won\'t convert any further')
258 x = regsub.sub (mudela_version_re_str, '\\version \"%s\"' % tup_to_str (last_conversion), x)
261 class UnknownVersion:
264 def do_one_file (infile_name):
265 sys.stderr.write ('Processing `%s\' ... '% infile_name)
268 outfile_name = infile_name + '.NEW'
269 elif __main__.outfile_name:
270 outfile_name = __main__.outfile_name
272 if __main__.from_version:
273 from_version = __main__.from_version
275 guess = guess_mudela_version (infile_name)
277 raise UnknownVersion()
278 from_version = str_to_tuple (guess)
280 if __main__.to_version:
281 to_version = __main__.to_version
283 to_version = latest_version ()
287 infile = open (infile_name,'r')
292 outfile = open (outfile_name, 'w')
297 do_conversion (infile, from_version, outfile, to_version)
306 os.rename (infile_name, infile_name + '~')
307 os.rename (infile_name + '.NEW', infile_name)
309 sys.stderr.write ('\n')
317 (options, files) = getopt.getopt (
318 sys.argv[1:], 'f:t:seh', ['show-rules', 'help', 'edit', 'from', 'to'])
323 if o== '--help' or o == '-h':
325 elif o== '--from' or o=='-f':
326 from_version = str_to_tuple (a)
327 elif o== '--to' or o=='-t':
328 to_version = str_to_tuple (a)
329 elif o== '--edit' or o == '-e':
331 elif o== '--show-rules' or o == '-s':
332 show_rules (sys.stdout)
344 except UnknownVersion: