From 69acf148036c062b05dd115cc45a80e28140f3e9 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sat, 8 Mar 2014 18:51:43 +0400 Subject: [PATCH] Allow * and exceptions for -p in the headers list Allow * and exceptions for -p in the headers list: -p *,-h1,-h2,-h3:p1,p2,p3 --- ANNOUNCE | 3 +++ mimedecode.docbook | 11 +++++++++++ mimedecode.py | 20 +++++++++++++++----- test/test_all | 1 + 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/ANNOUNCE b/ANNOUNCE index 23d113c..e23ea9b 100644 --- a/ANNOUNCE +++ b/ANNOUNCE @@ -34,6 +34,9 @@ WHAT'S NEW in version 2.4.0 (2014-03-??) Change option -p to accept lists of headers and parameters: -p h1,h2,h3,..:p1,p2,p3,.. + Allow * and exceptions for -p in the headers list: +-p *,-h1,-h2,-h3:p1,p2,p3 + Add ChangeLog. WHAT'S NEW in version 2.3.8 (2014-02-26) diff --git a/mimedecode.docbook b/mimedecode.docbook index d98db68..f9650bd 100644 --- a/mimedecode.docbook +++ b/mimedecode.docbook @@ -253,6 +253,17 @@ + + -p *[,-header1,-header2,-header3,...]:param1[,param2,param3,...] + + + Add the parameters(s) to a list of headers parameters to decode; + the parameters will be decoded for all headers except the given + ones. + + + + -P diff --git a/mimedecode.py b/mimedecode.py index 0e01572..6d4c7ea 100755 --- a/mimedecode.py +++ b/mimedecode.py @@ -94,6 +94,9 @@ def decode_header_param(msg, header, param): msg.set_param(param, new_value, header) +def _get_exceptions(list): + return [x[1:].lower() for x in list[1:] if x[0]=='-'] + def decode_headers(msg): "Decode message headers according to global options" @@ -116,20 +119,27 @@ def decode_headers(msg): for header_list in gopts.decode_headers: header_list = header_list.split(',') if header_list[0] == '*': # Decode all headers except listed - header_list = [h[1:].lower() for h in header_list[1:] if h[0]=='-'] + header_list = _get_exceptions(header_list) for header in msg.keys(): if header.lower() not in header_list: decode_header(msg, header) - else: # Decode listed hiders + else: # Decode listed headers for header in header_list: decode_header(msg, header) for header_list, param_list in gopts.decode_header_params: header_list = header_list.split(',') param_list = param_list.split(',') - for header in header_list: - for param in param_list: - decode_header_param(msg, header, param) + if header_list[0] == '*': # Decode for all headers except listed + header_list = _get_exceptions(header_list) + for header in msg.keys(): + if header.lower() not in header_list: + for param in param_list: + decode_header_param(msg, header, param) + else: # Decode for listed headers + for header in header_list: + for param in param_list: + decode_header_param(msg, header, param) def set_header(msg, header, value): diff --git a/test/test_all b/test/test_all index f61c63a..8c59da5 100755 --- a/test/test_all +++ b/test/test_all @@ -48,6 +48,7 @@ test_file msg_22.txt msg_22-1.txt -r content-id test_file msg_29.txt msg_29-1.txt -R Content-Type:title test_file msg_18.txt msg_18-1.txt --remove-params=X-Foobar-Spoink-Defrobnit test_file msg_22.txt msg_22.txt -P -p Content-Type,Content-Disposition:name,filename +test_file msg_22.txt msg_22.txt -P -p \*:name,filename if [ "$RC" -eq 0 ]; then echo "All tests passed!" -- 2.39.5