summaryrefslogtreecommitdiff
path: root/email-print-mime-structure
diff options
context:
space:
mode:
authorDaniel Kahn Gillmor <dkg@fifthhorseman.net>2019-11-02 01:28:17 -0400
committerSean Whitton <spwhitton@spwhitton.name>2019-11-02 08:44:19 -0700
commit75dbd9eb55cae90d6c962e9eb914ffa05d05d69e (patch)
tree85346060a76dd518b40dccadf5e74dfed3724c6c /email-print-mime-structure
parent8fde04b7d2743e393e93fd0677ef2fcfc53b351b (diff)
downloadmailscripts-75dbd9eb55cae90d6c962e9eb914ffa05d05d69e.tar.gz
email-print-mime-structure: refactor to a class
We will need to send arguments to the printer, so it's handy to wrap the functionality in a class. No functional changes. This diff is probably best reviewed with whitespace changes ignored. Signed-off-by: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
Diffstat (limited to 'email-print-mime-structure')
-rwxr-xr-xemail-print-mime-structure82
1 files changed, 42 insertions, 40 deletions
diff --git a/email-print-mime-structure b/email-print-mime-structure
index 185173f..b78ae91 100755
--- a/email-print-mime-structure
+++ b/email-print-mime-structure
@@ -37,51 +37,53 @@ from typing import Optional, Union, List, Tuple, Any
from email.charset import Charset
from email.message import Message
-def print_part(z:Message, prefix:str) -> None:
- ofname:Optional[str] = z.get_filename()
- fname:str = '' if ofname is None else f' [{ofname}]'
- ocharset:Union[Charset, str, None] = z.get_charset()
- cset:str = '' if ocharset is None else f' ({ocharset})'
- disp:Union[List[Tuple[str,str]], List[str], None] = z.get_params(None, header='Content-Disposition')
- disposition:str = ''
- if (disp is not None):
- for d in disp:
- if d[0] in [ 'attachment', 'inline' ]:
- disposition = ' ' + d[0]
- nbytes:int
- if z.is_multipart():
- # FIXME: it looks like we are counting chars here, not bytes:
- nbytes = len(z.as_string())
- else:
- payload:Union[List[Message], str, bytes, None] = z.get_payload()
- if not isinstance(payload, (str,bytes)):
- raise TypeError(f'expected payload to be either str or bytes, got {type(payload)}')
- nbytes = len(payload)
+class MimePrinter(object):
+ def print_part(self, z:Message, prefix:str) -> None:
+ ofname:Optional[str] = z.get_filename()
+ fname:str = '' if ofname is None else f' [{ofname}]'
+ ocharset:Union[Charset, str, None] = z.get_charset()
+ cset:str = '' if ocharset is None else f' ({ocharset})'
+ disp:Union[List[Tuple[str,str]], List[str], None] = z.get_params(None, header='Content-Disposition')
+ disposition:str = ''
+ if (disp is not None):
+ for d in disp:
+ if d[0] in [ 'attachment', 'inline' ]:
+ disposition = ' ' + d[0]
+ nbytes:int
+ if z.is_multipart():
+ # FIXME: it looks like we are counting chars here, not bytes:
+ nbytes = len(z.as_string())
+ else:
+ payload:Union[List[Message], str, bytes, None] = z.get_payload()
+ if not isinstance(payload, (str,bytes)):
+ raise TypeError(f'expected payload to be either str or bytes, got {type(payload)}')
+ nbytes = len(payload)
- print(f'{prefix}{z.get_content_type()}{cset}{disposition}{fname} {nbytes} bytes')
+ print(f'{prefix}{z.get_content_type()}{cset}{disposition}{fname} {nbytes} bytes')
-def test(z:Message, prefix:str='') -> None:
- if (z.is_multipart()):
- print_part(z, prefix+'┬╴')
- if prefix.endswith('└'):
- prefix = prefix.rpartition('└')[0] + ' '
- if prefix.endswith('├'):
- prefix = prefix.rpartition('├')[0] + '│'
- parts:Union[List[Message], str, bytes, None] = z.get_payload()
- if not isinstance(parts, list):
- raise TypeError(f'parts was {type(parts)}, expected List[Message]')
- i = 0
- while (i < len(parts)-1):
- test(parts[i], prefix + '├')
- i += 1
- test(parts[i], prefix + '└')
- # FIXME: show epilogue?
- else:
- print_part(z, prefix+'─╴')
+ def test(self, z:Message, prefix:str='') -> None:
+ if (z.is_multipart()):
+ self.print_part(z, prefix+'┬╴')
+ if prefix.endswith('└'):
+ prefix = prefix.rpartition('└')[0] + ' '
+ if prefix.endswith('├'):
+ prefix = prefix.rpartition('├')[0] + '│'
+ parts:Union[List[Message], str, bytes, None] = z.get_payload()
+ if not isinstance(parts, list):
+ raise TypeError(f'parts was {type(parts)}, expected List[Message]')
+ i = 0
+ while (i < len(parts)-1):
+ self.test(parts[i], prefix + '├')
+ i += 1
+ self.test(parts[i], prefix + '└')
+ # FIXME: show epilogue?
+ else:
+ self.print_part(z, prefix+'─╴')
msg:Union[Message, str, int, Any] = email.message_from_file(sys.stdin)
if isinstance(msg, Message):
- test(msg, '└')
+ printer:MimePrinter = MimePrinter()
+ printer.test(msg, '└')
else:
logging.error('Input was not an e-mail message')