python-virtinst

changeset 1229:4cc206766cce

Hopefully fix unicode locale use once and for all!
author Cole Robinson <crobinso@redhat.com>
date Mon Mar 01 17:52:49 2010 -0500 (2010-03-01)
parents 3eae7ab7e457
children 79371fd73f9c
files virtinst/__init__.py virtinst/cli.py
line diff
     1.1 --- a/virtinst/__init__.py	Mon Mar 01 17:18:13 2010 -0500
     1.2 +++ b/virtinst/__init__.py	Mon Mar 01 17:52:49 2010 -0500
     1.3 @@ -16,7 +16,7 @@
     1.4  
     1.5  import gettext
     1.6  
     1.7 -gettext_dir = "::LOCALEDIR::"
     1.8 +gettext_dir = "/usr/share/locale"
     1.9  gettext_app = "virtinst"
    1.10  
    1.11  gettext.bindtextdomain(gettext_app, gettext_dir)
     2.1 --- a/virtinst/cli.py	Mon Mar 01 17:18:13 2010 -0500
     2.2 +++ b/virtinst/cli.py	Mon Mar 01 17:52:49 2010 -0500
     2.3 @@ -45,6 +45,29 @@
     2.4          _util.is_uri_remote = lambda uri_: True
     2.5      return uri
     2.6  
     2.7 +class VirtStreamHandler(logging.StreamHandler):
     2.8 +
     2.9 +    def emit(self, record):
    2.10 +        """
    2.11 +        Based on the StreamHandler code from python 2.6: ripping out all
    2.12 +        the unicode handling and just uncoditionally logging seems to fix
    2.13 +        logging backtraces with unicode locales (for me at least).
    2.14 +
    2.15 +        No doubt this is atrocious, but it WORKSFORME!
    2.16 +        """
    2.17 +        try:
    2.18 +            msg = self.format(record)
    2.19 +            stream = self.stream
    2.20 +            fs = "%s\n"
    2.21 +
    2.22 +            stream.write(fs % msg)
    2.23 +
    2.24 +            self.flush()
    2.25 +        except (KeyboardInterrupt, SystemExit):
    2.26 +            raise
    2.27 +        except:
    2.28 +            self.handleError(record)
    2.29 +
    2.30  class VirtOptionParser(OptionParser):
    2.31      '''Subclass to get print_help to work properly with non-ascii text'''
    2.32  
    2.33 @@ -57,9 +80,18 @@
    2.34      def print_help(self, file=None):
    2.35          if file is None:
    2.36              file = sys.stdout
    2.37 +
    2.38          encoding = self._get_encoding(file)
    2.39 -        helpstr = self.format_help().encode(encoding, "replace")
    2.40 -        file.write(helpstr)
    2.41 +        helpstr = self.format_help()
    2.42 +        try:
    2.43 +            encodedhelp = helpstr.encode(encoding, "replace")
    2.44 +        except UnicodeError:
    2.45 +            # I don't know why the above fails hard, unicode makes my head
    2.46 +            # spin. Just printing the format_help() output seems to work
    2.47 +            # quite fine, with the occasional character ?.
    2.48 +            encodedhelp = helpstr
    2.49 +
    2.50 +        file.write(encodedhelp)
    2.51  
    2.52  class VirtHelpFormatter(optparse.IndentedHelpFormatter):
    2.53      """
    2.54 @@ -90,15 +122,17 @@
    2.55  #
    2.56  
    2.57  def setupParser(usage=None):
    2.58 -    parser = VirtOptionParser(usage=usage,
    2.59 -                              formatter=VirtHelpFormatter(),
    2.60 -                              version=virtinst.__version__)
    2.61 +    parse_class = VirtOptionParser
    2.62 +
    2.63 +    parser = parse_class(usage=usage,
    2.64 +                         formatter=VirtHelpFormatter(),
    2.65 +                         version=virtinst.__version__)
    2.66      return parser
    2.67  
    2.68  def setupGettext():
    2.69      locale.setlocale(locale.LC_ALL, '')
    2.70      gettext.bindtextdomain(virtinst.gettext_app, virtinst.gettext_dir)
    2.71 -    gettext.install(virtinst.gettext_app, virtinst.gettext_dir, unicode=1)
    2.72 +    gettext.install(virtinst.gettext_app, virtinst.gettext_dir)
    2.73  
    2.74  def setupLogging(appname, debug=False):
    2.75      # set up logging
    2.76 @@ -129,7 +163,7 @@
    2.77                                                 dateFormat))
    2.78      rootLogger.addHandler(fileHandler)
    2.79  
    2.80 -    streamHandler = logging.StreamHandler(sys.stderr)
    2.81 +    streamHandler = VirtStreamHandler(sys.stderr)
    2.82      if debug:
    2.83          streamHandler.setLevel(logging.DEBUG)
    2.84          streamHandler.setFormatter(logging.Formatter(streamDebugFormat,