Remove encoding and decoding of filepaths

This commit is contained in:
croneter 2020-12-23 15:29:27 +01:00
parent 5c81c15cfd
commit 88a84672c3
9 changed files with 7 additions and 96 deletions

View file

@ -364,10 +364,8 @@ def extra_fanart(plex_id, plex_path):
# Use existing cached images # Use existing cached images
fanart_dir = fanart_dir fanart_dir = fanart_dir
for root, _, files in path_ops.walk(fanart_dir): for root, _, files in path_ops.walk(fanart_dir):
root = utils.decode_path(root)
for file in files: for file in files:
file = utils.decode_path(file) art_file = path_ops.path.join(root, file)
art_file = utils.try_encode(path_ops.path.join(root, file))
listitem = ListItem(file, path=art_file) listitem = ListItem(file, path=art_file)
xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),
url=art_file, url=art_file,

View file

@ -73,7 +73,7 @@ def reset_cached_images():
for path in paths: for path in paths:
new_path = path_ops.translate_path('special://thumbnails/%s' % path) new_path = path_ops.translate_path('special://thumbnails/%s' % path)
try: try:
path_ops.makedirs(path_ops.encode_path(new_path)) path_ops.makedirs(new_path)
except OSError as err: except OSError as err:
LOG.warn('Could not create thumbnail directory %s: %s', LOG.warn('Could not create thumbnail directory %s: %s',
new_path, err) new_path, err)

View file

@ -61,8 +61,7 @@ class KodiVideoDB(common.KodiDBBase):
of parent path ids of parent path ids
""" """
parentpath = path_ops.path.abspath( parentpath = path_ops.path.abspath(
path_ops.path.join(path, path_ops.path.join(path, path_ops.path.pardir))
path_ops.decode_path(path_ops.path.pardir)))
pathid = self.get_path(parentpath) pathid = self.get_path(parentpath)
if pathid is None: if pathid is None:
self.cursor.execute(''' self.cursor.execute('''

View file

@ -22,33 +22,11 @@ import re
import xbmcvfs import xbmcvfs
from .tools import unicode_paths
# Kodi seems to encode in utf-8 in ALL cases (unlike e.g. the OS filesystem) # Kodi seems to encode in utf-8 in ALL cases (unlike e.g. the OS filesystem)
KODI_ENCODING = 'utf-8' KODI_ENCODING = 'utf-8'
REGEX_FILE_NUMBERING = re.compile(r'''_(\d\d)\.\w+$''') 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): def translate_path(path):
""" """
Returns the XBMC translated path [unicode] Returns the XBMC translated path [unicode]

View file

@ -125,7 +125,7 @@ def kodi_playlist_hash(path):
There are probably way more efficient ways out there to do this 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 # stat.st_size is of type long; stat.st_mtime is of type float - hash both
m = hashlib.md5() m = hashlib.md5()
m.update(repr(stat.st_size)) m.update(repr(stat.st_size))

View file

@ -92,7 +92,7 @@ def m3u_to_plex_ids(playlist):
Adapter to process *.m3u playlist files. Encoding is not uniform! Adapter to process *.m3u playlist files. Encoding is not uniform!
""" """
plex_ids = list() 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() text = f.read()
try: try:
text = text.decode(v.M3U_ENCODING) text = text.decode(v.M3U_ENCODING)

View file

@ -121,7 +121,7 @@ def _write_playlist_to_file(playlist, xml):
text += '\n' text += '\n'
text = text.encode(v.M3U_ENCODING, 'ignore') text = text.encode(v.M3U_ENCODING, 'ignore')
try: try:
with open(path_ops.encode_path(playlist.kodi_path), 'wb') as f: with open(playlist.kodi_path, 'wb') as f:
f.write(text) f.write(text)
except EnvironmentError as err: except EnvironmentError as err:
LOG.error('Could not write Kodi playlist file: %s', playlist) LOG.error('Could not write Kodi playlist file: %s', playlist)

View file

@ -325,7 +325,7 @@ class Media(object):
return return
else: else:
LOG.debug('Writing temp subtitle to %s', path) 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) f.write(response.content)
return path return path

View file

@ -1,64 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2013 Will Bond <will@wbond.net>
#
# 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