]> git.donarmstrong.com Git - lilypond.git/commitdiff
Use Python Unicode encoding for gettexted strings
authorJohn Mandereau <john.mandereau@gmail.com>
Wed, 30 Jan 2008 11:07:50 +0000 (12:07 +0100)
committerJohn Mandereau <john.mandereau@gmail.com>
Wed, 30 Jan 2008 11:07:50 +0000 (12:07 +0100)
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
scripts/convert-ly.py
scripts/lilypond-book.py
scripts/midi2ly.py
scripts/musicxml2ly.py

index 43722345932a551150674a65027ae6cc266dcd59..a512f2c6e5494098b5cfb4aaca2ffe5a6d21746b 100644 (file)
@@ -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:
index 9501b405fe69c43a7eeb8809402112cd87eba423..6d7eda4a41dd712884a96e137947ce2c7d91025c 100644 (file)
@@ -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
index 4e4c3077824008b296f638c6dd01a2b6022c738b..dd8e00d1929e692f81214c0f5d6104dbd4c6ef9e 100644 (file)
@@ -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:
index 157275c251e23d126995276615b7c9b0eb4d17b4..eaf7f5d616e55fb0227260bda2ce6a965f3b8749 100644 (file)
@@ -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
 
-\f
 
 
 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:
index 5884f536b945115fe19b1b7712913cc6c565c73c..9dfb81cfb7867f61a6993709d3c6ab30c94c32f3 100644 (file)
@@ -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 = []