X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=mimedecode.py;h=6c3ff717a64d063ddad362a29c055edabc975373;hb=2229a1e4737c8af820e0cf554ee2c7a5a3713bbd;hp=f819b84c62e3decd62374b021e7b3b41f80946be;hpb=e879f7fa2f3a50dcb1d5fe270abc31ba4383b8fd;p=mimedecode.git diff --git a/mimedecode.py b/mimedecode.py index f819b84..6c3ff71 100755 --- a/mimedecode.py +++ b/mimedecode.py @@ -1,25 +1,13 @@ -#! /usr/local/bin/python -O +#! /usr/bin/env python """Decode MIME message""" -_version = "2.1.0" -__version__ = "$Revision$"[11:-2] -__date__ = "$Date$"[7:-2] -__revision__ = "$Id$"[5:-2] -__author__ = "Oleg Broytmann " -__copyright__ = "Copyright (C) 2001-2006 PhiloSoft Design" -__license__ = "GNU GPL" +from __version__ import __version__, __author__, __copyright__, __license__ import sys, os import email -try: - import locale - use_locale = True -except ImportError: - use_locale = False - try: from cStringIO import StringIO except ImportError: @@ -34,8 +22,8 @@ me = os.path.basename(sys.argv[0]) def version(exit=1): sys.stdout.write("""\ -BroytMann mimedecode.py version %s, %s -""" % (_version, __copyright__)) +Broytman mimedecode.py version %s, %s +""" % (__version__, __copyright__)) if exit: sys.exit(0) @@ -60,11 +48,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 +186,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 +285,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 +322,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':