X-Git-Url: https://git.notmuchmail.org/git?a=blobdiff_plain;f=bindings%2Fpython%2Fnotmuch%2Ffilename.py;h=a7cd7e63d44bf03076b5e21087e77ae425761a7a;hb=50b0d0b8ade8aca7dd8434f638555c634b00ebe4;hp=20b90e98b86f864c39a72c549330e05d26d34417;hpb=b31247c354b54a3cbeb1c7f9df830e16f7c921d9;p=notmuch diff --git a/bindings/python/notmuch/filename.py b/bindings/python/notmuch/filename.py index 20b90e98..a7cd7e63 100644 --- a/bindings/python/notmuch/filename.py +++ b/bindings/python/notmuch/filename.py @@ -17,21 +17,23 @@ along with notmuch. If not, see . Copyright 2010 Sebastian Spaeth ' """ from ctypes import c_char_p -from notmuch.globals import nmlib, STATUS, NotmuchError +from notmuch.globals import (nmlib, STATUS, NotmuchError, + NotmuchFilenamesP, NotmuchMessageP) + -#------------------------------------------------------------------------------ class Filenames(object): """Represents a list of filenames as returned by notmuch - This object contains the Filenames iterator. The main function is as_generator() which will return a generator so we can do a Filenamesth an iterator over a list of notmuch filenames. Do - note that the underlying library only provides a one-time iterator - (it cannot reset the iterator to the start). Thus iterating over - the function will "exhaust" the list of tags, and a subsequent - iteration attempt will raise a :exc:`NotmuchError` - STATUS.NOT_INITIALIZED. Also note, that any function that uses - iteration (nearly all) will also exhaust the tags. So both:: + This object contains the Filenames iterator. The main function is + as_generator() which will return a generator so we can do a Filenamesth an + iterator over a list of notmuch filenames. Do note that the underlying + library only provides a one-time iterator (it cannot reset the iterator to + the start). Thus iterating over the function will "exhaust" the list of + tags, and a subsequent iteration attempt will raise a :exc:`NotmuchError` + STATUS.NOT_INITIALIZED. Also note, that any function that uses iteration + (nearly all) will also exhaust the tags. So both:: - for name in filenames: print name + for name in filenames: print name as well as:: @@ -49,6 +51,7 @@ class Filenames(object): #notmuch_filenames_get _get = nmlib.notmuch_filenames_get + _get.argtypes = [NotmuchFilenamesP] _get.restype = c_char_p def __init__(self, files_p, parent): @@ -67,12 +70,20 @@ class Filenames(object): once all derived objects are dead. """ if files_p is None: - NotmuchError(STATUS.NULL_POINTER) + 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 @@ -81,15 +92,16 @@ class Filenames(object): if self._files is None: raise NotmuchError(STATUS.NOT_INITIALIZED) - if not nmlib.notmuch_filenames_valid(self._files): - self._files = None - return + while self._valid(self._files): + yield Filenames._get(self._files) + self._move_to_next(self._files) + + self._files = None - file = Filenames._get (self._files) - nmlib.notmuch_filenames_move_to_next(self._files) - yield file - def __str__(self): + return unicode(self).encode('utf-8') + + def __unicode__(self): """Represent Filenames() as newline-separated list of full paths .. note:: As this iterates over the filenames, we will not be @@ -102,7 +114,11 @@ class Filenames(object): """ 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: - nmlib.notmuch_filenames_destroy (self._files) + self._destroy(self._files)