Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PEP8 fixes for stp_raet, stp_zmq, INDY-493 #325

Merged
merged 2 commits into from
Aug 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 41 additions & 21 deletions stp_raet/rstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import time
from collections import Callable
from collections import OrderedDict
from typing import Any, Set
from typing import Any
from typing import Dict

from raet.raeting import AutoMode, TrnsKind
Expand Down Expand Up @@ -125,7 +125,8 @@ def initLocalKeys(name, baseDir, sigseed, override=False):
rolePath = os.path.join(baseDir, name, "role", "local", "role.json")
if os.path.isfile(rolePath):
if not override:
raise FileExistsError("Keys exists for local role {}".format(name))
raise FileExistsError(
"Keys exists for local role {}".format(name))

if sigseed and not isinstance(sigseed, bytes):
sigseed = sigseed.encode()
Expand All @@ -134,7 +135,7 @@ def initLocalKeys(name, baseDir, sigseed, override=False):
keep = RoadKeep(stackname=name, baseroledirpath=baseDir)
sigkey, verkey = signer.keyhex, signer.verhex
prikey, pubkey = ed25519SkToCurve25519(sigkey, toHex=True), \
ed25519PkToCurve25519(verkey, toHex=True)
ed25519PkToCurve25519(verkey, toHex=True)
data = OrderedDict([
("role", name),
("prihex", prikey),
Expand All @@ -156,8 +157,12 @@ def initRemoteKeys(name, remoteName, baseDir, verkey, override=False):
:param override: overwrite the role.remoteName.json file if it already
exists.
"""
rolePath = os.path.join(baseDir, name, "role", "remote", "role.{}.json".
format(remoteName))
rolePath = os.path.join(
baseDir,
name,
"role",
"remote",
"role.{}.json". format(remoteName))
if os.path.isfile(rolePath):
if not override:
raise FileExistsError("Keys exists for remote role {}".
Expand Down Expand Up @@ -218,7 +223,13 @@ def getRemote(self, name: str = None, ha: HA = None):
return self.findInRemotesByHA(ha) if ha else \
self.findInRemotesByName(name)

def connect(self, name=None, remoteId=None, ha=None, verKeyRaw=None, publicKeyRaw=None):
def connect(
self,
name=None,
remoteId=None,
ha=None,
verKeyRaw=None,
publicKeyRaw=None):
"""
Connect to the node specified by name.

Expand All @@ -232,15 +243,15 @@ def connect(self, name=None, remoteId=None, ha=None, verKeyRaw=None, publicKeyRa
# format(self, name))
# return None
if not remoteId and not ha:
raise ValueError('Either Host Address or Remote ID must be provided to connect to a node in Raet stack')
raise ValueError(
'Either Host Address or Remote ID must be provided to connect to a node in Raet stack')

if remoteId:
remote = self.remotes[remoteId]
return self._doConnectRemote(remote, name)
else:
return self._doConnectByHA(ha, name)


def _doConnectByHA(self, ha, name=None):
remote = RemoteEstate(stack=self.raetStack,
ha=ha)
Expand Down Expand Up @@ -417,7 +428,11 @@ def send(self, msg: Any, remoteName: str, ha=None):


class SimpleRStack(RStack):
def __init__(self, stackParams: Dict, msgHandler: Callable, sighex: str=None):
def __init__(
self,
stackParams: Dict,
msgHandler: Callable,
sighex: str=None):
self.stackParams = stackParams
self.msgHandler = msgHandler
super().__init__(**stackParams, msgHandler=self.msgHandler, sighex=sighex)
Expand All @@ -426,7 +441,6 @@ def start(self):
super().start()



class KITRStack(SimpleRStack, KITNetworkInterface):
# Keep In Touch RStack. RStack which maintains connections mentioned in
# its registry
Expand Down Expand Up @@ -560,16 +574,17 @@ def start(self):
# (no need to connect to itself)
del self.registry[self.name]


def addRemote(self, remote, dump=False):
if not self.findInNodeRegByHA(remote.ha):
logger.debug('Remote {} with HA {} not added -> not found in registry'.format(remote.name, remote.ha))
logger.debug(
'Remote {} with HA {} not added -> not found in registry'.format(remote.name, remote.ha))
return
return self.raetStack.addRemote(remote, dump)

def createRemote(self, ha):
if ha and not self.findInNodeRegByHA(ha):
logger.debug('Remote with HA {} not added -> not found in registry'.format(ha))
logger.debug(
'Remote with HA {} not added -> not found in registry'.format(ha))
return
return super(KITRStack, self).createRemote(ha)

Expand All @@ -578,18 +593,18 @@ def processRx(self, packet):
# of unnecessary JSON files for remotes
tk = packet.data['tk']

if tk in [TrnsKind.join]: # join transaction
sha = (packet.data['sh'], packet.data['sp'])
if tk in [TrnsKind.join]: # join transaction
sha = (packet.data['sh'], packet.data['sp'])
if not self.findInNodeRegByHA(sha):
return self.handleJoinFromUnregisteredRemote(sha)

return super(KITRStack, self).processRx(packet)

def handleJoinFromUnregisteredRemote(self, sha):
logger.debug('Remote with HA {} not added -> not found in registry'.format(sha))
logger.debug(
'Remote with HA {} not added -> not found in registry'.format(sha))
return None


def connectToMissing(self, currentTime):
"""
Try to connect to the missing node within the time specified by
Expand Down Expand Up @@ -627,7 +642,8 @@ def handleDisconnectedRemote(self, cur, disconn):
# format(self, disconn.uid))
# return

logger.trace("{} handling disconnected remote {}".format(self, disconn))
logger.trace(
"{} handling disconnected remote {}".format(self, disconn))

if disconn.joinInProcess():
logger.trace("{} join already in process, so "
Expand Down Expand Up @@ -686,8 +702,13 @@ def handleDisconnectedRemote(self, cur, disconn):
else:
self.connect(dname, remoteId=disconn.uid)


def connect(self, name=None, remoteId=None, ha=None, verKeyRaw=None, publicKeyRaw=None):
def connect(
self,
name=None,
remoteId=None,
ha=None,
verKeyRaw=None,
publicKeyRaw=None):
"""
Connect to the node specified by name.

Expand All @@ -708,4 +729,3 @@ def connect(self, name=None, remoteId=None, ha=None, verKeyRaw=None, publicKeyRa
else:
ha = self.registry[name]
return self._doConnectByHA(ha, name)

4 changes: 2 additions & 2 deletions stp_raet/test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import asyncio

import pytest
from stp_core.common.temp_file_util import SafeTemporaryDirectory
from stp_core.loop.looper import Looper
from stp_core.network.port_dispenser import genHa
from stp_core.test.conftest import *


@pytest.fixture()
Expand Down Expand Up @@ -38,4 +38,4 @@ def tdir(tdirAndLooper):

@pytest.fixture()
def looper(tdirAndLooper):
return tdirAndLooper[1]
return tdirAndLooper[1]
11 changes: 7 additions & 4 deletions stp_raet/test/stack_message_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ def example2():

print("Finished Handshake\n")

msg = {'subject': 'Example message alpha to beta',
'content': 'The dict keys in this dict are not special any dict will do.',}
msg = {
'subject': 'Example message alpha to beta',
'content': 'The dict keys in this dict are not special any dict will do.',
}

alpha.transmit(msg, remote.uid)
while True:
Expand All @@ -47,8 +49,8 @@ def example2():
print("{0}\n".format(rx))
print("Finished Message alpha to beta\n")

msg = {'subject': 'Example message beta to alpha',
'content': 'Messages are the core of raet.',}
msg = {'subject': 'Example message beta to alpha',
'content': 'Messages are the core of raet.', }

beta.transmit(msg, remote.uid)
while True:
Expand Down Expand Up @@ -80,4 +82,5 @@ def example2():

print("Finished\n")


example2()
8 changes: 4 additions & 4 deletions stp_raet/test/test_kitrstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
from stp_raet.rstack import KITRStack
from stp_core.test.helper import Printer, prepStacks, checkStacksConnected, chkPrinted


@pytest.fixture()
def printers(registry):
printersDict = {}
for name, ha in registry.items():
printersDict[name] = Printer(name)
return printersDict


@pytest.fixture()
def stacks(registry, tdir, looper, printers):
rstacks = []
Expand Down Expand Up @@ -46,7 +48,5 @@ def testKitRStacksSendMesages(looper, stacks, printers):

stacks[0].send({'greetings': 'hi'}, stacks[1].name)

looper.run(eventually(chkPrinted, printers[stacks[1].name], {'greetings': 'hi'}))



looper.run(eventually(
chkPrinted, printers[stacks[1].name], {'greetings': 'hi'}))
10 changes: 5 additions & 5 deletions stp_raet/test/test_raet_comm_with_one_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ def keysAndNames():
alphaName = 'alpha'
betaName = 'beta'
return alphaSigner.keyhex, alphaPrikey, alphaSigner.verhex, alphaPubkey, \
alphaName, betaSigner.keyhex, betaPrikey, betaSigner.verhex, \
betaPubkey, betaName
alphaName, betaSigner.keyhex, betaPrikey, betaSigner.verhex, \
betaPubkey, betaName


def testNonPromiscousConnectionWithOneKey(tdir, keysAndNames):
# Simulating node to node connection
alphaSighex, alphaPrikey, alphaVerhex, alphaPubkey, alphaName, betaSighex,\
betaPrikey, betaVerhex, betaPubkey, betaName = keysAndNames
alphaSighex, alphaPrikey, alphaVerhex, alphaPubkey, alphaName, betaSighex,\
betaPrikey, betaVerhex, betaPubkey, betaName = keysAndNames
alpha = RoadStack(name=alphaName,
ha=genHa(),
sigkey=alphaSighex,
Expand Down Expand Up @@ -75,7 +75,7 @@ def testNonPromiscousConnectionWithOneKey(tdir, keysAndNames):
def testPromiscuousConnection(tdir, keysAndNames):
# Simulating node to client connection
alphaSighex, alphaPrikey, alphaVerhex, alphaPubkey, alphaName, betaSighex, \
betaPrikey, betaVerhex, betaPubkey, betaName = keysAndNames
betaPrikey, betaVerhex, betaPubkey, betaName = keysAndNames
alpha = RoadStack(name=alphaName,
ha=genHa(),
sigkey=alphaSighex,
Expand Down
7 changes: 2 additions & 5 deletions stp_raet/test/test_rstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test2RStackCommunication(tdir, looper):
"main": True,
"mutable": "mutable",
"messageTimeout": 30,
"basedirpath" : tdir
"basedirpath": tdir
}
stackParamsBeta = {
"name": names[1],
Expand All @@ -26,7 +26,7 @@ def test2RStackCommunication(tdir, looper):
"auth_mode": AuthMode.ALLOW_ANY.value,
"mutable": "mutable",
"messageTimeout": 30,
"basedirpath" : tdir
"basedirpath": tdir
}

alpha = SimpleRStack(stackParamsAlpha, msgHandler=alphaP.print)
Expand All @@ -45,6 +45,3 @@ def test2RStackCommunication(tdir, looper):

looper.run(eventually(chkPrinted, betaP, {'greetings': 'hi Beta'}))
looper.run(eventually(chkPrinted, alphaP, {'greetings': 'hi Alpha'}))



5 changes: 1 addition & 4 deletions stp_raet/util.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import json
import os
from collections import OrderedDict

from stp_core.crypto.nacl_wrappers import Signer, Privateer
from raet.road.keeping import RoadKeep

from stp_core.crypto.util import ed25519SkToCurve25519, ed25519PkToCurve25519


def getLocalKeep(name, baseDir=None):
keep = RoadKeep(stackname=name, baseroledirpath=baseDir)
Expand Down Expand Up @@ -73,6 +70,6 @@ def isPortUsedByRaetRemote(keepDir, port):
'estate.json')))
if localRemoteData['ha'][1] == port:
return True
except:
except BaseException:
continue
return False
9 changes: 4 additions & 5 deletions stp_zmq/kit_zstack.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from stp_core.network.keep_in_touch import KITNetworkInterface
from stp_zmq.simple_zstack import SimpleZStack
from typing import Dict, Mapping, Callable, Tuple, Any, Union
from typing import Dict, Callable
from stp_core.types import HA
import time
from stp_core.common.log import getlogger
Expand Down Expand Up @@ -51,10 +51,9 @@ def maintainConnections(self, force=False):

def reconcileNodeReg(self) -> set:
"""
Check whether registry contains some addresses
Check whether registry contains some addresses
that were never connected to

:return:
:return:
"""

matches = set()
Expand All @@ -73,7 +72,7 @@ def retryDisconnected(self, exclude=None):
if name in exclude or remote.isConnected:
continue

if not name in self._retry_connect:
if name not in self._retry_connect:
self._retry_connect[name] = 0

if not remote.socket or self._retry_connect[name] >= \
Expand Down
2 changes: 1 addition & 1 deletion stp_zmq/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,4 @@ def _get_monitor_events(socket, non_block=True):
events.append(message['event'])
except zmq.Again:
break
return events
return events
2 changes: 1 addition & 1 deletion stp_zmq/simple_zstack.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from stp_zmq.zstack import ZStack
from typing import Dict, Mapping, Callable, Tuple, Any, Union
from typing import Dict, Callable
from stp_core.network.auth_mode import AuthMode


Expand Down
3 changes: 0 additions & 3 deletions stp_zmq/test/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from distutils.dir_util import copy_tree
from stat import ST_MODE

from copy import deepcopy

from stp_core.common.util import adict
from stp_core.loop.eventually import eventually
from stp_core.network.port_dispenser import genHa
Expand All @@ -25,7 +23,6 @@ def genKeys(baseDir, names):

def patch_send_ping_counter(stack):
stack.ping_count = 0
origMethod = stack.sendPingPong


def add_counters_to_ping_pong(stack):
Expand Down
Loading