Merge 72759331945e3241bfeccda2c9c9d701d82fe03e into fa00d8cb3e141727703115e291283fc3ea7120f7
This commit is contained in:
commit
32f245d526
@ -1,120 +1,123 @@
|
|||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
import syncplay
|
import syncplay
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
from syncplay import utils
|
from syncplay import utils
|
||||||
|
|
||||||
class ConsoleUI(threading.Thread):
|
class ConsoleUI(threading.Thread):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.promptMode = threading.Event()
|
self.promptMode = threading.Event()
|
||||||
self.PromptResult = ""
|
self.PromptResult = ""
|
||||||
self.promptMode.set()
|
self.promptMode.set()
|
||||||
self._syncplayClient = None
|
self._syncplayClient = None
|
||||||
threading.Thread.__init__(self, name="ConsoleUI")
|
threading.Thread.__init__(self, name="ConsoleUI")
|
||||||
|
|
||||||
def addClient(self, client):
|
def addClient(self, client):
|
||||||
self._syncplayClient = client
|
self._syncplayClient = client
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while True:
|
while True:
|
||||||
data = raw_input()
|
data = raw_input()
|
||||||
data = data.rstrip('\n\r')
|
data = data.rstrip('\n\r')
|
||||||
if(not self.promptMode.isSet()):
|
if(not self.promptMode.isSet()):
|
||||||
self.PromptResult = data
|
self.PromptResult = data
|
||||||
self.promptMode.set()
|
self.promptMode.set()
|
||||||
elif(self._syncplayClient):
|
elif(self._syncplayClient):
|
||||||
self._executeCommand(data)
|
self._executeCommand(data)
|
||||||
|
|
||||||
def promptFor(self, prompt=">", message=""):
|
def promptFor(self, prompt=">", message=""):
|
||||||
if message <> "":
|
if message <> "":
|
||||||
print(message)
|
print(message)
|
||||||
self.promptMode.clear()
|
self.promptMode.clear()
|
||||||
print(prompt, end='')
|
print(prompt, end='')
|
||||||
self.promptMode.wait()
|
self.promptMode.wait()
|
||||||
return self.PromptResult
|
return self.PromptResult
|
||||||
|
|
||||||
def showMessage(self, message, noTimestamp=False):
|
def showMessage(self, message, noTimestamp=False):
|
||||||
if(os.name == "nt"):
|
if(os.name == "nt"):
|
||||||
message = message.encode('ascii', 'replace')
|
message = message.encode('ascii', 'replace')
|
||||||
if(noTimestamp):
|
if(noTimestamp):
|
||||||
print(message)
|
print(message)
|
||||||
else:
|
else:
|
||||||
print(time.strftime("[%X] ", time.localtime()) + message)
|
print(time.strftime("[%X] ", time.localtime()) + message)
|
||||||
|
|
||||||
def showDebugMessage(self, message):
|
def showDebugMessage(self, message):
|
||||||
print(message)
|
print(message)
|
||||||
|
|
||||||
def showErrorMessage(self, message):
|
def showErrorMessage(self, message):
|
||||||
print("ERROR:\t" + message)
|
print("ERROR:\t" + message)
|
||||||
|
|
||||||
def _extractRegexSign(self, m):
|
def _extractRegexSign(self, m):
|
||||||
if(m.group(1)):
|
if(m):
|
||||||
if(m.group(1) == "-"):
|
if(m == "-"):
|
||||||
return -1
|
return -1
|
||||||
else:
|
else:
|
||||||
return 1
|
return 1
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def _tryAdvancedCommands(self, data):
|
def _tryAdvancedCommands(self, data):
|
||||||
o = re.match(r"^(?:o|offset)\ ([+-])?\ ?(\d+[:\.]?)+$", data)
|
o = re.match(r"^(?:o|offset)\ ?(?P<sign>[/+-])?(?P<time>\d+(?::(?:\d+)){0,2}(?:\.(?:\d+))?)$", data)
|
||||||
s = re.match(r"^(?:s|seek)?\ ?([+-])?\ ?(\d+[:\.]?)+$", data) #careful! s will match o as well
|
s = re.match(r"^(?:s|seek)?\ ?(?P<sign>[+-])?(?P<time>\d+(?::(?:\d+)){0,2}(?:\.(?:\d+))?)$", data)
|
||||||
if(o):
|
if(o):
|
||||||
sign = self._extractRegexSign(o)
|
sign = self._extractRegexSign(o.group('sign'))
|
||||||
t = utils.parseTime(o.group(2))
|
t = utils.parseTime(o.group('time'))
|
||||||
if(t is None):
|
if(t is None):
|
||||||
return
|
return
|
||||||
if(sign):
|
if(sign):
|
||||||
t = self._syncplayClient.getUserOffset() + sign * t
|
if (o.group('sign') == "/"):
|
||||||
self._syncplayClient.setUserOffset(t)
|
t = t - self._syncplayClient.getPlayerPosition()
|
||||||
return True
|
else:
|
||||||
elif s:
|
t = self._syncplayClient.getUserOffset() + sign * t
|
||||||
sign = self._extractRegexSign(s)
|
self._syncplayClient.setUserOffset(t)
|
||||||
t = utils.parseTime(s.group(2))
|
return True
|
||||||
if(t is None):
|
elif s:
|
||||||
return
|
sign = self._extractRegexSign(s.group('sign'))
|
||||||
if(sign):
|
t = utils.parseTime(s.group('time'))
|
||||||
t = self._syncplayClient.getGlobalPosition() + sign * t
|
if(t is None):
|
||||||
self._syncplayClient.setPosition(t)
|
return
|
||||||
return True
|
if(sign):
|
||||||
return False
|
t = self._syncplayClient.getGlobalPosition() + sign * t
|
||||||
|
self._syncplayClient.setPosition(t)
|
||||||
def _executeCommand(self, data):
|
return True
|
||||||
command = re.match(r"^([^\ ]+)(?:\ (.+))?", data)
|
return False
|
||||||
if(not command):
|
|
||||||
return
|
def _executeCommand(self, data):
|
||||||
if(command.group(1) in ["u", "undo", "revert"]):
|
command = re.match(r"^(?P<command>[^\ ]+)(?:\ (?P<parameter>.+))?", data)
|
||||||
tmp_pos = self._syncplayClient.getPlayerPosition()
|
if(not command):
|
||||||
self._syncplayClient.setPosition(self._syncplayClient.playerPositionBeforeLastSeek)
|
return
|
||||||
self._syncplayClient.playerPositionBeforeLastSeek = tmp_pos
|
if(command.group('command') in ["u", "undo", "revert"]):
|
||||||
elif (command.group(1) in ["l", "list", "users"]):
|
tmp_pos = self._syncplayClient.getPlayerPosition()
|
||||||
self._syncplayClient.getUserList()
|
self._syncplayClient.setPosition(self._syncplayClient.playerPositionBeforeLastSeek)
|
||||||
elif (command.group(1) in ["p", "play", "pause"]):
|
self._syncplayClient.playerPositionBeforeLastSeek = tmp_pos
|
||||||
self._syncplayClient.setPaused(not self._syncplayClient.getPlayerPaused())
|
elif (command.group('command') in ["l", "list", "users"]):
|
||||||
elif (command.group(1) in ["r", "room"]):
|
self._syncplayClient.getUserList()
|
||||||
room = command.group(2)
|
elif (command.group('command') in ["p", "play", "pause"]):
|
||||||
if room == None:
|
self._syncplayClient.setPaused(not self._syncplayClient.getPlayerPaused())
|
||||||
if self._syncplayClient.userlist.currentUser.file:
|
elif (command.group('command') in ["r", "room"]):
|
||||||
room = self._syncplayClient.userlist.currentUser.file["name"]
|
room = command.group('parameter')
|
||||||
else:
|
if room == None:
|
||||||
room = self._syncplayClient.defaultRoom
|
if self._syncplayClient.userlist.currentUser.file:
|
||||||
|
room = self._syncplayClient.userlist.currentUser.file["name"]
|
||||||
self._syncplayClient.setRoom(room)
|
else:
|
||||||
self._syncplayClient.sendRoom()
|
room = self._syncplayClient.defaultRoom
|
||||||
else:
|
|
||||||
if(self._tryAdvancedCommands(data)):
|
self._syncplayClient.setRoom(room)
|
||||||
return
|
self._syncplayClient.sendRoom()
|
||||||
if (command.group(1) not in ['help', 'h', '?', '/?', '\?']):
|
else:
|
||||||
self.showMessage("Unrecognized command")
|
if(self._tryAdvancedCommands(data)):
|
||||||
self.showMessage("Available commands:", True)
|
return
|
||||||
self.showMessage("\tr [name] - change room", True)
|
if (command.group('command') not in ['help', 'h', '?', '/?', '\?']):
|
||||||
self.showMessage("\tl - show user list", True)
|
self.showMessage("Unrecognized command")
|
||||||
self.showMessage("\tu - undo last seek", True)
|
self.showMessage("Available commands:", True)
|
||||||
self.showMessage("\tp - toggle pause", True)
|
self.showMessage("\tr [name] - change room", True)
|
||||||
self.showMessage("\t[s][+-][time] - seek to the given value of time, if + or - is not specified it's absolute time in seconds or min:sec", True)
|
self.showMessage("\tl - show user list", True)
|
||||||
self.showMessage("\th - this help", True)
|
self.showMessage("\tu - undo last seek", True)
|
||||||
self.showMessage("Syncplay version: {}".format(syncplay.version), True)
|
self.showMessage("\tp - toggle pause", True)
|
||||||
self.showMessage("More info available at: {}".format(syncplay.projectURL), True)
|
self.showMessage("\t[s][+-][time] - seek to the given value of time, if + or - is not specified it's absolute time in seconds or min:sec", True)
|
||||||
|
self.showMessage("\th - this help", True)
|
||||||
|
self.showMessage("Syncplay version: {}".format(syncplay.version), True)
|
||||||
|
self.showMessage("More info available at: {}".format(syncplay.projectURL), True)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user