[Neo-report] r2671 jm - in /trunk/neo: lib/ master/ master/handlers/ tests/

nobody at svn.erp5.org nobody at svn.erp5.org
Mon Mar 21 11:53:59 CET 2011


Author: jm
Date: Mon Mar 21 11:53:59 2011
New Revision: 2671

Log:
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

Modified:
    trunk/neo/lib/connection.py
    trunk/neo/lib/handler.py
    trunk/neo/lib/protocol.py
    trunk/neo/master/app.py
    trunk/neo/master/handlers/identification.py
    trunk/neo/tests/__init__.py
    trunk/neo/tests/testConnection.py

Modified: trunk/neo/lib/connection.py
==============================================================================
--- trunk/neo/lib/connection.py [iso-8859-1] (original)
+++ trunk/neo/lib/connection.py [iso-8859-1] Mon Mar 21 11:53:59 2011
@@ -614,7 +614,7 @@ class Connection(BaseConnection):
         if self.connector is None:
             return
 
-        was_empty = not bool(self.write_buf)
+        was_empty = not self.write_buf
 
         self.write_buf.extend(packet.encode())
 

Modified: trunk/neo/lib/handler.py
==============================================================================
--- trunk/neo/lib/handler.py [iso-8859-1] (original)
+++ trunk/neo/lib/handler.py [iso-8859-1] Mon Mar 21 11:53:59 2011
@@ -80,7 +80,7 @@ class EventHandler(object):
             self.connectionClosed(conn)
 
     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:
             neo.lib.logging.error('reject an alien cluster')
             raise ProtocolError('invalid cluster name')

Modified: trunk/neo/lib/protocol.py
==============================================================================
--- trunk/neo/lib/protocol.py [iso-8859-1] (original)
+++ trunk/neo/lib/protocol.py [iso-8859-1] Mon Mar 21 11:53:59 2011
@@ -158,19 +158,17 @@ class Packet(object):
     _id = None
 
     def __init__(self, *args, **kw):
-        args = list(args)
         assert self._code is not None, "Packet class not registered"
         if args or kw:
-            assert self._fmt is not None
+            args = list(args)
             buf = StringIO()
             # load named arguments
             for item in self._fmt._items[len(args):]:
                 args.append(kw.get(item._name))
             self._fmt.encode(buf.write, args)
-            body = buf.getvalue()
+            self._body = buf.getvalue()
         else:
-            body = ''
-        self._body = body
+            self._body = ''
 
     def decode(self):
         assert self._body is not None

Modified: trunk/neo/master/app.py
==============================================================================
--- trunk/neo/master/app.py [iso-8859-1] (original)
+++ trunk/neo/master/app.py [iso-8859-1] Mon Mar 21 11:53:59 2011
@@ -120,7 +120,7 @@ class Application(object):
         # Start a normal operation.
         while True:
             # (Re)elect a new primary master.
-            self.primary = not bool(self.nm.getMasterList())
+            self.primary = not self.nm.getMasterList()
             if not self.primary:
                 self.electPrimary(bootstrap=bootstrap)
                 bootstrap = False

Modified: trunk/neo/master/handlers/identification.py
==============================================================================
--- trunk/neo/master/handlers/identification.py [iso-8859-1] (original)
+++ trunk/neo/master/handlers/identification.py [iso-8859-1] Mon Mar 21 11:53:59 2011
@@ -22,7 +22,6 @@ from neo.lib.protocol import BrokenNodeD
 from neo.master.handlers import MasterHandler
 
 class IdentificationHandler(MasterHandler):
-    """This class deals with messages from the admin node only"""
 
     def nodeLost(self, conn, node):
         neo.lib.logging.warning(
@@ -31,27 +30,19 @@ class IdentificationHandler(MasterHandle
     def requestIdentification(self, conn, node_type, uuid, address, name):
 
         self.checkClusterName(name)
-        app, nm = self.app, self.app.nm
-        node_by_uuid = nm.getByUUID(uuid)
-        node_by_addr = nm.getByAddress(address)
+        app = self.app
+        node_by_uuid = app.nm.getByUUID(uuid)
 
         # handle conflicts and broken nodes
-        node = node_by_uuid or node_by_addr
-        if node_by_uuid is not None:
-            if node.getAddress() == address:
+        node = node_by_uuid or app.nm.getByAddress(address)
+        if node:
+            if node_by_uuid and node.getAddress() == address:
                 # the node is still alive
                 if node.isBroken():
                     raise BrokenNodeDisallowedError
-            if node.getAddress() != address:
-                # 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():
+            elif node.isRunning():
                 # still running, reject this new node
                 raise ProtocolError('invalid server address')
-        if node is not None:
             if node.isConnected():
                 # more than one connection from this node
                 raise ProtocolError('already connected')
@@ -60,7 +51,7 @@ class IdentificationHandler(MasterHandle
 
         # ask the app the node identification, if refused, an exception is
         # raised
-        result = self.app.identifyNode(node_type, uuid, node)
+        result = app.identifyNode(node_type, uuid, node)
         (uuid, node, state, handler, node_ctor) = result
         if uuid is None:
             # no valid uuid, give it one

Modified: trunk/neo/tests/__init__.py
==============================================================================
--- trunk/neo/tests/__init__.py [iso-8859-1] (original)
+++ trunk/neo/tests/__init__.py [iso-8859-1] Mon Mar 21 11:53:59 2011
@@ -505,8 +505,8 @@ class SocketLock(object):
                     self._socket = s
                     return True
         finally:
-          if self._socket is None:
-              s.close()
+            if self._socket is None:
+                s.close()
 
     def release(self):
         s = self._socket

Modified: trunk/neo/tests/testConnection.py
==============================================================================
--- trunk/neo/tests/testConnection.py [iso-8859-1] (original)
+++ trunk/neo/tests/testConnection.py [iso-8859-1] Mon Mar 21 11:53:59 2011
@@ -1075,7 +1075,7 @@ class HandlerSwitcherTests(NeoUnitTestBa
         self.assertEqual(markers[0], (3, self._connection, msg_id_3))
         # answer to msg_3 must not be expected anymore (and it was the last
         # expected message)
-        self.assertFalse(bool(self._handlers.isPending()))
+        self.assertFalse(self._handlers.isPending())
         del markers[:]
         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




More information about the Neo-report mailing list