consoleUI.py - Regex improvements, add rel offset

Update /ui/consoleUI.py: Convert regex to use symbolic groups rather
than IDs; Fix seek/offset regex patterns; Add relative offseting via '/'
sign.
This commit is contained in:
Et0h 2012-12-27 03:28:54 +00:00
parent fa00d8cb3e
commit ed1a270edc

View File

@ -50,8 +50,8 @@ class ConsoleUI(threading.Thread):
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
@ -59,20 +59,23 @@ class ConsoleUI(threading.Thread):
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('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):
if (o.group('sign') == "/"):
t = t - self._syncplayClient.getPlayerPosition()
else:
t = self._syncplayClient.getUserOffset() + sign * t t = self._syncplayClient.getUserOffset() + sign * t
self._syncplayClient.setUserOffset(t) self._syncplayClient.setUserOffset(t)
return True return True
elif s: elif s:
sign = self._extractRegexSign(s) sign = self._extractRegexSign(s.group('sign'))
t = utils.parseTime(s.group(2)) t = utils.parseTime(s.group('time'))
if(t is None): if(t is None):
return return
if(sign): if(sign):
@ -82,19 +85,19 @@ class ConsoleUI(threading.Thread):
return False return False
def _executeCommand(self, data): def _executeCommand(self, data):
command = re.match(r"^([^\ ]+)(?:\ (.+))?", data) command = re.match(r"^(?P<command>[^\ ]+)(?:\ (?P<parameter>.+))?", data)
if(not command): if(not command):
return return
if(command.group(1) in ["u", "undo", "revert"]): if(command.group('command') in ["u", "undo", "revert"]):
tmp_pos = self._syncplayClient.getPlayerPosition() tmp_pos = self._syncplayClient.getPlayerPosition()
self._syncplayClient.setPosition(self._syncplayClient.playerPositionBeforeLastSeek) self._syncplayClient.setPosition(self._syncplayClient.playerPositionBeforeLastSeek)
self._syncplayClient.playerPositionBeforeLastSeek = tmp_pos self._syncplayClient.playerPositionBeforeLastSeek = tmp_pos
elif (command.group(1) in ["l", "list", "users"]): elif (command.group('command') in ["l", "list", "users"]):
self._syncplayClient.getUserList() self._syncplayClient.getUserList()
elif (command.group(1) in ["p", "play", "pause"]): elif (command.group('command') in ["p", "play", "pause"]):
self._syncplayClient.setPaused(not self._syncplayClient.getPlayerPaused()) self._syncplayClient.setPaused(not self._syncplayClient.getPlayerPaused())
elif (command.group(1) in ["r", "room"]): elif (command.group('command') in ["r", "room"]):
room = command.group(2) room = command.group('parameter')
if room == None: if room == None:
if self._syncplayClient.userlist.currentUser.file: if self._syncplayClient.userlist.currentUser.file:
room = self._syncplayClient.userlist.currentUser.file["name"] room = self._syncplayClient.userlist.currentUser.file["name"]
@ -106,7 +109,7 @@ class ConsoleUI(threading.Thread):
else: else:
if(self._tryAdvancedCommands(data)): if(self._tryAdvancedCommands(data)):
return return
if (command.group(1) not in ['help', 'h', '?', '/?', '\?']): if (command.group('command') not in ['help', 'h', '?', '/?', '\?']):
self.showMessage("Unrecognized command") self.showMessage("Unrecognized command")
self.showMessage("Available commands:", True) self.showMessage("Available commands:", True)
self.showMessage("\tr [name] - change room", True) self.showMessage("\tr [name] - change room", True)