Skip to content

Commit

Permalink
Merge branch 'release/3.6.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
fedelemantuano committed Oct 4, 2018
2 parents 6b37578 + d37f9b8 commit 62cb239
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 11 deletions.
13 changes: 13 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[run]
source = src/modules/

[report]
omit = mailparser/version.py

exclude_lines =
pragma: no cover
except OSError
def __repr__
raise AssertionError
raise NotImplementedError
if __name__ == .__main__.:
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ There are other properties to get:
- attachments
- sender IP address
- to domains
- timezone

To get custom headers you should replace "-" with "\_".
Example for header `X-MSMail-Priority`:
Expand Down Expand Up @@ -172,6 +173,7 @@ mail.text_plain: only text plain mail parts in a list
mail.text_html: only text html mail parts in a list
mail.to
mail.to_domains
mail.timezone: returns the timezone, offset from UTC
```

## Usage from command-line
Expand Down
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ headers <https://www.iana.org/assignments/message-headers/message-headers.xhtml>
- to

There are other properties to get: - body - body html - body plain -
headers - attachments - sender IP address - to domains
headers - attachments - sender IP address - to domains - timezone

To get custom headers you should replace “-” with “\_”. Example for
header ``X-MSMail-Priority``:
Expand Down Expand Up @@ -178,6 +178,7 @@ Then you can get all parts
mail.text_html: only text html mail parts in a list
mail.to
mail.to_domains
mail.timezone: returns the timezone, offset from UTC

Usage from command-line
-----------------------
Expand Down
4 changes: 3 additions & 1 deletion mailparser/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,6 @@
"body",
"date",
"received",
"to_domains"])
"timezone",
"to_domains",
])
15 changes: 14 additions & 1 deletion mailparser/mailparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,10 +547,23 @@ def date(self):
conv = None

try:
conv = convert_mail_date(date)
conv, _ = convert_mail_date(date)
finally:
return conv

@property
def timezone(self):
"""
Return timezone. Offset from UTC.
"""
date = self.message.get('date')
timezone = 0

try:
_, timezone = convert_mail_date(date)
finally:
return timezone

@property
def date_json(self):
"""
Expand Down
16 changes: 11 additions & 5 deletions mailparser/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,15 @@ def receiveds_parsing(receiveds):


def convert_mail_date(date):
log.debug("Date to parse: {!r}".format(date))
d = email.utils.parsedate_tz(date)
log.debug("Date parsed: {!r}".format(d))
t = email.utils.mktime_tz(d)
return datetime.datetime.utcfromtimestamp(t)
log.debug("Date parsed in timestamp: {!r}".format(t))
date_utc = datetime.datetime.utcfromtimestamp(t)
timezone = d[9] / 3600 if d[9] else 0
timezone = "{:+.0f}".format(timezone)
return date_utc, timezone


def receiveds_not_parsed(receiveds):
Expand Down Expand Up @@ -342,7 +348,7 @@ def receiveds_format(receiveds):
# "for <eboktor@romolo.com>; Tue, 7 Mar 2017 14:29:24 -0800",
i["date"] = i["date"].split(";")[-1]
try:
j["date_utc"] = convert_mail_date(i["date"])
j["date_utc"], _ = convert_mail_date(i["date"])
except TypeError:
j["date_utc"] = None

Expand Down Expand Up @@ -418,22 +424,22 @@ def get_mail_keys(message):
return all_parts


def safe_print(data):
def safe_print(data): # pragma: no cover
try:
print(data)
except UnicodeEncodeError:
print(data.encode('utf-8'))


def print_mail_fingerprints(data):
def print_mail_fingerprints(data): # pragma: no cover
md5, sha1, sha256, sha512 = fingerprints(data)
print("md5:\t{}".format(md5))
print("sha1:\t{}".format(sha1))
print("sha256:\t{}".format(sha256))
print("sha512:\t{}".format(sha512))


def print_attachments(attachments, flag_hash):
def print_attachments(attachments, flag_hash): # pragma: no cover
if flag_hash:
for i in attachments:
if i.get("content_transfer_encoding") == "base64":
Expand Down
4 changes: 2 additions & 2 deletions mailparser/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
limitations under the License.
"""

__version__ = "3.5.1" # pragma: no cover
__version__ = "3.6.0"

if __name__ == "__main__": # pragma: no cover
if __name__ == "__main__":
print(__version__)
30 changes: 29 additions & 1 deletion tests/test_mail_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@

import mailparser
from mailparser.utils import (
convert_mail_date,
fingerprints,
get_header,
get_to_domains,
msgconvert,
ported_open,
receiveds_parsing)
ported_string,
receiveds_parsing,
)

from mailparser.exceptions import MailParserEnvironmentError

Expand Down Expand Up @@ -463,6 +466,9 @@ def test_from_file_obj(self):
result = mail.defects
self.assertIsInstance(result, list)

result = mail.timezone
self.assertEquals(result, "+1")

def test_get_to_domains(self):
m = mailparser.parse_from_file(mail_test_6)

Expand All @@ -477,6 +483,28 @@ def test_get_to_domains(self):

self.assertIsInstance(m.to_domains_json, six.text_type)

def test_convert_mail_date(self):
s = "Mon, 20 Mar 2017 05:12:54 +0600"
d, t = convert_mail_date(s)
self.assertEquals(t, "+6")
self.assertEquals(str(d), "2017-03-19 23:12:54")
s = "Mon, 20 Mar 2017 05:12:54 -0600"
d, t = convert_mail_date(s)
self.assertEquals(t, "-6")

def test_ported_string(self):
raw_data = ""
s = ported_string(raw_data)
self.assertEquals(s, six.text_type())

raw_data = "test "
s = ported_string(raw_data)
self.assertEquals(s, "test")

raw_data = u"test "
s = ported_string(raw_data)
self.assertEquals(s, "test")


if __name__ == '__main__':
unittest.main(verbosity=2)

0 comments on commit 62cb239

Please sign in to comment.