Added proper inheritance of clients

Unified client executable
This commit is contained in:
Uriziel 2012-09-19 17:04:57 +02:00
parent 873bd4302b
commit 1cd05812a9
8 changed files with 91 additions and 128 deletions

25
syncplay/SyncplayMPC.py Normal file
View File

@ -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.")

View File

@ -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,))

View File

@ -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))
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.")

View File

@ -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

View File

@ -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()

13
syncplayClient.py Normal file
View File

@ -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()

View File

@ -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()

View File

@ -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()