Code quality improvements

This commit is contained in:
Peter Šurda 2017-06-24 12:13:35 +02:00
parent 243025a1aa
commit 2685fe29b1
Signed by untrusted user: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87
17 changed files with 65 additions and 115 deletions

View File

@ -27,7 +27,6 @@ import socket
import ctypes
from struct import pack
from subprocess import call
import time
from api import MySimpleXMLRPCRequestHandler, StoppableXMLRPCServer
from helper_startup import isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections

View File

@ -53,20 +53,20 @@ class SafeHTMLParser(HTMLParser):
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 + "\""

View File

@ -36,14 +36,13 @@ class BMConfigParser(ConfigParser.SafeConfigParser):
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]
@ -68,8 +67,8 @@ class BMConfigParser(ConfigParser.SafeConfigParser):
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())

View File

@ -5,7 +5,6 @@ import time
import asyncore_pollchoose as asyncore
from debug import logger
from bmconfigparser import BMConfigParser
class AdvancedDispatcher(asyncore.dispatcher):
_buf_len = 2097152 # 2MB
@ -34,11 +33,10 @@ class AdvancedDispatcher(asyncore.dispatcher):
def read_buf_sufficient(self, length=0):
if len(self.read_buf) < length:
return False
else:
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
@ -100,7 +98,7 @@ class AdvancedDispatcher(asyncore.dispatcher):
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

View File

@ -1,4 +1,3 @@
import Queue
import threading
import time
@ -8,7 +7,6 @@ from helper_threading import StoppableThread
from network.bmproto import BMProto
from network.connectionpool import BMConnectionPool
from network.udp import UDPSocket
import protocol
import state
class AnnounceThread(threading.Thread, StoppableThread):
@ -33,6 +31,3 @@ class AnnounceThread(threading.Thread, StoppableThread):
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()

View File

@ -604,11 +604,6 @@ class dispatcher:
# 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:

View File

@ -51,18 +51,18 @@ class BMObject(object):
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):

View File

@ -15,14 +15,12 @@ from network.bmobject import BMObject, BMObjectInsufficientPOWError, BMObjectInv
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
@ -173,7 +171,6 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
retval = []
size = None
insideDigit = False
i = 0
while i < len(pattern):
@ -353,14 +350,13 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
self.set_state("tls_init", self.payloadLength)
self.bm_proto_reset()
return False
else:
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)
@ -377,7 +373,8 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
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)):
@ -387,7 +384,6 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
self.set_state("tls_init", self.payloadLength)
self.bm_proto_reset()
return False
else:
self.set_connection_fully_established()
return True
return True
@ -415,7 +411,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
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.',
@ -442,7 +438,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
@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))

View File

@ -9,7 +9,6 @@ import state
def chooseConnection(stream):
if state.trustedPeer:
return state.trustedPeer
else:
try:
retval = portCheckerQueue.get(False)
portCheckerQueue.task_done()

View File

@ -15,7 +15,6 @@ from network.connectionchooser import chooseConnection
import network.asyncore_pollchoose as asyncore
import protocol
from singleton import Singleton
import shared
import state
@Singleton

View File

@ -1,4 +1,3 @@
import Queue
import threading
import addresses
@ -30,7 +29,7 @@ class DownloadThread(threading.Thread, StoppableThread):
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]
@ -43,6 +42,3 @@ class DownloadThread(threading.Thread, StoppableThread):
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()

View File

@ -43,7 +43,7 @@ class InvThread(threading.Thread, StoppableThread):
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:
@ -57,23 +57,23 @@ class InvThread(threading.Thread, StoppableThread):
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

View File

@ -18,20 +18,18 @@ 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()
def sentBytes():
if BMConfigParser().get("network", "asyncore"):
return asyncore.sentBytes
else:
return throttle.SendThrottle().total
def uploadSpeed():
@ -44,13 +42,11 @@ def uploadSpeed():
lastSentBytes = currentSentBytes
lastSentTimestamp = currentTimestamp
return currentSentSpeed
else:
return throttle.sendThrottle().getSpeed()
def receivedBytes():
if BMConfigParser().get("network", "asyncore"):
return asyncore.receivedBytes
else:
return throttle.ReceiveThrottle().total
def downloadSpeed():
@ -63,7 +59,6 @@ def downloadSpeed():
lastReceivedBytes = currentReceivedBytes
lastReceivedTimestamp = currentTimestamp
return currentReceivedSpeed
else:
return throttle.ReceiveThrottle().getSpeed()
def pendingDownload():
@ -73,7 +68,6 @@ def pendingDownload():
for k in connection.objectsNewToMe.keys():
tmp[k] = True
return len(tmp)
else:
return PendingDownloadQueue.totalSize()
def pendingUpload():
@ -84,5 +78,4 @@ def pendingUpload():
for k in connection.objectsNewToThem.keys():
tmp[k] = True
return len(tmp)
else:
return PendingUpload().len()

View File

@ -63,20 +63,18 @@ class TLSDispatcher(AdvancedDispatcher):
def writable(self):
try:
if self.tlsStarted and not self.tlsDone and len(self.write_buf) == 0 and self.writeQueue.empty():
if self.tlsStarted and not self.tlsDone and not self.write_buf and self.writeQueue.empty():
#print "tls writable, %r" % (self.want_write)
return self.want_write
else:
return AdvancedDispatcher.writable(self)
except AttributeError:
return AdvancedDispatcher.writable(self)
def readable(self):
try:
if self.tlsStarted and not self.tlsDone and len(self.write_buf) == 0 and self.writeQueue.empty():
if self.tlsStarted and not self.tlsDone and not self.write_buf and self.writeQueue.empty():
#print "tls readable, %r" % (self.want_read)
return self.want_read
else:
return AdvancedDispatcher.readable(self)
except AttributeError:
return AdvancedDispatcher.readable(self)
@ -84,7 +82,7 @@ class TLSDispatcher(AdvancedDispatcher):
def handle_read(self):
try:
# wait for write buffer flush
if self.tlsStarted and not self.tlsDone and len(self.write_buf) == 0 and self.writeQueue.empty():
if self.tlsStarted and not self.tlsDone and not self.write_buf and self.writeQueue.empty():
#print "handshaking (read)"
self.tls_handshake()
else:
@ -104,7 +102,7 @@ class TLSDispatcher(AdvancedDispatcher):
def handle_write(self):
try:
# wait for write buffer flush
if self.tlsStarted and not self.tlsDone and len(self.write_buf) == 0 and self.writeQueue.empty():
if self.tlsStarted and not self.tlsDone and not self.write_buf and self.writeQueue.empty():
#print "handshaking (write)"
self.tls_handshake()
else:
@ -123,13 +121,13 @@ class TLSDispatcher(AdvancedDispatcher):
def tls_handshake(self):
# wait for flush
if len(self.write_buf) > 0:
if self.write_buf:
return False
# Perform the handshake.
try:
#print "handshaking (internal)"
self.sslSocket.do_handshake()
except ssl.SSLError, err:
except ssl.SSLError as err:
#print "%s:%i: handshake fail" % (self.destination.host, self.destination.port)
self.want_read = self.want_write = False
if err.args[0] == ssl.SSL_ERROR_WANT_READ:

View File

@ -1,32 +1,15 @@
import base64
from binascii import hexlify
import hashlib
import math
import time
import Queue
import socket
import struct
import random
import traceback
from addresses import calculateInventoryHash
from debug import logger
from inventory import Inventory
import knownnodes
from network.advanceddispatcher import AdvancedDispatcher
from network.bmproto import BMProtoError, BMProtoInsufficientDataError, BMProtoExcessiveDataError, BMProto
from network.bmobject import BMObject, BMObjectInsufficientPOWError, BMObjectInvalidDataError, BMObjectExpiredError, BMObjectUnwantedStreamError, BMObjectInvalidError, BMObjectAlreadyHaveError
import network.connectionpool
from network.downloadqueue import DownloadQueue
from network.node import Node
from network.bmproto import BMProtoError, BMProtoInsufficientDataError, BMProto
from network.bmobject import BMObject, BMObjectInsufficientPOWError, BMObjectInvalidDataError, BMObjectExpiredError, BMObjectInvalidError, BMObjectAlreadyHaveError
import network.asyncore_pollchoose as asyncore
from network.objectracker import ObjectTracker
from network.uploadqueue import UploadQueue, UploadElem, AddrUploadQueue, ObjUploadQueue
import addresses
from bmconfigparser import BMConfigParser
from queues import objectProcessorQueue, peerDiscoveryQueue, portCheckerQueue, UISignalQueue
import shared
from queues import objectProcessorQueue, peerDiscoveryQueue, UISignalQueue
import state
import protocol
@ -35,7 +18,7 @@ class UDPSocket(BMProto):
announceInterval = 60
def __init__(self, host=None, sock=None):
BMProto.__init__(self, sock)
super(BMProto, self).__init__(sock=sock)
self.verackReceived = True
self.verackSent = True
# TODO sort out streams

View File

@ -47,7 +47,7 @@ class SqliteInventory(InventoryStorage):
with self.lock:
return len(self._inventory) + sqlQuery('SELECT count(*) FROM inventory')[0][0]
def by_type_and_tag(self, type, tag):
def by_type_and_tag(self, objectType, tag):
with self.lock:
values = [value for value in self._inventory.values() if value.type == type and value.tag == tag]
values += (InventoryItem(*value) for value in sqlQuery('SELECT objecttype, streamnumber, payload, expirestime, tag FROM inventory WHERE objecttype=? AND tag=?', type, tag))

View File

@ -30,7 +30,7 @@ class InventoryStorage(Storage, collections.MutableMapping):
def __len__(self):
raise NotImplementedError
def by_type_and_tag(self, type, tag):
def by_type_and_tag(self, objectType, tag):
raise NotImplementedError
def hashes_by_stream(self, stream):