Skip to content

Commit f2bb4d1

Browse files
committed
Refactor parent package imports in network module
1 parent 205e253 commit f2bb4d1

19 files changed

+68
-71
lines changed

src/network/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
"""
22
Network subsystem package
33
"""
4+
try:
5+
import networkdepsinterface
6+
except ImportError:
7+
from pybitmessage import networkdepsinterface
8+
49
from .dandelion import Dandelion
510
from .threads import StoppableThread
611

12+
(
13+
state, queues, config, protocol,
14+
randomtrackingdict, addresses, paths) = networkdepsinterface.importParentPackageDepsToNetwork()
15+
716
dandelion_ins = Dandelion()
817

918
__all__ = ["StoppableThread"]
1019

1120

1221
def start(config, state):
1322
"""Start network threads"""
14-
import state
1523
from .announcethread import AnnounceThread
1624
import connectionpool # pylint: disable=relative-import
1725
from .addrthread import AddrThread

src/network/addrthread.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import connectionpool
88
from helper_random import randomshuffle
99
from protocol import assembleAddrMessage
10-
from queues import addrQueue # FIXME: init with queue
10+
from network import queues # FIXME: init with queue
1111

1212
from threads import StoppableThread
1313

@@ -21,7 +21,7 @@ def run(self):
2121
chunk = []
2222
while True:
2323
try:
24-
data = addrQueue.get(False)
24+
data = queues.addrQueue.get(False)
2525
chunk.append(data)
2626
except queue.Empty:
2727
break
@@ -43,7 +43,7 @@ def run(self):
4343
if filtered:
4444
i.append_write_buf(assembleAddrMessage(filtered))
4545

46-
addrQueue.iterate()
46+
queues.addrQueue.iterate()
4747
for i in range(len(chunk)):
48-
addrQueue.task_done()
48+
queues.addrQueue.task_done()
4949
self.stop.wait(1)

src/network/advanceddispatcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import time
77

88
import network.asyncore_pollchoose as asyncore
9-
import state
9+
from network import state
1010
from threads import BusyError, nonBlocking
1111

1212

src/network/announcethread.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# magic imports!
77
import connectionpool
8-
from bmconfigparser import config
8+
from network import config
99
from protocol import assembleAddrMessage
1010

1111
from node import Peer

src/network/bmobject.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import logging
55
import time
66

7-
import protocol
8-
import state
7+
from network import state, protocol
98
import connectionpool
109
from network import dandelion_ins
1110
from highlevelcrypto import calculateInventoryHash

src/network/bmproto.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@
1111
import time
1212

1313
# magic imports!
14-
import addresses
1514
import knownnodes
16-
import protocol
17-
import state
15+
from network import protocol, state, config, queues, addresses, dandelion_ins
1816
import connectionpool
19-
from bmconfigparser import config
20-
from queues import invQueue, objectProcessorQueue, portCheckerQueue
2117
from randomtrackingdict import RandomTrackingDict
2218
from network.advanceddispatcher import AdvancedDispatcher
2319
from network.bmobject import (
@@ -26,7 +22,6 @@
2622
BMObjectUnwantedStreamError
2723
)
2824
from network.proxy import ProxyError
29-
from network import dandelion_ins
3025
from node import Node, Peer
3126
from objectracker import ObjectTracker, missingObjects
3227

@@ -409,7 +404,7 @@ def bm_command_object(self):
409404

410405
try:
411406
self.object.checkObjectByType()
412-
objectProcessorQueue.put((
407+
queues.objectProcessorQueue.put((
413408
self.object.objectType, buffer(self.object.data))) # noqa: F821
414409
except BMObjectInvalidError:
415410
BMProto.stopDownloadingObject(self.object.inventoryHash, True)
@@ -431,7 +426,7 @@ def bm_command_object(self):
431426
)
432427
self.handleReceivedObject(
433428
self.object.streamNumber, self.object.inventoryHash)
434-
invQueue.put((
429+
queues.invQueue.put((
435430
self.object.streamNumber, self.object.inventoryHash,
436431
self.destination))
437432
return True
@@ -472,7 +467,7 @@ def bm_command_addr(self):
472467

473468
def bm_command_portcheck(self):
474469
"""Incoming port check request, queue it."""
475-
portCheckerQueue.put(Peer(self.destination, self.peerNode.port))
470+
queues.portCheckerQueue.put(Peer(self.destination, self.peerNode.port))
476471
return True
477472

478473
def bm_command_ping(self):

src/network/connectionchooser.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
import random
77

88
import knownnodes
9-
import protocol
10-
import state
11-
from bmconfigparser import config
12-
from queues import queue, portCheckerQueue
9+
from network import protocol, state, config, queues
1310

1411
logger = logging.getLogger('default')
1512

@@ -34,10 +31,10 @@ def chooseConnection(stream):
3431
onionOnly = config.safeGetBoolean(
3532
"bitmessagesettings", "onionservicesonly")
3633
try:
37-
retval = portCheckerQueue.get(False)
38-
portCheckerQueue.task_done()
34+
retval = queues.portCheckerQueue.get(False)
35+
queues.portCheckerQueue.task_done()
3936
return retval
40-
except queue.Empty:
37+
except queues.queue.Empty:
4138
pass
4239
# with a probability of 0.5, connect to a discovered peer
4340
if random.choice((False, True)) and not haveOnion: # nosec B311

src/network/connectionpool.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
import asyncore_pollchoose as asyncore
1212
import helper_random
1313
import knownnodes
14-
import protocol
15-
import state
16-
from bmconfigparser import config
14+
from network import protocol, state, config
1715
from connectionchooser import chooseConnection
1816
from node import Peer
1917
from proxy import Proxy

src/network/downloadthread.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
`DownloadThread` class definition
33
"""
44
import time
5-
import state
6-
import addresses
5+
from network import state, protocol, addresses, dandelion_ins
76
import helper_random
8-
import protocol
97
import connectionpool
10-
from network import dandelion_ins
118
from objectracker import missingObjects
129
from threads import StoppableThread
1310

src/network/invthread.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55
import random
66
from time import time
77

8-
import addresses
9-
import protocol
10-
import state
8+
from network import protocol, state, queues, addresses
119
import connectionpool
1210
from network import dandelion_ins
13-
from queues import invQueue
1411
from threads import StoppableThread
1512

1613

@@ -52,9 +49,9 @@ def run(self): # pylint: disable=too-many-branches
5249
chunk = []
5350
while True:
5451
# Dandelion fluff trigger by expiration
55-
handleExpiredDandelion(dandelion_ins.expire(invQueue))
52+
handleExpiredDandelion(dandelion_ins.expire(queues.invQueue))
5653
try:
57-
data = invQueue.get(False)
54+
data = queues.invQueue.get(False)
5855
chunk.append((data[0], data[1]))
5956
# locally generated
6057
if len(data) == 2 or data[2] is None:
@@ -101,9 +98,9 @@ def run(self): # pylint: disable=too-many-branches
10198
addresses.encodeVarint(
10299
len(stems)) + ''.join(stems)))
103100

104-
invQueue.iterate()
101+
queues.invQueue.iterate()
105102
for _ in range(len(chunk)):
106-
invQueue.task_done()
103+
queues.invQueue.task_done()
107104

108105
dandelion_ins.reRandomiseStems()
109106

0 commit comments

Comments
 (0)