From 6904494e3189961c4345d3f7e031be897235e24b Mon Sep 17 00:00:00 2001 From: croneter Date: Sat, 19 Dec 2020 08:27:34 +0100 Subject: [PATCH] Get rid of utils.try_encode and utils.try_decode --- resources/lib/context_entry.py | 2 +- resources/lib/entrypoint.py | 14 +++++------ resources/lib/playback.py | 4 ++-- resources/lib/plex_functions.py | 4 ++-- resources/lib/utils.py | 41 ++++----------------------------- resources/lib/variables.py | 38 +++++++++--------------------- resources/lib/websocket.py | 2 +- 7 files changed, 27 insertions(+), 78 deletions(-) diff --git a/resources/lib/context_entry.py b/resources/lib/context_entry.py index da00db17..9009df30 100644 --- a/resources/lib/context_entry.py +++ b/resources/lib/context_entry.py @@ -91,7 +91,7 @@ class ContextMenu(object): options.append(OPTIONS['Addon']) context_menu = context.ContextMenu( "script-plex-context.xml", - utils.try_encode(v.ADDON_PATH), + v.ADDON_PATH, "default", "1080i") context_menu.set_options(options) diff --git a/resources/lib/entrypoint.py b/resources/lib/entrypoint.py index ef96e855..757ee9d6 100644 --- a/resources/lib/entrypoint.py +++ b/resources/lib/entrypoint.py @@ -284,7 +284,7 @@ def get_video_files(plex_id, params): app.init(entrypoint=True) item = PF.GetPlexMetadata(plex_id) try: - path = utils.try_decode(item[0][0][0].attrib['file']) + path = item[0][0][0].attrib['file'] except (TypeError, IndexError, AttributeError, KeyError): LOG.error('Could not get file path for item %s', plex_id) return xbmcplugin.endOfDirectory(int(sys.argv[1])) @@ -300,15 +300,14 @@ def get_video_files(plex_id, params): if path_ops.exists(path): for root, dirs, files in path_ops.walk(path): for directory in dirs: - item_path = utils.try_encode(path_ops.path.join(root, - directory)) + item_path = path_ops.path.join(root, directory) listitem = ListItem(item_path, path=item_path) xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=item_path, listitem=listitem, isFolder=True) for file in files: - item_path = utils.try_encode(path_ops.path.join(root, file)) + item_path = path_ops.path.join(root, file) listitem = ListItem(item_path, path=item_path) xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=file, @@ -353,18 +352,17 @@ def extra_fanart(plex_id, plex_path): backdrops = api.artwork()['Backdrop'] for count, backdrop in enumerate(backdrops): # Same ordering as in artwork - art_file = utils.try_encode(path_ops.path.join( - fanart_dir, "fanart%.3d.jpg" % count)) + art_file = path_ops.path.join(fanart_dir, "fanart%.3d.jpg" % count) listitem = ListItem("%.3d" % count, path=art_file) xbmcplugin.addDirectoryItem( handle=int(sys.argv[1]), url=art_file, listitem=listitem) - path_ops.copyfile(backdrop, utils.try_decode(art_file)) + path_ops.copyfile(backdrop, art_file) else: LOG.info("Found cached backdrop.") # Use existing cached images - fanart_dir = utils.try_decode(fanart_dir) + fanart_dir = fanart_dir for root, _, files in path_ops.walk(fanart_dir): root = utils.decode_path(root) for file in files: diff --git a/resources/lib/playback.py b/resources/lib/playback.py index bd9bebdc..16e854e9 100644 --- a/resources/lib/playback.py +++ b/resources/lib/playback.py @@ -528,13 +528,13 @@ def process_indirect(key, offset, resolve=True): return item.file = playurl - listitem.setPath(utils.try_encode(playurl)) + listitem.setPath(playurl) playqueue.items.append(item) if resolve is True: transfer.send(listitem) else: thread = Thread(target=app.APP.player.play, - args={'item': utils.try_encode(playurl), + args={'item': playurl, 'listitem': listitem}) thread.setDaemon(True) LOG.debug('Done initializing PKC playback, starting Kodi player') diff --git a/resources/lib/plex_functions.py b/resources/lib/plex_functions.py index 2bad94f2..1e37ebed 100644 --- a/resources/lib/plex_functions.py +++ b/resources/lib/plex_functions.py @@ -303,11 +303,11 @@ def _plex_gdm(): } for line in response['data'].split('\n'): if 'Content-Type:' in line: - pms['product'] = utils.try_decode(line.split(':')[1].strip()) + pms['product'] = line.split(':')[1].strip() elif 'Host:' in line: pms['baseURL'] = line.split(':')[1].strip() elif 'Name:' in line: - pms['name'] = utils.try_decode(line.split(':')[1].strip()) + pms['name'] = line.split(':')[1].strip() elif 'Port:' in line: pms['port'] = line.split(':')[1].strip() elif 'Resource-Identifier:' in line: diff --git a/resources/lib/utils.py b/resources/lib/utils.py index 6e79fc39..01a0c4b3 100644 --- a/resources/lib/utils.py +++ b/resources/lib/utils.py @@ -100,13 +100,12 @@ def window(prop, value=None, clear=False, windowid=10000): win = xbmcgui.Window(windowid) else: win = WINDOW - if clear: win.clearProperty(prop) elif value is not None: - win.setProperty(try_encode(prop), try_encode(value)) + win.setProperty(prop, value) else: - return try_decode(win.getProperty(prop)) + return win.getProperty(prop) def settings(setting, value=None): @@ -120,10 +119,10 @@ def settings(setting, value=None): addon = xbmcaddon.Addon(id='plugin.video.plexkodiconnect') if value is not None: # Takes string or unicode by default! - addon.setSetting(try_encode(setting), try_encode(value)) + addon.setSetting(setting, value) else: # Should return unicode by default, but just in case - return try_decode(addon.getSetting(setting)) + return addon.getSetting(setting) def lang(stringid): @@ -424,38 +423,6 @@ def unquote(s): return urllib.parse.unquote(s) -def try_encode(input_str, encoding='utf-8'): - """ - Will try to encode input_str (in unicode) to encoding. This possibly - fails with e.g. Android TV's Python, which does not accept arguments for - string.encode() - """ - if isinstance(input_str, str): - # already encoded - return input_str - try: - input_str = input_str.encode(encoding, "ignore") - except TypeError: - input_str = input_str.encode() - return input_str - - -def try_decode(string, encoding='utf-8'): - """ - Will try to decode string (encoded) using encoding. This possibly - fails with e.g. Android TV's Python, which does not accept arguments for - string.encode() - """ - if isinstance(string, str): - # already decoded - return string - try: - string = string.decode(encoding, "ignore") - except TypeError: - string = string.decode() - return string - - def slugify(text): """ Normalizes text (in unicode or string) to e.g. enable safe filenames. diff --git a/resources/lib/variables.py b/resources/lib/variables.py index 72db3b51..1b7dfa3f 100644 --- a/resources/lib/variables.py +++ b/resources/lib/variables.py @@ -14,22 +14,6 @@ from . import path_ops # For any file operations with KODI function, use encoded strings! -def try_decode(string, encoding='utf-8'): - """ - Will try to decode string (encoded) using encoding. This possibly - fails with e.g. Android TV's Python, which does not accept arguments for - string.encode() - """ - if isinstance(string, str): - # already decoded - return string - try: - string = string.decode(encoding, "ignore") - except TypeError: - string = string.decode() - return string - - # Percent of playback progress for watching item as partially watched. Anything # more and item will NOT be marked as partially, but fully watched MARK_PLAYED_AT = 0.9 @@ -41,14 +25,14 @@ _ADDON = Addon() ADDON_NAME = 'PlexKodiConnect' ADDON_ID = 'plugin.video.plexkodiconnect' ADDON_VERSION = _ADDON.getAddonInfo('version') -ADDON_PATH = try_decode(_ADDON.getAddonInfo('path')) -ADDON_FOLDER = try_decode(xbmcvfs.translatePath('special://home')) -ADDON_PROFILE = try_decode(xbmcvfs.translatePath(_ADDON.getAddonInfo('profile'))) +ADDON_PATH = _ADDON.getAddonInfo('path') +ADDON_FOLDER = xbmcvfs.translatePath('special://home') +ADDON_PROFILE = xbmcvfs.translatePath(_ADDON.getAddonInfo('profile')) KODILANGUAGE = xbmc.getLanguage(xbmc.ISO_639_1) KODIVERSION = int(xbmc.getInfoLabel("System.BuildVersion")[:2]) KODILONGVERSION = xbmc.getInfoLabel('System.BuildVersion') -KODI_PROFILE = try_decode(xbmcvfs.translatePath("special://profile")) +KODI_PROFILE = xbmcvfs.translatePath("special://profile") if xbmc.getCondVisibility('system.platform.osx'): DEVICE = "MacOSX" @@ -78,9 +62,9 @@ except IOError: # See https://github.com/psf/requests/issues/4434 MODEL = 'Unknown' -DEVICENAME = try_decode(_ADDON.getSetting('deviceName')) +DEVICENAME = _ADDON.getSetting('deviceName') if not DEVICENAME: - DEVICENAME = try_decode(xbmc.getInfoLabel('System.FriendlyName')) + DEVICENAME = xbmc.getInfoLabel('System.FriendlyName') _ADDON.setSetting('deviceName', DEVICENAME) DEVICENAME = DEVICENAME.replace(":", "") DEVICENAME = DEVICENAME.replace("/", "-") @@ -121,11 +105,11 @@ DB_MUSIC_VERSION = None DB_MUSIC_PATH = None DB_TEXTURE_VERSION = None DB_TEXTURE_PATH = None -DB_PLEX_PATH = try_decode(xbmcvfs.translatePath("special://database/plex.db")) -DB_PLEX_COPY_PATH = try_decode(xbmcvfs.translatePath("special://database/plex-copy.db")) +DB_PLEX_PATH = xbmcvfs.translatePath("special://database/plex.db") +DB_PLEX_COPY_PATH = xbmcvfs.translatePath("special://database/plex-copy.db") -EXTERNAL_SUBTITLE_TEMP_PATH = try_decode(xbmcvfs.translatePath( - "special://profile/addon_data/%s/temp/" % ADDON_ID)) +EXTERNAL_SUBTITLE_TEMP_PATH = xbmcvfs.translatePath( + "special://profile/addon_data/%s/temp/" % ADDON_ID) # Multiply Plex time by this factor to receive Kodi time @@ -699,7 +683,7 @@ def database_paths(): if KODIVERSION not in (19, ): raise RuntimeError('Kodiversion %s not supported by PKC' % KODIVERSION) - database_path = try_decode(xbmcvfs.translatePath('special://database')) + database_path = xbmcvfs.translatePath('special://database') thismodule = sys.modules[__name__] types = (('MyVideos%s.db', SUPPORTED_VIDEO_DB, 'DB_VIDEO_VERSION', 'DB_VIDEO_PATH'), diff --git a/resources/lib/websocket.py b/resources/lib/websocket.py index f1df3c6a..0150b551 100644 --- a/resources/lib/websocket.py +++ b/resources/lib/websocket.py @@ -309,7 +309,7 @@ class ABNF(object): opcode: operation code. please see OPCODE_XXX. """ if opcode == ABNF.OPCODE_TEXT and isinstance(data, str): - data = utils.try_encode(data) + data = data.encode() # mask must be set if send data from client return ABNF(1, 0, 0, 0, opcode, 1, data)