From a258f969ab8c1b599a7156e30dd4adffa8361bca Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Mon, 7 Mar 2016 16:30:50 +0100 Subject: [PATCH] Dedicated function to check whether directory exists --- resources/lib/artwork.py | 2 +- resources/lib/entrypoint.py | 3 +-- resources/lib/utils.py | 21 +++++++++++++++++++++ resources/lib/videonodes.py | 17 ++--------------- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/resources/lib/artwork.py b/resources/lib/artwork.py index a8012e6e..9bface89 100644 --- a/resources/lib/artwork.py +++ b/resources/lib/artwork.py @@ -177,7 +177,7 @@ class Artwork(): self.logMsg("Resetting all cache data first", 1) # Remove all existing textures first path = xbmc.translatePath("special://thumbnails/").decode('utf-8') - if xbmcvfs.exists(path): + if utils.IfExists(path): allDirs, allFiles = xbmcvfs.listdir(path) for dir in allDirs: allDirs, allFiles = xbmcvfs.listdir(path+dir) diff --git a/resources/lib/entrypoint.py b/resources/lib/entrypoint.py index e7be13cd..5080bccd 100644 --- a/resources/lib/entrypoint.py +++ b/resources/lib/entrypoint.py @@ -5,7 +5,6 @@ import json import os import sys -import urlparse import xbmc import xbmcaddon @@ -399,7 +398,7 @@ def getThemeMedia(): library = xbmc.translatePath( "special://profile/addon_data/plugin.video.plexkodiconnect/library/").decode('utf-8') # Create library directory - if not xbmcvfs.exists(library): + if not utils.IfExists(library): xbmcvfs.mkdir(library) # Set custom path for user diff --git a/resources/lib/utils.py b/resources/lib/utils.py index 4c11baac..d2db3fdf 100644 --- a/resources/lib/utils.py +++ b/resources/lib/utils.py @@ -25,6 +25,27 @@ import xbmcvfs addonName = xbmcaddon.Addon().getAddonInfo('name') +def IfExists(path): + """ + Kodi's xbmcvfs.exists is broken - it caches the results for directories. + + path: path to a directory (with a slash at the end) + + Returns True if path exists, else false + """ + dummyfile = os.path.join(path, 'dummyfile.txt').encode('utf-8') + try: + etree.ElementTree(etree.Element('test')).write(dummyfile) + except: + # folder does not exist yet + answer = False + else: + # Folder exists. Delete file again. + xbmcvfs.delete(dummyfile) + answer = True + return answer + + def LogTime(func): """ Decorator for functions and methods to log the time it took to run the code diff --git a/resources/lib/videonodes.py b/resources/lib/videonodes.py index cbcaad42..12768ad1 100644 --- a/resources/lib/videonodes.py +++ b/resources/lib/videonodes.py @@ -68,30 +68,17 @@ class VideoNodes(object): # KODI BUG # Kodi caches the result of exists for directories # so try creating a file - dummyfile = ospath.join(path, 'dummyfile.txt').encode('utf-8') - try: - etree.ElementTree(etree.Element('test')).write(dummyfile) - except: - # path does not exist yet + if utils.IfExists(path) is False: shutil.copytree( src=xbmc.translatePath("special://xbmc/system/library/video").decode('utf-8'), dst=xbmc.translatePath("special://profile/library/video").decode('utf-8')) - else: - # path exists - delete dummy file - xbmcvfs.delete(dummyfile) # Create the node directory if mediatype != "photo": - dummyfile = ospath.join(nodepath, 'dummyfile.txt').encode('utf-8') - try: - etree.ElementTree(etree.Element('test')).write(dummyfile) - except: + if utils.IfExists(nodepath) is False: # folder does not exist yet self.logMsg('Creating folder %s' % nodepath, 1) xbmcvfs.mkdirs(nodepath.encode('utf-8')) - else: - # path exists - delete dummy file - xbmcvfs.delete(dummyfile) if delete: dirs, files = xbmcvfs.listdir(nodepath.encode('utf-8')) for file in files: