From c6fa924adcd3f1d5f7afca4d84ecff06a2c24ef2 Mon Sep 17 00:00:00 2001
From: Piotr Maslanka <piotr.maslanka@henrietta.com.pl>
Date: Sun, 8 Oct 2017 11:15:17 +0200
Subject: [PATCH] code cleanup

---
 CHANGELOG.md                               |  4 ++
 coolamqp/framing/compilation/xml_fields.py |  8 ++-
 coolamqp/uplink/connection/connection.py   | 69 ++++++++++------------
 setup.cfg                                  |  2 +-
 4 files changed, 43 insertions(+), 40 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1b2b724..ce6e4b2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+* v0.94:
+
+ * ...
+
 * v0.93:
 
  * Large refactor of XML schema compiler
diff --git a/coolamqp/framing/compilation/xml_fields.py b/coolamqp/framing/compilation/xml_fields.py
index dfcb8f1..794973f 100644
--- a/coolamqp/framing/compilation/xml_fields.py
+++ b/coolamqp/framing/compilation/xml_fields.py
@@ -10,6 +10,9 @@ class _Required(object):
 def nop(x):
     return x
 
+def _get_tagchild(elem, tag):
+    return [e for e in elem.getchildren() if e.tag == tag]
+
 __all__ = [
     '_name', '_docs', '_ComputedField', '_ValueField', '_SimpleField',
     '_docs_with_label', '_get_tagchild', '_ChildField'
@@ -73,8 +76,8 @@ class _SimpleField(_ValueField):
     def __init__(self, name, field_type=nop, default=_Required):
         super(_SimpleField, self).__init__(name, name, field_type, default)
 
-def _get_tagchild(elem, tag):
-    return [e for e in elem.getchildren() if e.tag == tag]
+
+
 
 
 class _ChildField(_ComputedField):
@@ -85,6 +88,7 @@ class _ChildField(_ComputedField):
         super(_ChildField, self).__init__(name, lambda elem: \
             postexec([fun(c) for c in _get_tagchild(elem, xml_tag)]))
 
+
 def get_docs(elem, label):
     """Parse an XML element. Return documentation"""
     for kid in elem.getchildren():
diff --git a/coolamqp/uplink/connection/connection.py b/coolamqp/uplink/connection/connection.py
index 688c28f..73b3dbe 100644
--- a/coolamqp/uplink/connection/connection.py
+++ b/coolamqp/uplink/connection/connection.py
@@ -19,6 +19,34 @@ from coolamqp.objects import Callable
 logger = logging.getLogger(__name__)
 
 
+def alert_watches(watches, trigger):
+    """
+    Notify all watches in this collection.
+
+    Return a list of alive watches.
+    :param watches: list of Watch
+    :return: tuple of (list of Watch, bool - was any watch fired?)
+    """
+    watch_handled = False
+    alive_watches = []
+    while len(watches) > 0:
+        watch = watches.pop()
+
+        if watch.cancelled:
+            continue
+
+        watch_triggered = watch.is_triggered_by(trigger)
+        watch_handled |= watch_triggered
+
+        if watch.cancelled:
+            continue
+
+        if not any((watch_triggered, watch.oneshot, watch.cancelled)):
+            # Watch remains alive if it was NOT triggered, or it's NOT a oneshot
+            alive_watches.append(watch)
+    return alive_watches, watch_handled
+
+
 class Connection(object):
     """
     An object that manages a connection in a comprehensive way.
@@ -222,25 +250,8 @@ class Connection(object):
             watches = self.watches[frame.channel]  # a list
             self.watches[frame.channel] = []
 
-            alive_watches = []
-            while len(watches) > 0:
-                watch = watches.pop()
-
-                if watch.cancelled:
-                    # print('watch',watch,'was cancelled')
-                    continue
-
-                watch_triggered = watch.is_triggered_by(frame)
-                watch_handled |= watch_triggered
-
-                if watch.cancelled:
-                    # print('watch',watch,'was cancelled')
-                    continue
-
-                if ((not watch_triggered) or (not watch.oneshot)) and (
-                        not watch.cancelled):
-                    # Watch remains alive if it was NOT triggered, or it's NOT a oneshot
-                    alive_watches.append(watch)
+            alive_watches, f = alert_watches(watches, frame)
+            watch_handled |= f
 
             if frame.channel in self.watches:
                 # unwatch_all might have gotten called, check that
@@ -248,27 +259,11 @@ class Connection(object):
                     self.watches[frame.channel].append(watch)
 
         # ==================== process "any" watches
-        alive_watches = []
         any_watches = self.any_watches
         self.any_watches = []
-        while len(any_watches):
-            watch = any_watches.pop()
-
-            if watch.cancelled:
-                # print('any watch', watch, 'was cancelled')
-                continue
-
-            watch_triggered = watch.is_triggered_by(frame)
-            watch_handled |= watch_triggered
-
-            if watch.cancelled:
-                # print('any watch', watch, 'was cancelled')
-                continue
+        alive_watches, f = alert_watches(any_watches, frame)
 
-            if ((not watch_triggered) or (not watch.oneshot)) and (
-                    not watch.cancelled):
-                # Watch remains alive if it was NOT triggered, or it's NOT a oneshot
-                alive_watches.append(watch)
+        watch_handled |= f
 
         for watch in alive_watches:
             self.any_watches.append(watch)
diff --git a/setup.cfg b/setup.cfg
index 727a1be..daeed6a 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -1,7 +1,7 @@
 [metadata]
 description-file = README.md
 name = CoolAMQP
-version = 0.93
+version = 0.94rc1
 license = MIT License
 classifiers = 
     Programming Language :: Python
-- 
GitLab