Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more regexs cases for different devices/email providers #32

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 40 additions & 23 deletions email_reply_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,20 @@ class EmailMessage(object):
""" An email message represents a parsed email body.
"""

SIG_REGEX = re.compile(r'(--|__|-\w)|(^Sent from my (\w+\s*){1,3})')
QUOTE_HDR_REGEX = re.compile('On.*wrote:$')
QUOTED_REGEX = re.compile(r'(>+)')
HEADER_REGEX = re.compile(r'^\*?(From|Sent|To|Subject):\*? .+')
_MULTI_QUOTE_HDR_REGEX = r'(?!On.*On\s.+?wrote:)(On\s(.+?)wrote:)'
SIG_REGEX = re.compile(r"(--|__|-\w)|(^Sent from my (\w+\s*){1,3})")
BLACKBERRY_REGEX = re.compile(r"(--|__|-\w)|(^Sent with Black(\w+\s*){1,3})")
QUOTE_HDR_REGEX = re.compile("On.*wrote:$")
QUOTED_REGEX = re.compile(r"(>+)")
HEADER_REGEX = re.compile(r"^\*?(From|Sent|To|Subject):\*? .+")
ALT_HEADER_REGEX = re.compile(r"^\*{2}?(From|Sent|To|Subject):\*{2}? .+")
_MULTI_QUOTE_HDR_REGEX = r"(?!On.*On\s.+?wrote:)(On\s(.+?)wrote:)"
MULTI_QUOTE_HDR_REGEX = re.compile(_MULTI_QUOTE_HDR_REGEX, re.DOTALL | re.MULTILINE)
MULTI_QUOTE_HDR_REGEX_MULTILINE = re.compile(_MULTI_QUOTE_HDR_REGEX, re.DOTALL)

def __init__(self, text):
self.fragments = []
self.fragment = None
self.text = text.replace('\r\n', '\n')
self.text = text.replace("\r\n", "\n")
self.found_visible = False

def read(self):
Expand All @@ -61,13 +63,16 @@ def read(self):

is_multi_quote_header = self.MULTI_QUOTE_HDR_REGEX_MULTILINE.search(self.text)
if is_multi_quote_header:
self.text = self.MULTI_QUOTE_HDR_REGEX.sub(is_multi_quote_header.groups()[0].replace('\n', ''), self.text)
self.text = self.MULTI_QUOTE_HDR_REGEX.sub(
is_multi_quote_header.groups()[0].replace("\n", ""), self.text
)

# Fix any outlook style replies, with the reply immediately above the signature boundary line
# Fix any outlook style replies, with the reply
# immediately above the signature boundary line
# See email_2_2.txt for an example
self.text = re.sub('([^\n])(?=\n ?[_-]{7,})', '\\1\n', self.text, re.MULTILINE)
self.text = re.sub("([^\n])(?=\n ?[_-]{7,})", "\\1\n", self.text, re.MULTILINE)

self.lines = self.text.split('\n')
self.lines = self.text.split("\n")
self.lines.reverse()

for line in self.lines:
Expand All @@ -87,7 +92,7 @@ def reply(self):
for f in self.fragments:
if not (f.hidden or f.quoted):
reply.append(f.content)
return '\n'.join(reply)
return "\n".join(reply)

def _scan_line(self, line):
""" Reviews each line in email message and determines fragment type
Expand All @@ -96,16 +101,24 @@ def _scan_line(self, line):
"""
is_quote_header = self.QUOTE_HDR_REGEX.match(line) is not None
is_quoted = self.QUOTED_REGEX.match(line) is not None
is_header = is_quote_header or self.HEADER_REGEX.match(line) is not None
is_alt_header = self.ALT_HEADER_REGEX.match(line) is not None
is_header = (
is_quote_header
or self.HEADER_REGEX.match(line) is not None
or is_alt_header
)

if self.fragment and len(line.strip()) == 0:
if self.SIG_REGEX.match(self.fragment.lines[-1].strip()):
if self.SIG_REGEX.match(
self.fragment.lines[-1].strip()
) or self.BLACKBERRY_REGEX.match(self.fragment.lines[-1].strip()):
self.fragment.signature = True
self._finish_fragment()

if self.fragment \
and ((self.fragment.headers == is_header and self.fragment.quoted == is_quoted) or
(self.fragment.quoted and (is_quote_header or len(line.strip()) == 0))):
if self.fragment and (
(self.fragment.headers == is_header and self.fragment.quoted == is_quoted)
or (self.fragment.quoted and (is_quote_header or len(line.strip()) == 0))
):

self.fragment.lines.append(line)
else:
Expand All @@ -128,16 +141,20 @@ def _finish_fragment(self):
if self.fragment:
self.fragment.finish()
if self.fragment.headers:
# Regardless of what's been seen to this point, if we encounter a headers fragment,
# all the previous fragments should be marked hidden and found_visible set to False.
# Regardless of what's been seen to this point,
# if we encounter a headers fragment,
# all the previous fragments should be marked hidden
# and found_visible set to False.
self.found_visible = False
for f in self.fragments:
f.hidden = True
if not self.found_visible:
if self.fragment.quoted \
or self.fragment.headers \
or self.fragment.signature \
or (len(self.fragment.content.strip()) == 0):
if (
self.fragment.quoted
or self.fragment.headers
or self.fragment.signature
or (len(self.fragment.content.strip()) == 0)
):

self.fragment.hidden = True
else:
Expand All @@ -164,7 +181,7 @@ def finish(self):
belonging to fragment.
"""
self.lines.reverse()
self._content = '\n'.join(self.lines)
self._content = "\n".join(self.lines)
self.lines = None

@property
Expand Down
2 changes: 1 addition & 1 deletion email_reply_parser/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = '0.5.11'
VERSION = "0.5.11"
31 changes: 17 additions & 14 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,31 @@
except ImportError:
from distutils.core import setup

sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'email_reply_parser'))
import version
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "email_reply_parser"))
try:
import version
except Exception as e:
raise

setup(
name='email_reply_parser',
name="email_reply_parser",
version=version.VERSION,
description='Email reply parser',
packages=['email_reply_parser'],
package_data={'email_reply_parser': ['../VERSION']},
author='Royce Haynes',
author_email='royce.haynes@gmail.com',
url='https://github.com/zapier/email-reply-parser',
license='MIT',
test_suite='test',
description="Email reply parser",
packages=["email_reply_parser"],
package_data={"email_reply_parser": ["../VERSION"]},
author="Royce Haynes",
author_email="royce.haynes@gmail.com",
url="https://github.com/zapier/email-reply-parser",
license="MIT",
test_suite="test",
classifiers=[
'Topic :: Software Development',
"Topic :: Software Development",
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.6",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.3",
"Programming Language :: Python :: 3.4",
]
)
],
)