Skip to content

Commit

Permalink
Merge pull request #19 from Gentux/fix-python3
Browse files Browse the repository at this point in the history
Fix issue #18 (python3)
  • Loading branch information
Gentux committed Apr 21, 2018
2 parents 69827e8 + 06a3a00 commit ccd87f9
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 22 deletions.
3 changes: 2 additions & 1 deletion imap_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ def disconnect(imap_account):


def list_dir(imap_account):
status, data = imap_account.list()
status, data_bytes = imap_account.list()
data = [data_byte.decode('utf-8') for data_byte in data_bytes]
if status == const.STATUS_OK:
for datum in data:
datum_match = LIST_DIR_RE.match(datum)
Expand Down
14 changes: 12 additions & 2 deletions imap_cli/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,18 @@ def fetch(imap_account, message_set=None, message_parts=None):
log.warning(u'No directory specified, selecting {}'.format(
const.DEFAULT_DIRECTORY))
imap_cli.change_dir(imap_account, const.DEFAULT_DIRECTORY)
typ, data = imap_account.uid('FETCH',
request_message_set, request_message_parts)
typ, data_bytes = imap_account.uid(
'FETCH',
request_message_set, request_message_parts)
data = []
for mail in data_bytes:
if len(mail) == 1:
continue
mail_parts = []
for mail_part in mail:
mail_parts.append(mail_part.decode('utf-8'))
data.append(mail_parts)

if typ == const.STATUS_OK:
return data

Expand Down
15 changes: 11 additions & 4 deletions imap_cli/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,11 @@ def fetch_uids(imap_account, charset=None, limit=None, search_criterion=None):
const.DEFAULT_DIRECTORY))
imap_cli.change_dir(imap_account, const.DEFAULT_DIRECTORY)

status, data = imap_account.uid('SEARCH', charset,
request_search_criterion)
status, data_bytes = imap_account.uid(
'SEARCH',
charset,
request_search_criterion)
data = [data_bytes[0].decode('utf-8')]
if status == const.STATUS_OK:
return data[0].split() if limit is None else data[0].split()[-limit:]

Expand All @@ -386,8 +389,12 @@ def parse_thread_response(thread_string):
"""
# FIXME(rsoufflet) Not sure the use of "ast" module is the right solution.
# Any ideas are welcome here
return ast.literal_eval('[{}]'.format(thread_string.replace(
' ', ', ').replace('(', '[').replace(')', '], ')))
return ast.literal_eval('[{}]'.format(
thread_string
.decode('utf-8')
.replace(' ', ', ')
.replace('(', '[')
.replace(')', '], ')))


def threads_to_mail_set(threads):
Expand Down
38 changes: 24 additions & 14 deletions imap_cli/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
"""Imaplib IMAP4_SSL mocking class"""


from builtins import bytes

import mock


example_email_content = u'\r\n'.join([
example_email_content_unicode = u'\r\n'.join([
u'From: exampleFrom <example@from.org>',
u'Date: Tue, 03 Jan 1989 09:42:34 +0200',
u'Subject: Mocking IMAP Protocols',
Expand All @@ -18,6 +20,7 @@
u'',
u'EMAIL BODY CONTENT',
])
example_email_content = bytes(example_email_content_unicode, 'utf-8')


class ImapConnectionMock(mock.Mock):
Expand All @@ -35,9 +38,12 @@ def fetch(self, mails_id_set, request):
elif request.find('UID') >= 0:
uid_str = u'UID 1 '

imap_header = u'1 ({uid_str}{flag_str}BODY[HEADER] {{1621}}'.format(
flag_str=flag_str, uid_str=uid_str)
return (u'OK', [(imap_header, example_email_content), ')'])
imap_header = bytes(
u'1 ({uid_str}{flag_str}BODY[HEADER] {{1621}}'.format(
flag_str=flag_str,
uid_str=uid_str),
'utf-8')
return (u'OK', [(imap_header, example_email_content, b')')])

def store(self, mails_id_set, request, flags):
flags = [u'\\\\Answered', u'\\\\Seen', 'NonJunk']
Expand All @@ -46,15 +52,19 @@ def store(self, mails_id_set, request, flags):
return (u'OK', [u'1 (UID 1 FLAGS ({}))'.format(' '.join(flags))])

def list(self, *args):
wrong_chars_mailbox = bytes(
u' '.join([
u'(\\HasNoChildren)',
u'"."',
u'"&A5Q-i&A8A-ect&API-r&AP8-_&APEA5A-m&AOk-"']),
'utf-8')
if self.fail is True:
return (u'OK', [(
u'(\\HasNoChildren) ) "." '
u'"&A5Q-i&A8A-ect&API-r&AP8-_&APEA5A-m&AOk-"'),
u'(\\HasNoChildren) "." "INBOX"'])
return (u'OK', [(
u'(\\HasNoChildren) "." '
u'"&A5Q-i&A8A-ect&API-r&AP8-_&APEA5A-m&AOk-"'),
u'(\\HasNoChildren) "." "INBOX"'])
return (u'OK', [
wrong_chars_mailbox,
bytes(u'(\\HasNoChildren) "." "INBOX"', 'utf-8')])
return (u'OK', [
wrong_chars_mailbox,
bytes(u'(\\HasNoChildren) "." "INBOX"', 'utf-8')])

def login(self, *args):
return (u'OK', [u'Logged in'])
Expand All @@ -70,7 +80,7 @@ def select(self, *args):
return (u'OK', [u'1'])

def search(self, *args):
return (u'OK', [u'1'])
return (u'OK', [bytes(u'1', 'utf-8')])

def status(self, *args):
if self.fail is True:
Expand All @@ -93,4 +103,4 @@ def uid(self, command, *args):
return self.thread(*args)

def thread(self, *args):
return (u'OK', [u'((1)(2))(3 4)'])
return ('OK', [b'((1)(2))(3 4)'])
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
docopts>=0.6.1
mock>=2.0.0
future
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
install_requires=[
"docopts>=0.6",
"mock>=1.0.1",
"future",
],
keywords="imap cli high level",
license="MIT License",
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tox]
minversion = 1.6
envlist = py36,pep8,docs
envlist = py2,py3,pep8,docs
skipsdist = True

[testenv]
Expand Down

0 comments on commit ccd87f9

Please sign in to comment.