coding standards

This commit is contained in:
Yann Leboulanger 2008-11-24 17:00:20 +00:00
parent 482cb1ee1d
commit 04e88bab3c
1 changed files with 142 additions and 145 deletions

View File

@ -106,8 +106,7 @@ class SocksQueue:
return 1
return 0
def connect_to_hosts(self, account, sid, on_success = None,
on_failure = None):
def connect_to_hosts(self, account, sid, on_success=None, on_failure=None):
self.on_success = on_success
self.on_failure = on_failure
file_props = self.files_props[account][sid]
@ -130,7 +129,7 @@ class SocksQueue:
return
# set state -2, meaning that this streamhost is stopped,
# but it may be connectected later
if host['state'] >=0:
if host['state'] >= 0:
self.remove_receiver(host['idx'])
host['idx'] = -1
host['state'] = -2
@ -156,7 +155,8 @@ class SocksQueue:
for host in file_props['streamhosts']:
if host['state'] == -2:
host['state'] = 0
receiver = Socks5Receiver(self.idlequeue, host, host['sid'], file_props)
receiver = Socks5Receiver(self.idlequeue, host, host['sid'],
file_props)
self.add_receiver(receiver.account, receiver)
host['idx'] = receiver.queue_idx
# we still have chances to connect
@ -204,9 +204,7 @@ class SocksQueue:
def get_file_from_sender(self, file_props, account):
if file_props is None:
return
if 'hash' in file_props and \
file_props['hash'] in self.senders:
if 'hash' in file_props and file_props['hash'] in self.senders:
sender = self.senders[file_props['hash']]
sender.account = account
result = self.get_file_contents(0)
@ -244,8 +242,7 @@ class SocksQueue:
self.process_result(result, reader)
def send_file(self, file_props, account):
if 'hash' in file_props and \
file_props['hash'] in self.senders:
if 'hash' in file_props and file_props['hash'] in self.senders:
sender = self.senders[file_props['hash']]
file_props['streamhost-used'] = True
sender.account = account
@ -263,8 +260,7 @@ class SocksQueue:
''' file_prop to the dict of current file_props.
It is identified by account name and sid
'''
if file_props is None or \
('sid' in file_props) is False:
if file_props is None or ('sid' in file_props) is False:
return
_id = file_props['sid']
if account not in self.files_props:
@ -291,8 +287,8 @@ class SocksQueue:
def on_connection_accepted(self, sock):
sock_hash = sock.__hash__()
if sock_hash not in self.senders:
self.senders[sock_hash] = Socks5Sender(self.idlequeue,
sock_hash, self, sock[0], sock[1][0], sock[1][1])
self.senders[sock_hash] = Socks5Sender(self.idlequeue, sock_hash, self,
sock[0], sock[1][0], sock[1][1])
self.connected += 1
def process_result(self, result, actor):
@ -311,7 +307,7 @@ class SocksQueue:
elif self.progress_transfer_cb is not None:
self.progress_transfer_cb(actor.account, actor.file_props)
def remove_receiver(self, idx, do_disconnect = True):
def remove_receiver(self, idx, do_disconnect=True):
''' Remove reciver from the list and decrease
the number of active connections with 1'''
if idx != -1:
@ -326,7 +322,7 @@ class SocksQueue:
reader.streamhost['state'] = -1
del(self.readers[idx])
def remove_sender(self, idx, do_disconnect = True):
def remove_sender(self, idx, do_disconnect=True):
''' Remove sender from the list of senders and decrease the
number of active connections with 1'''
if idx != -1:
@ -348,7 +344,8 @@ class Socks5:
if host is not None:
try:
self.host = host
self.ais = socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM)
self.ais = socket.getaddrinfo(host, port, socket.AF_UNSPEC,
socket.SOCK_STREAM)
except socket.gaierror:
self.ais = None
self.idlequeue = idlequeue
@ -413,7 +410,6 @@ class Socks5:
except Exception:
pass
def receive(self):
''' Reads small chunks of data.
Calls owner's disconnected() method if appropriate.'''
@ -421,8 +417,8 @@ class Socks5:
try:
add = self._recv(64)
except Exception, e:
add=''
received +=add
add = ''
received += add
if len(add) == 0:
self.disconnect()
return add
@ -485,10 +481,8 @@ class Socks5:
return -1
def get_file_contents(self, timeout):
''' read file contents from socket and write them to file ''', \
self.file_props['type'], self.file_props['sid']
if self.file_props is None or \
('file-name' in self.file_props) is False:
''' read file contents from socket and write them to file '''
if self.file_props is None or ('file-name' in self.file_props) is False:
self.file_props['error'] = -2
return None
fd = None
@ -583,6 +577,7 @@ class Socks5:
except Exception:
return None
return auth_mechanisms
def _get_auth_response(self):
''' socks version(5), number of extra auth methods (we send
0x00 - no auth
@ -605,8 +600,8 @@ class Socks5:
def _parse_request_buff(self, buff):
try: # don't trust on what comes from the outside
version, req_type, reserved, host_type, = \
struct.unpack('!BBBB', buff[:4])
version, req_type, reserved, host_type, = struct.unpack('!BBBB',
buff[:4])
if host_type == 0x01:
host_arr = struct.unpack('!iiii', buff[4:8])
host, = '.'.join(str(s) for s in host_arr)
@ -651,11 +646,13 @@ class Socks5:
del(self.file_props['is_a_proxy'])
return sha.new('%s%s%s' % (self.sid, self.file_props['proxy_sender'],
self.file_props['proxy_receiver'])).hexdigest()
return sha.new('%s%s%s' % (self.sid, self.initiator, self.target)).hexdigest()
return sha.new('%s%s%s' % (self.sid, self.initiator, self.target)).\
hexdigest()
class Socks5Sender(Socks5, IdleObject):
''' class for sending file to socket over socks5 '''
def __init__(self, idlequeue, sock_hash, parent, _sock, host = None, port = None):
def __init__(self, idlequeue, sock_hash, parent, _sock, host=None,
port=None):
self.queue_idx = sock_hash
self.queue = parent
Socks5.__init__(self, idlequeue, host, port, None, None, None)
@ -729,8 +726,7 @@ class Socks5Sender(Socks5, IdleObject):
self.disconnect()
elif self.state == 5:
if self.file_props is not None and \
self.file_props['type'] == 'r':
if self.file_props is not None and self.file_props['type'] == 'r':
result = self.get_file_contents(0)
self.queue.process_result(result, self)
else:
@ -776,7 +772,7 @@ class Socks5Sender(Socks5, IdleObject):
self.idlequeue.plug_idle(self, True, False)
return None
def disconnect(self, cb = True):
def disconnect(self, cb=True):
''' Closes the socket. '''
# close connection and remove us from the queue
Socks5.disconnect(self)
@ -871,8 +867,9 @@ class Socks5Receiver(Socks5, IdleObject):
self.file_props['paused'] = False
self.file_props['continue_cb'] = self.continue_paused_transfer
self.file_props['stalled'] = False
Socks5.__init__(self, idlequeue, streamhost['host'], int(streamhost['port']),
streamhost['initiator'], streamhost['target'], sid)
Socks5.__init__(self, idlequeue, streamhost['host'],
int(streamhost['port']), streamhost['initiator'], streamhost['target'],
sid)
def read_timeout(self):
self.idlequeue.remove_timeout(self.fd)
@ -898,10 +895,10 @@ class Socks5Receiver(Socks5, IdleObject):
for ai in self.ais:
try:
self._sock=socket.socket(*ai[:3])
self._sock = socket.socket(*ai[:3])
# this will not block the GUI
self._sock.setblocking(False)
self._server=ai[4]
self._server = ai[4]
break
except Exception:
if sys.exc_value[0] == errno.EINPROGRESS:
@ -919,6 +916,7 @@ class Socks5Receiver(Socks5, IdleObject):
if self.state < 5:
return False
return True
def pollout(self):
self.idlequeue.remove_timeout(self.fd)
if self.state == 0:
@ -965,7 +963,6 @@ class Socks5Receiver(Socks5, IdleObject):
self.idlequeue.set_read_timeout(self.fd, STALLED_TIMEOUT)
result = self.get_file_contents(0)
self.queue.process_result(result, self)
else:
self.disconnect()
@ -1000,7 +997,7 @@ class Socks5Receiver(Socks5, IdleObject):
self.idlequeue.plug_idle(self, True, False)
return 1 # we are connected
def main(self, timeout = 0):
def main(self, timeout=0):
''' begin negotiation. on success 'address' != 0 '''
result = 1
buff = self.receive()
@ -1069,7 +1066,7 @@ class Socks5Receiver(Socks5, IdleObject):
self.state += 1
return None
def disconnect(self, cb = True):
def disconnect(self, cb=True):
''' Closes the socket. Remove self from queue if cb is True'''
# close connection
Socks5.disconnect(self)