Commit 75cd5ecb authored by Julien Muchembled's avatar Julien Muchembled

Small code cleanups

- remove useless calls to 'bool'
- small optimizations in lib.protocol.Packet.__init__
- code simplification in IdentificationHandler
- fix typo in docstring
- neo/tests/__init__.py: 2 lines were indented with 2-spaces instead of 4-spaces

git-svn-id: https://svn.erp5.org/repos/neo/trunk@2671 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 9eb67c81
...@@ -614,7 +614,7 @@ class Connection(BaseConnection): ...@@ -614,7 +614,7 @@ class Connection(BaseConnection):
if self.connector is None: if self.connector is None:
return return
was_empty = not bool(self.write_buf) was_empty = not self.write_buf
self.write_buf.extend(packet.encode()) self.write_buf.extend(packet.encode())
......
...@@ -80,7 +80,7 @@ class EventHandler(object): ...@@ -80,7 +80,7 @@ class EventHandler(object):
self.connectionClosed(conn) self.connectionClosed(conn)
def checkClusterName(self, name): def checkClusterName(self, name):
# raise an exception if the fiven name mismatch the current cluster name # raise an exception if the given name mismatch the current cluster name
if self.app.name != name: if self.app.name != name:
neo.lib.logging.error('reject an alien cluster') neo.lib.logging.error('reject an alien cluster')
raise ProtocolError('invalid cluster name') raise ProtocolError('invalid cluster name')
......
...@@ -158,19 +158,17 @@ class Packet(object): ...@@ -158,19 +158,17 @@ class Packet(object):
_id = None _id = None
def __init__(self, *args, **kw): def __init__(self, *args, **kw):
args = list(args)
assert self._code is not None, "Packet class not registered" assert self._code is not None, "Packet class not registered"
if args or kw: if args or kw:
assert self._fmt is not None args = list(args)
buf = StringIO() buf = StringIO()
# load named arguments # load named arguments
for item in self._fmt._items[len(args):]: for item in self._fmt._items[len(args):]:
args.append(kw.get(item._name)) args.append(kw.get(item._name))
self._fmt.encode(buf.write, args) self._fmt.encode(buf.write, args)
body = buf.getvalue() self._body = buf.getvalue()
else: else:
body = '' self._body = ''
self._body = body
def decode(self): def decode(self):
assert self._body is not None assert self._body is not None
......
...@@ -120,7 +120,7 @@ class Application(object): ...@@ -120,7 +120,7 @@ class Application(object):
# Start a normal operation. # Start a normal operation.
while True: while True:
# (Re)elect a new primary master. # (Re)elect a new primary master.
self.primary = not bool(self.nm.getMasterList()) self.primary = not self.nm.getMasterList()
if not self.primary: if not self.primary:
self.electPrimary(bootstrap=bootstrap) self.electPrimary(bootstrap=bootstrap)
bootstrap = False bootstrap = False
......
...@@ -22,7 +22,6 @@ from neo.lib.protocol import BrokenNodeDisallowedError, ProtocolError ...@@ -22,7 +22,6 @@ from neo.lib.protocol import BrokenNodeDisallowedError, ProtocolError
from neo.master.handlers import MasterHandler from neo.master.handlers import MasterHandler
class IdentificationHandler(MasterHandler): class IdentificationHandler(MasterHandler):
"""This class deals with messages from the admin node only"""
def nodeLost(self, conn, node): def nodeLost(self, conn, node):
neo.lib.logging.warning( neo.lib.logging.warning(
...@@ -31,27 +30,19 @@ class IdentificationHandler(MasterHandler): ...@@ -31,27 +30,19 @@ class IdentificationHandler(MasterHandler):
def requestIdentification(self, conn, node_type, uuid, address, name): def requestIdentification(self, conn, node_type, uuid, address, name):
self.checkClusterName(name) self.checkClusterName(name)
app, nm = self.app, self.app.nm app = self.app
node_by_uuid = nm.getByUUID(uuid) node_by_uuid = app.nm.getByUUID(uuid)
node_by_addr = nm.getByAddress(address)
# handle conflicts and broken nodes # handle conflicts and broken nodes
node = node_by_uuid or node_by_addr node = node_by_uuid or app.nm.getByAddress(address)
if node_by_uuid is not None: if node:
if node.getAddress() == address: if node_by_uuid and node.getAddress() == address:
# the node is still alive # the node is still alive
if node.isBroken(): if node.isBroken():
raise BrokenNodeDisallowedError raise BrokenNodeDisallowedError
if node.getAddress() != address: elif node.isRunning():
# this node has changed its address
if node.isRunning():
# still running, reject this new node
raise ProtocolError('invalid server address')
if node_by_uuid is None and node_by_addr is not None:
if node.isRunning():
# still running, reject this new node # still running, reject this new node
raise ProtocolError('invalid server address') raise ProtocolError('invalid server address')
if node is not None:
if node.isConnected(): if node.isConnected():
# more than one connection from this node # more than one connection from this node
raise ProtocolError('already connected') raise ProtocolError('already connected')
...@@ -60,7 +51,7 @@ class IdentificationHandler(MasterHandler): ...@@ -60,7 +51,7 @@ class IdentificationHandler(MasterHandler):
# ask the app the node identification, if refused, an exception is # ask the app the node identification, if refused, an exception is
# raised # raised
result = self.app.identifyNode(node_type, uuid, node) result = app.identifyNode(node_type, uuid, node)
(uuid, node, state, handler, node_ctor) = result (uuid, node, state, handler, node_ctor) = result
if uuid is None: if uuid is None:
# no valid uuid, give it one # no valid uuid, give it one
......
...@@ -505,8 +505,8 @@ class SocketLock(object): ...@@ -505,8 +505,8 @@ class SocketLock(object):
self._socket = s self._socket = s
return True return True
finally: finally:
if self._socket is None: if self._socket is None:
s.close() s.close()
def release(self): def release(self):
s = self._socket s = self._socket
......
...@@ -1075,7 +1075,7 @@ class HandlerSwitcherTests(NeoUnitTestBase): ...@@ -1075,7 +1075,7 @@ class HandlerSwitcherTests(NeoUnitTestBase):
self.assertEqual(markers[0], (3, self._connection, msg_id_3)) self.assertEqual(markers[0], (3, self._connection, msg_id_3))
# answer to msg_3 must not be expected anymore (and it was the last # answer to msg_3 must not be expected anymore (and it was the last
# expected message) # expected message)
self.assertFalse(bool(self._handlers.isPending())) self.assertFalse(self._handlers.isPending())
del markers[:] del markers[:]
self._handlers.emit(r4, msg_4_time, OnTimeout(msg_4_on_timeout)) self._handlers.emit(r4, msg_4_time, OnTimeout(msg_4_on_timeout))
# msg_4 timeout will fire msg_4_on_timeout callback, which lets the # msg_4 timeout will fire msg_4_on_timeout callback, which lets the
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment