]> git.notmuchmail.org Git - notmuch/blobdiff - bindings/python/notmuch/filename.py
python: improve Query.count_messages docstring
[notmuch] / bindings / python / notmuch / filename.py
index de4d785ad6085ddfd3fbf42ebe54ae33d5ca76c7..469b6a5a97640a463d9d8625ff21078271107181 100644 (file)
@@ -17,10 +17,11 @@ along with notmuch.  If not, see <http://www.gnu.org/licenses/>.
 Copyright 2010 Sebastian Spaeth <Sebastian@SSpaeth.de>'
 """
 from ctypes import c_char_p
 Copyright 2010 Sebastian Spaeth <Sebastian@SSpaeth.de>'
 """
 from ctypes import c_char_p
-from notmuch.globals import nmlib, STATUS, NotmuchError
+from notmuch.globals import (nmlib, STATUS, NotmuchError,
+    NotmuchFilenamesP, NotmuchMessageP, 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
     """Represents a list of filenames as returned by notmuch
 
     This object contains the Filenames iterator. The main function is
@@ -50,6 +51,7 @@ class Filenames(object):
 
     #notmuch_filenames_get
     _get = nmlib.notmuch_filenames_get
 
     #notmuch_filenames_get
     _get = nmlib.notmuch_filenames_get
+    _get.argtypes = [NotmuchFilenamesP]
     _get.restype = c_char_p
 
     def __init__(self, files_p, parent):
     _get.restype = c_char_p
 
     def __init__(self, files_p, parent):
@@ -67,13 +69,21 @@ class Filenames(object):
              reference to it, so we can automatically delete the db object
              once all derived objects are dead.
         """
              reference to it, so we can automatically delete the db object
              once all derived objects are dead.
         """
-        if files_p is None:
+        if not files_p:
             raise NotmuchError(STATUS.NULL_POINTER)
 
         self._files = files_p
         #save reference to parent object so we keep it alive
         self._parent = parent
 
             raise NotmuchError(STATUS.NULL_POINTER)
 
         self._files = files_p
         #save reference to parent object so we keep it alive
         self._parent = parent
 
+    _valid = nmlib.notmuch_filenames_valid
+    _valid.argtypes = [NotmuchFilenamesP]
+    _valid.restype = bool
+
+    _move_to_next = nmlib.notmuch_filenames_move_to_next
+    _move_to_next.argtypes = [NotmuchFilenamesP]
+    _move_to_next.restype = None
+
     def as_generator(self):
         """Return generator of Filenames
 
     def as_generator(self):
         """Return generator of Filenames
 
@@ -82,13 +92,13 @@ class Filenames(object):
         if self._files is None:
             raise NotmuchError(STATUS.NOT_INITIALIZED)
 
         if self._files is None:
             raise NotmuchError(STATUS.NOT_INITIALIZED)
 
-        while nmlib.notmuch_filenames_valid(self._files):
-            yield Filenames._get(self._files)
-            nmlib.notmuch_filenames_move_to_next(self._files)
+        while self._valid(self._files):
+            yield Filenames._get(self._files).decode('utf-8', 'ignore')
+            self._move_to_next(self._files)
 
         self._files = None
 
 
         self._files = None
 
-    def __str__(self):
+    def __unicode__(self):
         """Represent Filenames() as newline-separated list of full paths
 
         .. note:: As this iterates over the filenames, we will not be
         """Represent Filenames() as newline-separated list of full paths
 
         .. note:: As this iterates over the filenames, we will not be
@@ -101,7 +111,11 @@ class Filenames(object):
         """
         return "\n".join(self)
 
         """
         return "\n".join(self)
 
+    _destroy = nmlib.notmuch_filenames_destroy
+    _destroy.argtypes = [NotmuchMessageP]
+    _destroy.restype = None
+
     def __del__(self):
         """Close and free the notmuch filenames"""
         if self._files is not None:
     def __del__(self):
         """Close and free the notmuch filenames"""
         if self._files is not None:
-            nmlib.notmuch_filenames_destroy(self._files)
+            self._destroy(self._files)