]> git.donarmstrong.com Git - lilypond.git/blobdiff - scripts/convert-mudela.py
partial: 1.3.107.jcn
[lilypond.git] / scripts / convert-mudela.py
index 983abe0adfc36bfc9f4324e0a70f98c8d795f54b..8d86614ddb38feee70561aba8a4847fff83c23f7 100644 (file)
@@ -20,7 +20,7 @@ import os
 import sys
 import __main__
 import getopt
-from string import *
+import  string
 import re
 import time
 
@@ -81,10 +81,10 @@ def gulp_file(f):
        return s
 
 def str_to_tuple (s):
-       return tuple (map (atoi, split (s,'.')))
+       return tuple (map (string.atoi, string.split (s,'.')))
 
 def tup_to_str (t):
-       return join (map (lambda x: '%s' % x, list (t)), '.')
+       return string.join (map (lambda x: '%s' % x, list (t)), '.')
 
 def version_cmp (t1, t2):
        for x in [0,1,2]:
@@ -112,22 +112,22 @@ def show_rules (file):
 ############################
                
 if 1:                                  # need new a namespace
-       def conv (lines):
+       def conv (str):
                if re.search ('\\\\octave', str):
                        sys.stderr.write ('\nNot smart enough to convert \\octave')
                        raise FatalConversionError()
                
-               return lines
+               return str
 
        conversions.append (
                ((0,1,19), conv, 'deprecated \\octave; can\'t convert automatically'))
 
 
 if 1:                                  # need new a namespace
-       def conv (lines):
-               x = re.sub ('\\\\textstyle([^;]+);',
+       def conv (str):
+               str = re.sub ('\\\\textstyle([^;]+);',
                                         '\\\\property Lyrics . textstyle = \\1', str)
-               x = re.sub ('\\\\key([^;]+);', '\\\\accidentals \\1;', str)
+               str = re.sub ('\\\\key([^;]+);', '\\\\accidentals \\1;', str)
                        
                return str
 
@@ -136,9 +136,9 @@ if 1:                                       # need new a namespace
 
 
 if 1:
-       def conv (lines):
-               x = re.sub ('\\\\musical_pitch', '\\\\musicalpitch',str)
-               x = re.sub ('\\\\meter', '\\\\time',str)
+       def conv (str):
+               str = re.sub ('\\\\musical_pitch', '\\\\musicalpitch',str)
+               str = re.sub ('\\\\meter', '\\\\time',str)
                        
                return str
 
@@ -147,18 +147,18 @@ if 1:
                 '\\meter -> \\time'))
 
 if 1:
-       def conv (lines):
-               return lines
+       def conv (str):
+               return str
 
        conversions.append (
                ((1,0,0), conv, '0.1.21 -> 1.0.0 '))
 
 
 if 1:
-       def conv (lines):
-               x = re.sub ('\\\\accidentals', '\\\\keysignature',str)
-               x = re.sub ('specialaccidentals *= *1', 'keyoctaviation = 0',str)
-               x = re.sub ('specialaccidentals *= *0', 'keyoctaviation = 1',str)
+       def conv (str):
+               str = re.sub ('\\\\accidentals', '\\\\keysignature',str)
+               str = re.sub ('specialaccidentals *= *1', 'keyoctaviation = 0',str)
+               str = re.sub ('specialaccidentals *= *0', 'keyoctaviation = 1',str)
                        
                return str
 
@@ -167,41 +167,41 @@ if 1:
                 'specialaccidentals -> keyoctaviation'))
 
 if 1:
-       def conv(lines):
-               if re.search ('\\\\header', lines):
+       def conv(str):
+               if re.search ('\\\\header', str):
                        sys.stderr.write ('\nNot smart enough to convert to new \\header format')
-               return lines
+               return str
        
        conversions.append ((1,0,2), conv, '\\header { key = concat + with + operator }')
 
 if 1:
-       def conv(lines):
-               x =  re.sub ('\\\\melodic', '\\\\notes',str)
+       def conv(str):
+               str =  re.sub ('\\\\melodic', '\\\\notes',str)
                        
                return str
        
        conversions.append ((1,0,3), conv, '\\melodic -> \\notes')
 
 if 1:
-       def conv(lines):
-               x =  re.sub ('default_paper *=', '',str)
-               x =  re.sub ('default_midi *=', '',x)                   
+       def conv(str):
+               str =  re.sub ('default_paper *=', '',str)
+               str =  re.sub ('default_midi *=', '',x)                 
                        
                return str
        
        conversions.append ((1,0,4), conv, 'default_{paper,midi}')
 
 if 1:
-       def conv(lines):
-               x =  re.sub ('ChoireStaff', 'ChoirStaff',str)
-               x =  re.sub ('\\output', 'output = ',str)
+       def conv(str):
+               str =  re.sub ('ChoireStaff', 'ChoirStaff',str)
+               str =  re.sub ('\\output', 'output = ',str)
                        
                return str
        
        conversions.append ((1,0,5), conv, 'ChoireStaff -> ChoirStaff')
 
 if 1:
-       def conv(lines):
+       def conv(str):
                if re.search ('[a-zA-Z]+ = *\\translator',str):
                        sys.stderr.write ('\nNot smart enough to change \\translator syntax')
                        raise FatalConversionError()
@@ -211,46 +211,46 @@ if 1:
 
 
 if 1:
-       def conv(lines):
-               x =  re.sub ('\\\\lyric', '\\\\lyrics',str)
+       def conv(str):
+               str =  re.sub ('\\\\lyric', '\\\\lyrics',str)
                        
                return str
        
        conversions.append ((1,0,7), conv, '\\lyric -> \\lyrics')
 
 if 1:
-       def conv(lines):
-               x =  re.sub ('\\\\\\[/3+', '\\\\times 2/3 { ',str)
-               x =  re.sub ('\\[/3+', '\\\\times 2/3 { [',str)
-               x =  re.sub ('\\\\\\[([0-9/]+)', '\\\\times \\1 {',str)
-               x =  re.sub ('\\[([0-9/]+)', '\\\\times \\1 { [',str)
-               x =  re.sub ('\\\\\\]([0-9/]+)', '}', str)
-               x =  re.sub ('\\\\\\]', '}',str)
-               x =  re.sub ('\\]([0-9/]+)', '] }', str)
+       def conv(str):
+               str =  re.sub ('\\\\\\[/3+', '\\\\times 2/3 { ',str)
+               str =  re.sub ('\\[/3+', '\\\\times 2/3 { [',str)
+               str =  re.sub ('\\\\\\[([0-9/]+)', '\\\\times \\1 {',str)
+               str =  re.sub ('\\[([0-9/]+)', '\\\\times \\1 { [',str)
+               str =  re.sub ('\\\\\\]([0-9/]+)', '}', str)
+               str =  re.sub ('\\\\\\]', '}',str)
+               str =  re.sub ('\\]([0-9/]+)', '] }', str)
                return str
        
        conversions.append ((1,0,10), conv, '[2/3 ]1/1 -> \\times 2/3 ')
 
 if 1:
-       def conv(lines):
-               return lines
+       def conv(str):
+               return str
        conversions.append ((1,0,12), conv, 'Chord syntax stuff')
 
 
 if 1:
-       def conv(lines):
+       def conv(str):
                
                
-               x =  re.sub ('<([^>~]+)~([^>]*)>','<\\1 \\2> ~', str)
+               str =  re.sub ('<([^>~]+)~([^>]*)>','<\\1 \\2> ~', str)
                        
                return str
        
        conversions.append ((1,0,13), conv, '<a ~ b> c -> <a b> ~ c')
 
 if 1:
-       def conv(lines):
-               x =  re.sub ('<\\[','[<', str)
-               x =  re.sub ('\\]>','>]', str)
+       def conv(str):
+               str =  re.sub ('<\\[','[<', str)
+               str =  re.sub ('\\]>','>]', str)
                        
                return str
        
@@ -258,9 +258,9 @@ if 1:
 
 
 if 1:
-       def conv(lines):
-               x =  re.sub ('\\\\type','\\\\context', str)
-               x =  re.sub ('textstyle','textStyle', str)
+       def conv(str):
+               str =  re.sub ('\\\\type','\\\\context', str)
+               str =  re.sub ('textstyle','textStyle', str)
                        
                return str
        
@@ -268,7 +268,7 @@ if 1:
 
 
 if 1:
-       def conv(lines):
+       def conv(str):
                if re.search ('\\\\repeat',str):
                        sys.stderr.write ('\nNot smart enough to convert \\repeat')
                        raise FatalConversionError()
@@ -278,10 +278,10 @@ if 1:
                            '\\repeat NUM Music Alternative -> \\repeat FOLDSTR Music Alternative')
 
 if 1:
-       def conv(lines):
-               x =  re.sub ('SkipBars','skipBars', str)
-               x =  re.sub ('fontsize','fontSize', str)
-               x =  re.sub ('midi_instrument','midiInstrument', x)                     
+       def conv(str):
+               str =  re.sub ('SkipBars','skipBars', str)
+               str =  re.sub ('fontsize','fontSize', str)
+               str =  re.sub ('midi_instrument','midiInstrument', str)                 
                        
                return str
 
@@ -290,10 +290,10 @@ if 1:
 
 
 if 1:
-       def conv(lines):
-               x =  re.sub ('tieydirection','tieVerticalDirection', str)
-               x =  re.sub ('slurydirection','slurVerticalDirection', str)
-               x =  re.sub ('ydirection','verticalDirection', x)                       
+       def conv(str):
+               str =  re.sub ('tieydirection','tieVerticalDirection', str)
+               str =  re.sub ('slurydirection','slurVerticalDirection', str)
+               str =  re.sub ('ydirection','verticalDirection', str)                   
                        
                return str
 
@@ -302,8 +302,8 @@ if 1:
 
 
 if 1:
-       def conv(lines):
-               x =  re.sub ('hshift','horizontalNoteShift', str)
+       def conv(str):
+               str =  re.sub ('hshift','horizontalNoteShift', str)
                        
                return str
 
@@ -312,8 +312,8 @@ if 1:
 
 
 if 1:
-       def conv(lines):
-               x =  re.sub ('\\\\grouping[^;]*;','', str)
+       def conv(str):
+               str =  re.sub ('\\\\grouping[^;]*;','', str)
                        
                return str
 
@@ -322,8 +322,8 @@ if 1:
 
 
 if 1:
-       def conv(lines):
-               x =  re.sub ('\\\\wheel','\\\\coda', str)
+       def conv(str):
+               str =  re.sub ('\\\\wheel','\\\\coda', str)
                        
                return str
 
@@ -331,9 +331,9 @@ if 1:
                            '\\wheel -> \\coda')
 
 if 1:
-       def conv(lines):
-               x =  re.sub ('keyoctaviation','keyOctaviation', str)
-               x =  re.sub ('slurdash','slurDash', str)
+       def conv(str):
+               str =  re.sub ('keyoctaviation','keyOctaviation', str)
+               str =  re.sub ('slurdash','slurDash', str)
                        
                return str
 
@@ -341,8 +341,8 @@ if 1:
                            'slurdash -> slurDash, keyoctaviation -> keyOctaviation')
 
 if 1:
-       def conv(lines):
-               x =  re.sub ('\\\\repeat *\"?semi\"?','\\\\repeat "volta"', str)
+       def conv(str):
+               str =  re.sub ('\\\\repeat *\"?semi\"?','\\\\repeat "volta"', str)
                        
                return str
 
@@ -351,8 +351,8 @@ if 1:
 
 
 if 1:
-       def conv(lines):
-               x =  re.sub ('\"?beamAuto\"? *= *\"?0?\"?','noAutoBeaming = "1"', str)
+       def conv(str):
+               str =  re.sub ('\"?beamAuto\"? *= *\"?0?\"?','noAutoBeaming = "1"', str)
                        
                return str
 
@@ -360,8 +360,8 @@ if 1:
                            'beamAuto -> noAutoBeaming')
 
 if 1:
-       def conv(lines):
-               x =  re.sub ('automaticMelismas', 'automaticMelismata', str)
+       def conv(str):
+               str =  re.sub ('automaticMelismas', 'automaticMelismata', str)
                        
                return str
 
@@ -369,14 +369,199 @@ if 1:
                            'automaticMelismas -> automaticMelismata')
 
 if 1:
-       def conv(lines):
-               x =  re.sub ('dynamicDir', 'dynamicDirection', str)
+       def conv(str):
+               str =  re.sub ('dynamicDir\\b', 'dynamicDirection', str)
                        
                return str
 
        conversions.append ((1,2,1), conv,
                            'dynamicDir -> dynamicDirection')
 
+if 1:
+       def conv(str):
+               str =  re.sub ('\\\\cadenza *0 *;', '\\\\cadenzaOff', str)
+               str =  re.sub ('\\\\cadenza *1 *;', '\\\\cadenzaOn', str)               
+                       
+               return str
+
+       conversions.append ((1,3,4), conv,
+                           '\\cadenza -> \cadenza{On|Off}')
+
+if 1:
+       def conv (str):
+               str = re.sub ('beamAuto([^=]+)= *"([0-9]+)/([0-9]+)" *;',
+                             'beamAuto\\1 = #(make-moment \\2 \\3)',
+                             str)
+               return str
+
+       conversions.append ((1,3,5), conv, 'beamAuto moment properties')
+
+if 1:
+       def conv (str):
+               str = re.sub ('stemStyle',
+                             'flagStyle',
+                             str)
+               return str
+
+       conversions.append ((1,3,17), conv, 'stemStyle -> flagStyle')
+
+if 1:
+       def conv (str):
+               str = re.sub ('staffLineLeading',
+                             'staffSpace',
+                             str)
+               return str
+
+       conversions.append ((1,3,18), conv, 'staffLineLeading -> staffSpace')
+
+if 1:
+       def conv (str):
+               str = re.sub ('textEmptyDimension *= *##t',
+                             'textNonEmpty = ##f',
+                             str)
+               str = re.sub ('textEmptyDimension *= *##f',
+                             'textNonEmpty = ##t',
+                             str)
+               return str
+
+       conversions.append ((1,3,35), conv, 'textEmptyDimension -> textNonEmpty')
+
+if 1:
+       def conv (str):
+               str = re.sub ("([a-z]+)[ \t]*=[ \t]*\\\\musicalpitch *{([- 0-9]+)} *\n",
+                             "(\\1 . (\\2))\n", str)
+               str = re.sub ("\\\\musicalpitch *{([0-9 -]+)}",
+                             "\\\\musicalpitch #'(\\1)", str)
+               if re.search ('\\\\notenames',str):
+                       sys.stderr.write ('\nNot smart enough to convert to new \\notenames format')
+               return str
+
+       conversions.append ((1,3,38), conv, '\musicalpitch { a b c } -> #\'(a b c)')
+
+if 1:
+       def conv (str):
+               def replace (match):
+                       return '\\key %s;' % string.lower (match.group (1))
+               
+               str = re.sub ("\\\\key ([^;]+);",  replace, str)
+               return str
+       
+       conversions.append ((1,3,39), conv, '\\key A ;  ->\\key a;')
+
+if 1:
+       def conv (str):
+               if re.search ('\\[:',str):
+                       sys.stderr.write ('\nNot smart enough to convert to new tremolo format')
+               return str
+
+       conversions.append ((1,3,41), conv,
+                           '[:16 c4 d4 ] -> \\repeat "tremolo" 2 { c16 d16 }')
+
+if 1:
+       def conv (str):
+               str = re.sub ('Staff_margin_engraver' , 'Instrument_name_engraver', str)
+               return str
+
+       conversions.append ((1,3,42), conv,
+                           'Staff_margin_engraver deprecated, use Instrument_name_engraver')
+
+if 1:
+       def conv (str):
+               str = re.sub ('note[hH]eadStyle\\s*=\\s*"?(\\w+)"?' , "noteHeadStyle = #'\\1", str)
+               return str
+
+       conversions.append ((1,3,49), conv,
+                           'noteHeadStyle value: string -> symbol')
+
+if 1:
+       def conv (str):
+               str = re.sub (r"""\\key *([a-z]+) *;""", r"""\\key \1 \major;""",str);
+               return str
+       conversions.append ((1,3,59), conv,
+                           '\key X ; -> \key X major; ') 
+
+if 1:
+       def conv (str):
+               str = re.sub (r'latexheaders *= *"\\\\input ',
+                             'latexheaders = "',
+                             str)
+               return str
+       conversions.append ((1,3,68), conv, 'latexheaders = "\\input global" -> latexheaders = "global"')
+
+
+
+################ TODO: lots of other syntax change should be done here as well
+
+
+
+if 1:
+       def conv (str):
+               str = re.sub ('basicCollisionProperties', 'NoteCollision', str)
+               str = re.sub ('basicVoltaSpannerProperties' , "VoltaBracket", str)
+               str = re.sub ('basicKeyProperties' , "KeySignature", str)
+
+               str = re.sub ('basicClefItemProperties' ,"Clef", str)
+
+
+               str = re.sub ('basicLocalKeyProperties' ,"Accidentals", str)
+               str = re.sub ('basicMarkProperties' ,"Accidentals", str)                                
+               str = re.sub ('basic([A-Za-z_]+)Properties', '\\1', str)
+               return str
+       
+       conversions.append ((1,3,92), conv, 'basicXXXProperties -> XXX')
+
+if 1:
+       def conv (str):
+               str = re.sub ('\\\\stemup', '\\\\stemUp', str)
+               str = re.sub ('\\\\stemdown', '\\\\stemDown', str)
+               str = re.sub ('\\\\stemboth', '\\\\stemBoth', str)
+               str = re.sub ('\\\\slurup', '\\\\slurUp', str)
+               str = re.sub ('\\\\slurboth', '\\\\slurBoth', str)
+               str = re.sub ('\\\\slurdown', '\\\\slurDown', str)
+               str = re.sub ('\\\\slurdotted', '\\\\slurDotted', str)
+               str = re.sub ('\\\\slurnormal', '\\\\slurNoDots', str)          
+               
+               str = re.sub ('\\\\shiftoff', '\\\\shiftOff', str)
+               str = re.sub ('\\\\shifton', '\\\\shiftOn', str)
+               str = re.sub ('\\\\shiftonn', '\\\\shiftOnn', str)
+               str = re.sub ('\\\\shiftonnn', '\\\\shiftOnnn', str)
+
+               str = re.sub ('\\\\onevoice', '\\\\oneVoice', str)
+               str = re.sub ('\\\\voiceone', '\\\\voiceOne', str)
+               str = re.sub ('\\\\voicetwo', '\\\\voiceTwo', str)
+               str = re.sub ('\\\\voicethree', '\\\\voiceThree', str)
+               str = re.sub ('\\\\voicefour', '\\\\voiceFour', str)
+               
+               return str
+       
+       conversions.append ((1,3,93), conv,
+                           'property definiton case (eg. onevoice -> oneVoice)')
+
+
+if 1:
+       def conv (str):
+               str = re.sub ('ChordNames*', 'ChordNames', str)
+               if re.search ('\\\\textscript "[^"]* *"[^"]*"', str):
+                       sys.stderr.write ('\nNot smart enough to convert to new \\textscript markup text')
+
+               str = re.sub ('\\textscript +("[^"]*")', '\\textscript #\\1', str)
+
+               return str
+       
+       conversions.append ((1,3,97), conv, 'ChordName -> ChordNames')
+
+if 1:
+       def conv (str):
+               str = re.sub ('beamAutoEnd_([0-9]*) *= *(#\\([^)]*\\))', 'autoBeamSettings \\push #\'(end 1 \\1 * *) = \\2', str)
+               str = re.sub ('beamAutoBegin_([0-9]*) *= *(#\\([^)]*\))', 'autoBeamSettings \\push #\'(begin 1 \\1 * *) = \\2', str)
+               str = re.sub ('beamAutoEnd *= *(#\\([^)]*\\))', 'autoBeamSettings \\push #\'(end * * * *) = \\1', str)
+               str = re.sub ('beamAutoBegin *= *(#\\([^)]*\\))', 'autoBeamSettings \\push #\'(begin * * * *) = \\1', str)
+
+
+               return str
+       
+       conversions.append ((1,3,102), conv, 'beamAutoEnd -> autoBeamSettings \\push (end * * * *)')
+
 ############################
        
 
@@ -390,11 +575,10 @@ def latest_version ():
        return conversions[-1][0]
 
 def do_conversion (infile, from_version, outfile, to_version):
-       
        conv_list = get_conversions (from_version, to_version)
 
        sys.stderr.write ('Applying conversions: ')
-       str = infile.read (-1)
+       str = infile.read ()
        last_conversion = ()
        try:
                for x in conv_list:
@@ -408,13 +592,15 @@ def do_conversion (infile, from_version, outfile, to_version):
        if last_conversion:
                sys.stderr.write ('\n')
                new_ver =  '\\\\version \"%s\"' % tup_to_str (last_conversion)
-               if re.search (mudela_version_re_str):
+               if re.search (mudela_version_re_str, str):
                        str = re.sub (mudela_version_re_str,new_ver , str)
                else:
                        str = new_ver + '\n' + str
 
                outfile.write(str)
 
+       return last_conversion
+       
 class UnknownVersion:
        pass
 
@@ -450,8 +636,7 @@ def do_one_file (infile_name):
        else:
                outfile = sys.stdout
 
-       
-       do_conversion (infile, from_version, outfile, to_version)
+       touched = do_conversion (infile, from_version, outfile, to_version)
 
        if infile_name:
                infile.close ()
@@ -459,7 +644,7 @@ def do_one_file (infile_name):
        if outfile_name:
                outfile.close ()
 
-       if __main__.edit:
+       if __main__.edit and touched:
                try:
                        os.remove(infile_name + '~')
                except:
@@ -482,7 +667,8 @@ for opt in options:
        o = opt[0]
        a = opt[1]
        if o== '--help' or o == '-h':
-               help ()
+               usage ()
+               sys.exit (0)
        if o == '--version' or o == '-v':
                print_version ()
                sys.exit (0)