Refractor some decorators

This commit is contained in:
Odd Stråbø 2020-07-31 23:53:48 +02:00
parent 803081f65a
commit 3ec71e89d6
7 changed files with 101 additions and 106 deletions

View file

@ -235,7 +235,7 @@ def get_songs_by_artist(artist_id, basic=False):
yield items yield items
@stop() @stop
def _get_items(query, server_id=None): def _get_items(query, server_id=None):
''' query = { ''' query = {

View file

@ -38,6 +38,7 @@ def progress(message=None):
if item: if item:
args = (item,) + args args = (item,) + args
LOG.debug({'self': self, 'dialog': dialog, 'args': args, 'kwargs': kwargs})
result = func(self, dialog=dialog, *args, **kwargs) result = func(self, dialog=dialog, *args, **kwargs)
dialog.close() dialog.close()
@ -81,99 +82,93 @@ def silent_catch(errors=(Exception,)):
return decorator return decorator
def stop(default=None): def stop(func):
''' Wrapper to catch exceptions and return using catch ''' Wrapper to catch exceptions and return using catch
''' '''
def decorator(func): def wrapper(*args, **kwargs):
def wrapper(*args, **kwargs):
try: try:
if should_stop(): # ??? TODO: Fixme if should_stop(): # ??? TODO: Fixme
raise Exception raise Exception
except Exception as error: except Exception as error:
LOG.exception(error) LOG.exception(error)
if default is not None: raise LibraryException("StopCalled")
return default
raise LibraryException("StopCalled") LOG.debug({'args': args, 'kwargs': kwargs})
return func(*args, **kwargs)
return func(*args, **kwargs) return wrapper
return wrapper
return decorator
def jellyfin_item(): def jellyfin_item(func):
''' Wrapper to retrieve the jellyfin_db item. ''' Wrapper to retrieve the jellyfin_db item.
''' '''
def decorator(func): def wrapper(self, item, *args, **kwargs):
def wrapper(self, item, *args, **kwargs): e_item = self.jellyfin_db.get_item_by_id(item['Id'] if type(item) == dict else item)
e_item = self.jellyfin_db.get_item_by_id(item['Id'] if type(item) == dict else item)
return func(self, item, e_item=e_item, *args, **kwargs) LOG.debug({'self': self, 'item': item, 'e_item': e_item, 'args': args, 'kwargs': kwargs})
return func(self, item, e_item=e_item, *args, **kwargs)
return wrapper return wrapper
return decorator
def library_check(): def library_check(func):
''' Wrapper to retrieve the library ''' Wrapper to retrieve the library
''' '''
def decorator(func): def wrapper(self, item, *args, **kwargs):
def wrapper(self, item, *args, **kwargs):
''' TODO: Rethink this one... songs and albums cannot be found by library. expensive. ''' TODO: Rethink this one... songs and albums cannot be found by library. expensive.
''' '''
from database import get_sync from database import get_sync
if kwargs.get('library') is None: if kwargs.get('library') is None:
sync = get_sync() sync = get_sync()
if 'e_item' in kwargs: if 'e_item' in kwargs:
try: try:
view_id = kwargs['e_item'][6] view_id = kwargs['e_item'][6]
view_name = self.jellyfin_db.get_view_name(view_id) view_name = self.jellyfin_db.get_view_name(view_id)
view = {'Name': view_name, 'Id': view_id} view = {'Name': view_name, 'Id': view_id}
except Exception: except Exception:
view = None view = None
if view is None: if view is None:
ancestors = self.server.jellyfin.get_ancestors(item['Id']) ancestors = self.server.jellyfin.get_ancestors(item['Id'])
if not ancestors: if not ancestors:
if item['Type'] == 'MusicArtist': if item['Type'] == 'MusicArtist':
try: try:
views = self.jellyfin_db.get_views_by_media('music')[0] views = self.jellyfin_db.get_views_by_media('music')[0]
except Exception as error: except Exception as error:
LOG.exception(error) LOG.exception(error)
return
view = {'Id': views[0], 'Name': views[1]}
else: # Grab the first music library
return return
else:
for ancestor in ancestors:
if ancestor['Type'] == 'CollectionFolder':
view = self.jellyfin_db.get_view_name(ancestor['Id'])
view = {'Id': None, 'Name': None} if view is None else {'Name': ancestor['Name'], 'Id': ancestor['Id']}
break
if view['Id'] not in [x.replace('Mixed:', "") for x in sync['Whitelist'] + sync['Libraries']]:
LOG.info("Library %s is not synced. Skip update.", view['Id'])
view = {'Id': views[0], 'Name': views[1]}
else: # Grab the first music library
return return
else:
for ancestor in ancestors:
if ancestor['Type'] == 'CollectionFolder':
kwargs['library'] = view view = self.jellyfin_db.get_view_name(ancestor['Id'])
view = {'Id': None, 'Name': None} if view is None else {'Name': ancestor['Name'], 'Id': ancestor['Id']}
return func(self, item, *args, **kwargs) break
return wrapper if view['Id'] not in [x.replace('Mixed:', "") for x in sync['Whitelist'] + sync['Libraries']]:
return decorator LOG.info("Library %s is not synced. Skip update.", view['Id'])
return
kwargs['library'] = view
LOG.debug({'self': self, 'item': item, 'args': args, 'kwargs': kwargs})
return func(self, item, *args, **kwargs)
return wrapper

View file

@ -110,7 +110,7 @@ class Library(threading.Thread):
with Database('video'), Database('music'): with Database('video'), Database('music'):
pass pass
@stop() @stop
def service(self): def service(self):
''' If error is encountered, it will rerun this function. ''' If error is encountered, it will rerun this function.

View file

@ -37,9 +37,9 @@ class Movies(KodiDb):
KodiDb.__init__(self, videodb.cursor) KodiDb.__init__(self, videodb.cursor)
@stop() @stop
@jellyfin_item() @jellyfin_item
@library_check() @library_check
def movie(self, item, e_item, library): def movie(self, item, e_item, library):
''' If item does not exist, entry will be added. ''' If item does not exist, entry will be added.
@ -200,8 +200,8 @@ class Movies(KodiDb):
} }
obj['Filename'] = "%s?%s" % (obj['Path'], urlencode(params)) obj['Filename'] = "%s?%s" % (obj['Path'], urlencode(params))
@stop() @stop
@jellyfin_item() @jellyfin_item
def boxset(self, item, e_item): def boxset(self, item, e_item):
''' If item does not exist, entry will be added. ''' If item does not exist, entry will be added.
@ -281,8 +281,8 @@ class Movies(KodiDb):
for boxset in boxsets: for boxset in boxsets:
self.remove(boxset[0]) self.remove(boxset[0])
@stop() @stop
@jellyfin_item() @jellyfin_item
def userdata(self, item, e_item): def userdata(self, item, e_item):
''' This updates: Favorite, LastPlayedDate, Playcount, PlaybackPositionTicks ''' This updates: Favorite, LastPlayedDate, Playcount, PlaybackPositionTicks
@ -315,8 +315,8 @@ class Movies(KodiDb):
self.jellyfin_db.update_reference(*values(obj, QUEM.update_reference_obj)) self.jellyfin_db.update_reference(*values(obj, QUEM.update_reference_obj))
LOG.debug("USERDATA movie [%s/%s] %s: %s", obj['FileId'], obj['MovieId'], obj['Id'], obj['Title']) LOG.debug("USERDATA movie [%s/%s] %s: %s", obj['FileId'], obj['MovieId'], obj['Id'], obj['Title'])
@stop() @stop
@jellyfin_item() @jellyfin_item
def remove(self, item_id, e_item): def remove(self, item_id, e_item):
''' Remove movieid, fileid, jellyfin reference. ''' Remove movieid, fileid, jellyfin reference.

View file

@ -35,9 +35,9 @@ class Music(KodiDb):
KodiDb.__init__(self, musicdb.cursor) KodiDb.__init__(self, musicdb.cursor)
@stop() @stop
@jellyfin_item() @jellyfin_item
@library_check() @library_check
def artist(self, item, e_item, library): def artist(self, item, e_item, library):
''' If item does not exist, entry will be added. ''' If item does not exist, entry will be added.
@ -103,8 +103,8 @@ class Music(KodiDb):
self.jellyfin_db.update_reference(*values(obj, QUEM.update_reference_obj)) self.jellyfin_db.update_reference(*values(obj, QUEM.update_reference_obj))
LOG.debug("UPDATE artist [%s] %s: %s", obj['ArtistId'], obj['Name'], obj['Id']) LOG.debug("UPDATE artist [%s] %s: %s", obj['ArtistId'], obj['Name'], obj['Id'])
@stop() @stop
@jellyfin_item() @jellyfin_item
def album(self, item, e_item): def album(self, item, e_item):
''' Update object to kodi. ''' Update object to kodi.
@ -209,9 +209,9 @@ class Music(KodiDb):
self.link(*values(temp_obj, QU.update_link_obj)) self.link(*values(temp_obj, QU.update_link_obj))
self.item_ids.append(temp_obj['Id']) self.item_ids.append(temp_obj['Id'])
@stop() @stop
@jellyfin_item() @jellyfin_item
@library_check() @library_check
def song(self, item, e_item, library): def song(self, item, e_item, library):
''' Update object to kodi. ''' Update object to kodi.
@ -402,8 +402,8 @@ class Music(KodiDb):
obj['AlbumId'] = self.create_entry_album() obj['AlbumId'] = self.create_entry_album()
self.add_single(*values(obj, QU.add_single_obj)) self.add_single(*values(obj, QU.add_single_obj))
@stop() @stop
@jellyfin_item() @jellyfin_item
def userdata(self, item, e_item): def userdata(self, item, e_item):
''' This updates: Favorite, LastPlayedDate, Playcount, PlaybackPositionTicks ''' This updates: Favorite, LastPlayedDate, Playcount, PlaybackPositionTicks
@ -431,8 +431,8 @@ class Music(KodiDb):
self.jellyfin_db.update_reference(*values(obj, QUEM.update_reference_obj)) self.jellyfin_db.update_reference(*values(obj, QUEM.update_reference_obj))
LOG.debug("USERDATA %s [%s] %s: %s", obj['Media'], obj['KodiId'], obj['Id'], obj['Title']) LOG.debug("USERDATA %s [%s] %s: %s", obj['Media'], obj['KodiId'], obj['Id'], obj['Title'])
@stop() @stop
@jellyfin_item() @jellyfin_item
def remove(self, item_id, e_item): def remove(self, item_id, e_item):
''' This updates: Favorite, LastPlayedDate, Playcount, PlaybackPositionTicks ''' This updates: Favorite, LastPlayedDate, Playcount, PlaybackPositionTicks
@ -512,7 +512,7 @@ class Music(KodiDb):
self.delete_song(kodi_id) self.delete_song(kodi_id)
LOG.debug("DELETE song [%s] %s", kodi_id, item_id) LOG.debug("DELETE song [%s] %s", kodi_id, item_id)
@jellyfin_item() @jellyfin_item
def get_child(self, item_id, e_item): def get_child(self, item_id, e_item):
''' Get all child elements from tv show jellyfin id. ''' Get all child elements from tv show jellyfin id.

View file

@ -39,9 +39,9 @@ class MusicVideos(KodiDb):
KodiDb.__init__(self, videodb.cursor) KodiDb.__init__(self, videodb.cursor)
@stop() @stop
@jellyfin_item() @jellyfin_item
@library_check() @library_check
def musicvideo(self, item, e_item, library): def musicvideo(self, item, e_item, library):
''' If item does not exist, entry will be added. ''' If item does not exist, entry will be added.
@ -177,8 +177,8 @@ class MusicVideos(KodiDb):
} }
obj['Filename'] = "%s?%s" % (obj['Path'], urlencode(params)) obj['Filename'] = "%s?%s" % (obj['Path'], urlencode(params))
@stop() @stop
@jellyfin_item() @jellyfin_item
def userdata(self, item, e_item): def userdata(self, item, e_item):
''' This updates: Favorite, LastPlayedDate, Playcount, PlaybackPositionTicks ''' This updates: Favorite, LastPlayedDate, Playcount, PlaybackPositionTicks
@ -210,8 +210,8 @@ class MusicVideos(KodiDb):
self.jellyfin_db.update_reference(*values(obj, QUEM.update_reference_obj)) self.jellyfin_db.update_reference(*values(obj, QUEM.update_reference_obj))
LOG.debug("USERDATA mvideo [%s/%s] %s: %s", obj['FileId'], obj['MvideoId'], obj['Id'], obj['Title']) LOG.debug("USERDATA mvideo [%s/%s] %s: %s", obj['FileId'], obj['MvideoId'], obj['Id'], obj['Title'])
@stop() @stop
@jellyfin_item() @jellyfin_item
def remove(self, item_id, e_item): def remove(self, item_id, e_item):
''' Remove mvideoid, fileid, pathid, jellyfin reference. ''' Remove mvideoid, fileid, pathid, jellyfin reference.

View file

@ -41,9 +41,9 @@ class TVShows(KodiDb):
KodiDb.__init__(self, videodb.cursor) KodiDb.__init__(self, videodb.cursor)
@stop() @stop
@jellyfin_item() @jellyfin_item
@library_check() @library_check
def tvshow(self, item, e_item, library): def tvshow(self, item, e_item, library):
''' If item does not exist, entry will be added. ''' If item does not exist, entry will be added.
@ -201,7 +201,7 @@ class TVShows(KodiDb):
obj['TopLevel'] = "plugin://plugin.video.jellyfin/%s/" % obj['LibraryId'] obj['TopLevel'] = "plugin://plugin.video.jellyfin/%s/" % obj['LibraryId']
obj['Path'] = "%s%s/" % (obj['TopLevel'], obj['Id']) obj['Path'] = "%s%s/" % (obj['TopLevel'], obj['Id'])
@stop() @stop
def season(self, item, show_id=None): def season(self, item, show_id=None):
''' If item does not exist, entry will be added. ''' If item does not exist, entry will be added.
@ -235,8 +235,8 @@ class TVShows(KodiDb):
self.artwork.add(obj['Artwork'], obj['SeasonId'], "season") self.artwork.add(obj['Artwork'], obj['SeasonId'], "season")
LOG.debug("UPDATE season [%s/%s] %s: %s", obj['ShowId'], obj['SeasonId'], obj['Title'] or obj['Index'], obj['Id']) LOG.debug("UPDATE season [%s/%s] %s: %s", obj['ShowId'], obj['SeasonId'], obj['Title'] or obj['Index'], obj['Id'])
@stop() @stop
@jellyfin_item() @jellyfin_item
def episode(self, item, e_item): def episode(self, item, e_item):
''' If item does not exist, entry will be added. ''' If item does not exist, entry will be added.
@ -425,8 +425,8 @@ class TVShows(KodiDb):
return True return True
@stop() @stop
@jellyfin_item() @jellyfin_item
def userdata(self, item, e_item): def userdata(self, item, e_item):
''' This updates: Favorite, LastPlayedDate, Playcount, PlaybackPositionTicks ''' This updates: Favorite, LastPlayedDate, Playcount, PlaybackPositionTicks
@ -486,8 +486,8 @@ class TVShows(KodiDb):
self.jellyfin_db.update_reference(*values(obj, QUEM.update_reference_obj)) self.jellyfin_db.update_reference(*values(obj, QUEM.update_reference_obj))
LOG.debug("USERDATA %s [%s/%s] %s: %s", obj['Media'], obj['FileId'], obj['KodiId'], obj['Id'], obj['Title']) LOG.debug("USERDATA %s [%s/%s] %s: %s", obj['Media'], obj['FileId'], obj['KodiId'], obj['Id'], obj['Title'])
@stop() @stop
@jellyfin_item() @jellyfin_item
def remove(self, item_id, e_item): def remove(self, item_id, e_item):
''' Remove showid, fileid, pathid, jellyfin reference. ''' Remove showid, fileid, pathid, jellyfin reference.
@ -589,7 +589,7 @@ class TVShows(KodiDb):
self.delete_episode(kodi_id, file_id) self.delete_episode(kodi_id, file_id)
LOG.debug("DELETE episode [%s/%s] %s", file_id, kodi_id, item_id) LOG.debug("DELETE episode [%s/%s] %s", file_id, kodi_id, item_id)
@jellyfin_item() @jellyfin_item
def get_child(self, item_id, e_item): def get_child(self, item_id, e_item):
''' Get all child elements from tv show jellyfin id. ''' Get all child elements from tv show jellyfin id.