From fc09d21b965308d826973fd6e897bdc66a406830 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sun, 25 May 2014 17:03:18 +0400 Subject: [PATCH] Open all files in binary mode Output os.linesep instead of '\n'. --- ChangeLog | 5 +++++ TODO | 3 --- mimedecode.py | 26 +++++++++++++------------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4d7cd2e..e8b77c9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Version 2.5.1 (2014-04-??) + + Open all files in binary mode. Output os.linesep instead of '\n'. + + Version 2.5.0 (2014-03-18) Add option --set-header=header:value to set header's value (only at the top diff --git a/TODO b/TODO index a1001ec..36e44bf 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,3 @@ -Open all files in binary mode. Output os.linesep instead of '\n'. - - Add option -I to completely skip (remove) subparts -- no headers, no body, no boundary, nothing. diff --git a/mimedecode.py b/mimedecode.py index 48a6aba..0714c87 100755 --- a/mimedecode.py +++ b/mimedecode.py @@ -20,17 +20,17 @@ def usage(code=0, errormsg=''): Usage: %s [-h|--help] [-V|--version] [-cCDP] [-H|--host=hostname] [-f charset] [-d header1[,h2,...]|*[,-h1,...]] [-p header1[,h2,h3,...]:param1[,p2,p3,...]] [-r header1[,h2,...]|*[,-h1,...]] [-R header1[,h2,h3,...]:param1[,p2,p3,...]] [--set-header header:value] [--set-param header:param=value] [-Bbeit mask] [--save-headers|body|message mask] [-O dest_dir] [-o output_file] [input_file [output_file]] """ % me) if errormsg: - sys.stderr.write(errormsg + '\n') + sys.stderr.write(errormsg + os.linesep) sys.exit(code) def output_headers(msg): unix_from = msg.get_unixfrom() if unix_from: - output(unix_from + '\n') + output(unix_from + os.linesep) for key, value in msg.items(): - output("%s: %s\n" % (key, value)) - output("\n") # End of headers + output("%s: %s%s" % (key, value, os.linesep)) + output(os.linesep) # End of headers def recode_if_needed(s, charset): @@ -222,9 +222,9 @@ def decode_body(msg, s): if not command: return s - file = open(filename, 'w') - file.write(s) - file.close() + outfile = open(filename, 'wb') + outfile.write(s) + outfile.close() pipe = os.popen(command, 'r') s = pipe.read() @@ -353,7 +353,7 @@ def decode_part(msg): break elif content_type in g.ignore_mask: output_headers(msg) - output("\nMessage body of type %s skipped.\n" % ctype) + output("%sMessage body of type %s skipped.%s" % (os.linesep, ctype, os.linesep)) break elif content_type in g.error_mask: break @@ -382,7 +382,7 @@ def decode_multipart(msg): if msg.preamble: # Preserve the first part, it is probably not a RFC822-message output(msg.preamble) # Usually it is just a few lines of text (MIME warning) if msg.preamble is not None: - output("\n") + output(os.linesep) first_subpart = True boundary = msg.get_boundary() @@ -392,14 +392,14 @@ def decode_multipart(msg): if first_subpart: first_subpart = False else: - output("\n") - output("--%s\n" % boundary) + output(os.linesep) + output("--%s%s" % (boundary, os.linesep)) # Recursively decode all parts of the subpart decode_message(subpart) if boundary: - output("\n--%s--\n" % boundary) + output("%s--%s--%s" % (os.linesep, boundary, os.linesep)) if msg.epilogue: output(msg.epilogue) @@ -423,7 +423,7 @@ def open_output_file(filename): if create: os.makedirs(full_dir) try: - return open(fullpath, 'w') + return open(fullpath, 'wb') except: if create: os.removedirs(full_dir) -- 2.39.5