From 1f2c7c40dca5b1937436c71d529d0b1f733302ce Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Tue, 11 Mar 2014 00:36:47 +0400 Subject: [PATCH] Add option -O to set the destination directory --- ANNOUNCE | 2 ++ TODO | 4 +--- mimedecode.docbook | 16 +++++++++++++++- mimedecode.py | 13 ++++++++----- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/ANNOUNCE b/ANNOUNCE index 613982d..c9bb92f 100644 --- a/ANNOUNCE +++ b/ANNOUNCE @@ -33,6 +33,8 @@ level). Add option -B to skip content-transfer-decoding binary attachments. + Add option -O to set the destination directory. + Fix a minor bug: if a multipart message (or a subpart) lacks any textual content - avoid putting an excessive newline. diff --git a/TODO b/TODO index 29cc0a7..ec2be24 100644 --- a/TODO +++ b/TODO @@ -1,7 +1,5 @@ -Add option -O to set the destination directory. - Add options --save-headers, --save-body and --save-message to save -decoded headers/bodies/messages of parts to files. +decoded headers/bodies/messages to files. Release 2.5.0. diff --git a/mimedecode.docbook b/mimedecode.docbook index 4011979..555c5f2 100644 --- a/mimedecode.docbook +++ b/mimedecode.docbook @@ -92,6 +92,9 @@ + + + @@ -437,11 +440,22 @@ + + -O dest_dir + + + Set destination directory for the output files. Default is current + directory. + + + + -o output_file - Useful to set the output file in case of redirected stdin: + Set the output file. Uses destination directory from option -O. + Also useful in case of redirected stdin: mimedecode.py -o output_file < input_file cat input_file | mimedecode.py -o output_file diff --git a/mimedecode.py b/mimedecode.py index 3a8cbbe..e832017 100755 --- a/mimedecode.py +++ b/mimedecode.py @@ -18,7 +18,7 @@ Broytman mimedecode.py version %s, %s def usage(code=0, errormsg=''): version(0) sys.stdout.write("""\ - 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] [-o output_file] [input_file [output_file]] + 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] [-O dest_dir] [-o output_file] [input_file [output_file]] """ % me) if errormsg: sys.stderr.write(errormsg + '\n') @@ -394,6 +394,7 @@ class GlobalOptions: input_filename = None output_filename = None + destination_dir = os.curdir g = GlobalOptions @@ -403,7 +404,7 @@ def get_opts(): try: options, arguments = getopt(sys.argv[1:], - 'hVcCDPH:f:d:p:r:R:b:B:e:i:t:o:', + 'hVcCDPH:f:d:p:r:R:b:B:e:i:t:O:o:', ['help', 'version', 'host=', 'set-header=', 'set-param=']) except GetoptError: usage(1) @@ -454,6 +455,8 @@ def get_opts(): g.ignore_mask.append(value) elif option == '-e': g.error_mask.append(value) + elif option == '-O': + g.destination_dir = value elif option == '-o': g.output_filename = value else: @@ -470,7 +473,7 @@ if __name__ == "__main__": g.input_filename = '-' infile = sys.stdin if g.output_filename: - outfile = open(g.output_filename, 'w') + outfile = open(os.path.join(g.destination_dir, g.output_filename), 'w') else: g.output_filename = '-' outfile = sys.stdout @@ -483,7 +486,7 @@ if __name__ == "__main__": infile = open(arguments[0], 'r') if la == 1: if g.output_filename: - outfile = open(g.output_filename, 'w') + outfile = open(os.path.join(g.destination_dir, g.output_filename), 'w') else: g.output_filename = '-' outfile = sys.stdout @@ -495,7 +498,7 @@ if __name__ == "__main__": outfile = sys.stdout else: g.output_filename = arguments[1] - outfile = open(arguments[1], 'w') + outfile = open(os.path.join(g.destination_dir, g.output_filename), 'w') else: usage(1, 'Too many arguments') -- 2.39.2