def _mark_lines(lines, sender): """Mark message lines with markers to distinguish signature lines. Markers: * e - empty line * s - line identified as signature * t - other i.e. ordinary text line >>> mark_message_lines(['Some text', '', 'Bob'], 'Bob') 'tes' """ global EXTRACTOR candidate = get_signature_candidate(lines) # at first consider everything to be text no signature markers = bytearray('t' * len(lines)) # mark lines starting from bottom up # mark only lines that belong to candidate # no need to mark all lines of the message for i, line in reversed(list(enumerate(candidate))): # markers correspond to lines not candidate # so we need to recalculate our index to be # relative to lines not candidate j = len(lines) - len(candidate) + i if not line.strip(): markers[j] = 'e' elif is_signature_line(line, sender, EXTRACTOR): markers[j] = 's' return markers
def _mark_lines(lines, sender): """Mark message lines with markers to distinguish signature lines. Markers: * e - empty line * s - line identified as signature * t - other i.e. ordinary text line >>> mark_message_lines(['Some text', '', 'Bob'], 'Bob') 'tes' """ global EXTRACTOR candidate = get_signature_candidate(lines) # at first consider everything to be text no signature markers = bytearray('t'*len(lines)) # mark lines starting from bottom up # mark only lines that belong to candidate # no need to mark all lines of the message for i, line in reversed(list(enumerate(candidate))): # markers correspond to lines not candidate # so we need to recalculate our index to be # relative to lines not candidate j = len(lines) - len(candidate) + i if not line.strip(): markers[j] = 'e' elif is_signature_line(line, sender, EXTRACTOR): markers[j] = 's' return markers
def test_get_signature_candidate(): # if there aren't at least 2 non-empty lines there should be no signature for lines in [], [''], ['', ''], ['abc']: eq_([], bruteforce.get_signature_candidate(lines)) # first line never included lines = ['text', 'signature'] eq_(['signature'], bruteforce.get_signature_candidate(lines)) # test when message is shorter then SIGNATURE_MAX_LINES with patch.object(bruteforce, 'SIGNATURE_MAX_LINES', 3): lines = ['text', '', '', 'signature'] eq_(['signature'], bruteforce.get_signature_candidate(lines)) # test when message is longer then the SIGNATURE_MAX_LINES with patch.object(bruteforce, 'SIGNATURE_MAX_LINES', 2): lines = ['text1', 'text2', 'signature1', '', 'signature2'] eq_(['signature1', '', 'signature2'], bruteforce.get_signature_candidate(lines)) # test long lines not encluded with patch.object(bruteforce, 'TOO_LONG_SIGNATURE_LINE', 3): lines = ['BR,', 'long', 'Bob'] eq_(['Bob'], bruteforce.get_signature_candidate(lines)) # test list (with dashes as bullet points) not included lines = ['List:,', '- item 1', '- item 2', '--', 'Bob'] eq_(['--', 'Bob'], bruteforce.get_signature_candidate(lines))
def test_get_signature_candidate(): # if there aren't at least 2 non-empty lines there should be no signature for lines in [], [""], ["", ""], ["abc"]: eq_([], bruteforce.get_signature_candidate(lines)) # first line never included lines = ["text", "signature"] eq_(["signature"], bruteforce.get_signature_candidate(lines)) # test when message is shorter then SIGNATURE_MAX_LINES with patch.object(bruteforce, "SIGNATURE_MAX_LINES", 3): lines = ["text", "", "", "signature"] eq_(["signature"], bruteforce.get_signature_candidate(lines)) # test when message is longer then the SIGNATURE_MAX_LINES with patch.object(bruteforce, "SIGNATURE_MAX_LINES", 2): lines = ["text1", "text2", "signature1", "", "signature2"] eq_(["signature1", "", "signature2"], bruteforce.get_signature_candidate(lines)) # test long lines not encluded with patch.object(bruteforce, "TOO_LONG_SIGNATURE_LINE", 3): lines = ["BR,", "long", "Bob"] eq_(["Bob"], bruteforce.get_signature_candidate(lines)) # test list (with dashes as bullet points) not included lines = ["List:,", "- item 1", "- item 2", "--", "Bob"] eq_(["--", "Bob"], bruteforce.get_signature_candidate(lines))