# - rewrite in python
program_name = 'convert-mudela'
-version = '0.2'
-
+version = '0.3'
import os
import __main__
import getopt
from string import *
-import regex
-import regsub
+import re
+
import time
-mudela_version_re_str ='\\\\version *\"\(.*\)\"'
-mudela_version_re = regex.compile(mudela_version_re_str)
+mudela_version_re_str = '\\\\version *\"(.*)\"'
+mudela_version_re = re.compile(mudela_version_re_str)
def program_id ():
return '%s version %s' %(program_name, version);
def guess_mudela_version(filename):
s = gulp_file (filename)
- if mudela_version_re.search(s) <> -1:
- return mudela_version_re.group(1)
+ m = mudela_version_re.search (s)
+ if m:
+ return m.group(1)
else:
return ''
def conv (lines):
found =0
for x in lines:
- if regex.search ('\\\\octave', x) <> -1:
+ if re.search ('\\\\octave', x):
found = 1
break
if found:
- sys.stderr.write ('\nNot smart enough to convert \\octave\n')
+ sys.stderr.write ('\nNot smart enough to convert \\octave')
raise FatalConversionError()
return lines
def conv (lines):
newlines = []
for x in lines:
- x = regsub.gsub ('\\\\textstyle\\([^;]+\\);',
+ x = re.sub ('\\\\textstyle([^;]+);',
'\\\\property Lyrics . textstyle = \\1', x)
- x = regsub.gsub ('\\\\key\\([^;]+\\);', '\\\\accidentals \\1;', x)
+ x = re.sub ('\\\\key([^;]+);', '\\\\accidentals \\1;', x)
newlines.append (x)
return newlines
def conv (lines):
newlines = []
for x in lines:
- x = regsub.gsub ('\\\\musical_pitch',
+ x = re.sub ('\\\\musical_pitch',
'\\\\musicalpitch',x)
- x = regsub.gsub ('\\\\meter',
+ x = re.sub ('\\\\meter',
'\\\\time',x)
newlines.append (x)
return newlines
conversions.append (
((0,1,21), conv, '\\musical_pitch -> \\musicalpitch, '+
- '\\meter -> \\time\n'))
+ '\\meter -> \\time'))
if 1: # need new a namespace
def conv (lines):
def conv (lines):
newlines = []
for x in lines:
- x = regsub.gsub ('\\\\accidentals',
- '\\\\keysignature',x)
- x = regsub.gsub ('specialaccidentals *= *1',
+ x = re.sub ('\\\\accidentals',
+ '\\\\keysignature',x)
+ x = re.sub ('specialaccidentals *= *1',
'keyoctaviation = 0',x)
- x = regsub.gsub ('specialaccidentals *= *0',
+ x = re.sub ('specialaccidentals *= *0',
'keyoctaviation = 1',x)
newlines.append (x)
return newlines
conversions.append (
((1,0,1), conv, '\\accidentals -> \\keysignature, ' +
- 'specialaccidentals -> keyoctaviation\n'))
+ 'specialaccidentals -> keyoctaviation'))
if 1:
def conv(lines):
found = 0
for x in lines:
- if regex.search ('\\\\header', x) <> -1:
+ if re.search ('\\\\header', x):
found = 1
break
if found:
- sys.stderr.write ('\nNot smart enough to convert to new \\header format\n')
+ sys.stderr.write ('\nNot smart enough to convert to new \\header format')
return lines
- conversions.append ((1,0,2), conv, '\header { key = concat + with + operator }\n')
+ conversions.append ((1,0,2), conv, '\\header { key = concat + with + operator }')
+
+if 1:
+ def conv(lines):
+ newlines =[]
+ for x in lines:
+ x = re.sub ('\\\\melodic', '\\\\notes',x)
+ newlines.append (x)
+ return newlines
+
+ conversions.append ((1,0,3), conv, '\\melodic -> \\notes')
+
+if 1:
+ def conv(lines):
+ newlines =[]
+ for x in lines:
+ x = re.sub ('default_paper *=', '',x)
+ x = re.sub ('default_midi *=', '',x)
+ newlines.append (x)
+ return newlines
+
+ conversions.append ((1,0,4), conv, 'default_{paper,midi}')
+
+if 1:
+ def conv(lines):
+ newlines =[]
+ for x in lines:
+ x = re.sub ('ChoireStaff', 'ChoirStaff',x)
+ x = re.sub ('\\output', 'output = ',x)
+ newlines.append (x)
+ return newlines
+
+ conversions.append ((1,0,5), conv, 'ChoireStaff -> ChoirStaff')
+
+if 1:
+ def conv(lines):
+ newlines =[]
+ found = None
+ for x in lines:
+ found = re.search ('[a-zA-Z]+ = *\\translator',x)
+ newlines.append (x)
+ if found: break
+ if found:
+ sys.stderr.write ('\nNot smart enough to \\translator syntax')
+ raise FatalConversionError()
+ return newlines
+
+ conversions.append ((1,0,6), conv, 'foo = \\translator {\\type .. } ->\\translator {\\type ..; foo; }')
+
+
+if 1:
+ def conv(lines):
+ newlines =[]
+ for x in lines:
+ x = re.sub ('\\\\lyric', '\\\\lyrics',x)
+ newlines.append (x)
+ return newlines
+
+ conversions.append ((1,0,7), conv, '\\lyric -> \\lyrics')
+
############################
+
def get_conversions (from_version, to_version):
def version_b (v, f = from_version, t = to_version):
lines = x[1] (lines)
last_conversion = x[0]
- sys.stderr.write ('\n')
except FatalConversionError:
sys.stderr.write ('Error while converting; I won\'t convert any further')
for x in lines:
if last_conversion:
- x = regsub.sub (mudela_version_re_str, '\\version \"%s\"' % tup_to_str (last_conversion), x)
+ x = re.sub (mudela_version_re_str, '\\\\version \"%s\"' % tup_to_str (last_conversion), x)
outfile.write(x)
class UnknownVersion:
pass
def do_one_file (infile_name):
+ sys.stderr.write ('Processing `%s\' ... '% infile_name)
outfile_name = ''
if __main__.edit:
outfile_name = infile_name + '.NEW'
do_conversion (infile, from_version, outfile, to_version)
+ if infile_name:
+ infile.close ()
+
+ if outfile_name:
+ outfile.close ()
+
if __main__.edit:
os.rename (infile_name, infile_name + '~')
os.rename (infile_name + '.NEW', infile_name)
+ sys.stderr.write ('\n')
+ sys.stderr.flush ()
edit = 0
to_version = ()