diff sat/plugins/plugin_exp_jingle_stream.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/plugins/plugin_exp_jingle_stream.py	Wed Jun 27 07:51:29 2018 +0200
+++ b/sat/plugins/plugin_exp_jingle_stream.py	Wed Jun 27 20:14:46 2018 +0200
@@ -21,6 +21,7 @@
 from sat.core.constants import Const as C
 from sat.core import exceptions
 from sat.core.log import getLogger
+
 log = getLogger(__name__)
 from sat.tools import xml_tools
 from sat.tools import stream
@@ -35,8 +36,8 @@
 from zope import interface
 import errno
 
-NS_STREAM = 'http://salut-a-toi.org/protocol/stream'
-SECURITY_LIMIT=30
+NS_STREAM = "http://salut-a-toi.org/protocol/stream"
+SECURITY_LIMIT = 30
 START_PORT = 8888
 
 PLUGIN_INFO = {
@@ -47,7 +48,7 @@
     C.PI_DEPENDENCIES: ["XEP-0166"],
     C.PI_MAIN: "JingleStream",
     C.PI_HANDLER: "no",
-    C.PI_DESCRIPTION: _("""Jingle Stream plugin""")
+    C.PI_DESCRIPTION: _("""Jingle Stream plugin"""),
 }
 
 CONFIRM = D_(u"{peer} wants to send you a stream, do you accept ?")
@@ -55,7 +56,6 @@
 
 
 class StreamProtocol(protocol.Protocol):
-
     def __init__(self):
         self.pause = False
 
@@ -118,7 +118,9 @@
 
     def startStream(self, consumer):
         if self.consumer is not None:
-            raise exceptions.InternalError(_(u"stream can't be used with multiple consumers"))
+            raise exceptions.InternalError(
+                _(u"stream can't be used with multiple consumers")
+            )
         assert self.deferred is None
         self.consumer = consumer
         consumer.registerProducer(self, True)
@@ -176,9 +178,17 @@
     def __init__(self, host):
         log.info(_("Plugin Stream initialization"))
         self.host = host
-        self._j = host.plugins["XEP-0166"] # shortcut to access jingle
+        self._j = host.plugins["XEP-0166"]  # shortcut to access jingle
         self._j.registerApplication(NS_STREAM, self)
-        host.bridge.addMethod("streamOut", ".plugin", in_sign='ss', out_sign='s', method=self._streamOut, async=True)
+        host.bridge.addMethod(
+            "streamOut",
+            ".plugin",
+            in_sign="ss",
+            out_sign="s",
+            method=self._streamOut,
+            async=True,
+        )
+
     # jingle callbacks
 
     def _streamOut(self, to_jid_s, profile_key):
@@ -206,64 +216,75 @@
             else:
                 factory.port_listening = port_listening
                 break
-        self._j.initiate(client,
-                         to_jid,
-                         [{'app_ns': NS_STREAM,
-                           'senders': self._j.ROLE_INITIATOR,
-                           'app_kwargs': {'stream_object': factory},
-                         }])
+        self._j.initiate(
+            client,
+            to_jid,
+            [
+                {
+                    "app_ns": NS_STREAM,
+                    "senders": self._j.ROLE_INITIATOR,
+                    "app_kwargs": {"stream_object": factory},
+                }
+            ],
+        )
         defer.returnValue(unicode(port))
 
     def jingleSessionInit(self, client, session, content_name, stream_object):
-        content_data = session['contents'][content_name]
-        application_data = content_data['application_data']
-        assert 'stream_object' not in application_data
-        application_data['stream_object'] = stream_object
-        desc_elt = domish.Element((NS_STREAM, 'description'))
+        content_data = session["contents"][content_name]
+        application_data = content_data["application_data"]
+        assert "stream_object" not in application_data
+        application_data["stream_object"] = stream_object
+        desc_elt = domish.Element((NS_STREAM, "description"))
         return desc_elt
 
     @defer.inlineCallbacks
     def jingleRequestConfirmation(self, client, action, session, content_name, desc_elt):
         """This method request confirmation for a jingle session"""
-        content_data = session['contents'][content_name]
-        if content_data['senders'] not in (self._j.ROLE_INITIATOR, self._j.ROLE_RESPONDER):
+        content_data = session["contents"][content_name]
+        if content_data["senders"] not in (
+            self._j.ROLE_INITIATOR,
+            self._j.ROLE_RESPONDER,
+        ):
             log.warning(u"Bad sender, assuming initiator")
-            content_data['senders'] = self._j.ROLE_INITIATOR
+            content_data["senders"] = self._j.ROLE_INITIATOR
 
-        confirm_data = yield xml_tools.deferDialog(self.host,
-            _(CONFIRM).format(peer=session['peer_jid'].full()),
+        confirm_data = yield xml_tools.deferDialog(
+            self.host,
+            _(CONFIRM).format(peer=session["peer_jid"].full()),
             _(CONFIRM_TITLE),
             type_=C.XMLUI_DIALOG_CONFIRM,
-            action_extra={'meta_from_jid': session['peer_jid'].full(),
-                          'meta_type': "STREAM",
-                         },
+            action_extra={
+                "meta_from_jid": session["peer_jid"].full(),
+                "meta_type": "STREAM",
+            },
             security_limit=SECURITY_LIMIT,
-            profile=client.profile)
+            profile=client.profile,
+        )
 
-        if not C.bool(confirm_data['answer']):
+        if not C.bool(confirm_data["answer"]):
             defer.returnValue(False)
         try:
-            port = int(confirm_data['port'])
+            port = int(confirm_data["port"])
         except (ValueError, KeyError):
-            raise exceptions.DataError(_(u'given port is invalid'))
-        endpoint = endpoints.TCP4ClientEndpoint(reactor, 'localhost', port)
+            raise exceptions.DataError(_(u"given port is invalid"))
+        endpoint = endpoints.TCP4ClientEndpoint(reactor, "localhost", port)
         factory = StreamFactory()
         yield endpoint.connect(factory)
-        content_data['stream_object'] = factory
-        finished_d = content_data['finished_d'] = defer.Deferred()
+        content_data["stream_object"] = factory
+        finished_d = content_data["finished_d"] = defer.Deferred()
         args = [client, session, content_name, content_data]
         finished_d.addCallbacks(self._finishedCb, self._finishedEb, args, None, args)
         defer.returnValue(True)
 
     def jingleHandler(self, client, action, session, content_name, desc_elt):
-        content_data = session['contents'][content_name]
-        application_data = content_data['application_data']
+        content_data = session["contents"][content_name]
+        application_data = content_data["application_data"]
         if action in (self._j.A_ACCEPTED_ACK, self._j.A_SESSION_INITIATE):
             pass
         elif action == self._j.A_SESSION_ACCEPT:
-            assert not 'stream_object' in content_data
-            content_data['stream_object'] = application_data['stream_object']
-            finished_d = content_data['finished_d'] = defer.Deferred()
+            assert not "stream_object" in content_data
+            content_data["stream_object"] = application_data["stream_object"]
+            finished_d = content_data["finished_d"] = defer.Deferred()
             args = [client, session, content_name, content_data]
             finished_d.addCallbacks(self._finishedCb, self._finishedEb, args, None, args)
         else:
@@ -273,9 +294,11 @@
     def _finishedCb(self, dummy, client, session, content_name, content_data):
         log.info(u"Pipe transfer completed")
         self._j.contentTerminate(client, session, content_name)
-        content_data['stream_object'].stopStream()
+        content_data["stream_object"].stopStream()
 
     def _finishedEb(self, failure, client, session, content_name, content_data):
         log.warning(u"Error while streaming pipe: {}".format(failure))
-        self._j.contentTerminate(client, session, content_name, reason=self._j.REASON_FAILED_TRANSPORT)
-        content_data['stream_object'].stopStream()
+        self._j.contentTerminate(
+            client, session, content_name, reason=self._j.REASON_FAILED_TRANSPORT
+        )
+        content_data["stream_object"].stopStream()