mirror of
https://git.notmuchmail.org/git/notmuch
synced 2024-11-25 12:28:09 +01:00
python: pep8 compliance for database.py
This commit is contained in:
parent
61f0184707
commit
e69e30edd7
1 changed files with 80 additions and 80 deletions
|
@ -24,6 +24,7 @@ from notmuch.thread import Threads
|
||||||
from notmuch.message import Messages, Message
|
from notmuch.message import Messages, Message
|
||||||
from notmuch.tag import Tags
|
from notmuch.tag import Tags
|
||||||
|
|
||||||
|
|
||||||
class Database(object):
|
class Database(object):
|
||||||
"""Represents a notmuch database (wraps notmuch_database_t)
|
"""Represents a notmuch database (wraps notmuch_database_t)
|
||||||
|
|
||||||
|
@ -36,7 +37,7 @@ class Database(object):
|
||||||
_std_db_path = None
|
_std_db_path = None
|
||||||
"""Class attribute to cache user's default database"""
|
"""Class attribute to cache user's default database"""
|
||||||
|
|
||||||
MODE = Enum(['READ_ONLY','READ_WRITE'])
|
MODE = Enum(['READ_ONLY', 'READ_WRITE'])
|
||||||
"""Constants: Mode in which to open the database"""
|
"""Constants: Mode in which to open the database"""
|
||||||
|
|
||||||
"""notmuch_database_get_directory"""
|
"""notmuch_database_get_directory"""
|
||||||
|
@ -71,7 +72,7 @@ class Database(object):
|
||||||
_create = nmlib.notmuch_database_create
|
_create = nmlib.notmuch_database_create
|
||||||
_create.restype = c_void_p
|
_create.restype = c_void_p
|
||||||
|
|
||||||
def __init__(self, path=None, create=False, mode= 0):
|
def __init__(self, path=None, create=False, mode=0):
|
||||||
"""If *path* is `None`, we will try to read a users notmuch
|
"""If *path* is `None`, we will try to read a users notmuch
|
||||||
configuration and use his configured database. The location of the
|
configuration and use his configured database. The location of the
|
||||||
configuration file can be specified through the environment variable
|
configuration file can be specified through the environment variable
|
||||||
|
@ -100,7 +101,7 @@ class Database(object):
|
||||||
Database._std_db_path = self._get_user_default_db()
|
Database._std_db_path = self._get_user_default_db()
|
||||||
path = Database._std_db_path
|
path = Database._std_db_path
|
||||||
|
|
||||||
assert isinstance(path, basestring), 'Path needs to be a string or None.'
|
assert isinstance(path, basestring), 'Path must be a string or None.'
|
||||||
if create == False:
|
if create == False:
|
||||||
self.open(path, mode)
|
self.open(path, mode)
|
||||||
else:
|
else:
|
||||||
|
@ -128,8 +129,8 @@ class Database(object):
|
||||||
(after printing an error message on stderr).
|
(after printing an error message on stderr).
|
||||||
"""
|
"""
|
||||||
if self._db is not None:
|
if self._db is not None:
|
||||||
raise NotmuchError(
|
raise NotmuchError(message="Cannot create db, this Database() "
|
||||||
message="Cannot create db, this Database() already has an open one.")
|
"already has an open one.")
|
||||||
|
|
||||||
res = Database._create(path, Database.MODE.READ_WRITE)
|
res = Database._create(path, Database.MODE.READ_WRITE)
|
||||||
|
|
||||||
|
@ -138,7 +139,7 @@ class Database(object):
|
||||||
message="Could not create the specified database")
|
message="Could not create the specified database")
|
||||||
self._db = res
|
self._db = res
|
||||||
|
|
||||||
def open(self, path, mode= 0):
|
def open(self, path, mode=0):
|
||||||
"""Opens an existing database
|
"""Opens an existing database
|
||||||
|
|
||||||
This function is used by __init__() and usually does not need
|
This function is used by __init__() and usually does not need
|
||||||
|
@ -178,7 +179,7 @@ class Database(object):
|
||||||
# Raise a NotmuchError if not initialized
|
# Raise a NotmuchError if not initialized
|
||||||
self._verify_initialized_db()
|
self._verify_initialized_db()
|
||||||
|
|
||||||
return Database._get_version (self._db)
|
return Database._get_version(self._db)
|
||||||
|
|
||||||
def needs_upgrade(self):
|
def needs_upgrade(self):
|
||||||
"""Does this database need to be upgraded before writing to it?
|
"""Does this database need to be upgraded before writing to it?
|
||||||
|
@ -215,7 +216,7 @@ class Database(object):
|
||||||
# Raise a NotmuchError if not initialized
|
# Raise a NotmuchError if not initialized
|
||||||
self._verify_initialized_db()
|
self._verify_initialized_db()
|
||||||
|
|
||||||
status = Database._upgrade (self._db, None, None)
|
status = Database._upgrade(self._db, None, None)
|
||||||
#TODO: catch exceptions, document return values and etc
|
#TODO: catch exceptions, document return values and etc
|
||||||
return status
|
return status
|
||||||
|
|
||||||
|
@ -250,13 +251,14 @@ class Database(object):
|
||||||
if not path.startswith(self.get_path()):
|
if not path.startswith(self.get_path()):
|
||||||
# but its initial components are not equal to the db path
|
# but its initial components are not equal to the db path
|
||||||
raise NotmuchError(STATUS.FILE_ERROR,
|
raise NotmuchError(STATUS.FILE_ERROR,
|
||||||
message="Database().get_directory() called with a wrong absolute path.")
|
message="Database().get_directory() called "
|
||||||
|
"with a wrong absolute path.")
|
||||||
abs_dirpath = path
|
abs_dirpath = path
|
||||||
else:
|
else:
|
||||||
#we got a relative path, make it absolute
|
#we got a relative path, make it absolute
|
||||||
abs_dirpath = os.path.abspath(os.path.join(self.get_path(),path))
|
abs_dirpath = os.path.abspath(os.path.join(self.get_path(), path))
|
||||||
|
|
||||||
dir_p = Database._get_directory(self._db, path);
|
dir_p = Database._get_directory(self._db, path)
|
||||||
|
|
||||||
# return the Directory, init it with the absolute path
|
# return the Directory, init it with the absolute path
|
||||||
return Directory(abs_dirpath, dir_p, self)
|
return Directory(abs_dirpath, dir_p, self)
|
||||||
|
@ -303,7 +305,8 @@ class Database(object):
|
||||||
An error occurred trying to open the file, (such as
|
An error occurred trying to open the file, (such as
|
||||||
permission denied, or file not found, etc.).
|
permission denied, or file not found, etc.).
|
||||||
STATUS.FILE_NOT_EMAIL
|
STATUS.FILE_NOT_EMAIL
|
||||||
The contents of filename don't look like an email message.
|
The contents of filename don't look like an email
|
||||||
|
message.
|
||||||
STATUS.READ_ONLY_DATABASE
|
STATUS.READ_ONLY_DATABASE
|
||||||
Database was opened in read-only mode so no message can
|
Database was opened in read-only mode so no message can
|
||||||
be added.
|
be added.
|
||||||
|
@ -348,7 +351,8 @@ class Database(object):
|
||||||
database with at least one other filename.
|
database with at least one other filename.
|
||||||
|
|
||||||
:exception: Raises a :exc:`NotmuchError` with the following meaning.
|
:exception: Raises a :exc:`NotmuchError` with the following meaning.
|
||||||
If such an exception occurs, nothing was removed from the database.
|
If such an exception occurs, nothing was removed from the
|
||||||
|
database.
|
||||||
|
|
||||||
STATUS.READ_ONLY_DATABASE
|
STATUS.READ_ONLY_DATABASE
|
||||||
Database was opened in read-only mode so no message can be
|
Database was opened in read-only mode so no message can be
|
||||||
|
@ -397,7 +401,7 @@ class Database(object):
|
||||||
# Raise a NotmuchError if not initialized
|
# Raise a NotmuchError if not initialized
|
||||||
self._verify_initialized_db()
|
self._verify_initialized_db()
|
||||||
|
|
||||||
tags_p = Database._get_all_tags (self._db)
|
tags_p = Database._get_all_tags(self._db)
|
||||||
if tags_p == None:
|
if tags_p == None:
|
||||||
raise NotmuchError(STATUS.NULL_POINTER)
|
raise NotmuchError(STATUS.NULL_POINTER)
|
||||||
return Tags(tags_p, self)
|
return Tags(tags_p, self)
|
||||||
|
@ -442,10 +446,10 @@ class Database(object):
|
||||||
conf_f = os.getenv('NOTMUCH_CONFIG',
|
conf_f = os.getenv('NOTMUCH_CONFIG',
|
||||||
os.path.expanduser('~/.notmuch-config'))
|
os.path.expanduser('~/.notmuch-config'))
|
||||||
config.read(conf_f)
|
config.read(conf_f)
|
||||||
if not config.has_option('database','path'):
|
if not config.has_option('database', 'path'):
|
||||||
raise NotmuchError(message=
|
raise NotmuchError(message="No DB path specified"
|
||||||
"No DB path specified and no user default found")
|
" and no user default found")
|
||||||
return config.get('database','path')
|
return config.get('database', 'path')
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def db_p(self):
|
def db_p(self):
|
||||||
|
@ -456,7 +460,7 @@ class Database(object):
|
||||||
"""
|
"""
|
||||||
return self._db
|
return self._db
|
||||||
|
|
||||||
#------------------------------------------------------------------------------
|
|
||||||
class Query(object):
|
class Query(object):
|
||||||
"""Represents a search query on an opened :class:`Database`.
|
"""Represents a search query on an opened :class:`Database`.
|
||||||
|
|
||||||
|
@ -476,7 +480,7 @@ class Query(object):
|
||||||
other unexpected behavior. See above for more details.
|
other unexpected behavior. See above for more details.
|
||||||
"""
|
"""
|
||||||
# constants
|
# constants
|
||||||
SORT = Enum(['OLDEST_FIRST','NEWEST_FIRST','MESSAGE_ID', 'UNSORTED'])
|
SORT = Enum(['OLDEST_FIRST', 'NEWEST_FIRST', 'MESSAGE_ID', 'UNSORTED'])
|
||||||
"""Constants: Sort order in which to return results"""
|
"""Constants: Sort order in which to return results"""
|
||||||
|
|
||||||
"""notmuch_query_create"""
|
"""notmuch_query_create"""
|
||||||
|
@ -491,7 +495,6 @@ class Query(object):
|
||||||
_search_messages = nmlib.notmuch_query_search_messages
|
_search_messages = nmlib.notmuch_query_search_messages
|
||||||
_search_messages.restype = c_void_p
|
_search_messages.restype = c_void_p
|
||||||
|
|
||||||
|
|
||||||
"""notmuch_query_count_messages"""
|
"""notmuch_query_count_messages"""
|
||||||
_count_messages = nmlib.notmuch_query_count_messages
|
_count_messages = nmlib.notmuch_query_count_messages
|
||||||
_count_messages.restype = c_uint
|
_count_messages.restype = c_uint
|
||||||
|
@ -582,7 +585,7 @@ class Query(object):
|
||||||
if threads_p is None:
|
if threads_p is None:
|
||||||
raise NotmuchError(STATUS.NULL_POINTER)
|
raise NotmuchError(STATUS.NULL_POINTER)
|
||||||
|
|
||||||
return Threads(threads_p,self)
|
return Threads(threads_p, self)
|
||||||
|
|
||||||
def search_messages(self):
|
def search_messages(self):
|
||||||
"""Filter messages according to the query and return
|
"""Filter messages according to the query and return
|
||||||
|
@ -605,7 +608,7 @@ class Query(object):
|
||||||
if msgs_p is None:
|
if msgs_p is None:
|
||||||
NotmuchError(STATUS.NULL_POINTER)
|
NotmuchError(STATUS.NULL_POINTER)
|
||||||
|
|
||||||
return Messages(msgs_p,self)
|
return Messages(msgs_p, self)
|
||||||
|
|
||||||
def count_messages(self):
|
def count_messages(self):
|
||||||
"""Estimate the number of messages matching the query
|
"""Estimate the number of messages matching the query
|
||||||
|
@ -630,10 +633,9 @@ class Query(object):
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
"""Close and free the Query"""
|
"""Close and free the Query"""
|
||||||
if self._query is not None:
|
if self._query is not None:
|
||||||
nmlib.notmuch_query_destroy (self._query)
|
nmlib.notmuch_query_destroy(self._query)
|
||||||
|
|
||||||
|
|
||||||
#------------------------------------------------------------------------------
|
|
||||||
class Directory(object):
|
class Directory(object):
|
||||||
"""Represents a directory entry in the notmuch directory
|
"""Represents a directory entry in the notmuch directory
|
||||||
|
|
||||||
|
@ -665,7 +667,7 @@ class Directory(object):
|
||||||
_get_child_directories.restype = c_void_p
|
_get_child_directories.restype = c_void_p
|
||||||
|
|
||||||
def _verify_dir_initialized(self):
|
def _verify_dir_initialized(self):
|
||||||
"""Raises a NotmuchError(STATUS.NOT_INITIALIZED) if the dir_p is None"""
|
"""Raises a NotmuchError(STATUS.NOT_INITIALIZED) if dir_p is None"""
|
||||||
if self._dir_p is None:
|
if self._dir_p is None:
|
||||||
raise NotmuchError(STATUS.NOT_INITIALIZED)
|
raise NotmuchError(STATUS.NOT_INITIALIZED)
|
||||||
|
|
||||||
|
@ -683,8 +685,7 @@ class Directory(object):
|
||||||
self._dir_p = dir_p
|
self._dir_p = dir_p
|
||||||
self._parent = parent
|
self._parent = parent
|
||||||
|
|
||||||
|
def set_mtime(self, mtime):
|
||||||
def set_mtime (self, mtime):
|
|
||||||
"""Sets the mtime value of this directory in the database
|
"""Sets the mtime value of this directory in the database
|
||||||
|
|
||||||
The intention is for the caller to use the mtime to allow efficient
|
The intention is for the caller to use the mtime to allow efficient
|
||||||
|
@ -731,7 +732,7 @@ class Directory(object):
|
||||||
#fail with Exception otherwise
|
#fail with Exception otherwise
|
||||||
raise NotmuchError(status)
|
raise NotmuchError(status)
|
||||||
|
|
||||||
def get_mtime (self):
|
def get_mtime(self):
|
||||||
"""Gets the mtime value of this directory in the database
|
"""Gets the mtime value of this directory in the database
|
||||||
|
|
||||||
Retrieves a previously stored mtime for this directory.
|
Retrieves a previously stored mtime for this directory.
|
||||||
|
@ -746,7 +747,7 @@ class Directory(object):
|
||||||
#Raise a NotmuchError(STATUS.NOT_INITIALIZED) if self.dir_p is None
|
#Raise a NotmuchError(STATUS.NOT_INITIALIZED) if self.dir_p is None
|
||||||
self._verify_dir_initialized()
|
self._verify_dir_initialized()
|
||||||
|
|
||||||
return Directory._get_mtime (self._dir_p)
|
return Directory._get_mtime(self._dir_p)
|
||||||
|
|
||||||
# Make mtime attribute a property of Directory()
|
# Make mtime attribute a property of Directory()
|
||||||
mtime = property(get_mtime, set_mtime, doc="""Property that allows getting
|
mtime = property(get_mtime, set_mtime, doc="""Property that allows getting
|
||||||
|
@ -795,10 +796,9 @@ class Directory(object):
|
||||||
if self._dir_p is not None:
|
if self._dir_p is not None:
|
||||||
nmlib.notmuch_directory_destroy(self._dir_p)
|
nmlib.notmuch_directory_destroy(self._dir_p)
|
||||||
|
|
||||||
#------------------------------------------------------------------------------
|
|
||||||
class Filenames(object):
|
class Filenames(object):
|
||||||
"""An iterator over File- or Directory names that are stored in the database
|
"""An iterator over File- or Directory names stored in the database"""
|
||||||
"""
|
|
||||||
|
|
||||||
#notmuch_filenames_get
|
#notmuch_filenames_get
|
||||||
_get = nmlib.notmuch_filenames_get
|
_get = nmlib.notmuch_filenames_get
|
||||||
|
@ -828,7 +828,7 @@ class Filenames(object):
|
||||||
self._files_p = None
|
self._files_p = None
|
||||||
raise StopIteration
|
raise StopIteration
|
||||||
|
|
||||||
file = Filenames._get (self._files_p)
|
file = Filenames._get(self._files_p)
|
||||||
nmlib.notmuch_filenames_move_to_next(self._files_p)
|
nmlib.notmuch_filenames_move_to_next(self._files_p)
|
||||||
return file
|
return file
|
||||||
|
|
||||||
|
@ -847,7 +847,7 @@ class Filenames(object):
|
||||||
if self._files_p is None:
|
if self._files_p is None:
|
||||||
raise NotmuchError(STATUS.NOT_INITIALIZED)
|
raise NotmuchError(STATUS.NOT_INITIALIZED)
|
||||||
|
|
||||||
i=0
|
i = 0
|
||||||
while nmlib.notmuch_filenames_valid(self._files_p):
|
while nmlib.notmuch_filenames_valid(self._files_p):
|
||||||
nmlib.notmuch_filenames_move_to_next(self._files_p)
|
nmlib.notmuch_filenames_move_to_next(self._files_p)
|
||||||
i += 1
|
i += 1
|
||||||
|
|
Loading…
Reference in a new issue