From 88a84672c33cab45a0f58a317ccd0ae45e62c57c Mon Sep 17 00:00:00 2001 From: croneter Date: Wed, 23 Dec 2020 15:29:27 +0100 Subject: [PATCH] Remove encoding and decoding of filepaths --- resources/lib/entrypoint.py | 4 +- resources/lib/kodi_db/__init__.py | 2 +- resources/lib/kodi_db/video.py | 3 +- resources/lib/path_ops.py | 22 ---------- resources/lib/playlists/common.py | 2 +- resources/lib/playlists/db.py | 2 +- resources/lib/playlists/kodi_pl.py | 2 +- resources/lib/plex_api/media.py | 2 +- resources/lib/tools/unicode_paths.py | 64 ---------------------------- 9 files changed, 7 insertions(+), 96 deletions(-) delete mode 100644 resources/lib/tools/unicode_paths.py diff --git a/resources/lib/entrypoint.py b/resources/lib/entrypoint.py index ce21bdc3..69a41b2c 100644 --- a/resources/lib/entrypoint.py +++ b/resources/lib/entrypoint.py @@ -364,10 +364,8 @@ def extra_fanart(plex_id, plex_path): # Use existing cached images fanart_dir = fanart_dir for root, _, files in path_ops.walk(fanart_dir): - root = utils.decode_path(root) for file in files: - file = utils.decode_path(file) - art_file = utils.try_encode(path_ops.path.join(root, file)) + art_file = path_ops.path.join(root, file) listitem = ListItem(file, path=art_file) xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=art_file, diff --git a/resources/lib/kodi_db/__init__.py b/resources/lib/kodi_db/__init__.py index 90033fad..c3cd3346 100644 --- a/resources/lib/kodi_db/__init__.py +++ b/resources/lib/kodi_db/__init__.py @@ -73,7 +73,7 @@ def reset_cached_images(): for path in paths: new_path = path_ops.translate_path('special://thumbnails/%s' % path) try: - path_ops.makedirs(path_ops.encode_path(new_path)) + path_ops.makedirs(new_path) except OSError as err: LOG.warn('Could not create thumbnail directory %s: %s', new_path, err) diff --git a/resources/lib/kodi_db/video.py b/resources/lib/kodi_db/video.py index 370476fd..f307bb54 100644 --- a/resources/lib/kodi_db/video.py +++ b/resources/lib/kodi_db/video.py @@ -61,8 +61,7 @@ class KodiVideoDB(common.KodiDBBase): of parent path ids """ parentpath = path_ops.path.abspath( - path_ops.path.join(path, - path_ops.decode_path(path_ops.path.pardir))) + path_ops.path.join(path, path_ops.path.pardir)) pathid = self.get_path(parentpath) if pathid is None: self.cursor.execute(''' diff --git a/resources/lib/path_ops.py b/resources/lib/path_ops.py index f6f62b16..f0363f5d 100644 --- a/resources/lib/path_ops.py +++ b/resources/lib/path_ops.py @@ -22,33 +22,11 @@ import re import xbmcvfs -from .tools import unicode_paths - # Kodi seems to encode in utf-8 in ALL cases (unlike e.g. the OS filesystem) KODI_ENCODING = 'utf-8' REGEX_FILE_NUMBERING = re.compile(r'''_(\d\d)\.\w+$''') -def encode_path(path): - """ - Filenames and paths are not necessarily utf-8 encoded. Use this function - instead of try_encode/trydecode if working with filenames and paths! - (os.walk only feeds on encoded paths. sys.getfilesystemencoding returns None - for Raspberry Pi) - """ - return unicode_paths.encode(path) - - -def decode_path(path): - """ - Filenames and paths are not necessarily utf-8 encoded. Use this function - instead of try_encode/trydecode if working with filenames and paths! - (os.walk only feeds on encoded paths. sys.getfilesystemencoding returns None - for Raspberry Pi) - """ - return unicode_paths.decode(path) - - def translate_path(path): """ Returns the XBMC translated path [unicode] diff --git a/resources/lib/playlists/common.py b/resources/lib/playlists/common.py index 4de357ea..02131ad9 100644 --- a/resources/lib/playlists/common.py +++ b/resources/lib/playlists/common.py @@ -125,7 +125,7 @@ def kodi_playlist_hash(path): There are probably way more efficient ways out there to do this """ - stat = os.stat(path_ops.encode_path(path)) + stat = os.stat(path) # stat.st_size is of type long; stat.st_mtime is of type float - hash both m = hashlib.md5() m.update(repr(stat.st_size)) diff --git a/resources/lib/playlists/db.py b/resources/lib/playlists/db.py index 43c6b8fa..ccfd337c 100644 --- a/resources/lib/playlists/db.py +++ b/resources/lib/playlists/db.py @@ -92,7 +92,7 @@ def m3u_to_plex_ids(playlist): Adapter to process *.m3u playlist files. Encoding is not uniform! """ plex_ids = list() - with open(path_ops.encode_path(playlist.kodi_path), 'rb') as f: + with open(playlist.kodi_path, 'rb') as f: text = f.read() try: text = text.decode(v.M3U_ENCODING) diff --git a/resources/lib/playlists/kodi_pl.py b/resources/lib/playlists/kodi_pl.py index 91808441..75685f76 100644 --- a/resources/lib/playlists/kodi_pl.py +++ b/resources/lib/playlists/kodi_pl.py @@ -121,7 +121,7 @@ def _write_playlist_to_file(playlist, xml): text += '\n' text = text.encode(v.M3U_ENCODING, 'ignore') try: - with open(path_ops.encode_path(playlist.kodi_path), 'wb') as f: + with open(playlist.kodi_path, 'wb') as f: f.write(text) except EnvironmentError as err: LOG.error('Could not write Kodi playlist file: %s', playlist) diff --git a/resources/lib/plex_api/media.py b/resources/lib/plex_api/media.py index 24090a2c..5d7cc080 100644 --- a/resources/lib/plex_api/media.py +++ b/resources/lib/plex_api/media.py @@ -325,7 +325,7 @@ class Media(object): return else: LOG.debug('Writing temp subtitle to %s', path) - with open(path_ops.encode_path(path), 'wb') as f: + with open(path, 'wb') as f: f.write(response.content) return path diff --git a/resources/lib/tools/unicode_paths.py b/resources/lib/tools/unicode_paths.py deleted file mode 100644 index 3f8df963..00000000 --- a/resources/lib/tools/unicode_paths.py +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -# -# Copyright (c) 2013 Will Bond -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - - -import sys - -from . import platform - -try: - # Python 2 - str_cls = str - bytes_cls = str -except NameError: - # Python 3 - str_cls = str - bytes_cls = bytes - - -# This is used by Linux when the locale seems to be improperly set. UTF-8 tends -# to be the encoding used by all distros, so this is a good fallback. -fs_fallback_encoding = 'utf-8' -fs_encoding = sys.getfilesystemencoding() or fs_fallback_encoding - - -def encode(path): - if isinstance(path, str_cls): - try: - path = path.encode(fs_encoding, 'strict') - except UnicodeEncodeError: - if not platform.is_linux(): - raise - path = path.encode(fs_fallback_encoding, 'strict') - return path - - -def decode(path): - if isinstance(path, bytes_cls): - try: - path = path.decode(fs_encoding, 'strict') - except UnicodeDecodeError: - if not platform.is_linux(): - raise - path = path.decode(fs_fallback_encoding, 'strict') - return path