Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A tiny bit of code cleanup #345

Merged
merged 5 commits into from Aug 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion jellyfin_kodi/connect.py
Expand Up @@ -11,7 +11,7 @@
from helper import settings, addon_id, event, api, window
from jellyfin import Jellyfin
from jellyfin.connection_manager import CONNECTION_STATE
from jellyfin.exceptions import HTTPException
from helper.exceptions import HTTPException
from helper import LazyLogger

##################################################################################################
Expand Down
4 changes: 2 additions & 2 deletions jellyfin_kodi/downloader.py
Expand Up @@ -14,7 +14,7 @@
from helper import settings, stop, event, window, create_id
from jellyfin import Jellyfin
from jellyfin import api
from jellyfin.exceptions import HTTPException
from helper.exceptions import HTTPException
from helper import LazyLogger

#################################################################################################
Expand Down Expand Up @@ -235,7 +235,7 @@ def get_songs_by_artist(artist_id, basic=False):
yield items


@stop()
@stop
def _get_items(query, server_id=None):

''' query = {
Expand Down
15 changes: 9 additions & 6 deletions jellyfin_kodi/full_sync.py
Expand Up @@ -12,9 +12,10 @@
import helper.xmls as xmls
from objects import Movies, TVShows, MusicVideos, Music
from database import Database, get_sync, save_sync, jellyfin_db
from helper import translate, settings, window, progress, dialog, LibraryException
from helper import translate, settings, window, progress, dialog
from helper.utils import get_screensaver, set_screensaver
from helper import LazyLogger
from helper.exceptions import LibraryException, PathValidationException

##################################################################################################

Expand Down Expand Up @@ -239,14 +240,16 @@ def process_library(self, library_id):

raise

except PathValidationException:
raise

except Exception as error:
LOG.exception(error)
dialog("ok", "{jellyfin}", translate(33119))

if 'Failed to validate path' not in error:
LOG.error("full sync exited unexpectedly")
LOG.exception(error)

dialog("ok", "{jellyfin}", translate(33119))
LOG.error("full sync exited unexpectedly")
save_sync(self.sync)
save_sync(self.sync)

raise

Expand Down
3 changes: 0 additions & 3 deletions jellyfin_kodi/helper/__init__.py
Expand Up @@ -3,7 +3,6 @@
from .lazylogger import LazyLogger

from .translate import translate
from .exceptions import LibraryException

from .utils import addon_id
from .utils import window
Expand All @@ -26,8 +25,6 @@
from .utils import get_filesystem_encoding

from .wrapper import progress
from .wrapper import catch
from .wrapper import silent_catch
from .wrapper import stop
from .wrapper import jellyfin_item
from .wrapper import library_check
16 changes: 16 additions & 0 deletions jellyfin_kodi/helper/exceptions.py
Expand Up @@ -4,7 +4,23 @@
#################################################################################################


class HTTPException(Exception):
# Jellyfin HTTP exception
def __init__(self, status, message):
self.status = status
self.message = message


class LibraryException(Exception):
# Jellyfin library sync exception
def __init__(self, status):
self.status = status


class PathValidationException(Exception):
"""
Replacing generic `Exception`

TODO: Investigate the usage of this to see if it can be done better.
"""
pass
149 changes: 53 additions & 96 deletions jellyfin_kodi/helper/wrapper.py
Expand Up @@ -47,133 +47,90 @@ def wrapper(self, item=None, *args, **kwargs):
return decorator


def catch(errors=(Exception,)):
def stop(func):

''' Wrapper to catch exceptions and return using catch
'''
def decorator(func):
def wrapper(*args, **kwargs):

try:
return func(*args, **kwargs)
except errors as error:
LOG.exception(error)

raise Exception("Caught exception")

return wrapper
return decorator


def silent_catch(errors=(Exception,)):

''' Wrapper to catch exceptions and ignore them
'''
def decorator(func):
def wrapper(*args, **kwargs):

try:
return func(*args, **kwargs)
except errors as error:
LOG.error(error)

return wrapper
return decorator


def stop(default=None):
def wrapper(*args, **kwargs):

''' Wrapper to catch exceptions and return using catch
'''
def decorator(func):
def wrapper(*args, **kwargs):
try:
if should_stop(): # ??? TODO: Fixme
raise Exception

try:
if should_stop(): # ??? TODO: Fixme
raise Exception
except Exception as error:
LOG.exception(error)

except Exception as error:
LOG.exception(error)
raise LibraryException("StopCalled")

if default is not None:
return default
return func(*args, **kwargs)

raise LibraryException("StopCalled")
return wrapper

return func(*args, **kwargs)

return wrapper
return decorator


def jellyfin_item():
def jellyfin_item(func):

''' Wrapper to retrieve the jellyfin_db item.
'''
def decorator(func):
def wrapper(self, item, *args, **kwargs):
e_item = self.jellyfin_db.get_item_by_id(item['Id'] if type(item) == dict else item)
def wrapper(self, item, *args, **kwargs):
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)
return func(self, item, e_item=e_item, *args, **kwargs)

return wrapper
return decorator
return wrapper


def library_check():
def library_check(func):

''' 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.
'''
from database import get_sync
''' TODO: Rethink this one... songs and albums cannot be found by library. expensive.
'''
from database import get_sync

if kwargs.get('library') is None:
sync = get_sync()
if kwargs.get('library') is None:
sync = get_sync()

if 'e_item' in kwargs:
try:
view_id = kwargs['e_item'][6]
view_name = self.jellyfin_db.get_view_name(view_id)
view = {'Name': view_name, 'Id': view_id}
except Exception:
view = None
if 'e_item' in kwargs:
try:
view_id = kwargs['e_item'][6]
view_name = self.jellyfin_db.get_view_name(view_id)
view = {'Name': view_name, 'Id': view_id}
except Exception:
view = None

if view is None:
ancestors = self.server.jellyfin.get_ancestors(item['Id'])
if view is None:
ancestors = self.server.jellyfin.get_ancestors(item['Id'])

if not ancestors:
if item['Type'] == 'MusicArtist':
if not ancestors:
if item['Type'] == 'MusicArtist':

try:
views = self.jellyfin_db.get_views_by_media('music')[0]
except Exception as error:
LOG.exception(error)
return

view = {'Id': views[0], 'Name': views[1]}
else: # Grab the first music library
try:
views = self.jellyfin_db.get_views_by_media('music')[0]
except Exception as error:
LOG.exception(error)
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']}
view = {'Id': views[0], 'Name': views[1]}
else: # Grab the first music library
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
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'])
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'])

return
return

kwargs['library'] = view
kwargs['library'] = view

return func(self, item, *args, **kwargs)
return func(self, item, *args, **kwargs)

return wrapper
return decorator
return wrapper
11 changes: 0 additions & 11 deletions jellyfin_kodi/jellyfin/exceptions.py

This file was deleted.

3 changes: 1 addition & 2 deletions jellyfin_kodi/jellyfin/http.py
Expand Up @@ -10,8 +10,7 @@

from helper.utils import JsonDebugPrinter
from helper import LazyLogger

from .exceptions import HTTPException
from helper.exceptions import HTTPException

#################################################################################################

Expand Down
11 changes: 6 additions & 5 deletions jellyfin_kodi/library.py
Expand Up @@ -15,8 +15,9 @@
from full_sync import FullSync
from views import Views
from downloader import GetItemWorker
from helper import translate, api, stop, settings, window, dialog, event, LibraryException
from helper import translate, api, stop, settings, window, dialog, event
from helper.utils import split_list, set_screensaver, get_screensaver
from helper.exceptions import LibraryException
from jellyfin import Jellyfin
from helper import LazyLogger

Expand Down Expand Up @@ -109,7 +110,7 @@ def test_databases(self):
with Database('video'), Database('music'):
pass

@stop()
@stop
def service(self):

''' If error is encountered, it will rerun this function.
Expand Down Expand Up @@ -395,14 +396,14 @@ def fast_sync(self):
try:
# Get list of updates from server for synced library types and populate work queues
result = self.server.jellyfin.get_sync_queue(last_sync, ",".join([ x for x in query_filter ]))

if result is None:
return True

updated = []
userdata = []
removed = []

updated.extend(result['ItemsAdded'])
updated.extend(result['ItemsUpdated'])
userdata.extend(result['UserDataChanged'])
Expand Down