Browse Source

Decode bytes objects obtained in Python 3

The imaplib module in Python 3 returns payloads as bytes, which are not
automatically converted to strs. That's a good thing! But we need to
encode manually.
pull/342/head
Leonardo Pistone 6 years ago
committed by Leonardo Pistone
parent
commit
1498437ae7
  1. 15
      mail_check_mailbox_size/models/fetchmail.py

15
mail_check_mailbox_size/models/fetchmail.py

@ -53,7 +53,7 @@ class Fetchmail(models.Model):
number_of_messages_all = 0
size_all = 0
for item in list:
x = item.split()
x = item.decode().split()
mailbox = x[-1]
# Select the desired folder
@ -64,13 +64,14 @@ class Fetchmail(models.Model):
"Server %s responded %s for folder %s"
% (server.name, number_of_messages, mailbox))
continue
number_of_messages_all += int(number_of_messages[0])
number_of_messages_all += int(
number_of_messages[0].decode())
size_folder = 0
# Go through all the messages in the selected folder
typ, msg = imap_server.search(None, 'ALL')
# Find the first and last messages
m = [int(msg_part) for msg_part in msg[0].split()]
m = [int(msg_part.decode()) for msg_part in msg[0].split()]
m.sort()
if m:
message_set = "%d:%d" % (m[0], m[-1])
@ -78,7 +79,7 @@ class Fetchmail(models.Model):
message_set, "(UID RFC822.SIZE)")
for i in range(m[-1]):
tmp = sizes_response[i].split()
size_folder += int(tmp[-1].replace(')', ''))
size_folder += int(tmp[-1].decode().replace(')', ''))
else:
size_folder = 0
result_msg += (
@ -87,7 +88,11 @@ class Fetchmail(models.Model):
"<td style=\"text-align:right\">%i</td>"
"<td style=\"text-align:right\">%s</td>"
"</tr>"
) % (mailbox, int(number_of_messages[0]), size_folder)
) % (
mailbox,
int(number_of_messages[0].decode()),
size_folder
)
size_all += size_folder
result_msg += _(

Loading…
Cancel
Save