Dedicated function to check whether directory exists

This commit is contained in:
tomkat83 2016-03-07 16:30:50 +01:00
parent cbfa41de99
commit a258f969ab
4 changed files with 25 additions and 18 deletions

View file

@ -177,7 +177,7 @@ class Artwork():
self.logMsg("Resetting all cache data first", 1) self.logMsg("Resetting all cache data first", 1)
# Remove all existing textures first # Remove all existing textures first
path = xbmc.translatePath("special://thumbnails/").decode('utf-8') path = xbmc.translatePath("special://thumbnails/").decode('utf-8')
if xbmcvfs.exists(path): if utils.IfExists(path):
allDirs, allFiles = xbmcvfs.listdir(path) allDirs, allFiles = xbmcvfs.listdir(path)
for dir in allDirs: for dir in allDirs:
allDirs, allFiles = xbmcvfs.listdir(path+dir) allDirs, allFiles = xbmcvfs.listdir(path+dir)

View file

@ -5,7 +5,6 @@
import json import json
import os import os
import sys import sys
import urlparse
import xbmc import xbmc
import xbmcaddon import xbmcaddon
@ -399,7 +398,7 @@ def getThemeMedia():
library = xbmc.translatePath( library = xbmc.translatePath(
"special://profile/addon_data/plugin.video.plexkodiconnect/library/").decode('utf-8') "special://profile/addon_data/plugin.video.plexkodiconnect/library/").decode('utf-8')
# Create library directory # Create library directory
if not xbmcvfs.exists(library): if not utils.IfExists(library):
xbmcvfs.mkdir(library) xbmcvfs.mkdir(library)
# Set custom path for user # Set custom path for user

View file

@ -25,6 +25,27 @@ import xbmcvfs
addonName = xbmcaddon.Addon().getAddonInfo('name') 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): def LogTime(func):
""" """
Decorator for functions and methods to log the time it took to run the code Decorator for functions and methods to log the time it took to run the code

View file

@ -68,30 +68,17 @@ class VideoNodes(object):
# KODI BUG # KODI BUG
# Kodi caches the result of exists for directories # Kodi caches the result of exists for directories
# so try creating a file # so try creating a file
dummyfile = ospath.join(path, 'dummyfile.txt').encode('utf-8') if utils.IfExists(path) is False:
try:
etree.ElementTree(etree.Element('test')).write(dummyfile)
except:
# path does not exist yet
shutil.copytree( shutil.copytree(
src=xbmc.translatePath("special://xbmc/system/library/video").decode('utf-8'), src=xbmc.translatePath("special://xbmc/system/library/video").decode('utf-8'),
dst=xbmc.translatePath("special://profile/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 # Create the node directory
if mediatype != "photo": if mediatype != "photo":
dummyfile = ospath.join(nodepath, 'dummyfile.txt').encode('utf-8') if utils.IfExists(nodepath) is False:
try:
etree.ElementTree(etree.Element('test')).write(dummyfile)
except:
# folder does not exist yet # folder does not exist yet
self.logMsg('Creating folder %s' % nodepath, 1) self.logMsg('Creating folder %s' % nodepath, 1)
xbmcvfs.mkdirs(nodepath.encode('utf-8')) xbmcvfs.mkdirs(nodepath.encode('utf-8'))
else:
# path exists - delete dummy file
xbmcvfs.delete(dummyfile)
if delete: if delete:
dirs, files = xbmcvfs.listdir(nodepath.encode('utf-8')) dirs, files = xbmcvfs.listdir(nodepath.encode('utf-8'))
for file in files: for file in files: