PROTON-2322 Fix various flake8 warnings (manual changes) (#289)

* PROTON-2320 Configure and enable flake8 in tox
diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt
index 1e2c52c..44bc36a 100644
--- a/python/CMakeLists.txt
+++ b/python/CMakeLists.txt
@@ -259,7 +259,7 @@
 else ()
   option(ENABLE_TOX_TEST "Enable multi-version python testing with TOX" ON)
 
-  set(tox_default "py27,py35,py36,py37,py38")
+  set(tox_default "pep8,py27,py35,py36,py37,py38")
   set(TOX_ENVLIST "" CACHE STRING "List of python environments for TOX tests" )
   mark_as_advanced(TOX_ENVLIST)
 
@@ -287,7 +287,7 @@
       set_tests_properties(python-tox-test
         PROPERTIES
         PASS_REGULAR_EXPRESSION "Totals: .* ignored, 0 failed"
-        FAIL_REGULAR_EXPRESSION "ERROR:[ ]+py[0-9]*: commands failed")
+        FAIL_REGULAR_EXPRESSION "ERROR:.*commands failed")
     endif ()
   endif (ENABLE_TOX_TEST)
 endif (NOT TOX_MODULE_FOUND)
diff --git a/python/examples/db_common.py b/python/examples/db_common.py
index c12417d..7d5c9b7 100755
--- a/python/examples/db_common.py
+++ b/python/examples/db_common.py
@@ -20,7 +20,7 @@
 
 try:
     import Queue
-except:
+except ImportError:
     import queue as Queue
 
 import sqlite3
diff --git a/python/examples/db_send.py b/python/examples/db_send.py
index 2060079..c18f17f 100755
--- a/python/examples/db_send.py
+++ b/python/examples/db_send.py
@@ -23,7 +23,7 @@
 import time
 try:
     import Queue
-except:
+except ImportError:
     import queue as Queue
 
 
diff --git a/python/proton/_data.py b/python/proton/_data.py
index b13af94..699a16f 100644
--- a/python/proton/_data.py
+++ b/python/proton/_data.py
@@ -582,31 +582,31 @@
         * :const:`MAP`
     """
 
-    NULL = PN_NULL; "A null value."
-    BOOL = PN_BOOL; "A boolean value."
-    UBYTE = PN_UBYTE; "An unsigned byte value."
-    BYTE = PN_BYTE; "A signed byte value."
-    USHORT = PN_USHORT; "An unsigned short value."
-    SHORT = PN_SHORT; "A short value."
-    UINT = PN_UINT; "An unsigned int value."
-    INT = PN_INT; "A signed int value."
-    CHAR = PN_CHAR; "A character value."
-    ULONG = PN_ULONG; "An unsigned long value."
-    LONG = PN_LONG; "A signed long value."
-    TIMESTAMP = PN_TIMESTAMP; "A timestamp value."
-    FLOAT = PN_FLOAT; "A float value."
-    DOUBLE = PN_DOUBLE; "A double value."
-    DECIMAL32 = PN_DECIMAL32; "A DECIMAL32 value."
-    DECIMAL64 = PN_DECIMAL64; "A DECIMAL64 value."
-    DECIMAL128 = PN_DECIMAL128; "A DECIMAL128 value."
-    UUID = PN_UUID; "A UUID value."
-    BINARY = PN_BINARY; "A binary string."
-    STRING = PN_STRING; "A unicode string."
-    SYMBOL = PN_SYMBOL; "A symbolic string."
-    DESCRIBED = PN_DESCRIBED; "A described value."
-    ARRAY = PN_ARRAY; "An array value."
-    LIST = PN_LIST; "A list value."
-    MAP = PN_MAP; "A map value."
+    NULL = PN_NULL  #: A null value.
+    BOOL = PN_BOOL  #: A boolean value.
+    UBYTE = PN_UBYTE  #: An unsigned byte value.
+    BYTE = PN_BYTE  #: A signed byte value.
+    USHORT = PN_USHORT  #: An unsigned short value.
+    SHORT = PN_SHORT  #: A short value.
+    UINT = PN_UINT  #: An unsigned int value.
+    INT = PN_INT  #: A signed int value.
+    CHAR = PN_CHAR  #: A character value.
+    ULONG = PN_ULONG  #: An unsigned long value.
+    LONG = PN_LONG  #: A signed long value.
+    TIMESTAMP = PN_TIMESTAMP  #: A timestamp value.
+    FLOAT = PN_FLOAT  #: A float value.
+    DOUBLE = PN_DOUBLE  #: A double value.
+    DECIMAL32 = PN_DECIMAL32  #: A DECIMAL32 value.
+    DECIMAL64 = PN_DECIMAL64  #: A DECIMAL64 value.
+    DECIMAL128 = PN_DECIMAL128  #: A DECIMAL128 value.
+    UUID = PN_UUID  #: A UUID value.
+    BINARY = PN_BINARY  #: A binary string.
+    STRING = PN_STRING  #: A unicode string.
+    SYMBOL = PN_SYMBOL  #: A symbolic string.
+    DESCRIBED = PN_DESCRIBED  #: A described value.
+    ARRAY = PN_ARRAY  #: An array value.
+    LIST = PN_LIST  #: A list value.
+    MAP = PN_MAP  #: A map value.
 
     type_names = {
         NULL: "null",
diff --git a/python/proton/_delivery.py b/python/proton/_delivery.py
index ce474dc..a9243df 100644
--- a/python/proton/_delivery.py
+++ b/python/proton/_delivery.py
@@ -34,7 +34,7 @@
 
 
 class NamedInt(int):
-    values = {}  # type: Dict[int, str]
+    values = {}  # type: Dict[int, str]  # noqa  # TODO(PROTON-2323) typing.Dict is not available on Python 2.7
 
     def __new__(cls, i, name):
         ni = super(NamedInt, cls).__new__(cls, i)
diff --git a/python/proton/_reactor.py b/python/proton/_reactor.py
index 5c16d4d..4eeb6b3 100644
--- a/python/proton/_reactor.py
+++ b/python/proton/_reactor.py
@@ -19,7 +19,6 @@
 
 from __future__ import absolute_import
 
-#from functools import total_ordering
 import heapq
 import json
 import logging
@@ -28,6 +27,7 @@
 import time
 import traceback
 import uuid
+from functools import total_ordering
 
 from cproton import PN_PYREF, PN_ACCEPTED, PN_EVENT_NONE
 
@@ -60,7 +60,8 @@
 def _now():
     return time.time()
 
-#@total_ordering
+
+@total_ordering
 class Task(object):
 
     def __init__(self, reactor, deadline, handler):
@@ -189,7 +190,7 @@
     # Cross thread reactor wakeup
     def wakeup(self):
         # TODO: Do this with pipe and write?
-        #os.write(self._wakeup[1], "x", 1);
+        #  os.write(self._wakeup[1], "x", 1);
         pass
 
     def start(self):
@@ -197,8 +198,8 @@
         self._selectable = TimerSelectable(self)
         self._selectable.deadline = self.timer_deadline
         # TODO set up fd to read for wakeups - but problematic on windows
-        # self._selectable.fileno(self._wakeup[0])
-        #self._selectable.reading = True
+        #  self._selectable.fileno(self._wakeup[0])
+        #  self._selectable.reading = True
         self.update(self._selectable)
 
     @property
@@ -679,7 +680,8 @@
         """
         pass
 
-    def test(self, link): return link.is_sender
+    def test(self, link):
+        return link.is_sender
 
 
 class ReceiverOption(LinkOption):
@@ -696,7 +698,8 @@
         """
         pass
 
-    def test(self, link): return link.is_receiver
+    def test(self, link):
+        return link.is_receiver
 
 
 class DynamicNodeProperties(LinkOption):
diff --git a/python/setup.py.in b/python/setup.py.in
index 2005c4c..1c77c30 100644
--- a/python/setup.py.in
+++ b/python/setup.py.in
@@ -46,7 +46,6 @@
 """
 
 import os
-import shutil
 
 from setuptools import setup, Extension
 from setuptools.command.sdist import sdist
@@ -60,9 +59,7 @@
 from setuputils import misc
 
 
-_PROTON_VERSION=(@PN_VERSION_MAJOR@,
-                 @PN_VERSION_MINOR@,
-                 @PN_VERSION_POINT@)
+_PROTON_VERSION = (@PN_VERSION_MAJOR@, @PN_VERSION_MINOR@, @PN_VERSION_POINT@)
 _PROTON_VERSION_STR = "%d.%d.%d" % _PROTON_VERSION
 
 
@@ -73,8 +70,8 @@
         cproton.py.
         """
         ext = Extension('_cproton',
-                             sources=['cproton.i'],
-                             swig_opts=['-threads', '-Iinclude'])
+                        sources=['cproton.i'],
+                        swig_opts=['-threads', '-Iinclude'])
 
         if 'SWIG' in os.environ:
             self.swig = os.environ['SWIG']
diff --git a/python/tests/proton_tests/main.py b/python/tests/proton_tests/main.py
index 01c524f..1bf6823 100644
--- a/python/tests/proton_tests/main.py
+++ b/python/tests/proton_tests/main.py
@@ -45,9 +45,7 @@
     "ERROR": ERROR
 }
 
-sorted_levels = [(v, k) for k, v in list(levels.items())]
-sorted_levels.sort()
-sorted_levels = [v for k, v in sorted_levels]
+sorted_levels = [k for k, v in sorted(levels.items(), key=lambda kv: kv[1])]
 
 parser = optparse.OptionParser(usage="usage: %prog [options] PATTERN ...",
                                description="Run tests matching the specified PATTERNs.")
@@ -604,9 +602,7 @@
         yield None
 
     def extract(self, cls):
-        names = dir(cls)
-        names.sort()
-        for name in names:
+        for name in sorted(dir(cls)):
             obj = getattr(cls, name)
             if hasattr(obj, '__call__') and name.startswith("test"):
                 yield MethodTest(cls, name)
@@ -618,9 +614,7 @@
         return type(obj) == types.ModuleType
 
     def descend(self, obj):
-        names = dir(obj)
-        names.sort()
-        for name in names:
+        for name in sorted(dir(obj)):
             yield getattr(obj, name)
 
     def extract(self, obj):
diff --git a/python/tox.ini b/python/tox.ini
index e70b23c..02f995b 100644
--- a/python/tox.ini
+++ b/python/tox.ini
@@ -25,6 +25,33 @@
 changedir = {[tox]setupdir}
 commands = flake8
 
+[flake8]
+# TODO(PROTON-2322) decrease the limit
+max-line-length = 150
+
+# TODO(PROTON-2322) re-enable all these warnings
+ignore =
+    # do not use bare 'except'
+    E722,
+    # imported but unused
+    F401,
+    # 'from proton import *' used; unable to detect undefined names
+    F403,
+    # may be undefined, or defined from star imports: proton
+    F405,
+    # ambiguous variable name 'l'
+    E741,
+    # local variable 'int_nodes' is assigned to but never used
+    F841,
+    # TODO(PROTON-2322) decide which of these two warnings we want disabled
+    #  _transport.py:908:21: W503 line break before binary operator
+    #  _transport.py:907:56: W504 line break after binary operator
+    W504,
+
+exclude =
+    # TODO(PROTON-2095) generated by SWIG
+    cproton.py,
+
 [testenv:docs]
 deps =
     sphinx