]> git.phdru.name Git - mimedecode.git/blobdiff - mimedecode.py
Phony targets.
[mimedecode.git] / mimedecode.py
index bfee2fe65c0d0c9f001c40c6245b3d9a80c31c17..1e6852ceadce0158f2468846045f04204101d0c0 100755 (executable)
@@ -1,10 +1,6 @@
 #! /usr/local/bin/python -O
-"""Decode MIME message.
+"""Decode MIME message"""
 
-Author: Oleg Broytmann <phd@phd.pp.ru>
-Copyright: (C) 2001-2006 PhiloSoft Design
-License: GPL
-"""
 
 _version = "2.1.0"
 __version__ = "$Revision$"[11:-2]
@@ -13,12 +9,17 @@ __revision__ = "$Id$"[5:-2]
 __author__ = "Oleg Broytmann <phd@phd.pp.ru>"
 __copyright__ = "Copyright (C) 2001-2006 PhiloSoft Design"
 __license__ = "GNU GPL"
-__docformat__ = "epytext en"
 
 
 import sys, os
 import email
 
+try:
+   import locale
+   use_locale = True
+except ImportError:
+   use_locale = False
+
 try:
    from cStringIO import StringIO
 except ImportError:
@@ -296,7 +297,25 @@ def decode_file(infile):
 
 
 class GlobalOptions:
-   default_charset = sys.getdefaultencoding()
+   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()
+
    recode_charset = 1 # recode charset of message body
 
    decode_headers = ["Subject", "From"] # A list of headers to decode
@@ -359,9 +378,10 @@ def init():
 if __name__ == "__main__":
    arguments = init()
 
-   if len(arguments) == 0:
+   la = len(arguments)
+   if la == 0:
       infile = sys.stdin
-   elif len(arguments) <> 1:
+   elif la <> 1:
       usage(1)
    elif arguments[0] == '-':
       infile = sys.stdin