From 1cd05812a97d125d33641c83ce8dc87644738e80 Mon Sep 17 00:00:00 2001 From: Uriziel Date: Wed, 19 Sep 2012 17:04:57 +0200 Subject: [PATCH] Added proper inheritance of clients Unified client executable --- syncplay/SyncplayMPC.py | 25 +++++++++++++++++ syncplay/SyncplayMplayer.py | 22 +++++++++++++++ syncplay/client.py | 39 ++++++++++++++++++++------ syncplay/localtesting.py | 56 ------------------------------------- syncplay/ui/__init__.py | 2 +- syncplayClient.py | 13 +++++++++ syncplay_mpc.py | 31 -------------------- syncplay_mplayer.py | 31 -------------------- 8 files changed, 91 insertions(+), 128 deletions(-) create mode 100644 syncplay/SyncplayMPC.py create mode 100644 syncplay/SyncplayMplayer.py delete mode 100644 syncplay/localtesting.py create mode 100644 syncplayClient.py delete mode 100644 syncplay_mpc.py delete mode 100644 syncplay_mplayer.py diff --git a/syncplay/SyncplayMPC.py b/syncplay/SyncplayMPC.py new file mode 100644 index 0000000..3a48dd7 --- /dev/null +++ b/syncplay/SyncplayMPC.py @@ -0,0 +1,25 @@ +from syncplay.client import SyncplayClient +from syncplay.client import SyncplayClientManager + +from syncplay.players import mpc +from syncplay import utils + +class SyncplayMPC(SyncplayClient): + def __init__(self): + SyncplayClient.__init__(self) + syncplayClient = SyncplayClientManager(self.args.name, lambda m: mpc.run_mpc(m, self.args.mpc_path, self.args.file, self.args._args), self.interface, self.args.debug, self.args.room, self.args.password) + self.interface.addClient(syncplayClient) + syncplayClient.start(self.args.host, self.args.port) + + def _prepareArguments(self): + self.argsGetter = utils.MPCConfigurationGetter() + self.args = self.argsGetter.getConfiguration() + self.argsGetter.saveValuesIntoConfigFile() + + def _promptForMissingArguments(self): + SyncplayClient._promptForMissingArguments(self) + #if(self.args.no_gui) + while (self.args.mpc_path == None): + self.args.mpc_path = self.interface.promptFor(promptName = "Path to mpc-hc.exe", message = "You must supply path to mpc on the first run, it's easier through command line arguments.") + + \ No newline at end of file diff --git a/syncplay/SyncplayMplayer.py b/syncplay/SyncplayMplayer.py new file mode 100644 index 0000000..04da561 --- /dev/null +++ b/syncplay/SyncplayMplayer.py @@ -0,0 +1,22 @@ +from syncplay.client import SyncplayClient +from syncplay.client import SyncplayClientManager + +from syncplay.players import mplayer +from syncplay import utils + +class SyncplayMplayer(SyncplayClient): + def __init__(self): + SyncplayClient.__init__(self) + syncplayClient = SyncplayClientManager(self.args.name, lambda m: mplayer.run_mplayer(m, 'mplayer', self.args._args), self.interface, self.args.debug, self.args.room, self.args.password) + self.interface.addClient(syncplayClient) + syncplayClient.start(self.args.host, self.args.port) + + def _prepareArguments(self): + self.argsGetter = utils.ConfigurationGetter() + self.args = self.argsGetter.getConfiguration() + + def _promptForMissingArguments(self): + SyncplayClient._promptForMissingArguments(self) + + self.args._args.extend(('-slave', '-msglevel', 'all=1:global=4')) + if(self.args.file): self.args._args.extend((self.args.file,)) diff --git a/syncplay/client.py b/syncplay/client.py index 81ebdf8..98ff8c6 100644 --- a/syncplay/client.py +++ b/syncplay/client.py @@ -62,7 +62,7 @@ class SyncClientProtocol(CommandProtocol): who, where, what = args else: who, where, what = args[0], args[1], None - self.__syncplayClient.users.addUser(SyncplayClient.SyncplayUser(who, what, where)) + self.__syncplayClient.users.addUser(SyncplayClientManager.SyncplayUser(who, what, where)) if what: message = '%s is present and is playing \'%s\' in the room: \'%s\'' % (who, what, where) self.__syncplayClient.ui.showMessage(message) @@ -206,7 +206,7 @@ class SyncClientFactory(ClientFactory): def stop_retrying(self): self.retry = False -class SyncplayClient(object): +class SyncplayClientManager(object): def __init__(self, name, make_player, ui, debug, room, password = None): self.users = self.UserList() self.users.currentUser.name = name @@ -490,16 +490,16 @@ class SyncplayClient(object): class UserList(object): def __init__(self): self.users = [] - self.currentUser = SyncplayClient.SyncplayUser() + self.currentUser = SyncplayClientManager.SyncplayUser() def addUser(self, user): - if(not isinstance(user,SyncplayClient.SyncplayUser)): - user = SyncplayClient.SyncplayUser(user) + if(not isinstance(user,SyncplayClientManager.SyncplayUser)): + user = SyncplayClientManager.SyncplayUser(user) self.users.append(user) def removeUser(self, user): - if(not isinstance(user,SyncplayClient.SyncplayUser)): - user = SyncplayClient.SyncplayUser(user) + if(not isinstance(user,SyncplayClientManager.SyncplayUser)): + user = SyncplayClientManager.SyncplayUser(user) self.users[:] = list(itertools.ifilterfalse(lambda x: user.name == x.name, self.users)) def getUsersWithNotMatchingFilenames(self): @@ -514,7 +514,7 @@ class SyncplayClient(object): u.room = room break #did not find a user, add - self.users.append(SyncplayClient.SyncplayUser(username, None, room)) + self.users.append(SyncplayClientManager.SyncplayUser(username, None, room)) def setUsersFilename(self, username, filename): for u in self.users: @@ -522,4 +522,25 @@ class SyncplayClient(object): u.filename = filename break #did not find a user, add - self.users.append(SyncplayClient.SyncplayUser(username, filename, None)) \ No newline at end of file + self.users.append(SyncplayClientManager.SyncplayUser(username, filename, None)) + +from syncplay import ui +from syncplay import utils + +class SyncplayClient(object): + def __init__(self): + self._prepareArguments() + self.interface = ui.getUi(graphical = not self.args.no_gui) + self._promptForMissingArguments() + self.argsGetter.saveValuesIntoConfigFile() + + def _prepareArguments(self): + self.argsGetter = utils.ConfigurationGetter() + self.args = self.argsGetter.getConfiguration() + + def _promptForMissingArguments(self): + #if(self.args.no_gui) + if (self.args.host == None): + self.args.host = self.interface.promptFor(promptName = "Hostname", message = "You must supply hostname on the first run, it's easier through command line arguments.") + if (self.args.name == None): + self.args.name = self.interface.promptFor(promptName = "Username", message = "You must supply username on the first run, it's easier through command line arguments.") diff --git a/syncplay/localtesting.py b/syncplay/localtesting.py deleted file mode 100644 index 316dcd2..0000000 --- a/syncplay/localtesting.py +++ /dev/null @@ -1,56 +0,0 @@ -#coding:utf8 -''' -Monkey patches for testing with emulated network latency and time offset -''' - -from collections import deque -import os -import time -import random - -from twisted.internet import reactor - -from . import network_utils - - -try: - OFFSET = float(os.environ['OFFSET']) -except (ValueError, KeyError): - pass -else: - orig_time = time.time - def new_time(*args, **kwargs): - return orig_time(*args, **kwargs) + OFFSET - time.time = new_time - -try: - LAG_MU = float(os.environ['LAG_MU']) - LAG_SIGMA = float(os.environ['LAG_SIGMA']) -except (ValueError, KeyError): - pass -else: - random.seed() - OriginalCommandProtocol = network_utils.CommandProtocol - - class LaggedCommandProtocol(OriginalCommandProtocol): - def __init__(self, *args, **kwargs): - self._queue_in = deque() - self._queue_out = deque() - OriginalCommandProtocol.__init__(self, *args, **kwargs) - - def lineReceived(self, line): - self._queue_in.append(line) - reactor.callLater( - abs(random.gauss(LAG_MU, LAG_SIGMA)), - lambda: OriginalCommandProtocol.lineReceived(self, self._queue_in.popleft()) - ) - - def sendLine(self, line): - self._queue_out.append(line) - reactor.callLater( - abs(random.gauss(LAG_MU, LAG_SIGMA)), - lambda: OriginalCommandProtocol.sendLine(self, self._queue_out.popleft()) - ) - - network_utils.CommandProtocol = LaggedCommandProtocol - diff --git a/syncplay/ui/__init__.py b/syncplay/ui/__init__.py index 3762faa..e67dbf1 100644 --- a/syncplay/ui/__init__.py +++ b/syncplay/ui/__init__.py @@ -1,7 +1,7 @@ from syncplay.ui.gui import GraphicalUI from syncplay.ui.consoleUI import ConsoleUI def getUi(graphical = True): - if(graphical): + if(False): #graphical): #TODO: Add graphical ui return GraphicalUI() else: ui = ConsoleUI() diff --git a/syncplayClient.py b/syncplayClient.py new file mode 100644 index 0000000..8c00b3c --- /dev/null +++ b/syncplayClient.py @@ -0,0 +1,13 @@ +import os + +class Syncplay(object): + def __init__(self): + if(os.name == 'posix'): + from syncplay.SyncplayMplayer import SyncplayMplayer + SyncplayMplayer() + else: + from syncplay.SyncplayMPC import SyncplayMPC + SyncplayMPC() + +if(__name__ == '__main__'): + Syncplay() \ No newline at end of file diff --git a/syncplay_mpc.py b/syncplay_mpc.py deleted file mode 100644 index 6fdc879..0000000 --- a/syncplay_mpc.py +++ /dev/null @@ -1,31 +0,0 @@ -#coding:utf8 -from syncplay import client -from syncplay.players import mpc -from syncplay import ui -from syncplay import utils - -class SyncplayMPC: - def runClient(self): - self._prepareArguments() -# self.interface = ui.getUi(graphical = not self.args.no_gui) - self.interface = ui.getUi(graphical = False) #TODO: add gui - self._promptForMissingArguments() - syncplayClient = client.SyncplayClient(self.args.name, lambda m: mpc.run_mpc(m, self.args.mpc_path, self.args.file, self.args._args), self.interface, self.args.debug, self.args.room, self.args.password) - self.interface.addClient(syncplayClient) - syncplayClient.start(self.args.host, self.args.port) - def _prepareArguments(self): - self.argsGetter = utils.MPCConfigurationGetter() - self.args = self.argsGetter.getConfiguration() - - def _promptForMissingArguments(self): - if (self.args.host == None): - self.args.host = self.interface.promptFor(promptName = "Hostname", message = "You must supply hostname on the first run, it's easier trough command line arguments.") - if (self.args.name == None): - self.args.name = self.interface.promptFor(promptName = "Username", message = "You must supply username on the first run, it's easier trough command line arguments.") - if (self.args.mpc_path == None): - self.args.mpc_path = self.interface.promptFor(promptName = "Path to mpc-hc.exe", message = "You must supply path to mpc on the first run, it's easier trough command line arguments.") - self.argsGetter.saveValuesIntoConfigFile() - -if __name__ == '__main__': - SyncplayMPC().runClient() - diff --git a/syncplay_mplayer.py b/syncplay_mplayer.py deleted file mode 100644 index 89277f8..0000000 --- a/syncplay_mplayer.py +++ /dev/null @@ -1,31 +0,0 @@ -#coding:utf8 -from syncplay import client -from syncplay.players import mplayer -from syncplay import ui -from syncplay import utils - -class SyncplayMplayer: - def runClient(self): - self._prepareArguments() -# self.interface = ui.getUi(graphical = not self.args.no_gui) - self.interface = ui.getUi(graphical = False) #TODO: add gui - self._promptForMissingArguments() - self.args._args.extend(('-slave', '-msglevel', 'all=1:global=4')) - if(self.args.file): self.args._args.extend((self.args.file,)) - syncplayClient = client.SyncplayClient(self.args.name, lambda m: mplayer.run_mplayer(m, 'mplayer', self.args._args), self.interface, self.args.debug, self.args.room, self.args.password) - self.interface.addClient(syncplayClient) - syncplayClient.start(self.args.host, self.args.port) - def _prepareArguments(self): - self.argsGetter = utils.ConfigurationGetter() - self.args = self.argsGetter.getConfiguration() - - def _promptForMissingArguments(self): - if (self.args.host == None): - self.args.host = self.interface.promptFor(promptName = "Hostname", message = "You must supply hostname on the first run, it's easier through command line arguments.") - if (self.args.name == None): - self.args.name = self.interface.promptFor(promptName = "Username", message = "You must supply username on the first run, it's easier through command line arguments.") - self.argsGetter.saveValuesIntoConfigFile() - -if __name__ == '__main__': - SyncplayMplayer().runClient() -