Basic setters API

This commit is contained in:
Kovid Goyal 2013-07-14 08:54:59 +05:30
parent 69c3f37f33
commit 9c64054826
2 changed files with 89 additions and 13 deletions

View File

@ -52,10 +52,12 @@ class LibraryDatabase(object):
self.get_property = self.data.get_property self.get_property = self.data.get_property
MT = lambda func: types.MethodType(func, self, LibraryDatabase)
for prop in ( for prop in (
'author_sort', 'authors', 'comment', 'comments', 'author_sort', 'authors', 'comment', 'comments', 'publisher',
'publisher', 'rating', 'series', 'series_index', 'tags', 'rating', 'series', 'series_index', 'tags', 'title', 'title_sort',
'title', 'timestamp', 'uuid', 'pubdate', 'ondevice', 'timestamp', 'uuid', 'pubdate', 'ondevice',
'metadata_last_modified', 'languages', 'metadata_last_modified', 'languages',
): ):
fm = {'comment':'comments', 'metadata_last_modified': fm = {'comment':'comments', 'metadata_last_modified':
@ -63,7 +65,9 @@ class LibraryDatabase(object):
setattr(self, prop, partial(self.get_property, setattr(self, prop, partial(self.get_property,
loc=self.FIELD_MAP[fm])) loc=self.FIELD_MAP[fm]))
MT = lambda func: types.MethodType(func, self, LibraryDatabase) self.has_cover = MT(lambda self, book_id:self.new_api.field_for('cover', book_id))
self.get_identifiers = MT(
lambda self, index, index_is_id=False: self.new_api.field_for('identifiers', index if index_is_id else self.data.index_to_id(index)))
for meth in ('get_next_series_num_for', 'has_book', 'author_sort_from_authors'): for meth in ('get_next_series_num_for', 'has_book', 'author_sort_from_authors'):
setattr(self, meth, getattr(self.new_api, meth)) setattr(self, meth, getattr(self.new_api, meth))
@ -115,6 +119,36 @@ class LibraryDatabase(object):
setattr(self, func, getattr(self.field_metadata, func)) setattr(self, func, getattr(self.field_metadata, func))
self.metadata_for_field = self.field_metadata.get self.metadata_for_field = self.field_metadata.get
# Legacy setter API
for field in (
'!authors', 'author_sort', 'comment', 'has_cover', 'identifiers', 'languages',
'pubdate', '!publisher', 'rating', '!series', 'series_index', 'timestamp', 'uuid',
):
def setter(field):
has_case_change = field.startswith('!')
field = {'comment':'comments',}.get(field, field)
if has_case_change:
field = field[1:]
acc = field == 'series'
def func(self, book_id, val, notify=True, commit=True, allow_case_change=acc):
ret = self.new_api.set_field(field, {book_id:val}, allow_case_change=allow_case_change)
if notify:
self.notify([book_id])
return ret
elif field == 'has_cover':
def func(self, book_id, val):
self.new_api.set_field('cover', {book_id:bool(val)})
else:
def func(self, book_id, val, notify=True, commit=True):
if not val and field == 'uuid':
return
ret = self.new_api.set_field(field, {book_id:val})
if notify:
self.notify([book_id])
return ret if field == 'languages' else None
return func
setattr(self, 'set_%s' % field.replace('!', ''), MT(setter(field)))
self.last_update_check = self.last_modified() self.last_update_check = self.last_modified()
self.book_on_device_func = None self.book_on_device_func = None
# Cleaning is not required anymore # Cleaning is not required anymore

View File

@ -47,11 +47,15 @@ def run_funcs(self, db, ndb, funcs):
meth(*args) meth(*args)
else: else:
fmt = lambda x:x fmt = lambda x:x
if meth[0] in {'!', '@', '#'}: if meth[0] in {'!', '@', '#', '+'}:
if meth[0] != '+':
fmt = {'!':dict, '@':frozenset, '#':lambda x:set((x or '').split(','))}[meth[0]] fmt = {'!':dict, '@':frozenset, '#':lambda x:set((x or '').split(','))}[meth[0]]
else:
fmt = args[-1]
args = args[:-1]
meth = meth[1:] meth = meth[1:]
self.assertEqual(fmt(getattr(db, meth)(*args)), fmt(getattr(ndb, meth)(*args)), res1, res2 = fmt(getattr(db, meth)(*args)), fmt(getattr(ndb, meth)(*args))
'The method: %s() returned different results for argument %s' % (meth, args)) self.assertEqual(res1, res2, 'The method: %s() returned different results for argument %s' % (meth, args))
class LegacyTest(BaseTest): class LegacyTest(BaseTest):
@ -129,7 +133,7 @@ class LegacyTest(BaseTest):
def test_legacy_getters(self): # {{{ def test_legacy_getters(self): # {{{
' Test various functions to get individual bits of metadata ' ' Test various functions to get individual bits of metadata '
old = self.init_old() old = self.init_old()
getters = ('path', 'abspath', 'title', 'authors', 'series', getters = ('path', 'abspath', 'title', 'title_sort', 'authors', 'series',
'publisher', 'author_sort', 'authors', 'comments', 'publisher', 'author_sort', 'authors', 'comments',
'comment', 'publisher', 'rating', 'series_index', 'tags', 'comment', 'publisher', 'rating', 'series_index', 'tags',
'timestamp', 'uuid', 'pubdate', 'ondevice', 'timestamp', 'uuid', 'pubdate', 'ondevice',
@ -327,6 +331,7 @@ class LegacyTest(BaseTest):
'construct_path_name', 'clear_dirtied', 'commit_dirty_cache', 'initialize_database', 'initialize_dynamic', 'construct_path_name', 'clear_dirtied', 'commit_dirty_cache', 'initialize_database', 'initialize_dynamic',
'run_import_plugins', 'vacuum', 'set_path', 'row', 'row_factory', 'rows', 'rmtree', 'series_index_pat', 'run_import_plugins', 'vacuum', 'set_path', 'row', 'row_factory', 'rows', 'rmtree', 'series_index_pat',
'import_old_database', 'dirtied_lock', 'dirtied_cache', 'dirty_queue_length', 'dirty_books_referencing', 'import_old_database', 'dirtied_lock', 'dirtied_cache', 'dirty_queue_length', 'dirty_books_referencing',
'windows_check_if_files_in_use',
} }
SKIP_ARGSPEC = { SKIP_ARGSPEC = {
'__init__', 'get_next_series_num_for', 'has_book', 'author_sort_from_authors', '__init__', 'get_next_series_num_for', 'has_book', 'author_sort_from_authors',
@ -400,12 +405,51 @@ class LegacyTest(BaseTest):
ndb = self.init_legacy(self.cloned_library) ndb = self.init_legacy(self.cloned_library)
db = self.init_old(self.cloned_library) db = self.init_old(self.cloned_library)
run_funcs(self, db, ndb, (
('set_authors', 1, ('author one',),), ('set_authors', 2, ('author two',), True, True, True),
('set_author_sort', 3, 'new_aus'),
('set_comment', 1, ''), ('set_comment', 2, None), ('set_comment', 3, '<p>a comment</p>'),
('set_has_cover', 1, True), ('set_has_cover', 2, True), ('set_has_cover', 3, 1),
('set_identifiers', 2, {'test':'', 'a':'b'}), ('set_identifiers', 3, {'id':'1', 'url':'http://acme.com'}), ('set_identifiers', 1, {}),
('set_languages', 1, ('en',)),
('set_languages', 2, ()),
('set_languages', 3, ('deu', 'spa', 'fra')),
('set_pubdate', 1, None), ('set_pubdate', 2, '2011-1-7'),
('set_series', 1, 'a series one'), ('set_series', 2, 'another series [7]'), ('set_series', 3, 'a third series'),
('set_publisher', 1, 'publisher two'), ('set_publisher', 2, None), ('set_publisher', 3, 'a third puB'),
('set_rating', 1, 2.3), ('set_rating', 2, 0), ('set_rating', 3, 8),
('set_timestamp', 1, None), ('set_timestamp', 2, '2011-1-7'),
('set_uuid', 1, None), ('set_uuid', 2, 'a test uuid'),
(db.refresh,),
('authors', 0), ('authors', 1), ('authors', 2),
('author_sort', 0), ('author_sort', 1), ('author_sort', 2),
('has_cover', 3), ('has_cover', 1), ('has_cover', 2),
('get_identifiers', 0), ('get_identifiers', 1), ('get_identifiers', 2),
('pubdate', 0), ('pubdate', 1), ('pubdate', 2),
('timestamp', 0), ('timestamp', 1), ('timestamp', 2),
('publisher', 0), ('publisher', 1), ('publisher', 2),
('rating', 0), ('+rating', 1, lambda x: x or 0), ('rating', 2),
('series', 0), ('series', 1), ('series', 2),
('series_index', 0), ('series_index', 1), ('series_index', 2),
('uuid', 0), ('uuid', 1), ('uuid', 2),
('set_series_index', 1, 2.3), ('set_series_index', 2, 0), ('set_series_index', 3, 8),
(db.refresh,),
('series_index', 0), ('series_index', 1), ('series_index', 2),
))
db.close()
ndb = self.init_legacy(self.cloned_library)
db = self.init_old(self.cloned_library)
run_funcs(self, db, ndb, ( run_funcs(self, db, ndb, (
('set', 0, 'title', 'newtitle'), ('set', 0, 'title', 'newtitle'),
('set', 0, 'tags', 't1,t2,tag one', True), ('set', 0, 'tags', 't1,t2,tag one', True),
('set', 0, 'authors', 'author one & Author Two', True), ('set', 0, 'authors', 'author one & Author Two', True),
('set', 0, 'rating', 3.2), ('set', 0, 'rating', 3.2),
('set', 0, 'publisher', 'publisher one', True), ('set', 0, 'publisher', 'publisher one', False),
(db.refresh,), (db.refresh,),
('title', 0), ('title', 0),
('rating', 0), ('rating', 0),
@ -413,6 +457,4 @@ class LegacyTest(BaseTest):
('authors', 0), ('authors', 1), ('authors', 2), ('authors', 0), ('authors', 1), ('authors', 2),
('publisher', 0), ('publisher', 1), ('publisher', 2), ('publisher', 0), ('publisher', 1), ('publisher', 2),
)) ))
db.close()
# }}} # }}}