From 1bb47d64c37679695d6a7e43b2cc9e162127a765 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Thu, 27 Apr 2006 14:43:18 +0000 Subject: [PATCH] A patch by Bogdan Maryniuk : portable way to get the default charset. git-svn-id: file:///home/phd/archive/SVN/mimedecode@4 a778b35f-2f12-0410-a9f6-9b1356d76ba6 --- mimedecode.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/mimedecode.py b/mimedecode.py index bfee2fe..e596f1d 100755 --- a/mimedecode.py +++ b/mimedecode.py @@ -1,10 +1,5 @@ #! /usr/local/bin/python -O -"""Decode MIME message. - -Author: Oleg Broytmann -Copyright: (C) 2001-2006 PhiloSoft Design -License: GPL -""" +"""Decode MIME message""" _version = "2.1.0" __version__ = "$Revision$"[11:-2] @@ -13,11 +8,11 @@ __revision__ = "$Id$"[5:-2] __author__ = "Oleg Broytmann " __copyright__ = "Copyright (C) 2001-2006 PhiloSoft Design" __license__ = "GNU GPL" -__docformat__ = "epytext en" import sys, os import email +import locale try: from cStringIO import StringIO @@ -296,7 +291,22 @@ def decode_file(infile): class GlobalOptions: - default_charset = sys.getdefaultencoding() + # Get a default charset. + try: + lcAll = locale.setlocale(locale.LC_ALL, '').split('.') + except locale.Error, err: + print >> sys.stderr, "WARNING:", err + lcAll = [] + + if len(lcAll) == 2: + default_charset = lcAll[1] + else: + try: + default_charset = locale.getpreferredencoding() + except locale.Error, err: + print >> sys.stderr, "WARNING:", err + default_charset = sys.getdefaultencoding() + recode_charset = 1 # recode charset of message body decode_headers = ["Subject", "From"] # A list of headers to decode -- 2.39.5