Commit e48841f7 authored by Siltaar's avatar Siltaar

use Python's __debug__ variable instead of a custom one

parent db896b82
#!/usr/bin/env python3
#!/usr/bin/python3 -O
# coding: utf-8
# author : Simon Descarpentries - simon /\ acoeuro [] com
# date: 2017 - 2018
......@@ -32,19 +32,19 @@ LIM = .82
BON = .91
def drop_alternatives(msg_bytes, debug=0):
def drop_alternatives(msg_bytes):
eml = BytesParser().parsebytes(msg_bytes)
# debug and _structure(eml)
# __debug__ and _structure(eml)
if not eml.is_multipart():
debug and print('not multipart', file=stderr)
__debug__ and print('not multipart', file=stderr)
return eml
x_drop_alt = []
flat_eml = [[part for part in eml.walk()]]
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)
__debug__ and print('no HTML to drop', file=stderr)
return eml
flat_eml[0].reverse()
......@@ -56,34 +56,34 @@ def drop_alternatives(msg_bytes, debug=0):
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)
__debug__ and print(part.get_content_type(), end=' ', file=stderr)
part_content_subtype = part.get_content_subtype()
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)
__debug__ and print('txt '+candidate_txt.get_content_type(), end=' ', file=stderr)
candidate_htm = flat_eml[-1].pop()
debug and print('htm '+candidate_htm.get_content_type(), end=' ', file=stderr)
__debug__ and print('htm '+candidate_htm.get_content_type(), end=' ', file=stderr)
candidate_htm_subtype = candidate_htm.get_content_subtype()
if candidate_htm_subtype.startswith('htm'): # html
if are_idem_txt(candidate_txt, candidate_htm, debug):
if are_idem_txt(candidate_txt, candidate_htm):
x_drop_alt.append(candidate_htm_subtype)
else:
new_eml[-1].attach(candidate_htm)
debug and print('htm diff', end=' ', file=stderr)
__debug__ and print('htm diff', end=' ', file=stderr)
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 sub_part.get_content_subtype().startswith('htm'):
if are_idem_txt(candidate_txt, sub_part, debug):
if are_idem_txt(candidate_txt, sub_part):
x_drop_alt.append(sub_part.get_content_subtype())
else:
new_eml[-1].attach(sub_part)
debug and print('rel htm diff', end=' ', file=stderr)
__debug__ and print('rel htm diff', end=' ', file=stderr)
else:
x_drop_alt.append(sub_part.get_content_type())
......@@ -93,9 +93,9 @@ def drop_alternatives(msg_bytes, debug=0):
flat_eml[-1].append(sub_part)
else: # unknown configuration yet
new_eml[-1].attach(candidate_htm)
debug and print('new configuration ?', end=' ', file=stderr)
__debug__ and print('new configuration ?', end=' ', file=stderr)
elif part.get_content_type().startswith('me'): # message
debug and print(' clne', file=stderr)
__debug__ and print(' clne', file=stderr)
flat_sub_eml = [sub_part for sub_part in part.walk()]
flat_sub_eml.reverse()
flat_sub_eml.pop() # replaced by multipart/mixed
......@@ -103,10 +103,10 @@ def drop_alternatives(msg_bytes, debug=0):
flat_eml.append(flat_sub_eml)
new_eml.append(clone_message(part))
elif part_content_subtype.startswith('rel'): # related
debug and print('drop rel', file=stderr)
__debug__ and print('drop rel', file=stderr)
else:
new_eml[-1].attach(part)
debug and print(' kept', file=stderr)
__debug__ and print(' kept', file=stderr)
if len(new_eml) > 1:
new_eml[-2].attach(new_eml[-1])
......@@ -116,13 +116,13 @@ def drop_alternatives(msg_bytes, debug=0):
if len(x_drop_alt):
new_eml[0]['x-drop-alt'] = ', '.join(x_drop_alt)
# debug and _structure(new_eml[0])
# __debug__ and _structure(new_eml[0])
return new_eml[0]
else:
return eml
def are_idem_txt(part_txt, part_htm, debug=0):
def are_idem_txt(part_txt, part_htm):
txt_1, txt_2 = get_txt(part_txt, 3000)
htm_1, htm_2 = get_txt(part_htm, 40000)
s_1, s_2 = min(len(htm_1), len(txt_1)), min(len(htm_2), len(txt_2))
......@@ -134,7 +134,7 @@ def are_idem_txt(part_txt, part_htm, debug=0):
idem_ratio_2 = SequenceMatcher(a=htm_2[-s_2:], b=txt_2[-s_2:]).quick_ratio()
idem_ratio = (idem_ratio_1 + idem_ratio_2) / 2
if debug:
if __debug__:
from shutil import get_terminal_size
ir = ' '+color_ratio(idem_ratio)
cols, rows = get_terminal_size()
......@@ -191,18 +191,18 @@ def clone_message(eml):
if __name__ == "__main__":
DEBUG = 0
debug = 0
for i, arg in enumerate(argv):
if i == 0: # 1st arg of argv is the program name
continue
elif '--debug' in arg:
DEBUG = 1
debug = 1
if DEBUG:
drop_alternatives(stdin.buffer.raw.read(), DEBUG)
if debug:
drop_alternatives(stdin.buffer.raw.read())
else:
input_eml = stdin.buffer.raw.read()
input_eml = stdin.buffer.raw.read() # separate lines to allow debugging
output_eml = drop_alternatives(input_eml)
str_eml = str(output_eml.as_bytes(), errors='replace')
print(str_eml, end='')
#!/usr/bin/env python3
#!/usr/bin/python3
# coding: utf-8
# author : Simon Descarpentries
# date: 2017 - 2018
......@@ -8,109 +8,106 @@ from doctest import run_docstring_examples
from drop_alternatives import drop_alternatives
DEBUG = 0
def test_drop_alternatives(msg_bytes, debug):
def test_drop_alternatives(msg_bytes):
"""
>>> test_drop_alternatives(b'Content-Type: text/plain;\\nA', DEBUG)
>>> test_drop_alternatives(b'Content-Type: text/plain;\\nA')
text/plain
>>> test_drop_alternatives(b'Content-Type: multipart/mixed; boundary=""\\n'
... b'--\\nContent-Type: text/plain;\\nA\\n'
... b'--\\nContent-Type: text/plain;\\nB', DEBUG)
... b'--\\nContent-Type: text/plain;\\nB')
multipart/mixed text/plain text/plain
>>> test_drop_alternatives(b'Content-Type: multipart/mixed; boundary=""\\n'
... b'--\\nContent-Type: multipart/alternative;\\n'
... b'--\\nContent-Type: text/plain;\\nA\\n'
... b'--\\nContent-Type: text/html;\\n <sty> B <!-- D -->/* C */\\n</sty>A', DEBUG)
... b'--\\nContent-Type: text/html;\\n <sty> B <!-- D -->/* C */\\n</sty>A')
multipart/mixed text/plain
>>> test_drop_alternatives(b'Content-Type: multipart/mixed; boundary=""\\n'
... b'--\\nContent-Type: multipart/alternative;\\n'
... b'--\\nContent-Type: text/plain;\\nA\\n'
... b'--\\nContent-Type: text/html;\\n<html>\\t\\t\\t\\t\\t\\t\\t<p>A</p>', DEBUG)
... b'--\\nContent-Type: text/html;\\n<html>\\t\\t\\t\\t\\t\\t\\t<p>A</p>')
multipart/mixed text/plain
>>> test_drop_alternatives(b'Content-Type: multipart/mixed; boundary=""\\n'
... b'--\\nContent-Type: multipart/alternative;\\n'
... b'--\\nContent-Type: text/plain;\\nA\\n'
... b'--\\nContent-Type: text/html;\\n<html>B', DEBUG)
... b'--\\nContent-Type: text/html;\\n<html>B')
multipart/mixed multipart/alternative text/plain text/html
>>> test_drop_alternatives(b'Content-Type: multipart/mixed; boundary=""\\n'
... b'--\\nContent-Type: text/plain;\\nA\\n'
... b'--\\nContent-Type: multipart/alternative;\\n'
... b'--\\nContent-Type: text/plain;\\nB\\n'
... b'--\\nContent-Type: text/html;\\nB', DEBUG)
... b'--\\nContent-Type: text/html;\\nB')
multipart/mixed text/plain text/plain
>>> test_drop_alternatives(b'Content-Type: multipart/mixed; boundary=""\\n'
... b'--\\nContent-Type: text/plain;\\nA\\n'
... b'--\\nContent-Type: text/plain;\\nB\\n'
... b'--\\nContent-Type: text/html;\\n<p>C</p>', DEBUG)
... b'--\\nContent-Type: text/html;\\n<p>C</p>')
multipart/mixed text/plain text/plain text/html
>>> test_drop_alternatives(open('email_test/20160916.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20160916.eml', 'rb').read())
multipart/alternative text/plain
>>> test_drop_alternatives(open('email_test/20170901.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20170901.eml', 'rb').read())
multipart/alternative text/plain
>>> test_drop_alternatives(open('email_test/20170917.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20170917.eml', 'rb').read())
multipart/alternative text/plain
>>> test_drop_alternatives(open('email_test/20170925.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20170925.eml', 'rb').read())
multipart/alternative text/plain
>>> test_drop_alternatives(open('email_test/20171003.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20171003.eml', 'rb').read())
multipart/alternative text/plain
>>> test_drop_alternatives(open('email_test/20171003-2.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20171003-2.eml', 'rb').read())
multipart/alternative text/plain
>>> test_drop_alternatives(open('email_test/20171004.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20171004.eml', 'rb').read())
multipart/mixed text/plain text/plain
>>> test_drop_alternatives(open('email_test/20171004-2.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20171004-2.eml', 'rb').read())
multipart/alternative text/plain
>>> test_drop_alternatives(open('email_test/20171004-3.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20171004-3.eml', 'rb').read())
multipart/mixed text/plain multipart/external-body application/octet-stream
>>> test_drop_alternatives(open('email_test/20171004-4.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20171004-4.eml', 'rb').read())
multipart/alternative text/plain
>>> test_drop_alternatives(open('email_test/20171004-5.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20171004-5.eml', 'rb').read())
multipart/mixed text/plain multipart/rfc822 text/plain text/plain
>>> test_drop_alternatives(open('email_test/20171005.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20171005.eml', 'rb').read())
multipart/related text/plain multipart/external-body image/png
>>> test_drop_alternatives(open('email_test/20171005-3.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20171005-3.eml', 'rb').read())
multipart/alternative text/plain
>>> test_drop_alternatives(open('email_test/20171011.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20171011.eml', 'rb').read())
multipart/alternative text/plain
>>> test_drop_alternatives(open('email_test/20171018.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20171018.eml', 'rb').read())
multipart/alternative text/plain
>>> test_drop_alternatives(open('email_test/20171018-2.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20171018-2.eml', 'rb').read())
multipart/mixed text/plain multipart/external-body application/pdf
>>> test_drop_alternatives(open('email_test/20171018-3.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20171018-3.eml', 'rb').read())
multipart/alternative text/plain
>>> test_drop_alternatives(open('email_test/20171020.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20171020.eml', 'rb').read())
multipart/alternative text/plain
>>> test_drop_alternatives(open('email_test/20171022.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20171022.eml', 'rb').read())
multipart/alternative text/plain
>>> test_drop_alternatives(open('email_test/20171023.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20171023.eml', 'rb').read())
multipart/alternative text/plain
>>> test_drop_alternatives(open('email_test/20171025.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20171025.eml', 'rb').read())
multipart/alternative text/plain
>>> test_drop_alternatives(open('email_test/20171109.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20171109.eml', 'rb').read())
multipart/alternative text/plain
>>> test_drop_alternatives(open('email_test/20171122.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20171122.eml', 'rb').read())
multipart/related text/plain multipart/external-body multipart/external-body text/plain multipart/external-body image/jpeg multipart/external-body image/png
>>> test_drop_alternatives(open('email_test/20180312.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20180312.eml', 'rb').read())
multipart/alternative text/plain
>>> test_drop_alternatives(open('email_test/20180314.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20180314.eml', 'rb').read())
multipart/alternative text/plain
>>> test_drop_alternatives(open('email_test/20180315.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20180315.eml', 'rb').read())
multipart/alternative text/plain
>>> test_drop_alternatives(open('email_test/20180319.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20180319.eml', 'rb').read())
multipart/alternative text/plain
>>> test_drop_alternatives(open('email_test/20180320.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20180320.eml', 'rb').read())
multipart/alternative text/plain
>>> test_drop_alternatives(open('email_test/20180402.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20180402.eml', 'rb').read())
text/html
>>> test_drop_alternatives(open('email_test/20180418.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20180418.eml', 'rb').read())
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)
>>> test_drop_alternatives(open('email_test/20180620.eml', 'rb').read())
multipart/alternative text/plain text/html
>>> test_drop_alternatives(open('email_test/20180708.eml', 'rb').read(), DEBUG)
>>> test_drop_alternatives(open('email_test/20180708.eml', 'rb').read())
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()]))
print(' '.join([p.get_content_type() for p in drop_alternatives(msg_bytes).walk()]))
if __name__ == "__main__":
......
#!/usr/bin/env python3
#!/usr/bin/python3 -O
# coding: utf-8
# author : Simon Descarpentries
# date: 2017-2018
......@@ -60,7 +60,6 @@ Python 3.5.3
"""
DEBUG = 0
startTime = datetime.now()
for i in range(0, 10):
......
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