]> git.donarmstrong.com Git - lilypond.git/blobdiff - scripts/convert-mudela.py
release: 1.1.69
[lilypond.git] / scripts / convert-mudela.py
index 6481c5b77b5876abb058a40aec6e9f6a7b561403..39b46d05e38dda73f5b0a7f1657b535f0b46f12b 100644 (file)
 #  - rewrite in python
 
 program_name = 'convert-mudela'
-version = '0.3'
-
+version = '0.4'
 
 import os
 import sys
 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);
@@ -49,7 +48,6 @@ def gulp_file(f):
        i.close ()
        return s
 
-
 def str_to_tuple (s):
        return tuple (map (atoi, split (s,'.')))
 
@@ -65,8 +63,9 @@ def version_cmp (t1, t2):
 
 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 ''
 
@@ -97,11 +96,11 @@ if 1:                                       # need new a namespace
        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
                
@@ -114,9 +113,9 @@ if 1:                                       # need new a namespace
        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
                
@@ -125,13 +124,13 @@ if 1:                                     # need new a namespace
                ((0,1,20), conv, 'deprecated \\textstyle, new \key syntax'))
 
 
-if 1:                                  # need new a namespace
+if 1:
        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
@@ -139,9 +138,9 @@ if 1:                                       # need new a namespace
 
        conversions.append (
                ((0,1,21), conv, '\\musical_pitch -> \\musicalpitch, '+
-                '\\meter -> \\time\n'))
+                '\\meter -> \\time'))
 
-if 1:                                  # need new a namespace
+if 1:
        def conv (lines):
                return lines
 
@@ -149,15 +148,15 @@ if 1:                                     # need new a namespace
                ((1,0,0), conv, '0.1.21 -> 1.0.0 '))
 
 
-if 1:                                  # need new a namespace
+if 1:
        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
@@ -165,67 +164,253 @@ if 1:                                    # need new a namespace
 
        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 =  regsub.gsub ('\\\\melodic', '\\\\notes',x)
+                       x =  re.sub ('\\\\melodic', '\\\\notes',x)
                        newlines.append (x)
                return newlines
        
-       conversions.append ((1,0,3), conv, '\melodic -> \notes\n')
+       conversions.append ((1,0,3), conv, '\\melodic -> \\notes')
+
 if 1:
        def conv(lines):
                newlines =[]
                for x in lines:
-                       x =  regsub.gsub ('default_paper *=', '',x)
-                       x =  regsub.gsub ('default_midi *=', '',x)                      
+                       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}\n')
+       conversions.append ((1,0,4), conv, 'default_{paper,midi}')
 
 if 1:
        def conv(lines):
                newlines =[]
                for x in lines:
-                       x =  regsub.gsub ('ChoireStaff', 'ChoirStaff',x)
-                       x =  regsub.gsub ('\\output', 'output = ',x)
+                       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\n')
+       conversions.append ((1,0,5), conv, 'ChoireStaff -> ChoirStaff')
 
 if 1:
        def conv(lines):
                newlines =[]
-               found = 0
+               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')
+
+if 1:
+       def conv(lines):
+               newlines =[]
+               for x in lines:
+                       x =  re.sub ('\\\\\\[/3+', '\\\\times 2/3 { ',x)
+                       x =  re.sub ('\\[/3+', '\\\\times 2/3 { [',x)
+                       x =  re.sub ('\\\\\\[([0-9/]+)', '\\\\times \\1 {',x)
+                       x =  re.sub ('\\[([0-9/]+)', '\\\\times \\1 { [',x)
+                       x =  re.sub ('\\\\\\]([0-9/]+)', '}', x)
+                       x =  re.sub ('\\\\\\]', '}',x)
+                       x =  re.sub ('\\]([0-9/]+)', '] }', x)
+
+                       newlines.append (x)
+               return newlines
+       
+       conversions.append ((1,0,10), conv, '[2/3 ]1/1 -> \\times 2/3 ')
+
+if 1:
+       def conv(lines):
+               return lines
+       conversions.append ((1,0,12), conv, 'Chord syntax stuff')
+
+
+if 1:
+       def conv(lines):
+               newlines =[]
+               for x in lines:
+                       x =  re.sub ('<([^>~]+)~([^>]*)>','<\\1 \\2> ~', x)
+                       newlines.append (x)
+               return newlines
+       
+       conversions.append ((1,0,13), conv, '<a ~ b> c -> <a b> ~ c')
+
+if 1:
+       def conv(lines):
+               newlines =[]
+               for x in lines:
+                       x =  re.sub ('<\\[','[<', x)
+                       x =  re.sub ('\\]>','>]', x)
+                       newlines.append (x)
+               return newlines
+       
+       conversions.append ((1,0,14), conv, '<[a b> <a b]>c -> [<a b> <a b>]')
+
+
+if 1:
+       def conv(lines):
+               newlines =[]
+               for x in lines:
+                       x =  re.sub ('\\\\type','\\\\context', x)
+                       x =  re.sub ('textstyle','textStyle', x)
+                       newlines.append (x)
+               return newlines
+       
+       conversions.append ((1,0,16), conv, '\\type -> \\context, textstyle -> textStyle')
+
+
+if 1:
+       def conv(lines):
+               newlines =[]
+               found = None
                for x in lines:
-                       found = regex.search ('[a-zA-Z]+ = *\\translator',x) <> -1
+                       found = re.search ('\\\\repeat',x)
                        newlines.append (x)
                        if found: break
                if found:
-                       sys.stderr.write ('\nNot smart enough to \\translator syntax\n')
+                       sys.stderr.write ('\nNot smart enough to convert \\repeat')
                        raise FatalConversionError()
                return newlines
        
-       conversions.append ((1,0,6), conv, 'foo = \\translator {\type .. } ->\translator {\type ..; foo; }\n')
+       conversions.append ((1,0,18), conv,
+                           '\\repeat NUM Music Alternative -> \\repeat FOLDSTR Music Alternative')
+
+if 1:
+       def conv(lines):
+               newlines =[]
+               for x in lines:
+                       x =  re.sub ('SkipBars','skipBars', x)
+                       x =  re.sub ('fontsize','fontSize', x)
+                       x =  re.sub ('midi_instrument','midiInstrument', x)                     
+                       newlines.append (x)
+               return newlines
+
+       conversions.append ((1,0,19), conv,
+                           'fontsize -> fontSize, midi_instrument -> midiInstrument, SkipBars -> skipBars')
+
+
+if 1:
+       def conv(lines):
+               newlines =[]
+               for x in lines:
+                       x =  re.sub ('tieydirection','tieVerticalDirection', x)
+                       x =  re.sub ('slurydirection','slurVerticalDirection', x)
+                       x =  re.sub ('ydirection','verticalDirection', x)                       
+                       newlines.append (x)
+               return newlines
+
+       conversions.append ((1,0,20), conv,
+                           '{,tie,slur}ydirection -> {v,tieV,slurV}erticalDirection')
+
+
+if 1:
+       def conv(lines):
+               newlines =[]
+               for x in lines:
+                       x =  re.sub ('hshift','horizontalNoteShift', x)
+                       newlines.append (x)
+               return newlines
+
+       conversions.append ((1,0,21), conv,
+                           'hshift -> horizontalNoteShift')
+
+
+if 1:
+       def conv(lines):
+               newlines =[]
+               for x in lines:
+                       x =  re.sub ('\\\\grouping[^;]*;','', x)
+                       newlines.append (x)
+               return newlines
+
+       conversions.append ((1,1,52), conv,
+                           'deprecate \\grouping')
+
+
+if 1:
+       def conv(lines):
+               newlines =[]
+               for x in lines:
+                       x =  re.sub ('\\\\wheel','\\\\coda', x)
+                       newlines.append (x)
+               return newlines
+
+       conversions.append ((1,1,55), conv,
+                           '\\wheel -> \\coda')
+
+if 1:
+       def conv(lines):
+               newlines =[]
+               for x in lines:
+                       x =  re.sub ('keyoctaviation','keyOctaviation', x)
+                       x =  re.sub ('slurdash','slurDash', x)
+                       newlines.append (x)
+               return newlines
+
+       conversions.append ((1,1,65), conv,
+                           'slurdash -> slurDash, keyoctaviation -> keyOctaviation')
+
+if 1:
+       def conv(lines):
+               newlines =[]
+               for x in lines:
+                       x =  re.sub ('\\repeat *\"?semi\"?','\\repeat "volta"', x)
+                       newlines.append (x)
+               return newlines
+
+       conversions.append ((1,1,66), conv,
+                           'semi -> volta')
+
+
+if 1:
+       def conv(lines):
+               newlines =[]
+               for x in lines:
+                       x =  re.sub ('\"?beamAuto\"? *= *\"?0?\"?','noAutoBeaming = "1"', x)
+                       newlines.append (x)
+               return newlines
+
+       conversions.append ((1,1,67), conv,
+                           'beamAuto -> noAutoBeaming, automaticMelismas -> automaticMelismata',)
+
+
 ############################
+       
 
 def get_conversions (from_version, to_version):
        def version_b (v, f = from_version, t = to_version):
@@ -255,7 +440,7 @@ def do_conversion (infile, from_version, outfile, to_version):
 
        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:
@@ -303,10 +488,15 @@ def do_one_file (infile_name):
                outfile.close ()
 
        if __main__.edit:
+               try:
+                       os.remove(infile_name + '~')
+               except:
+                       pass
                os.rename (infile_name, infile_name + '~')
                os.rename (infile_name + '.NEW', infile_name)
 
        sys.stderr.write ('\n')
+       sys.stderr.flush ()
 
 edit = 0
 to_version = ()
@@ -315,7 +505,7 @@ outfile_name = ''
 
 identify ()
 (options, files) = getopt.getopt (
-       sys.argv[1:], 'f:t:seh', ['show-rules', 'help', 'edit', 'from', 'to'])
+       sys.argv[1:], 'o:f:t:seh', ['output', 'show-rules', 'help', 'edit', 'from', 'to'])
 
 for opt in options:
        o = opt[0]
@@ -331,6 +521,8 @@ for opt in options:
        elif o== '--show-rules' or o == '-s':
                show_rules (sys.stdout)
                sys.exit(0)
+       elif o == '--output' or o == '-o':
+               outfile_name = a
        else:
                print o
                raise getopt.error