From 71d2ec88e37266e1ec74866000df0cbf8964a71a Mon Sep 17 00:00:00 2001 From: Uriziel Date: Sun, 19 Aug 2012 18:33:15 +0200 Subject: [PATCH] Refactoring, moved command handling here --- syncplay/ui/consoleUI.py | 50 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/syncplay/ui/consoleUI.py b/syncplay/ui/consoleUI.py index 63b147e..705224e 100644 --- a/syncplay/ui/consoleUI.py +++ b/syncplay/ui/consoleUI.py @@ -5,8 +5,8 @@ Created on 05-07-2012 ''' from __future__ import print_function import threading - - +import re +import time class ConsoleUI(threading.Thread): def __init__(self): self.promptMode = threading.Event() @@ -29,7 +29,7 @@ class ConsoleUI(threading.Thread): self.PromptResult = data self.promptMode.set() elif(self._syncplayClient): - self._syncplayClient.executeCommand(data) + self._executeCommand(data) except: raise @@ -41,10 +41,50 @@ class ConsoleUI(threading.Thread): return self.PromptResult def showMessage(self, message): - print(message) + print(time.strftime("[%X] ", time.localtime()) + message) def showDebugMessage(self, message): print(message) def showErrorMessage(self, message): - print(message) + print("ERROR desu!:\t" + message) + + def __exectueSeekCmd(self, seek_type, minutes, seconds): + self.player_position_before_last_seek = self.player_position + if seek_type == 's': + seconds = int(seconds) if seconds <> None else 0 + seconds += int(minutes) * 60 if minutes <> None else 0 + self._syncplayClient.player.set_position(seconds) + else: #seek_type s+ + seconds = int(seconds) if seconds <> None and minutes <> None else 20 + seconds += int(minutes) * 60 if minutes <> None else 60 + self._syncplayClient.player.set_position(self.player_position+seconds) + + def _executeCommand(self, data): + RE_SEEK = re.compile("^([\+\-s]+) ?(-?\d+)?([^0-9](\d+))?$") + RE_ROOM = re.compile("^room( (\w+))?") + matched_seek = RE_SEEK.match(data) + matched_room = RE_ROOM.match(data) + if matched_seek : + self.__exectueSeekCmd(matched_seek.group(1), matched_seek.group(2), matched_seek.group(4)) + elif matched_room: + room = matched_room.group(2) + if room == None: + room = 'default' + self._syncplayClient.users.currentUser.room = room + self._syncplayClient.checkIfFileMatchesOthers() + #self._syncplayClient.protocol.sender.send_room(room) + elif data == "r": + tmp_pos = self._syncplayClient.player_position + self._syncplayClient.player.set_position(self.player_position_before_last_seek) + self._syncplayClient.player_position_before_last_seek = tmp_pos + elif data == "p": + self._syncplayClient.player.set_paused(not self._syncplayClient.player_paused) + elif data == 'help': + self.showMessage( "Available commands:" ) + self.showMessage( "\thelp - this help" ) + self.showMessage( "\ts [time] - seek" ) + self.showMessage( "\ts+ [time] - seek to: current position += time" ) + self.showMessage( "\tr - revert last seek" ) + self.showMessage( "\tp - toggle pause" ) + self.showMessage( "\troom [room] - change room, if unspecified go to default" )