Skip to content

Commit

Permalink
Code quality improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterSurda committed Jun 24, 2017
1 parent 243025a commit 2685fe2
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 115 deletions.
1 change: 0 additions & 1 deletion src/bitmessagemain.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import ctypes
from struct import pack
from subprocess import call
import time

from api import MySimpleXMLRPCRequestHandler, StoppableXMLRPCServer
from helper_startup import isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections
Expand Down
6 changes: 3 additions & 3 deletions src/bitmessageqt/safehtmlparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,20 @@ def reset_safe(self):
self.allow_external_src = False

def add_if_acceptable(self, tag, attrs = None):
if not tag in SafeHTMLParser.acceptable_elements:
if tag not in SafeHTMLParser.acceptable_elements:
return
self.sanitised += "<"
if inspect.stack()[1][3] == "handle_endtag":
self.sanitised += "/"
self.sanitised += tag
if not attrs is None:
if attrs is not None:
for attr, val in attrs:
if tag == "img" and attr == "src" and not self.allow_picture:
val = ""
elif attr == "src" and not self.allow_external_src:
url = urlparse(val)
if url.scheme not in SafeHTMLParser.src_schemes:
val == ""
val = ""
self.sanitised += " " + quote_plus(attr)
if not (val is None):
self.sanitised += "=\"" + val + "\""
Expand Down
13 changes: 6 additions & 7 deletions src/bmconfigparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ def set(self, section, option, value=None):
raise TypeError("option values must be strings")
return ConfigParser.ConfigParser.set(self, section, option, value)

def get(self, section, option, raw=False, vars=None):
def get(self, section, option, raw=False, variables=None):
try:
if section == "bitmessagesettings" and option == "timeformat":
return ConfigParser.ConfigParser.get(self, section, option, raw, vars)
else:
return ConfigParser.ConfigParser.get(self, section, option, True, vars)
return ConfigParser.ConfigParser.get(self, section, option, raw, variables)
return ConfigParser.ConfigParser.get(self, section, option, True, variables)
except ConfigParser.InterpolationError:
return ConfigParser.ConfigParser.get(self, section, option, True, vars)
return ConfigParser.ConfigParser.get(self, section, option, True, variables)
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e:
try:
return BMConfigDefaults[section][option]
Expand All @@ -68,8 +67,8 @@ def safeGet(self, section, option, default = None):
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError, ValueError, AttributeError):
return default

def items(self, section, raw=False, vars=None):
return ConfigParser.ConfigParser.items(self, section, True, vars)
def items(self, section, raw=False, variables=None):
return ConfigParser.ConfigParser.items(self, section, True, variables)

def addresses(self):
return filter(lambda x: x.startswith('BM-'), BMConfigParser().sections())
Expand Down
8 changes: 3 additions & 5 deletions src/network/advanceddispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import asyncore_pollchoose as asyncore
from debug import logger
from bmconfigparser import BMConfigParser

class AdvancedDispatcher(asyncore.dispatcher):
_buf_len = 2097152 # 2MB
Expand Down Expand Up @@ -34,11 +33,10 @@ def slice_read_buf(self, length=0):
def read_buf_sufficient(self, length=0):
if len(self.read_buf) < length:
return False
else:
return True
return True

def process(self):
if self.state != "tls_handshake" and len(self.read_buf) == 0:
if self.state != "tls_handshake" and not self.read_buf:
return
if not self.connected:
return
Expand Down Expand Up @@ -100,7 +98,7 @@ def handle_write(self):
break
if bufSize <= 0:
return
if len(self.write_buf) > 0:
if self.write_buf:
written = self.send(self.write_buf[0:bufSize])
asyncore.update_sent(written)
self.sentBytes += written
Expand Down
5 changes: 0 additions & 5 deletions src/network/announcethread.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Queue
import threading
import time

Expand All @@ -8,7 +7,6 @@
from network.bmproto import BMProto
from network.connectionpool import BMConnectionPool
from network.udp import UDPSocket
import protocol
import state

class AnnounceThread(threading.Thread, StoppableThread):
Expand All @@ -33,6 +31,3 @@ def announceSelf(self):
for stream in state.streamsInWhichIAmParticipating:
addr = (stream, state.Peer('127.0.0.1', BMConfigParser().safeGetInt("bitmessagesettings", "port")), time.time())
connection.writeQueue.put(BMProto.assembleAddr([addr]))

def stopThread(self):
super(AnnounceThread, self).stopThread()
5 changes: 0 additions & 5 deletions src/network/asyncore_pollchoose.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,11 +604,6 @@ def close(self):
# cheap inheritance, used to pass all other attribute
# references to the underlying socket object.
def __getattr__(self, attr):
try:
sys._getframe(200)
logger.error("Stack depth warning")
except ValueError:
pass
try:
retattr = getattr(self.socket, attr)
except AttributeError:
Expand Down
6 changes: 3 additions & 3 deletions src/network/bmobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ def checkProofOfWorkSufficient(self):
def checkEOLSanity(self):
# EOL sanity check
if self.expiresTime - int(time.time()) > BMObject.maxTTL:
logger.info('This object\'s End of Life time is too far in the future. Ignoring it. Time is %s' % self.expiresTime)
logger.info('This object\'s End of Life time is too far in the future. Ignoring it. Time is %i', self.expiresTime)
# TODO: remove from download queue
raise BMObjectExpiredError()

if self.expiresTime - int(time.time()) < BMObject.minTTL:
logger.info('This object\'s End of Life time was too long ago. Ignoring the object. Time is %s' % self.expiresTime)
logger.info('This object\'s End of Life time was too long ago. Ignoring the object. Time is %i', self.expiresTime)
# TODO: remove from download queue
raise BMObjectExpiredError()

def checkStream(self):
if self.streamNumber not in state.streamsInWhichIAmParticipating:
logger.debug('The streamNumber %s isn\'t one we are interested in.' % self.streamNumber)
logger.debug('The streamNumber %i isn\'t one we are interested in.', self.streamNumber)
raise BMObjectUnwantedStreamError()

def checkAlreadyHave(self):
Expand Down
26 changes: 11 additions & 15 deletions src/network/bmproto.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
import network.connectionpool
from network.downloadqueue import DownloadQueue
from network.node import Node
import network.asyncore_pollchoose as asyncore
from network.objectracker import ObjectTracker
from network.proxy import Proxy, ProxyError, GeneralProxyError
from network.uploadqueue import UploadQueue, UploadElem, AddrUploadQueue, ObjUploadQueue

import addresses
from bmconfigparser import BMConfigParser
from queues import objectProcessorQueue, portCheckerQueue, UISignalQueue, invQueue
from queues import objectProcessorQueue, portCheckerQueue, invQueue
import shared
import state
import protocol
Expand Down Expand Up @@ -173,7 +171,6 @@ def decode_payload_content(self, pattern = "v"):

retval = []
size = None
insideDigit = False
i = 0

while i < len(pattern):
Expand Down Expand Up @@ -353,14 +350,13 @@ def bm_command_verack(self):
self.set_state("tls_init", self.payloadLength)
self.bm_proto_reset()
return False
else:
self.set_connection_fully_established()
return True
self.set_connection_fully_established()
return True
return True

def bm_command_version(self):
#self.remoteProtocolVersion, self.services, self.timestamp, padding1, self.myExternalIP, padding2, self.remoteNodeIncomingPort = protocol.VersionPacket.unpack(self.payload[:protocol.VersionPacket.size])
self.remoteProtocolVersion, self.services, self.timestamp, self.sockNode, self.peerNode, self.nonce, self.userAgent, self.streams = self.decode_payload_content("IQQiiQlslv")
self.remoteProtocolVersion, self.services, self.timestamp, self.sockNode, self.peerNode, self.nonce, \
self.userAgent, self.streams = self.decode_payload_content("IQQiiQlslv")
self.nonce = struct.pack('>Q', self.nonce)
self.timeOffset = self.timestamp - int(time.time())
logger.debug("remoteProtocolVersion: %i", self.remoteProtocolVersion)
Expand All @@ -377,7 +373,8 @@ def bm_command_version(self):
self.writeQueue.put(protocol.CreatePacket('verack'))
self.verackSent = True
if not self.isOutbound:
self.writeQueue.put(protocol.assembleVersionMessage(self.destination.host, self.destination.port, network.connectionpool.BMConnectionPool().streams, True))
self.writeQueue.put(protocol.assembleVersionMessage(self.destination.host, self.destination.port, \
network.connectionpool.BMConnectionPool().streams, True))
#print "%s:%i: Sending version" % (self.destination.host, self.destination.port)
if ((self.services & protocol.NODE_SSL == protocol.NODE_SSL) and
protocol.haveSSL(not self.isOutbound)):
Expand All @@ -387,9 +384,8 @@ def bm_command_version(self):
self.set_state("tls_init", self.payloadLength)
self.bm_proto_reset()
return False
else:
self.set_connection_fully_established()
return True
self.set_connection_fully_established()
return True
return True

def peerValidityChecks(self):
Expand All @@ -415,7 +411,7 @@ def peerValidityChecks(self):
return False
else:
shared.timeOffsetWrongCount = 0
if len(self.streams) == 0:
if not self.streams:
self.writeQueue.put(protocol.assembleErrorMessage(fatal=2,
errorText="We don't have shared stream interests. Closing connection."))
logger.debug ('Closed connection to %s because there is no overlapping interest in streams.',
Expand All @@ -442,7 +438,7 @@ def peerValidityChecks(self):

@staticmethod
def assembleAddr(peerList):
if type(peerList) is state.Peer:
if isinstance(peerList, state.Peer):
peerList = (peerList)
# TODO handle max length, now it's done by upper layers
payload = addresses.encodeVarint(len(peerList))
Expand Down
17 changes: 8 additions & 9 deletions src/network/connectionchooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
def chooseConnection(stream):
if state.trustedPeer:
return state.trustedPeer
else:
try:
retval = portCheckerQueue.get(False)
portCheckerQueue.task_done()
except Queue.Empty:
try:
retval = portCheckerQueue.get(False)
portCheckerQueue.task_done()
retval = peerDiscoveryQueue.get(False)
peerDiscoveryQueue.task_done()
except Queue.Empty:
try:
retval = peerDiscoveryQueue.get(False)
peerDiscoveryQueue.task_done()
except Queue.Empty:
return random.choice(knownnodes.knownNodes[stream].keys())
return retval
return random.choice(knownnodes.knownNodes[stream].keys())
return retval
1 change: 0 additions & 1 deletion src/network/connectionpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import network.asyncore_pollchoose as asyncore
import protocol
from singleton import Singleton
import shared
import state

@Singleton
Expand Down
6 changes: 1 addition & 5 deletions src/network/downloadthread.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Queue
import threading

import addresses
Expand Down Expand Up @@ -30,7 +29,7 @@ def run(self):
continue
# keys with True values in the dict
request = list((k for k, v in i.objectsNewToMe.iteritems() if v))
if len(request) == 0:
if not request:
continue
if len(request) > DownloadThread.requestChunk - downloadPending:
request = request[:DownloadThread.requestChunk - downloadPending]
Expand All @@ -43,6 +42,3 @@ def run(self):
logger.debug("%s:%i Requesting %i objects", i.destination.host, i.destination.port, len(request))
requested += len(request)
self.stop.wait(1)

def stopThread(self):
super(DownloadThread, self).stopThread()
16 changes: 8 additions & 8 deletions src/network/invthread.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ def run(self):
try:
data = invQueue.get(False)
if len(data) == 2:
BMConnectionPool().handleReceivedObject(data[0], data[1])
BMConnectionPool().handleReceivedObject(data[0], data[1])
else:
BMConnectionPool().handleReceivedObject(data[0], data[1], data[2])
BMConnectionPool().handleReceivedObject(data[0], data[1], data[2])
self.holdHash (data[0], data[1])
except Queue.Empty:
break

if len(self.collectionOfInvs[iterator]) > 0:
if self.collectionOfInvs[iterator]:
for connection in BMConnectionPool().inboundConnections.values() + BMConnectionPool().outboundConnections.values():
hashes = []
for stream in connection.streams:
Expand All @@ -57,23 +57,23 @@ def run(self):
pass
except KeyError:
continue
if len(hashes) > 0:
if hashes:
connection.writeQueue.put(protocol.CreatePacket('inv', addresses.encodeVarint(len(hashes)) + "".join(hashes)))
self.collectionOfInvs[iterator] = {}
iterator += 1
iterator %= InvThread.size
self.stop.wait(1)

def holdHash(self, stream, hash):
def holdHash(self, stream, hashId):
i = random.randrange(0, InvThread.size)
if stream not in self.collectionOfInvs[i]:
self.collectionOfInvs[i][stream] = []
self.collectionOfInvs[i][stream].append(hash)
self.collectionOfInvs[i][stream].append(hashId)

def hasHash(self, hash):
def hasHash(self, hashId):
for streamlist in self.collectionOfInvs:
for stream in streamlist:
if hash in streamlist[stream]:
if hashId in streamlist[stream]:
return True
return False

Expand Down
23 changes: 8 additions & 15 deletions src/network/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,19 @@ def connectedHostsList():
if BMConfigParser().get("network", "asyncore"):
retval = []
for i in BMConnectionPool().inboundConnections.values() + BMConnectionPool().outboundConnections.values():
if not i.connected:
if not i.fullyEstablished:
continue
try:
retval.append((i.destination, i.streams[0]))
except AttributeError:
pass
return retval
else:
return shared.connectedHostsList.items()
return shared.connectedHostsList.items()

def sentBytes():
if BMConfigParser().get("network", "asyncore"):
return asyncore.sentBytes
else:
return throttle.SendThrottle().total
return throttle.SendThrottle().total

def uploadSpeed():
global lastSentTimestamp, lastSentBytes, currentSentSpeed
Expand All @@ -44,14 +42,12 @@ def uploadSpeed():
lastSentBytes = currentSentBytes
lastSentTimestamp = currentTimestamp
return currentSentSpeed
else:
return throttle.sendThrottle().getSpeed()
return throttle.sendThrottle().getSpeed()

def receivedBytes():
if BMConfigParser().get("network", "asyncore"):
return asyncore.receivedBytes
else:
return throttle.ReceiveThrottle().total
return throttle.ReceiveThrottle().total

def downloadSpeed():
global lastReceivedTimestamp, lastReceivedBytes, currentReceivedSpeed
Expand All @@ -63,8 +59,7 @@ def downloadSpeed():
lastReceivedBytes = currentReceivedBytes
lastReceivedTimestamp = currentTimestamp
return currentReceivedSpeed
else:
return throttle.ReceiveThrottle().getSpeed()
return throttle.ReceiveThrottle().getSpeed()

def pendingDownload():
if BMConfigParser().get("network", "asyncore"):
Expand All @@ -73,8 +68,7 @@ def pendingDownload():
for k in connection.objectsNewToMe.keys():
tmp[k] = True
return len(tmp)
else:
return PendingDownloadQueue.totalSize()
return PendingDownloadQueue.totalSize()

def pendingUpload():
if BMConfigParser().get("network", "asyncore"):
Expand All @@ -84,5 +78,4 @@ def pendingUpload():
for k in connection.objectsNewToThem.keys():
tmp[k] = True
return len(tmp)
else:
return PendingUpload().len()
return PendingUpload().len()

0 comments on commit 2685fe2

Please sign in to comment.