From 2ad303a6af235099f6334c1549c587c51e8fb216 Mon Sep 17 00:00:00 2001 From: John Mandereau Date: Wed, 30 Jan 2008 12:07:50 +0100 Subject: [PATCH] Use Python Unicode encoding for gettexted strings Python 2.5 strings encoding handling seems not always consistent between various modules, so we must use Python Unicode strings and thus reencode strings when writing to stderr/stdout. This commit applies changes proposed on lilypond-devel a while ago in thread "Gettexted messages encoding with Python 2.5". Even if GUB still uses Python 2.4, LilyPond packages on some distros (e.g. Fedora) already use 2.5, so it's important to fix this. I think these changes are also compatible with 2.4, but anyway I must check GUB binary when it's out. By the way, further localization work is needed: localize musicxml2ly strings, and fix some nitpicks. --- python/lilylib.py | 19 ++++++++++++++----- scripts/convert-ly.py | 10 +++++----- scripts/lilypond-book.py | 15 +++++++-------- scripts/midi2ly.py | 26 +++++++++----------------- scripts/musicxml2ly.py | 4 ++-- 5 files changed, 37 insertions(+), 37 deletions(-) diff --git a/python/lilylib.py b/python/lilylib.py index 4372234593..a512f2c6e5 100644 --- a/python/lilylib.py +++ b/python/lilylib.py @@ -38,19 +38,28 @@ if os.environ.has_key ('LILYPOND_DATADIR') : sys.path.insert (0, os.path.join (datadir, 'python')) - +# Python 2.5 only accepts strings with proper Python internal encoding +# (i.e. ASCII or Unicode) when writing to stdout/stderr, so we must +# use ugettext iso gettext, and encode the string when writing to +# stdout/stderr localedir = '@localedir@' try: import gettext - gettext.bindtextdomain ('lilypond', localedir) - gettext.textdomain ('lilypond') - _ = gettext.gettext + t = gettext.translation ('lilypond', localedir) + _ = t.ugettext except: def _ (s): return s underscore = _ -progress = sys.stderr.write + +def encoded_write(f, s): + f.write (s.encode (f.encoding)) + +def stderr_write (s): + encoded_write (sys.stderr, s) + +progress = stderr_write # Modified version of the commands.mkarg(x), which always uses # double quotes (since Windows can't handle the single quotes: diff --git a/scripts/convert-ly.py b/scripts/convert-ly.py index 9501b405fe..6d7eda4a41 100644 --- a/scripts/convert-ly.py +++ b/scripts/convert-ly.py @@ -44,17 +44,17 @@ program_name = os.path.basename (sys.argv[0]) program_version = '@TOPLEVEL_VERSION@' def warning (s): - sys.stderr.write (program_name + ": " + _ ("warning: %s") % s + '\n') + ly.stderr_write (program_name + ": " + _ ("warning: %s") % s + '\n') def error (s): - sys.stderr.write (program_name + ": " + _ ("error: %s") % s + '\n') + ly.stderr_write (program_name + ": " + _ ("error: %s") % s + '\n') def identify (port=sys.stderr): - port.write ('%s (GNU LilyPond) %s\n' % (program_name, program_version)) + ly.encoded_write (port, '%s (GNU LilyPond) %s\n' % (program_name, program_version)) def warranty (): identify () - sys.stdout.write (''' + ly.encoded_write (sys.stdout, ''' Copyright (c) %s by Han-Wen Nienhuys @@ -178,7 +178,7 @@ class UnknownVersion: pass def do_one_file (infile_name): - sys.stderr.write (_ ("Processing `%s\'... ") % infile_name) + ly.stderr_write (_ ("Processing `%s\'... ") % infile_name) sys.stderr.write ('\n') from_version = None diff --git a/scripts/lilypond-book.py b/scripts/lilypond-book.py index 4e4c307782..dd8e00d192 100644 --- a/scripts/lilypond-book.py +++ b/scripts/lilypond-book.py @@ -74,16 +74,15 @@ def exit (i): sys.exit (i) def identify (): - sys.stdout.write ('%s (GNU LilyPond) %s\n' % (program_name, program_version)) + ly.encoded_write (sys.stdout, '%s (GNU LilyPond) %s\n' % (program_name, program_version)) -def progress (s): - sys.stderr.write (s) +progress = ly.progress def warning (s): - sys.stderr.write (program_name + ": " + _ ("warning: %s") % s + '\n') + ly.stderr_write (program_name + ": " + _ ("warning: %s") % s + '\n') def error (s): - sys.stderr.write (program_name + ": " + _ ("error: %s") % s + '\n') + ly.stderr_write (program_name + ": " + _ ("error: %s") % s + '\n') def ps_page_count (ps_name): header = open (ps_name).read (1024) @@ -94,7 +93,7 @@ def ps_page_count (ps_name): def warranty (): identify () - sys.stdout.write (''' + ly.encoded_write (sys.stdout, ''' %s %s @@ -1417,8 +1416,8 @@ def filter_pipe (input, cmd): exit_status = status >> 8 error (_ ("`%s' failed (%d)") % (cmd, exit_status)) error (_ ("The error log is as follows:")) - sys.stderr.write (error) - sys.stderr.write (stderr.read ()) + ly.stderr_write (error) + ly.stderr_write (stderr.read ()) exit (status) if global_options.verbose: diff --git a/scripts/midi2ly.py b/scripts/midi2ly.py index 157275c251..eaf7f5d616 100644 --- a/scripts/midi2ly.py +++ b/scripts/midi2ly.py @@ -31,6 +31,7 @@ import sys import midi import lilylib as ly +global _;_=ly._ ################################################################ ## CONSTANTS @@ -55,15 +56,6 @@ allowed_tuplet_clocks = [] ################################################################ -localedir = '@localedir@' -try: - import gettext - gettext.bindtextdomain ('lilypond', localedir) - gettext.textdomain ('lilypond') - _ = gettext.gettext -except: - def _ (s): - return s program_name = sys.argv[0] program_version = '@TOPLEVEL_VERSION@' @@ -75,7 +67,7 @@ def identify (): def warranty (): identify () - sys.stdout.write (''' + ly.encoded_write (sys.stdout, ''' Copyright (c) %s by Han-Wen Nienhuys @@ -89,14 +81,14 @@ Copyright (c) %s by def progress (s): - errorport.write (s + '\n') + ly.encoded_write (errorport, s + '\n') def warning (s): progress (_ ("warning: ") + s) def error (s): progress (_ ("error: ") + s) - raise _ ("Exiting ... ") + raise _ ("Exiting... ") def system (cmd, ignore_error = 0): return ly.system (cmd, ignore_error=ignore_error) @@ -107,7 +99,6 @@ def strip_extension (f, ext): e = '' return p + e - class Duration: @@ -891,12 +882,13 @@ def get_option_parser (): p.add_option ('-x', '--text-lyrics', help=_ ("treat every text as a lyric"), action='store_true') - p.add_option_group (_ ("Examples"), +# urg, Python 2.5 optparse is broken, it doesn't accept Unicode strings + p.add_option_group (_ ("Examples").encode (sys.stdout.encoding), description = r''' midi2ly --key=-2:1 --duration-quant=32 \ --allow-tuplet=4*2/3 --allow-tuplet=2*4/3 foo.midi ''') - p.add_option_group ('bugs', + p.add_option_group (_ ('Bugs').encode (sys.stdout.encoding), description=(_ ('Report bugs via') + ''' http://post.gmane.org/post.php''' '''?group=gmane.comp.gnu.lilypond.bugs\n''')) @@ -910,8 +902,8 @@ def do_options (): if not args or args[0] == '-': opt_parser.print_help () - sys.stderr.write ('\n%s: %s %s\n' % (program_name, _ ("error: "), - _ ("no files specified on command line."))) + ly.stderr_write ('\n%s: %s %s\n' % (program_name, _ ("error: "), + _ ("no files specified on command line."))) sys.exit (2) if options.duration_quant: diff --git a/scripts/musicxml2ly.py b/scripts/musicxml2ly.py index 5884f536b9..9dfb81cfb7 100644 --- a/scripts/musicxml2ly.py +++ b/scripts/musicxml2ly.py @@ -25,11 +25,11 @@ from rational import Rational options = None def progress (str): - sys.stderr.write (str + '\n') + stderr_write (str + '\n') sys.stderr.flush () def error_message (str): - sys.stderr.write (str + '\n') + stderr_write (str + '\n') sys.stderr.flush () needed_additional_definitions = [] -- 2.39.5