From: Justus Winter <4winter@informatik.uni-hamburg.de>
To: notmuch@notmuchmail.org
Subject: [PATCH 6/7] py3k: Add and use a mixin class that implements __str__
Date: Wed, 14 Dec 2011 14:04:35 +0100 [thread overview]
Message-ID: <1323867875-30684-1-git-send-email-4winter@informatik.uni-hamburg.de> (raw)
In-Reply-To: <E1RaoOA-0007ka-G7@thinkbox.jade-hamburg.de>
---
bindings/python/notmuch/filename.py | 7 ++-----
bindings/python/notmuch/globals.py | 15 +++++++++++----
bindings/python/notmuch/message.py | 8 +++-----
bindings/python/notmuch/tag.py | 7 ++-----
bindings/python/notmuch/thread.py | 7 ++-----
5 files changed, 20 insertions(+), 24 deletions(-)
diff --git a/bindings/python/notmuch/filename.py b/bindings/python/notmuch/filename.py
index a7cd7e6..969931a 100644
--- a/bindings/python/notmuch/filename.py
+++ b/bindings/python/notmuch/filename.py
@@ -18,10 +18,10 @@ Copyright 2010 Sebastian Spaeth <Sebastian@SSpaeth.de>'
"""
from ctypes import c_char_p
from notmuch.globals import (nmlib, STATUS, NotmuchError,
- NotmuchFilenamesP, NotmuchMessageP)
+ NotmuchFilenamesP, NotmuchMessageP, _str, Python3StringMixIn)
-class Filenames(object):
+class Filenames(Python3StringMixIn):
"""Represents a list of filenames as returned by notmuch
This object contains the Filenames iterator. The main function is
@@ -98,9 +98,6 @@ class Filenames(object):
self._files = None
- def __str__(self):
- return unicode(self).encode('utf-8')
-
def __unicode__(self):
"""Represent Filenames() as newline-separated list of full paths
diff --git a/bindings/python/notmuch/globals.py b/bindings/python/notmuch/globals.py
index c52790c..2111b86 100644
--- a/bindings/python/notmuch/globals.py
+++ b/bindings/python/notmuch/globals.py
@@ -28,6 +28,16 @@ except:
raise ImportError("Could not find shared 'notmuch' library.")
+if sys.version_info[0] == 2:
+ class Python3StringMixIn(object):
+ def __str__(self):
+ return unicode(self).encode('utf-8')
+else:
+ class Python3StringMixIn(object):
+ def __str__(self):
+ return self.__unicode__()
+
+
class Enum(object):
"""Provides ENUMS as "code=Enum(['a','b','c'])" where code.a=0 etc..."""
def __init__(self, names):
@@ -90,7 +100,7 @@ argument to receive a human readable string"""
STATUS.__name__ = 'STATUS'
-class NotmuchError(Exception):
+class NotmuchError(Exception, Python3StringMixIn):
"""Is initiated with a (notmuch.STATUS[, message=None]). It will not
return an instance of the class NotmuchError, but a derived instance
of a more specific Error Message, e.g. OutOfMemoryError. Each status
@@ -134,9 +144,6 @@ class NotmuchError(Exception):
self.status = status
self.message = message
- def __str__(self):
- return unicode(self).encode('utf-8')
-
def __unicode__(self):
if self.message is not None:
return self.message
diff --git a/bindings/python/notmuch/message.py b/bindings/python/notmuch/message.py
index bf0c4da..955382d 100644
--- a/bindings/python/notmuch/message.py
+++ b/bindings/python/notmuch/message.py
@@ -21,7 +21,8 @@ Copyright 2010 Sebastian Spaeth <Sebastian@SSpaeth.de>'
from ctypes import c_char_p, c_long, c_uint, c_int
from datetime import date
-from notmuch.globals import (nmlib, STATUS, NotmuchError, Enum, _str,
+from notmuch.globals import (
+ nmlib, STATUS, NotmuchError, Enum, _str, Python3StringMixIn,
NotmuchTagsP, NotmuchMessagesP, NotmuchMessageP, NotmuchFilenamesP)
from notmuch.tag import Tags
from notmuch.filename import Filenames
@@ -239,7 +240,7 @@ class Messages(object):
sys.stdout.write(set_end)
-class Message(object):
+class Message(Python3StringMixIn):
"""Represents a single Email message
Technically, this wraps the underlying *notmuch_message_t*
@@ -796,9 +797,6 @@ class Message(object):
"""Represent a Message() object by str()"""
return self.__str__()
- def __str__(self):
- return unicode(self).encode('utf-8')
-
def __unicode__(self):
format = "%s (%s) (%s)"
return format % (self.get_header('from'),
diff --git a/bindings/python/notmuch/tag.py b/bindings/python/notmuch/tag.py
index d42ba77..ceb7244 100644
--- a/bindings/python/notmuch/tag.py
+++ b/bindings/python/notmuch/tag.py
@@ -17,10 +17,10 @@ along with notmuch. If not, see <http://www.gnu.org/licenses/>.
Copyright 2010 Sebastian Spaeth <Sebastian@SSpaeth.de>'
"""
from ctypes import c_char_p
-from notmuch.globals import nmlib, STATUS, NotmuchError, NotmuchTagsP
+from notmuch.globals import nmlib, STATUS, NotmuchError, NotmuchTagsP, _str, Python3StringMixIn
-class Tags(object):
+class Tags(Python3StringMixIn):
"""Represents a list of notmuch tags
This object provides an iterator over a list of notmuch tags (which
@@ -111,9 +111,6 @@ class Tags(object):
left."""
return self._valid(self._tags) > 0
- def __str__(self):
- return unicode(self).encode('utf-8')
-
def __unicode__(self):
"""string representation of :class:`Tags`: a space separated list of tags
diff --git a/bindings/python/notmuch/thread.py b/bindings/python/notmuch/thread.py
index 39285d6..7393097 100644
--- a/bindings/python/notmuch/thread.py
+++ b/bindings/python/notmuch/thread.py
@@ -20,13 +20,13 @@ Copyright 2010 Sebastian Spaeth <Sebastian@SSpaeth.de>'
from ctypes import c_char_p, c_long, c_int
from notmuch.globals import (nmlib, STATUS,
NotmuchError, NotmuchThreadP, NotmuchThreadsP, NotmuchMessagesP,
- NotmuchTagsP,)
+ NotmuchTagsP, Python3StringMixIn)
from notmuch.message import Messages
from notmuch.tag import Tags
from datetime import date
-class Threads(object):
+class Threads(Python3StringMixIn):
"""Represents a list of notmuch threads
This object provides an iterator over a list of notmuch threads
@@ -392,9 +392,6 @@ class Thread(object):
raise NotmuchError(STATUS.NULL_POINTER)
return Tags(tags_p, self)
- def __str__(self):
- return unicode(self).encode('utf-8')
-
def __unicode__(self):
frm = "thread:%s %12s [%d/%d] %s; %s (%s)"
--
1.7.7.3
next prev parent reply other threads:[~2011-12-14 13:04 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-12-14 10:58 [python] RFC: supporting python 2 and 3 with one codebase Justus Winter
2011-12-14 10:58 ` [PATCH 1/7] py3k: The execfile built-in has been removed in python 3 Justus Winter
2011-12-14 10:58 ` [PATCH 2/7] py3k: The ConfigParser module has been renamed to configparser Justus Winter
2011-12-14 10:58 ` [PATCH 3/7] py3k: All strings are unicode strings in py3k Justus Winter
2011-12-14 12:17 ` Tomi Ollila
2011-12-14 12:52 ` Justus Winter
2011-12-14 10:58 ` [PATCH 4/7] py3k: Rename .next() to __next__(), add python2.x compatibility alias Justus Winter
2011-12-14 10:58 ` [PATCH 5/7] py3k: the basestring and unicode types are removed in python 3 Justus Winter
2012-01-02 15:15 ` Sebastian Spaeth
2012-01-04 18:07 ` Tomi Ollila
2012-01-08 14:04 ` Justus Winter
2012-01-08 14:06 ` [PATCH] " Justus Winter
2011-12-14 10:58 ` [PATCH 6/7] py3k: Add and use a mixin class that implements __str__ Justus Winter
2011-12-14 12:26 ` Tomi Ollila
2011-12-14 12:56 ` Justus Winter
2011-12-14 13:04 ` Justus Winter [this message]
2011-12-14 10:58 ` [PATCH 7/7] python: add missing conversions from and to utf-8 Justus Winter
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://notmuchmail.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1323867875-30684-1-git-send-email-4winter@informatik.uni-hamburg.de \
--to=4winter@informatik.uni-hamburg.de \
--cc=notmuch@notmuchmail.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://yhetil.org/notmuch.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).