coding standards

This commit is contained in:
Yann Leboulanger 2012-08-22 12:21:45 +02:00
parent f801d33b53
commit 133593e1ca
3 changed files with 35 additions and 38 deletions

View File

@ -54,7 +54,8 @@ class JingleFileTransfer(JingleContent):
# events we might be interested in
self.callbacks['session-initiate'] += [self.__on_session_initiate]
self.callbacks['session-initiate-sent'] += [self.__on_session_initiate_sent]
self.callbacks['session-initiate-sent'] += [
self.__on_session_initiate_sent]
self.callbacks['content-add'] += [self.__on_session_initiate]
self.callbacks['session-accept'] += [self.__on_session_accept]
self.callbacks['session-terminate'] += [self.__on_session_terminate]
@ -105,7 +106,7 @@ class JingleFileTransfer(JingleContent):
STATE_TRANSFERING : StateTransfering(self),
STATE_TRANSPORT_REPLACE : StateTransportReplace(self),
STATE_CAND_SENT_AND_RECEIVED : StateCandSentAndRecv(self)
}
}
def __state_changed(self, nextstate, args=None):
# Executes the next state action and sets the next state
@ -130,9 +131,8 @@ class JingleFileTransfer(JingleContent):
def __send_hash(self):
# Send hash in a session info
checksum = xmpp.Node(tag='checksum',
payload=[xmpp.Node(tag='file',
payload=[self._calcHash()])])
checksum = xmpp.Node(tag='checksum', payload=[xmpp.Node(tag='file',
payload=[self._calcHash()])])
checksum.setNamespace(xmpp.NS_JINGLE_FILE_TRANSFER)
self.session.__session_info(checksum )

View File

@ -17,11 +17,11 @@ from jingle_transport import *
from common.socks5 import Socks5ReceiverClient, Socks5SenderClient
class JingleFileTransferStates:
# This class implements the state machine design pattern
'''
This class implements the state machine design pattern
'''
def __init__(self, jingleft):
self.jft = jingleft
def action(self, args=None):
@ -32,7 +32,6 @@ class JingleFileTransferStates:
class StateInitialized(JingleFileTransferStates):
'''
This state initializes the file transfer
'''
@ -40,21 +39,19 @@ class StateInitialized(JingleFileTransferStates):
def action(self, args=None):
if self.jft.weinitiate:
# update connection's fileprops
self.jft._listen_host()
self.jft._listen_host()
# Listen on configured port for file transfer
else:
fingerprint = None
if self.jft.use_security:
fingerprint = 'client'
# Connect to the candidate host, on success call on_connect method
gajim.socks5queue.connect_to_hosts(
self.jft.session.connection.name,
self.jft.file_props.sid, self.jft.on_connect,
self.jft._on_connect_error, fingerprint=fingerprint)
gajim.socks5queue.connect_to_hosts(self.jft.session.connection.name,
self.jft.file_props.sid, self.jft.on_connect,
self.jft._on_connect_error, fingerprint=fingerprint)
class StateCandSent(JingleFileTransferStates):
'''
This state sends our nominated candidate
'''
@ -89,7 +86,6 @@ class StateCandSent(JingleFileTransferStates):
self._sendCand(args)
class StateCandReceived(JingleFileTransferStates):
'''
This state happens when we receive a candidate.
It takes the arguments: canError if we receive a candidate-error
@ -112,13 +108,10 @@ class StateCandReceived(JingleFileTransferStates):
# We save the candidate nominated by peer
self.jft.nominated_cand['peer-cand'] = streamhost_used
def action(self, args=None):
self._recvCand(args)
class StateCandSentAndRecv( StateCandSent, StateCandReceived):
'''
This state happens when we have received and sent the candidates.
It takes the boolean argument: sendCand in order to decide whether
@ -126,14 +119,12 @@ class StateCandSentAndRecv( StateCandSent, StateCandReceived):
'''
def action(self, args=None):
if args['sendCand']:
self._sendCand(args)
else:
self._recvCand(args)
class StateTransportReplace(JingleFileTransferStates):
'''
This state initiates transport replace
'''
@ -142,7 +133,6 @@ class StateTransportReplace(JingleFileTransferStates):
self.jft.session.transport_replace()
class StateTransfering(JingleFileTransferStates):
'''
This state will start the transfer depeding on the type of transport
we have.
@ -150,8 +140,8 @@ class StateTransfering(JingleFileTransferStates):
def __start_IBB_transfer(self, con):
fp = open(self.jft.file_props.file_name, 'r')
con.OpenStream( self.jft.transport.sid,
self.jft.session.peerjid, fp, blocksize=4096)
con.OpenStream( self.jft.transport.sid, self.jft.session.peerjid, fp,
blocksize=4096)
def __start_SOCK5_transfer(self):
# It tells wether we start the transfer as client or server
@ -163,7 +153,7 @@ class StateTransfering(JingleFileTransferStates):
else:
mode = 'server'
streamhost_used = self.jft.nominated_cand['peer-cand']
if streamhost_used['type'] == 'proxy':
self.jft.file_props.is_a_proxy = True
# This needs to be changed when requesting
@ -172,7 +162,8 @@ class StateTransfering(JingleFileTransferStates):
self.jft.file_props.proxy_receiver = streamhost_used['target']
else:
self.jft.file_props.proxy_sender = streamhost_used['target']
self.jft.file_props.proxy_receiver = streamhost_used['initiator']
self.jft.file_props.proxy_receiver = streamhost_used[
'initiator']
# This needs to be changed when requesting
if not self.jft.weinitiate and streamhost_used['type'] == 'proxy':
@ -190,7 +181,7 @@ class StateTransfering(JingleFileTransferStates):
s[sender].connected:
return
if streamhost_used['type'] == 'proxy':
if streamhost_used['type'] == 'proxy':
self.jft.file_props.streamhost_used = True
streamhost_used['sid'] = self.jft.file_props.sid
self.jft.file_props.streamhosts = []

View File

@ -63,8 +63,8 @@ def load_cert_file(cert_path, cert_store):
try:
f = open(cert_path)
except IOError, e:
log.warning('Unable to open certificate file %s: %s' % \
(cert_path, str(e)))
log.warning('Unable to open certificate file %s: %s' % (cert_path,
str(e)))
return
lines = f.readlines()
i = 0
@ -76,14 +76,14 @@ def load_cert_file(cert_path, cert_store):
cert = ''.join(lines[begin:i+2])
try:
x509cert = OpenSSL.crypto.load_certificate(
OpenSSL.crypto.FILETYPE_PEM, cert)
OpenSSL.crypto.FILETYPE_PEM, cert)
cert_store.add_cert(x509cert)
except OpenSSL.crypto.Error, exception_obj:
log.warning('Unable to load a certificate from file %s: %s' %\
(cert_path, exception_obj.args[0][0][2]))
(cert_path, exception_obj.args[0][0][2]))
except:
log.warning('Unknown error while loading certificate from file '
'%s' % cert_path)
'%s' % cert_path)
begin = -1
i += 1
@ -94,7 +94,8 @@ def get_context(fingerprint, verify_cb=None):
ctx = SSL.Context(SSL.TLSv1_METHOD)
if fingerprint == 'server': # for testing purposes only
ctx.set_verify(SSL.VERIFY_NONE|SSL.VERIFY_FAIL_IF_NO_PEER_CERT, verify_cb or default_callback)
ctx.set_verify(SSL.VERIFY_NONE|SSL.VERIFY_FAIL_IF_NO_PEER_CERT,
verify_cb or default_callback)
elif fingerprint == 'client':
ctx.set_verify(SSL.VERIFY_PEER, verify_cb or default_callback)
@ -103,13 +104,15 @@ def get_context(fingerprint, verify_cb=None):
ctx.use_certificate_file(cert_name + '.cert')
store = ctx.get_cert_store()
for f in os.listdir(os.path.expanduser(gajim.MY_PEER_CERTS_PATH)):
load_cert_file(os.path.join(os.path.expanduser(gajim.MY_PEER_CERTS_PATH), f), store)
load_cert_file(os.path.join(os.path.expanduser(
gajim.MY_PEER_CERTS_PATH), f), store)
log.debug('certificate file ' + f + ' loaded fingerprint ' + \
fingerprint)
return ctx
def send_cert(con, jid_from, sid):
certpath = os.path.join(gajim.MY_CERT_DIR, SELF_SIGNED_CERTIFICATE) + '.cert'
certpath = os.path.join(gajim.MY_CERT_DIR, SELF_SIGNED_CERTIFICATE) + \
'.cert'
certfile = open(certpath, 'r')
certificate = ''
for line in certfile.readlines():
@ -225,14 +228,17 @@ def createCertificate(req, (issuerCert, issuerKey), serial, (notBefore, notAfter
def make_certs(filepath, CN):
"""
make self signed certificates
filepath : absolute path of certificate file, will be appended the '.pkey' and '.cert' extensions
filepath : absolute path of certificate file, will be appended the '.pkey'
and '.cert' extensions
CN : common name
"""
key = createKeyPair(TYPE_RSA, 1024)
req = createCertRequest(key, CN=CN)
cert = createCertificate(req, (req, key), 0, (0, 60*60*24*365*5)) # five years
open(filepath + '.pkey', 'w').write(crypto.dump_privatekey(crypto.FILETYPE_PEM, key))
open(filepath + '.cert', 'w').write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert))
open(filepath + '.pkey', 'w').write(crypto.dump_privatekey(
crypto.FILETYPE_PEM, key))
open(filepath + '.cert', 'w').write(crypto.dump_certificate(
crypto.FILETYPE_PEM, cert))
if __name__ == '__main__':