Commit db896b82 authored by Siltaar's avatar Siltaar

startswith avoid mistakes : message <> officedocument

parent e73af808
......@@ -43,23 +43,23 @@ def drop_alternatives(msg_bytes, debug=0):
x_drop_alt = []
flat_eml = [[part for part in eml.walk()]]
if not len([part for part in eml.walk() if 'htm' in part.get_content_subtype()]):
if not len([part for part in eml.walk() if part.get_content_subtype().startswith('htm')]):
debug and print('no HTML to drop', file=stderr)
return eml
flat_eml[0].reverse()
new_eml = [clone_message(eml)]
if 'alt' not in eml.get_content_subtype(): # alternative
if not eml.get_content_subtype().startswith('alt'): # alternative
flat_eml[0].pop() # replaced by multipart/mixed
while len(flat_eml) > 0:
while len(flat_eml[-1]) > 0:
part = flat_eml[-1].pop()
debug and print(part.get_content_type(), end=' ', file=stderr)
part_content_type = part.get_content_type()
part_content_subtype = part.get_content_subtype()
if 'alt' in part_content_type and len(flat_eml[-1]) > 1: # alternative
if part_content_subtype.startswith('alt') and len(flat_eml[-1]) > 1: # alternative
candidate_txt = flat_eml[-1].pop()
new_eml[-1].attach(candidate_txt)
debug and print('txt '+candidate_txt.get_content_type(), end=' ', file=stderr)
......@@ -68,17 +68,17 @@ def drop_alternatives(msg_bytes, debug=0):
debug and print('htm '+candidate_htm.get_content_type(), end=' ', file=stderr)
candidate_htm_subtype = candidate_htm.get_content_subtype()
if 'htm' in candidate_htm_subtype: # html
if candidate_htm_subtype.startswith('htm'): # html
if are_idem_txt(candidate_txt, candidate_htm, debug):
x_drop_alt.append(candidate_htm_subtype)
else:
new_eml[-1].attach(candidate_htm)
debug and print('htm diff', end=' ', file=stderr)
elif 'rel' in candidate_htm_subtype: # related
elif candidate_htm_subtype.startswith('rel'): # related
sub_part = flat_eml[-1].pop()
while not sub_part.is_multipart() and len(flat_eml[-1]) > 0:
if 'htm' in sub_part.get_content_subtype():
if sub_part.get_content_subtype().startswith('htm'):
if are_idem_txt(candidate_txt, sub_part, debug):
x_drop_alt.append(sub_part.get_content_subtype())
else:
......@@ -94,7 +94,7 @@ def drop_alternatives(msg_bytes, debug=0):
else: # unknown configuration yet
new_eml[-1].attach(candidate_htm)
debug and print('new configuration ?', end=' ', file=stderr)
elif 'me' in part_content_type: # message
elif part.get_content_type().startswith('me'): # message
debug and print(' clne', file=stderr)
flat_sub_eml = [sub_part for sub_part in part.walk()]
flat_sub_eml.reverse()
......@@ -102,7 +102,7 @@ def drop_alternatives(msg_bytes, debug=0):
flat_eml[-1] = flat_eml[-1][:-len(flat_sub_eml)] # consume input
flat_eml.append(flat_sub_eml)
new_eml.append(clone_message(part))
elif 'rel' in part_content_type: # related
elif part_content_subtype.startswith('rel'): # related
debug and print('drop rel', file=stderr)
else:
new_eml[-1].attach(part)
......
This diff is collapsed.
......@@ -107,6 +107,8 @@ def test_drop_alternatives(msg_bytes, debug):
multipart/mixed text/plain multipart/external-body image/jpeg multipart/external-body image/jpeg text/plain multipart/external-body image/jpeg multipart/external-body image/jpeg text/plain
>>> test_drop_alternatives(open('email_test/20180620.eml', 'rb').read(), DEBUG)
multipart/alternative text/plain text/html
>>> test_drop_alternatives(open('email_test/20180708.eml', 'rb').read(), DEBUG)
multipart/mixed text/plain application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
"""
print(' '.join([p.get_content_type() for p in drop_alternatives(msg_bytes, debug).walk()]))
......
......@@ -56,6 +56,7 @@ Python 3.5.3
2018-04-25 : 37 tests ; 2.80s ; 7.56ms/t (in 50%< startswith, append/pop 30%< inser(0,_)/pop(0))
2018-05-16 : 37 tests ; 2.80s ; 7.56ms/t (.lower() each compared text portions)
2018-06-20 : 38 tests ; 1.95s ; 5.13ms/t (don't compare with zero-sized elts, 40k HTML read)
2018-07-08 : 39 tests ; 1.97s ; 5.05ms/t (startswith avoid mistakes : message <> officedocument)
"""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment