diff sat/tools/sat_defer.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 378188abe941
line wrap: on
line diff
--- a/sat/tools/sat_defer.py	Wed Jun 27 07:51:29 2018 +0200
+++ b/sat/tools/sat_defer.py	Wed Jun 27 20:14:46 2018 +0200
@@ -20,6 +20,7 @@
 """tools related to deferred"""
 
 from sat.core.log import getLogger
+
 log = getLogger(__name__)
 from sat.core import exceptions
 from twisted.internet import defer
@@ -29,8 +30,8 @@
 from sat.core.constants import Const as C
 from sat.memory import memory
 
-KEY_DEFERREDS = 'deferreds'
-KEY_NEXT = 'next_defer'
+KEY_DEFERREDS = "deferreds"
+KEY_NEXT = "next_defer"
 
 
 class DelayedDeferred(object):
@@ -52,19 +53,19 @@
         self._deferred.cancel()
 
     def addCallbacks(self, *args, **kwargs):
-        self._deferred.addCallbacks(*args,**kwargs)
+        self._deferred.addCallbacks(*args, **kwargs)
 
     def addCallback(self, *args, **kwargs):
-        self._deferred.addCallback(*args,**kwargs)
+        self._deferred.addCallback(*args, **kwargs)
 
     def addErrback(self, *args, **kwargs):
-        self._deferred.addErrback(*args,**kwargs)
+        self._deferred.addErrback(*args, **kwargs)
 
     def addBoth(self, *args, **kwargs):
-        self._deferred.addBoth(*args,**kwargs)
+        self._deferred.addBoth(*args, **kwargs)
 
     def chainDeferred(self, *args, **kwargs):
-        self._deferred.chainDeferred(*args,**kwargs)
+        self._deferred.chainDeferred(*args, **kwargs)
 
     def pause(self):
         self._deferred.pause()
@@ -76,13 +77,14 @@
 class RTDeferredSessions(memory.Sessions):
     """Real Time Deferred Sessions"""
 
-
     def __init__(self, timeout=120):
         """Manage list of Deferreds in real-time, allowing to get intermediate results
 
         @param timeout (int): nb of seconds before deferreds cancellation
         """
-        super(RTDeferredSessions, self).__init__(timeout=timeout, resettable_timeout=False)
+        super(RTDeferredSessions, self).__init__(
+            timeout=timeout, resettable_timeout=False
+        )
 
     def newSession(self, deferreds, profile):
         """Launch a new session with a list of deferreds
@@ -92,7 +94,9 @@
         @param return (tupe[str, defer.Deferred]): tuple with session id and a deferred wich fire *WITHOUT RESULT* when all results are received
         """
         data = {KEY_NEXT: defer.Deferred()}
-        session_id, session_data = super(RTDeferredSessions, self).newSession(data, profile=profile)
+        session_id, session_data = super(RTDeferredSessions, self).newSession(
+            data, profile=profile
+        )
         if isinstance(deferreds, dict):
             session_data[KEY_DEFERREDS] = deferreds.values()
             iterator = deferreds.iteritems()
@@ -107,7 +111,9 @@
             d.addErrback(self._errback, d, session_id, profile)
         return session_id
 
-    def _purgeSession(self, session_id, reason=u"timeout", no_warning=False, got_result=False):
+    def _purgeSession(
+        self, session_id, reason=u"timeout", no_warning=False, got_result=False
+    ):
         """Purge the session
 
         @param session_id(str): id of the session to purge
@@ -121,7 +127,9 @@
             try:
                 timer, session_data, profile = self._sessions[session_id]
             except ValueError:
-                raise exceptions.InternalError(u'was expecting timer, session_data and profile; is profile set ?')
+                raise exceptions.InternalError(
+                    u"was expecting timer, session_data and profile; is profile set ?"
+                )
 
             # next_defer must be called before deferreds,
             # else its callback will be called by _gotResult
@@ -134,7 +142,9 @@
                 d.cancel()
 
             if not no_warning:
-                log.warning(u"RTDeferredList cancelled: {} (profile {})".format(reason, profile))
+                log.warning(
+                    u"RTDeferredList cancelled: {} (profile {})".format(reason, profile)
+                )
 
         super(RTDeferredSessions, self)._purgeSession(session_id)
 
@@ -165,7 +175,9 @@
         """
         self._purgeSession(session_id, reason=reason, no_warning=no_log)
 
-    def getResults(self, session_id, on_success=None, on_error=None, profile=C.PROF_KEY_NONE):
+    def getResults(
+        self, session_id, on_success=None, on_error=None, profile=C.PROF_KEY_NONE
+    ):
         """Get current results of a real-time deferred session
 
         result already gotten are deleted
@@ -197,11 +209,13 @@
         def next_cb(dummy):
             # we got one or several results
             results = {}
-            filtered_data = [] # used to keep deferreds without results
+            filtered_data = []  # used to keep deferreds without results
             deferreds = session_data[KEY_DEFERREDS]
 
             for d in deferreds:
-                if d._RTDeferred_return: # we don't use d.called as called is True before the full callbacks chain has been called
+                if (
+                    d._RTDeferred_return
+                ):  # we don't use d.called as called is True before the full callbacks chain has been called
                     # we have a result
                     idx = d._RTDeferred_index
                     success, result = d._RTDeferred_return
@@ -210,7 +224,9 @@
                             if callable(on_success):
                                 result = yield on_success(result)
                             else:
-                                raise exceptions.InternalError('Unknown value of on_success: {}'.format(on_success))
+                                raise exceptions.InternalError(
+                                    "Unknown value of on_success: {}".format(on_success)
+                                )
 
                     else:
                         if on_error is not None:
@@ -219,7 +235,9 @@
                             elif callable(on_error):
                                 result = yield on_error(result)
                             else:
-                                raise exceptions.InternalError('Unknown value of on_error: {}'.format(on_error))
+                                raise exceptions.InternalError(
+                                    "Unknown value of on_error: {}".format(on_error)
+                                )
                     results[idx] = (success, result)
                 else:
                     filtered_data.append(d)