From fd84d97a462551503c278037d3b83518105f1a2e Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Thu, 11 May 2017 19:26:13 +0200 Subject: [PATCH] Use xbmcvfs exists instead of os.path.exists - Partially fixes #296 --- resources/lib/PlexAPI.py | 31 ++++++++++++++++++------------- resources/lib/artwork.py | 6 +++--- resources/lib/entrypoint.py | 9 +++++---- resources/lib/librarysync.py | 2 +- resources/lib/userclient.py | 2 +- resources/lib/utils.py | 28 ++++++++++++++++++++++++++-- resources/lib/videonodes.py | 12 ++++++------ 7 files changed, 60 insertions(+), 30 deletions(-) diff --git a/resources/lib/PlexAPI.py b/resources/lib/PlexAPI.py index ab96c6c4..07572f40 100644 --- a/resources/lib/PlexAPI.py +++ b/resources/lib/PlexAPI.py @@ -39,16 +39,16 @@ import xml.etree.ElementTree as etree from re import compile as re_compile, sub from json import dumps from urllib import urlencode, quote_plus, unquote -from os.path import basename, join, exists +from os.path import basename, join import xbmcgui from xbmc import sleep, executebuiltin -from xbmcvfs import mkdirs +from xbmcvfs import mkdirs, exists import clientinfo as client from downloadutils import DownloadUtils from utils import window, settings, language as lang, tryDecode, tryEncode, \ - DateToKodi + DateToKodi, exists_dir from PlexFunctions import PMSHttpsEnabled import plexdb_functions as plexdb import variables as v @@ -2117,10 +2117,9 @@ class API(): continue if fanartcount > maxfanarts: break - if exists(tryEncode(entry['url'])): - allartworks['Backdrop'].append( - entry['url'].replace(' ', '%20')) - fanartcount += 1 + allartworks['Backdrop'].append( + entry['url'].replace(' ', '%20')) + fanartcount += 1 return allartworks def getSetArtwork(self, parentInfo=False): @@ -2344,7 +2343,7 @@ class API(): Returns the path to the downloaded subtitle or None """ - if not exists(v.EXTERNAL_SUBTITLE_TEMP_PATH): + if not exists_dir(v.EXTERNAL_SUBTITLE_TEMP_PATH): mkdirs(v.EXTERNAL_SUBTITLE_TEMP_PATH) path = join(v.EXTERNAL_SUBTITLE_TEMP_PATH, filename) r = DownloadUtils().downloadUrl(url, return_response=True) @@ -2547,11 +2546,17 @@ class API(): check = exists(tryEncode(path)) else: # directories - if "\\" in path and not path.endswith('\\'): - # Add the missing backslash - check = exists(tryEncode(path + "\\")) - elif "/" in path and not path.endswith('/'): - check = exists(tryEncode(path + "/")) + if "\\" in path: + if not path.endswith('\\'): + # Add the missing backslash + check = exists_dir(tryEncode(path + "\\")) + else: + check = exists_dir(tryEncode(path)) + else: + if not path.endswith('/'): + check = exists_dir(tryEncode(path + "/")) + else: + check = exists_dir(tryEncode(path)) if not check: if forceCheck is False: diff --git a/resources/lib/artwork.py b/resources/lib/artwork.py index be076403..4efc05d4 100644 --- a/resources/lib/artwork.py +++ b/resources/lib/artwork.py @@ -4,16 +4,16 @@ import logging from json import dumps, loads import requests -from os.path import exists from shutil import rmtree from urllib import quote_plus, unquote from threading import Thread from Queue import Queue, Empty from xbmc import executeJSONRPC, sleep, translatePath +from xbmcvfs import exists from utils import window, settings, language as lang, kodiSQL, tryEncode, \ - ThreadMethods, ThreadMethodsAdditionalStop, dialog + ThreadMethods, ThreadMethodsAdditionalStop, dialog, exists_dir # Disable annoying requests warnings import requests.packages.urllib3 @@ -229,7 +229,7 @@ class Artwork(): log.info("Resetting all cache data first") # Remove all existing textures first path = translatePath("special://thumbnails/") - if exists(path): + if exists_dir(path): rmtree(path, ignore_errors=True) # remove all existing data from texture DB diff --git a/resources/lib/entrypoint.py b/resources/lib/entrypoint.py index dbac9e39..a7f14d23 100644 --- a/resources/lib/entrypoint.py +++ b/resources/lib/entrypoint.py @@ -3,7 +3,7 @@ import logging from shutil import copyfile from os import walk -from os.path import basename, join, exists +from os.path import basename, join from sys import argv from urllib import urlencode @@ -13,7 +13,7 @@ from xbmcgui import ListItem from xbmcvfs import mkdirs from utils import window, settings, language as lang, dialog, tryEncode, \ - CatchExceptions, JSONRPC + CatchExceptions, JSONRPC, exists_dir import downloadutils from PlexFunctions import GetPlexMetadata, GetPlexSectionResults, \ @@ -499,7 +499,7 @@ def getVideoFiles(plexId, params): path = path.replace('\\', '\\\\') # Directory only, get rid of filename path = path.replace(basename(path), '') - if exists(path): + if exists_dir(path): for root, dirs, files in walk(path): for directory in dirs: item_path = join(root, directory) @@ -514,6 +514,7 @@ def getVideoFiles(plexId, params): xbmcplugin.addDirectoryItem(handle=HANDLE, url=file, listitem=li) + break else: log.error('Kodi cannot access folder %s' % path) xbmcplugin.endOfDirectory(HANDLE) @@ -537,7 +538,7 @@ def getExtraFanArt(plexid, plexPath): # We need to store the images locally for this to work # because of the caching system in xbmc fanartDir = translatePath("special://thumbnails/plex/%s/" % plexid) - if not exists(fanartDir): + if not exists_dir(fanartDir): # Download the images to the cache directory mkdirs(fanartDir) xml = GetPlexMetadata(plexid) diff --git a/resources/lib/librarysync.py b/resources/lib/librarysync.py index d0e50cda..60e1a0e9 100644 --- a/resources/lib/librarysync.py +++ b/resources/lib/librarysync.py @@ -4,10 +4,10 @@ import logging from threading import Thread import Queue from random import shuffle -from os.path import exists import xbmc import xbmcgui +from xbmcvfs import exists from utils import window, settings, getUnixTimestamp, sourcesXML,\ ThreadMethods, ThreadMethodsAdditionalStop, LogTime, getScreensaver,\ diff --git a/resources/lib/userclient.py b/resources/lib/userclient.py index ebd790bd..339334a1 100644 --- a/resources/lib/userclient.py +++ b/resources/lib/userclient.py @@ -3,11 +3,11 @@ ############################################################################### import logging import threading -from os.path import exists import xbmc import xbmcgui import xbmcaddon +from xbmcvfs import exists from utils import window, settings, language as lang, ThreadMethods, \ diff --git a/resources/lib/utils.py b/resources/lib/utils.py index b040cdbd..7d17bd0c 100644 --- a/resources/lib/utils.py +++ b/resources/lib/utils.py @@ -21,10 +21,10 @@ from urllib import quote_plus import xbmc import xbmcaddon import xbmcgui -from xbmcvfs import exists as kodi_exists, mkdirs +from xbmcvfs import exists, mkdirs, delete from variables import DB_VIDEO_PATH, DB_MUSIC_PATH, DB_TEXTURE_PATH, \ - DB_PLEX_PATH, KODI_PROFILE + DB_PLEX_PATH, KODI_PROFILE, KODIVERSION ############################################################################### @@ -92,6 +92,30 @@ def settings(setting, value=None): return tryDecode(addon.getSetting(setting)) +def exists_dir(path): + """ + Safe way to check whether the directory path exists already (broken in Kodi + <17) + + Feed with encoded string + """ + if KODIVERSION >= 17: + answ = exists(path) + else: + dummyfile = join(path, 'dummyfile.txt') + try: + with open(dummyfile, 'w') as f: + f.write('text') + except IOError: + # folder does not exist yet + answ = 0 + else: + # Folder exists. Delete file again. + delete(dummyfile) + answ = 1 + return answ + + def language(stringid): # Central string retrieval return ADDON.getLocalizedString(stringid) diff --git a/resources/lib/videonodes.py b/resources/lib/videonodes.py index 1b200726..8e331a09 100644 --- a/resources/lib/videonodes.py +++ b/resources/lib/videonodes.py @@ -4,13 +4,13 @@ import logging from shutil import copytree import xml.etree.ElementTree as etree from os import remove, listdir -from os.path import exists, isfile, join +from os.path import isfile, join import xbmc -from xbmcvfs import mkdirs +from xbmcvfs import mkdirs, exists from utils import window, settings, language as lang, tryEncode, indent, \ - normalize_nodes + normalize_nodes, exists_dir import variables as v ############################################################################### @@ -75,14 +75,14 @@ class VideoNodes(object): return # Verify the video directory - if exists(path) is False: + if exists_dir(path) is False: copytree( src=xbmc.translatePath("special://xbmc/system/library/video"), dst=xbmc.translatePath("special://profile/library/video")) # Create the node directory if mediatype != "photos": - if exists(nodepath) is False: + if exists_dir(nodepath) is False: # folder does not exist yet log.debug('Creating folder %s' % nodepath) mkdirs(nodepath) @@ -388,7 +388,7 @@ class VideoNodes(object): windowpath = "ActivateWindow(Video,%s,return)" % path # Create the video node directory - if not exists(nodepath): + if not exists_dir(nodepath): # We need to copy over the default items copytree( src=xbmc.translatePath("special://xbmc/system/library/video"),