From: Oleg Broytman Date: Sun, 13 Jan 2008 18:50:48 +0000 (+0000) Subject: Encoding => charset. X-Git-Tag: v2.3.2~83 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=44c0a4b8df2e4c5868ccbf0eaa366197bf246373;p=mimedecode.git Encoding => charset. git-svn-id: file:///home/phd/archive/SVN/mimedecode@13 a778b35f-2f12-0410-a9f6-9b1356d76ba6 --- diff --git a/mimedecode.py b/mimedecode.py index f819b84..bfb0ece 100755 --- a/mimedecode.py +++ b/mimedecode.py @@ -1,4 +1,4 @@ -#! /usr/local/bin/python -O +#! /usr/bin/env python """Decode MIME message""" @@ -14,12 +14,6 @@ __license__ = "GNU GPL" import sys, os import email -try: - import locale - use_locale = True -except ImportError: - use_locale = False - try: from cStringIO import StringIO except ImportError: @@ -60,11 +54,11 @@ def output_headers(msg, outfile = sys.stdout): def recode(s, charset): - return unicode(s, charset, "replace").encode(GlobalOptions.default_charset, "replace") + return unicode(s, charset, "replace").encode(GlobalOptions.default_encoding, "replace") def recode2(s, charset): - if charset and charset.lower() <> GlobalOptions.default_charset: + if charset and charset.lower() <> GlobalOptions.default_encoding: s = recode(s, charset) return s @@ -198,11 +192,11 @@ def recode_charset(msg, s): "Recode charset of the message to the default charset" save_charset = charset = msg.get_content_charset() - if charset and charset.lower() <> GlobalOptions.default_charset: + if charset and charset.lower() <> GlobalOptions.default_encoding: s = recode2(s, charset) content_type = msg.get_content_type() - set_content_type(msg, content_type, GlobalOptions.default_charset) - msg["X-MIME-Autoconverted"] = "from %s to %s by %s id %s" % (save_charset, GlobalOptions.default_charset, host_name, me) + set_content_type(msg, content_type, GlobalOptions.default_encoding) + msg["X-MIME-Autoconverted"] = "from %s to %s by %s id %s" % (save_charset, GlobalOptions.default_encoding, host_name, me) return s @@ -297,27 +291,7 @@ def decode_file(infile): class GlobalOptions: - if use_locale: - # Get the 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() - else: - default_charset = sys.getdefaultencoding() - - default_charset = default_charset.lower() - + from m_lib.defenc import default_encoding recode_charset = 1 # recode charset of message body decode_headers = ["Subject", "From"] # A list of headers to decode @@ -354,7 +328,7 @@ def init(): elif option == '-C': GlobalOptions.recode_charset = 0 elif option == '-f': - GlobalOptions.default_charset = value + GlobalOptions.default_encoding = value elif option == '-d': GlobalOptions.decode_headers.append(value) elif option == '-D':