From f0f29653cee9d49769a2c78e04a05f85b96da2c7 Mon Sep 17 00:00:00 2001 From: Et0h Date: Wed, 26 Aug 2015 15:52:39 +0100 Subject: [PATCH] Add 1 second timeout for media search, and make it more efficient --- syncplay/constants.py | 2 ++ syncplay/messages.py | 3 +++ syncplay/ui/gui.py | 22 ++++++++++++++++++++-- syncplay/utils.py | 10 +++++++--- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/syncplay/constants.py b/syncplay/constants.py index 7aefb2d..59b406f 100644 --- a/syncplay/constants.py +++ b/syncplay/constants.py @@ -48,6 +48,8 @@ SERVER_STATE_INTERVAL = 1 WARNING_OSD_MESSAGES_LOOP_INTERVAL = 1 AUTOPLAY_DELAY = 3.0 SYNC_ON_PAUSE = True # Client seek to global position - subtitles may disappear on some media players +FOLDER_SEARCH_TIMEOUT = 1.0 # Secs + #Usually there's no need to adjust these LAST_PAUSED_DIFF_THRESHOLD = 2 FILENAME_STRIP_REGEX = u"[-~_\.\[\](): ]" diff --git a/syncplay/messages.py b/syncplay/messages.py index ba560c6..2e9f336 100755 --- a/syncplay/messages.py +++ b/syncplay/messages.py @@ -135,6 +135,7 @@ en = { "invalid-offset-value" : u"Invalid offset value", "switch-file-not-found-error" : u"Could not switch to file '{0}'. Syncplay looks in the folder of the currently playing file and specified media directories.", # File not found + "folder-search-timeout-error" : u"The search for media in '{}' was aborted as it took too long. This will occur if you select a folder with too many sub-folders in your list of media folders to search through. Until Syncplay is restarted only the directory of the currently open file will be checked.", #Folder # Client arguments "argument-description" : 'Solution to synchronize playback of multiple MPlayer and MPC-HC instances over the network.', @@ -497,6 +498,7 @@ ru = { "invalid-offset-value" : u"Некорректное смещение", "switch-file-not-found-error" : u"Невозможно переключиться на файл '{0}'. Syncplay looks in the folder of the currently playing file and specified media directories.", # File not found # TODO: Translate last part into Russian + "folder-search-timeout-error" : u"The search for media in '{}' was aborted as it took too long. This will occur if you select a folder with too many sub-folders in your list of media folders to search through. Until Syncplay is restarted only the directory of the currently open file will be checked.", #Folder # TODO: Translate into Russian # Client arguments "argument-description" : u'Решение для синхронного воспроизведения в VLC, MPlayer или MPC-HC через Интернет.', @@ -859,6 +861,7 @@ de = { "invalid-offset-value" : u"Ungültiger Offset-Wert", "switch-file-not-found-error" : u"Could not switch to file '{0}'. Syncplay looks in the folder of the currently playing file and specified media directories.", # File not found, folder it was not found in # TODO: Translate into German + "folder-search-timeout-error" : u"The search for media in '{}' was aborted as it took too long. This will occur if you select a folder with too many sub-folders in your list of media folders to search through. Until Syncplay is restarted only the directory of the currently open file will be checked.", #Folder # TODO: Translate into German # Client arguments "argument-description" : u'Syncplay ist eine Anwendung um mehrere MPlayer, MPC-HC und VLC-Instanzen über das Internet zu synchronisieren.', diff --git a/syncplay/ui/gui.py b/syncplay/ui/gui.py index a19762c..cc001fd 100644 --- a/syncplay/ui/gui.py +++ b/syncplay/ui/gui.py @@ -152,7 +152,16 @@ class MainWindow(QtGui.QMainWindow): return constants.FILEITEM_SWITCH_STREAM_SWITCH else: currentPath = self._syncplayClient.userlist.currentUser.file["path"] if self._syncplayClient.userlist.currentUser.file else None - if utils.findFilenameInDirectories(filename, self.config["mediaSearchDirectories"]): + if self.folderSearchEnabled: + try: + filenamesInDirectories = utils.findFilenameInDirectories(filename, self.config["mediaSearchDirectories"]) + except IOError as errorMessage: + self.showErrorMessage(errorMessage) + filenamesInDirectories = None + self.folderSearchEnabled = False + else: + filenamesInDirectories = None + if filenamesInDirectories: return constants.FILEITEM_SWITCH_FILE_SWITCH elif currentPath: currentDirectory = os.path.dirname(currentPath) @@ -301,7 +310,15 @@ class MainWindow(QtGui.QMainWindow): self._syncplayClient._player.openFile(filename) else: currentPath = self._syncplayClient.userlist.currentUser.file["path"] if self._syncplayClient.userlist.currentUser.file else None - pathFound = utils.findFilenameInDirectories(filename, self.config["mediaSearchDirectories"]) + if self.folderSearchEnabled: + try: + pathFound = utils.findFilenameInDirectories(filename, self.config["mediaSearchDirectories"]) + except IOError as errorMessage: + self.showErrorMessage(errorMessage) + pathFound = None + self.folderSearchEnabled = False + else: + pathFound = None if pathFound: self._syncplayClient._player.openFile(pathFound) elif currentPath: @@ -907,6 +924,7 @@ class MainWindow(QtGui.QMainWindow): def __init__(self): super(MainWindow, self).__init__() self._syncplayClient = None + self.folderSearchEnabled = True self.QtGui = QtGui if sys.platform.startswith('win'): self.resourcespath = utils.findWorkingDir() + "\\resources\\" diff --git a/syncplay/utils.py b/syncplay/utils.py index ecada19..2e76541 100644 --- a/syncplay/utils.py +++ b/syncplay/utils.py @@ -11,6 +11,8 @@ import random import string import urllib +folderSearchEnabled = True + def retry(ExceptionToCheck, tries=4, delay=3, backoff=2, logger=None): """Retry calling the decorated function using an exponential backoff. @@ -249,11 +251,13 @@ def convertMultilineStringToList(multilineString): def findFilenameInDirectories(filename, directoryList): if filename and directoryList: + startTime = time.time() for directory in directoryList: for root, dirs, files in os.walk(directory): - candidatePath = os.path.join(root,filename) - if os.path.isfile(candidatePath): - return candidatePath + if filename in files: + return os.path.join(root,filename) + if time.time() - startTime > constants.FOLDER_SEARCH_TIMEOUT: + raise IOError(getMessage("folder-search-timeout-error").format(directory)) return None class RoomPasswordProvider(object):