X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=scripts%2Fmup2ly.py;h=def68308d66679d84b4eb3c5af03e86d6d82d8fd;hb=26c5f769a9b941b000b543f4f559e0d05be9f10f;hp=0bb9cbf01b8f55073015ffb8876de4f92dea9334;hpb=68a168bff1fec7f8011b2afa82d4fc89182c4bf7;p=lilypond.git diff --git a/scripts/mup2ly.py b/scripts/mup2ly.py index 0bb9cbf01b..def68308d6 100644 --- a/scripts/mup2ly.py +++ b/scripts/mup2ly.py @@ -34,39 +34,70 @@ import __main__ import operator import tempfile -# let's not yet clutter lily's po with this mup converter junk -def _ (s): - return s -#sys.path.append ('@datadir@/python') -#import gettext -#gettext.bindtextdomain ('lilypond', '@localedir@') -#gettext.textdomain('lilypond') -#_ = gettext.gettext +# if set, LILYPONDPREFIX must take prevalence +# if datadir is not set, we're doing a build and LILYPONDPREFIX +datadir = '@local_lilypond_datadir@' +if os.environ.has_key ('LILYPONDPREFIX') \ + or '@local_lilypond_datadir@' == '@' + 'local_lilypond_datadir' + '@': + datadir = os.environ['LILYPONDPREFIX'] +else: + datadir = '@local_lilypond_datadir@' +sys.path.append (os.path.join (datadir, 'python')) +sys.path.append (os.path.join (datadir, 'python/out')) +program_name = sys.argv[0] +program_version = '@TOPLEVEL_VERSION@' +original_dir = os.getcwd () +temp_dir = os.path.join (original_dir, '%s.dir' % program_name) +errorport = sys.stderr +keep_temp_dir_p = 0 +verbose_p = 0 + +localedir = '@localedir@' +try: + import gettext + gettext.bindtextdomain ('lilypond', localedir) + gettext.textdomain ('lilypond') + _ = gettext.gettext +except: + def _ (s): + return s program_name = 'mup2ly' -help_summary = _("Convert mup to ly") -output = 0 +help_summary = _ ("Convert mup to LilyPond source.") + +option_definitions = [ + ('', 'd', 'debug', _ ("debug")), + ('NAME[=EXP]', 'D', 'define', _ ("define macro NAME [optional expansion EXP]")), + ('', 'h', 'help', _ ("print this help")), + ('FILE', 'o', 'output', _ ("write output to FILE")), + ('', 'E', 'pre-process', _ ("only pre-process")), + ('', 'V', 'verbose', _ ("be verbose")), + ('', 'v', 'version', _ ("print version number")), + ('', 'w', 'warranty', _ ("show warranty and copyright")), + ] -# lily_py.py -- options and stuff + +################################################################ +# lilylib.py -- options and stuff # # source file of the GNU LilyPond music typesetter -# BEGIN Library for these? -# cut-n-paste from ly2dvi +try: + import gettext + gettext.bindtextdomain ('lilypond', localedir) + gettext.textdomain ('lilypond') + _ = gettext.gettext +except: + def _ (s): + return s program_version = '@TOPLEVEL_VERSION@' if program_version == '@' + 'TOPLEVEL_VERSION' + '@': - program_version = '1.3.142' - - -original_dir = os.getcwd () -temp_dir = '%s.dir' % program_name -keep_temp_dir_p = 0 -verbose_p = 0 + program_version = '1.5.54' def identify (): sys.stdout.write ('%s (GNU LilyPond) %s\n' % (program_name, program_version)) @@ -74,29 +105,37 @@ def identify (): def warranty (): identify () sys.stdout.write ('\n') - sys.stdout.write (_ ('Copyright (c) %s by' % ' 2001')) + sys.stdout.write (_ ('Copyright (c) %s by') % '2001--2006') sys.stdout.write ('\n') sys.stdout.write (' Han-Wen Nienhuys') sys.stdout.write (' Jan Nieuwenhuizen') - sys.stdout.write ('\n') - sys.stdout.write (_ (r''' -Distributed under terms of the GNU General Public License. It comes with -absolutely NO WARRANTY.''')) + sys.stdout.write ('\n\n') + sys.stdout.write (_ ("Distributed under terms of the GNU General Public License.")) + sys.stdout.write (_ ("It comes with NO WARRANTY.")) sys.stdout.write ('\n') def progress (s): - if s[-1] != '\n': - s = s + '\n' - sys.stderr.write (s) + errorport.write (s + '\n') def warning (s): - sys.stderr.write (_ ("warning: ") + s) - sys.stderr.write ('\n') + progress (_ ("warning: ") + s) + +def user_error (s, e=1): + errorport.write (program_name + ":" + _ ("error: ") + s + '\n') + sys.exit (e) - def error (s): - sys.stderr.write (_ ("error: ") + s) - sys.stderr.write ('\n') + '''Report the error S. Exit by raising an exception. Please + do not abuse by trying to catch this error. If you do not want + a stack trace, write to the output directly. + + RETURN VALUE + + None + + ''' + + progress (_ ("error: ") + s) raise _ ("Exiting ... ") def getopt_args (opts): @@ -121,9 +160,9 @@ def option_help_str (o): if o[1]: sh = '-%s' % o[1] - sep = ' ' + sep = ' ' if o[1] and o[2]: - sep = ',' + sep = ', ' long = '' if o[2]: @@ -155,22 +194,24 @@ def options_help_str (opts): return str def help (): - sys.stdout.write (_ ("Usage: %s [OPTION]... FILE") % program_name) - sys.stdout.write ('\n\n') - sys.stdout.write (help_summary) - sys.stdout.write ('\n\n') - sys.stdout.write (_ ("Options:")) - sys.stdout.write ('\n') - sys.stdout.write (options_help_str (option_definitions)) - sys.stdout.write ('\n') - warning (_ ("%s is far from completed. Not all constructs are recognised.") % program_name) - sys.stdout.write ('\n') - sys.stdout.write (_ ("Report bugs to %s") % 'bug-gnu-music@gnu.org') - sys.stdout.write ('\n') - sys.exit (0) - - + ls = [(_ ("Usage: %s [OPTIONS]... FILE") % program_name), + ('\n\n'), + (help_summary), + ('\n\n'), + (_ ("Options:")), + ('\n'), + (options_help_str (option_definitions)), + ('\n\n'), + (_ ("Report bugs to %s.") % + "http://post.gmane.org/post.php?group=gmane.comp.gnu.lilypond.bugs"), + ('\n')] + map (sys.stdout.write, ls) + def setup_temp (): + """ + Create a temporary directory, and return its name. + """ + global temp_dir if not keep_temp_dir_p: temp_dir = tempfile.mktemp (program_name) @@ -178,16 +219,28 @@ def setup_temp (): os.mkdir (temp_dir, 0777) except OSError: pass - + + return temp_dir + + +def system (cmd, ignore_error = 0, quiet =0): + """Run CMD. If IGNORE_ERROR is set, don't complain when CMD returns non zero. + + RETURN VALUE + + Exit status of CMD + """ -def system (cmd, ignore_error = 0): if verbose_p: progress (_ ("Invoking `%s\'") % cmd) + st = os.system (cmd) if st: - msg = ( _ ("error: ") + _ ("command exited with value %d") % st) + name = re.match ('[ \t]*([^ \t]*)', cmd).group (1) + msg = name + ': ' + _ ("command exited with value %d") % st if ignore_error: - sys.stderr.write (msg + ' ' + _ ("(ignored)") + ' ') + if not quiet: + warning (msg + ' ' + _ ("(ignored)") + ' ') else: error (msg) @@ -197,22 +250,9 @@ def system (cmd, ignore_error = 0): def cleanup_temp (): if not keep_temp_dir_p: if verbose_p: - progress (_ ('Cleaning up `%s\'') % temp_dir) - system ('rm -rf %s' % temp_dir) - + progress (_ ("Cleaning %s...") % temp_dir) + shutil.rmtree (temp_dir) -def set_setting (dict, key, val): - try: - val = string.atof (val) - except ValueError: - #warning (_ ("invalid value: %s") % `val`) - pass - - try: - dict[key].append (val) - except KeyError: - warning (_ ("no such setting: %s") % `key`) - dict[key] = [val] def strip_extension (f, ext): (p, e) = os.path.splitext (f) @@ -220,9 +260,41 @@ def strip_extension (f, ext): e = '' return p + e + +def cp_to_dir (pattern, dir): + "Copy files matching re PATTERN from cwd to DIR" + # Duh. Python style portable: cp *.EXT OUTDIR + # system ('cp *.%s %s' % (ext, outdir), 1) + files = filter (lambda x, p=pattern: re.match (p, x), os.listdir ('.')) + map (lambda x, d=dir: shutil.copy2 (x, os.path.join (d, x)), files) + + +def mkdir_p (dir, mode=0777): + if not os.path.isdir (dir): + makedirs (dir, mode) + + +# if set, LILYPONDPREFIX must take prevalence +# if datadir is not set, we're doing a build and LILYPONDPREFIX +datadir = '@local_lilypond_datadir@' + +if os.environ.has_key ('LILYPONDPREFIX') : + datadir = os.environ['LILYPONDPREFIX'] +else: + datadir = '@local_lilypond_datadir@' + + +while datadir[-1] == os.sep: + datadir= datadir[:-1] + +sys.path.insert (0, os.path.join (datadir, 'python')) + +################################################################ # END Library +output = 0 + # # PMX cut and paste # @@ -345,8 +417,8 @@ class Slur: e= self.end_chord if e and s: - s.note_suffix = s.note_suffix + '(' - e.note_prefix = ')' + e.note_prefix + s.note_suffix = s.note_suffix + '-(' + e.note_prefix = e.note_suffix + "-)" else: sys.stderr.write ("\nOrphaned slur") @@ -415,7 +487,7 @@ class Voice: str = str + ln id = self.idstring () - str = '''%s = \\context Voice = %s \\notes { + str = '''%s = \\context Voice = %s { %s } @@ -457,9 +529,9 @@ class Key: if self.sharps and self.flats: k = '\\keysignature %s ' % 'TODO' elif self.sharps: - k = '\\notes\\key %s \major' % key_sharps[self.sharps] + k = '\\key %s \major' % key_sharps[self.sharps] elif self.flats: - k = '\\notes\\key %s \major' % key_flats[self.flats] + k = '\\key %s \major' % key_flats[self.flats] return k class Time: @@ -528,8 +600,8 @@ class Staff: str = str + v.dump() refs = refs + '\n \\' + v.idstring () str = str + ''' -%s = \context Staff = %s <%s -> +%s = \context Staff = %s <<%s +>> ''' % (self.idstring (), self.idstring (), refs) return str @@ -611,19 +683,21 @@ class Chord: for p in self.pitches: if str: str = str + ' ' - str = str + pitch_to_lily_string (p) + sd + str = str + pitch_to_lily_string (p) - for s in self.scripts: - str = str + '-' + s str = self.note_prefix +str + self.note_suffix if len (self.pitches) > 1: str = '<%s>' % str elif self.multimeasure: - str = 'R' + sd + str = 'R' elif len (self.pitches) == 0: - str = 'r' + sd + str = 'r' + + str = str + sd + for s in self.scripts: + str = str + '-' + s str = self.chord_prefix + str + self.chord_suffix @@ -988,9 +1062,9 @@ class Parser: str = str + ''' \score { - <%s - > - \paper {} + <<%s + >> + \layout {} \midi {} } ''' % refs @@ -1110,18 +1184,6 @@ class Pre_processor: s = '' - -option_definitions = [ - ('', 'd', 'debug', _ ("debug")), - ('NAME[=EXP]', 'D', 'define', _ ("define macro NAME [optional expansion EXP]")), - ('', 'h', 'help', _ ("this help")), - ('FILE', 'o', 'output', _ ("write output to FILE")), - ('', 'E', 'pre-process', _ ("only pre-process")), - ('', 'V', 'verbose', _ ("verbose")), - ('', 'v', 'version', _ ("print version number")), - ('', 'w', 'warranty', _ ("show warranty and copyright")), - ] - debug_p = 0 only_pre_process_p = 0 def debug (s): @@ -1184,17 +1246,22 @@ for opt in options: # sys.stdout.flush () # handy emacs testing -if not files: - files = ['template.mup'] +# if not files: +# files = ['template.mup'] +if not files: + files = ['-'] + for f in files: + h = None if f == '-': - f = '' - - if f and not os.path.isfile (f): + h = sys.stdin + elif f and not os.path.isfile (f): f = strip_extension (f, '.mup') + '.mup' - progress ( _("Processing %s..." % f)) - raw_lines = open (f).readlines () + + h = open (f) + progress ( _("Processing `%s'..." % f)) + raw_lines = h.readlines () p = Pre_processor (raw_lines) if only_pre_process_p: if not output: @@ -1205,8 +1272,14 @@ for f in files: output = os.path.basename (re.sub ('(?i).mup$', '.ly', f)) if output == f: output = os.path.basename (f + '.ly') - - progress (_ ("Writing %s...") % output) + + if f == '-': + output = '-' + out_h = sys.stdout + else: + out_h = open (output, 'w') + + progress (_ ("Writing `%s'...") % output) tag = '%% Lily was here -- automatically converted by %s from %s' % ( program_name, f) if only_pre_process_p: @@ -1215,9 +1288,8 @@ for f in files: else: ly = tag + '\n\n' + e.dump () - o = open (output, 'w') - o.write (ly) - o.close () + out_h.write (ly) + out_h.close () if debug_p: print (ly)