From: Naveen Nathan Date: Thu, 24 Oct 2013 23:23:54 +0000 (+1100) Subject: added string representation for Address() X-Git-Tag: 0.11~3^2~15 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=0f845edcd909158bb4ea32e8fc884611dcf84764;p=imapclient.git added string representation for Address() --- diff --git a/imapclient/response_types.py b/imapclient/response_types.py index 7e51d31..d8b01b5 100644 --- a/imapclient/response_types.py +++ b/imapclient/response_types.py @@ -9,62 +9,63 @@ Inspired by Ruby Net::IMAP::Envelope and Net::IMAP::Address """ from collections import namedtuple +from email.utils import formataddr -# the following hacks to add docstrings to namedtuples is courtesy of: +# the following hack to add docstrings to namedtuples is courtesy of: # http://stackoverflow.com/questions/1606436/adding-docstrings-to-namedtuples-in-python -class Address(namedtuple("Address", "Name Route Mailbox Host")): +class Address(namedtuple("Address", "name route mailbox host")): """ Represents electronic mail addresses. Fields (in-order): - Name -- Returns the phrase from [RFC-822] mailbox. + name -- Returns the phrase from [RFC-822] mailbox. - Route -- Returns the route from [RFC-822] route-addr. + route -- Returns the route from [RFC-822] route-addr. - Mailbox -- None indicates end of [RFC-822] group. If not None and host is None, + mailbox -- None indicates end of [RFC-822] group. If not None and host is None, returns [RFC-822] group name. Otherwise, returns [RFC-822] local-part. - Host -- None indicates [RFC-822] group syntax. Otherwise, returns [RFC-822] domain name. - + host -- None indicates [RFC-822] group syntax. Otherwise, returns [RFC-822] domain name. """ -# note lowercase "from" is a keyword -class Envelope(namedtuple("Envelope", "Date Subject From Sender Reply_To To " + \ - "Cc Bcc In_Reply_To Message_Id")): + def __str__(self): + return formataddr((self.name, self.mailbox + '@' + self.host)) + +class Envelope(namedtuple("Envelope", "date subject from_ sender reply_to to " + + "cc bcc in_reply_to message_id")): """ Represents envelope structures of messages. Fields (in-order): - Date -- Returns a string that represents the "Date:" header. + date -- Returns a string that represents the "Date:" header. - Subject -- Returns a string that represents the "Subject:" header. + subject -- Returns a string that represents the "Subject:" header. - From -- Returns a tuple sequence of Address structure that represents "From:" header, - or None if header does not exist. + from_ -- Returns a tuple sequence of Address structure that represents "From:" header, + or None if header does not exist. - Sender -- Returns a tuple sequence of Address structure that represents "Sender:" header, + sender -- Returns a tuple sequence of Address structure that represents "Sender:" header, or None if header does not exist. - Reply_To -- Returns a tuple sequence of Address structure that represents "Reply_To:" header, + reply_to -- Returns a tuple sequence of Address structure that represents "Reply_To:" header, or None if header does not exist. - To -- Returns a tuple sequence of Address structure that represents "To:" header, + to -- Returns a tuple sequence of Address structure that represents "To:" header, or None if header does not exist. - Cc -- Returns a tuple sequence of Address structure that represents "Cc:" header, + cc -- Returns a tuple sequence of Address structure that represents "Cc:" header, or None if header does not exist. - Bcc -- Returns a tuple sequence of Address structure that represents "Bcc:" header, + bcc -- Returns a tuple sequence of Address structure that represents "Bcc:" header, or None if header does not exist. - In_Reply_To -- Returns a string that represents the "In-Reply-To:" header. - - Message_Id -- Returns a string that represents the "Message-Id:" header. + in_reply_to -- Returns a string that represents the "In-Reply-To:" header. + message_id -- Returns a string that represents the "Message-Id:" header. """