Small boring cleanups

This commit is contained in:
Tomasz Fluxid Kowalczyk 2012-01-28 13:06:50 +01:00
parent cb7bab4da0
commit e87950716d

View File

@ -7,17 +7,17 @@ from twisted.protocols.basic import LineReceiver
class SyncProtocol(LineReceiver): class SyncProtocol(LineReceiver):
def __init__(self, factory): def __init__(self, factory):
self.factory = factory self._factory = factory
self.state = 'init' self._state = 'init'
self.active = False self._active = False
def connectionMade(self): def connectionMade(self):
self.active = True self._active = True
def connectionLost(self, reason): def connectionLost(self, reason):
self.active = False self._active = False
self.factory.remove_watcher(self) self._factory.remove_watcher(self)
def lineReceived(self, line): def lineReceived(self, line):
line = line.strip() line = line.strip()
@ -29,7 +29,7 @@ class SyncProtocol(LineReceiver):
return return
command, arg = line command, arg = line
available_commands = self.states.get(self.state) available_commands = self.states.get(self._state)
if not available_commands: if not available_commands:
return # TODO log it return # TODO log it
@ -42,7 +42,6 @@ class SyncProtocol(LineReceiver):
handler(arg) handler(arg)
# Own
def _get_ident(self): def _get_ident(self):
return '|'.join(( return '|'.join((
@ -58,7 +57,7 @@ class SyncProtocol(LineReceiver):
)) ))
def _drop(self): def _drop(self):
self.active = False self._active = False
self.transport.loseConnection() self.transport.loseConnection()
def _drop_with_error(self, error): def _drop_with_error(self, error):
@ -66,8 +65,8 @@ class SyncProtocol(LineReceiver):
self._drop() self._drop()
def _handle_init_iam(self, arg): def _handle_init_iam(self, arg):
self.factory.add_watcher(self, arg.strip()) self._factory.add_watcher(self, arg.strip())
self.state = 'connected' self._state = 'connected'
def _handle_connected_state(self, arg): def _handle_connected_state(self, arg):
arg = arg.split(None, 1) arg = arg.split(None, 1)
@ -89,7 +88,7 @@ class SyncProtocol(LineReceiver):
position /= 100.0 position /= 100.0
self.factory.update_state(self, paused, position) self._factory.update_state(self, paused, position)
def _handle_connected_seek(self, arg): def _handle_connected_seek(self, arg):
try: try:
@ -99,7 +98,11 @@ class SyncProtocol(LineReceiver):
position /= 100.0 position /= 100.0
self.factory.seek(self, position) self._factory.seek(self, position)
def __hash__(self):
return hash(self._get_ident())
def send_state(self, paused, position, who_last_changed): def send_state(self, paused, position, who_last_changed):
self._send('state', ('paused' if paused else 'playing'), int(position*100), who_last_changed) self._send('state', ('paused' if paused else 'playing'), int(position*100), who_last_changed)
@ -107,6 +110,7 @@ class SyncProtocol(LineReceiver):
def send_seek(self, position, who_seeked): def send_seek(self, position, who_seeked):
self._send('seek', int(position*100), who_seeked) self._send('seek', int(position*100), who_seeked)
states = dict( states = dict(
init = dict( init = dict(
iam = '_handle_init_iam', iam = '_handle_init_iam',
@ -118,9 +122,6 @@ class SyncProtocol(LineReceiver):
), ),
) )
def __hash__(self):
return hash(self._get_ident())
class WatcherInfo(object): class WatcherInfo(object):
def __init__(self, watcher_proto, name): def __init__(self, watcher_proto, name):
@ -152,7 +153,6 @@ class SyncFactory(Factory):
def buildProtocol(self, addr): def buildProtocol(self, addr):
return SyncProtocol(self) return SyncProtocol(self)
# Own
def add_watcher(self, watcher_proto, name): def add_watcher(self, watcher_proto, name):
watcher = WatcherInfo(watcher_proto, name) watcher = WatcherInfo(watcher_proto, name)
@ -196,8 +196,9 @@ class SyncFactory(Factory):
self._send_state_to(receiver, position, curtime) self._send_state_to(receiver, position, curtime)
def seek(self, watcher_proto, position): def seek(self, watcher_proto, position):
pass #TODO
#for receiver in self.watchers.itervalues(): #for receiver in self.watchers.itervalues():
pass
def _send_state_to(self, watcher, position=None, curtime=None): def _send_state_to(self, watcher, position=None, curtime=None):
if position is None: if position is None: