Add message parameter to commit instead of logging separately.

The upside is that we can get rid of some redundancy and reduce
the friction of adding more commit messages.
The downside of this is that the log statement always reports from
commit, instead of the function calling commit. But with unique
messages this shouldn't be too much trouble and should be worth it.
This commit is contained in:
voussoir 2019-04-01 23:29:01 -07:00
parent 68a567d266
commit d24f93809b
2 changed files with 43 additions and 79 deletions

View file

@ -108,8 +108,7 @@ class GroupableMixin:
self.photodb._cached_frozen_children = None self.photodb._cached_frozen_children = None
if commit: if commit:
self.photodb.log.debug('Committing - add to group') self.photodb.commit(message='add to group')
self.photodb.commit()
@decorators.transaction @decorators.transaction
def add_children(self, members, *, commit=True): def add_children(self, members, *, commit=True):
@ -117,8 +116,7 @@ class GroupableMixin:
self.add_child(member, commit=False) self.add_child(member, commit=False)
if commit: if commit:
self.photodb.log.debug('Committing - add multiple to group') self.photodb.commit(message='add multiple to group')
self.photodb.commit()
def assert_same_type(self, other): def assert_same_type(self, other):
if not isinstance(other, type(self)): if not isinstance(other, type(self)):
@ -152,8 +150,7 @@ class GroupableMixin:
self.photodb.sql_delete(table=self.group_table, pairs={'memberid': self.id}) self.photodb.sql_delete(table=self.group_table, pairs={'memberid': self.id})
self._uncache() self._uncache()
if commit: if commit:
self.photodb.log.debug('Committing - delete tag') self.photodb.commit(message='delete tag')
self.photodb.commit()
def get_children(self): def get_children(self):
child_rows = self.photodb.sql_select( child_rows = self.photodb.sql_select(
@ -207,8 +204,7 @@ class GroupableMixin:
self.photodb._cached_frozen_children = None self.photodb._cached_frozen_children = None
if commit: if commit:
self.photodb.log.debug('Committing - remove from group') self.photodb.commit(message='remove from group')
self.photodb.commit()
def walk_children(self): def walk_children(self):
yield self yield self
@ -315,8 +311,7 @@ class Album(ObjectBase, GroupableMixin):
self.photodb.sql_insert(table='album_associated_directories', data=data) self.photodb.sql_insert(table='album_associated_directories', data=data)
if commit: if commit:
self.photodb.log.debug('Committing - add associated directory') self.photodb.commit(message='add associated directory')
self.photodb.commit()
def _add_photo(self, photo): def _add_photo(self, photo):
self.photodb.log.debug('Adding photo %s to %s', photo, self) self.photodb.log.debug('Adding photo %s to %s', photo, self)
@ -332,8 +327,7 @@ class Album(ObjectBase, GroupableMixin):
self._add_photo(photo) self._add_photo(photo)
if commit: if commit:
self.photodb.log.debug('Committing - add photo to album') self.photodb.commit(message='add photo to album')
self.photodb.commit()
@decorators.required_feature('album.edit') @decorators.required_feature('album.edit')
@decorators.transaction @decorators.transaction
@ -346,8 +340,7 @@ class Album(ObjectBase, GroupableMixin):
self._add_photo(photo) self._add_photo(photo)
if commit: if commit:
self.photodb.log.debug('Committing - add photos to album') self.photodb.commit(message='add photos to album')
self.photodb.commit()
# Photo.add_tag already has @required_feature # Photo.add_tag already has @required_feature
@decorators.transaction @decorators.transaction
@ -370,8 +363,7 @@ class Album(ObjectBase, GroupableMixin):
photo.add_tag(tag, commit=False) photo.add_tag(tag, commit=False)
if commit: if commit:
self.photodb.log.debug('Committing - add tag to all') self.photodb.commit(message='add tag to all')
self.photodb.commit()
@decorators.required_feature('album.edit') @decorators.required_feature('album.edit')
@decorators.transaction @decorators.transaction
@ -383,8 +375,7 @@ class Album(ObjectBase, GroupableMixin):
self.photodb.sql_delete(table='albums', pairs={'id': self.id}) self.photodb.sql_delete(table='albums', pairs={'id': self.id})
self._uncache() self._uncache()
if commit: if commit:
self.photodb.log.debug('Committing - delete album') self.photodb.commit(message='delete album')
self.photodb.commit()
@property @property
def display_name(self): def display_name(self):
@ -416,8 +407,7 @@ class Album(ObjectBase, GroupableMixin):
self.photodb.sql_update(table='albums', pairs=data, where_key='id') self.photodb.sql_update(table='albums', pairs=data, where_key='id')
if commit: if commit:
self.photodb.log.debug('Committing - edit album') self.photodb.commit(message='edit album')
self.photodb.commit()
def get_associated_directories(self): def get_associated_directories(self):
directory_rows = self.photodb.sql_select( directory_rows = self.photodb.sql_select(
@ -495,8 +485,7 @@ class Album(ObjectBase, GroupableMixin):
self._remove_photo(photo) self._remove_photo(photo)
if commit: if commit:
self.photodb.log.debug('Committing - remove photo from album') self.photodb.commit(message='remove photo from album')
self.photodb.commit()
@decorators.required_feature('album.edit') @decorators.required_feature('album.edit')
@decorators.transaction @decorators.transaction
@ -509,8 +498,7 @@ class Album(ObjectBase, GroupableMixin):
self._remove_photo(photo) self._remove_photo(photo)
if commit: if commit:
self.photodb.log.debug('Committing - remove photos from album') self.photodb.commit(message='remove photos from album')
self.photodb.commit()
def sum_bytes(self, recurse=True): def sum_bytes(self, recurse=True):
query = ''' query = '''
@ -637,8 +625,7 @@ class Bookmark(ObjectBase):
self.photodb.sql_update(table='bookmarks', pairs=data, where_key='id') self.photodb.sql_update(table='bookmarks', pairs=data, where_key='id')
if commit: if commit:
self.photodb.log.debug('Committing - edit bookmark') self.photodb.commit(message='edit bookmark')
self.photodb.commit()
class Photo(ObjectBase): class Photo(ObjectBase):
@ -737,8 +724,7 @@ class Photo(ObjectBase):
self.photodb.sql_update(table='photos', pairs=data, where_key='id') self.photodb.sql_update(table='photos', pairs=data, where_key='id')
if commit: if commit:
self.photodb.log.debug('Committing - add photo tag') self.photodb.commit(message='add photo tag')
self.photodb.commit()
return tag return tag
@property @property
@ -763,8 +749,7 @@ class Photo(ObjectBase):
for tag in other_photo.get_tags(): for tag in other_photo.get_tags():
self.add_tag(tag, commit=False) self.add_tag(tag, commit=False)
if commit: if commit:
self.photodb.log.debug('Committing - copy tags') self.photodb.commit(message='copy tags')
self.photodb.commit()
@decorators.required_feature('photo.edit') @decorators.required_feature('photo.edit')
@decorators.transaction @decorators.transaction
@ -786,8 +771,7 @@ class Photo(ObjectBase):
self.photodb.on_commit_queue.append(queue_action) self.photodb.on_commit_queue.append(queue_action)
self._uncache() self._uncache()
if commit: if commit:
self.photodb.log.debug('Committing - delete photo') self.photodb.commit(message='delete photo')
self.photodb.commit()
@property @property
def duration_string(self): def duration_string(self):
@ -848,8 +832,7 @@ class Photo(ObjectBase):
self._uncache() self._uncache()
if commit: if commit:
self.photodb.log.debug('Committing - generate thumbnail') self.photodb.commit(message='generate thumbnail')
self.photodb.commit()
self.__reinit__() self.__reinit__()
return self.thumbnail return self.thumbnail
@ -987,8 +970,7 @@ class Photo(ObjectBase):
self._uncache() self._uncache()
if commit: if commit:
self.photodb.log.debug('Committing - reload metadata') self.photodb.commit(message='reload metadata')
self.photodb.commit()
@decorators.required_feature('photo.edit') @decorators.required_feature('photo.edit')
@decorators.transaction @decorators.transaction
@ -1018,8 +1000,7 @@ class Photo(ObjectBase):
self._uncache() self._uncache()
if commit: if commit:
self.photodb.log.debug('Committing - relocate photo') self.photodb.commit(message='relocate photo')
self.photodb.commit()
@decorators.required_feature('photo.add_remove_tag') @decorators.required_feature('photo.add_remove_tag')
@decorators.transaction @decorators.transaction
@ -1040,8 +1021,7 @@ class Photo(ObjectBase):
self.photodb.sql_update(table='photos', pairs=data, where_key='id') self.photodb.sql_update(table='photos', pairs=data, where_key='id')
if commit: if commit:
self.photodb.log.debug('Committing - remove photo tag') self.photodb.commit(message='remove photo tag')
self.photodb.commit()
@decorators.required_feature('photo.edit') @decorators.required_feature('photo.edit')
@decorators.transaction @decorators.transaction
@ -1100,8 +1080,7 @@ class Photo(ObjectBase):
self._uncache() self._uncache()
if commit: if commit:
action(*args) action(*args)
self.photodb.log.debug('Committing - rename file') self.photodb.commit(message='rename file')
self.photodb.commit()
else: else:
queue_action = {'action': action, 'args': args} queue_action = {'action': action, 'args': args}
self.photodb.on_commit_queue.append(queue_action) self.photodb.on_commit_queue.append(queue_action)
@ -1120,8 +1099,7 @@ class Photo(ObjectBase):
self.searchhidden = searchhidden self.searchhidden = searchhidden
if commit: if commit:
self.photodb.log.debug('Committing - set searchhidden') self.photodb.commit(message='set searchhidden')
self.photodb.commit()
@decorators.required_feature('photo.edit') @decorators.required_feature('photo.edit')
@decorators.transaction @decorators.transaction
@ -1140,8 +1118,7 @@ class Photo(ObjectBase):
self.photodb.sql_update(table='photos', pairs=data, where_key='id') self.photodb.sql_update(table='photos', pairs=data, where_key='id')
if commit: if commit:
self.photodb.log.debug('Committing - set override filename') self.photodb.commit(message='set override filename')
self.photodb.commit()
self.__reinit__() self.__reinit__()
@ -1245,8 +1222,7 @@ class Tag(ObjectBase, GroupableMixin):
self._cached_synonyms.add(synname) self._cached_synonyms.add(synname)
if commit: if commit:
self.photodb.log.debug('Committing - add synonym') self.photodb.commit(message='add synonym')
self.photodb.commit()
return synname return synname
@ -1315,8 +1291,7 @@ class Tag(ObjectBase, GroupableMixin):
# Enjoy your new life as a monk. # Enjoy your new life as a monk.
mastertag.add_synonym(self.name, commit=False) mastertag.add_synonym(self.name, commit=False)
if commit: if commit:
self.photodb.log.debug('Committing - convert to synonym') self.photodb.commit(message='convert to synonym')
self.photodb.commit()
@decorators.required_feature('tag.edit') @decorators.required_feature('tag.edit')
@decorators.transaction @decorators.transaction
@ -1329,8 +1304,7 @@ class Tag(ObjectBase, GroupableMixin):
self.photodb.sql_delete(table='tags', pairs={'id': self.id}) self.photodb.sql_delete(table='tags', pairs={'id': self.id})
self._uncache() self._uncache()
if commit: if commit:
self.photodb.log.debug('Committing - delete tag') self.photodb.commit(message='delete tag')
self.photodb.commit()
@decorators.required_feature('tag.edit') @decorators.required_feature('tag.edit')
@decorators.transaction @decorators.transaction
@ -1351,8 +1325,7 @@ class Tag(ObjectBase, GroupableMixin):
self._uncache() self._uncache()
if commit: if commit:
self.photodb.log.debug('Committing - edit tag') self.photodb.commit(message='edit tag')
self.photodb.commit()
def get_synonyms(self): def get_synonyms(self):
if self._cached_synonyms is not None: if self._cached_synonyms is not None:
@ -1402,8 +1375,7 @@ class Tag(ObjectBase, GroupableMixin):
self._cached_synonyms.remove(synname) self._cached_synonyms.remove(synname)
if commit: if commit:
self.photodb.log.debug('Committing - remove synonym') self.photodb.commit(message='remove synonym')
self.photodb.commit()
@decorators.required_feature('tag.edit') @decorators.required_feature('tag.edit')
@decorators.transaction @decorators.transaction
@ -1440,8 +1412,7 @@ class Tag(ObjectBase, GroupableMixin):
self.name = new_name self.name = new_name
self._uncache() self._uncache()
if commit: if commit:
self.photodb.log.debug('Committing - rename tag') self.photodb.commit(message='rename tag')
self.photodb.commit()
class User(ObjectBase): class User(ObjectBase):
@ -1507,8 +1478,7 @@ class User(ObjectBase):
self._display_name = display_name self._display_name = display_name
if commit: if commit:
self.photodb.log.debug('Committing - set display name') self.photodb.commit(message='set display name')
self.photodb.commit()
class WarningBag: class WarningBag:

View file

@ -116,8 +116,7 @@ class PDBAlbumMixin:
album.add_photos(photos, commit=False) album.add_photos(photos, commit=False)
if commit: if commit:
self.log.debug('Committing - new Album') self.commit(message='new album')
self.commit()
return album return album
@ -153,8 +152,7 @@ class PDBBookmarkMixin:
bookmark = self.get_cached_instance('bookmark', data) bookmark = self.get_cached_instance('bookmark', data)
if commit: if commit:
self.log.debug('Committing - new Bookmark') self.commit(message='new bookmark')
self.commit()
return bookmark return bookmark
@ -274,8 +272,7 @@ class PDBPhotoMixin:
photo.add_tag(tag, commit=False) photo.add_tag(tag, commit=False)
if commit: if commit:
self.log.debug('Committing - new_photo') self.commit(message='new photo')
self.commit()
return photo return photo
@decorators.transaction @decorators.transaction
@ -296,8 +293,7 @@ class PDBPhotoMixin:
photo.delete(commit=False) photo.delete(commit=False)
if commit: if commit:
self.log.debug('Committing - purge deleted photos') self.commit(message='purge deleted photos')
self.commit()
@decorators.transaction @decorators.transaction
def purge_empty_albums(self, albums=None, *, commit=True): def purge_empty_albums(self, albums=None, *, commit=True):
@ -317,8 +313,7 @@ class PDBPhotoMixin:
album.delete(commit=False) album.delete(commit=False)
if commit: if commit:
self.log.debug('Committing - purge empty albums') self.commit(message='purge empty albums')
self.commit()
def search( def search(
self, self,
@ -683,7 +678,10 @@ class PDBSQLMixin:
self.on_commit_queue = [] self.on_commit_queue = []
self.savepoints = [] self.savepoints = []
def commit(self): def commit(self, message=None):
if message is not None:
self.log.debug('Committing - %s.', message)
while len(self.on_commit_queue) > 0: while len(self.on_commit_queue) > 0:
task = self.on_commit_queue.pop() task = self.on_commit_queue.pop()
if isinstance(task, str): if isinstance(task, str):
@ -886,8 +884,7 @@ class PDBTagMixin:
self.sql_insert(table='tags', data=data) self.sql_insert(table='tags', data=data)
if commit: if commit:
self.log.debug('Committing - new_tag') self.commit(message='new tag')
self.commit()
tag = self.get_cached_instance('tag', data) tag = self.get_cached_instance('tag', data)
return tag return tag
@ -1051,8 +1048,7 @@ class PDBUserMixin:
self.sql_insert(table='users', data=data) self.sql_insert(table='users', data=data)
if commit: if commit:
self.log.debug('Committing - register user') self.commit(message='register user')
self.commit()
return self.get_cached_instance('user', data) return self.get_cached_instance('user', data)
@ -1186,8 +1182,7 @@ class PDBUtilMixin:
current_album.add_photos(photos, commit=False) current_album.add_photos(photos, commit=False)
if commit: if commit:
self.log.debug('Committing - digest_directory') self.commit(message='digest directory')
self.commit()
if make_albums: if make_albums:
return main_album return main_album
@ -1239,8 +1234,7 @@ class PDBUtilMixin:
output_notes.append(note) output_notes.append(note)
if commit: if commit:
self.log.debug('Committing - easybake') self.commit(message='easybake')
self.commit()
return output_notes return output_notes