diff sat/plugins/plugin_xep_0071.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_xep_0071.py	Wed Jun 27 07:51:29 2018 +0200
+++ b/sat/plugins/plugin_xep_0071.py	Wed Jun 27 20:14:46 2018 +0200
@@ -21,24 +21,28 @@
 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.common import data_format
 
 from twisted.internet import defer
 from wokkel import disco, iwokkel
 from zope.interface import implements
+
 # from lxml import etree
 try:
     from lxml import html
 except ImportError:
-    raise exceptions.MissingModule(u"Missing module lxml, please download/install it from http://lxml.de/")
+    raise exceptions.MissingModule(
+        u"Missing module lxml, please download/install it from http://lxml.de/"
+    )
 try:
     from twisted.words.protocols.xmlstream import XMPPHandler
 except ImportError:
     from wokkel.subprotocols import XMPPHandler
 
-NS_XHTML_IM = 'http://jabber.org/protocol/xhtml-im'
-NS_XHTML = 'http://www.w3.org/1999/xhtml'
+NS_XHTML_IM = "http://jabber.org/protocol/xhtml-im"
+NS_XHTML = "http://www.w3.org/1999/xhtml"
 
 PLUGIN_INFO = {
     C.PI_NAME: "XHTML-IM Plugin",
@@ -48,28 +52,39 @@
     C.PI_DEPENDENCIES: ["TEXT-SYNTAXES"],
     C.PI_MAIN: "XEP_0071",
     C.PI_HANDLER: "yes",
-    C.PI_DESCRIPTION: _("""Implementation of XHTML-IM""")
+    C.PI_DESCRIPTION: _("""Implementation of XHTML-IM"""),
 }
 
 allowed = {
-          "a": set(["href", "style", "type"]),
-          "blockquote": set(["style"]),
-          "body": set(["style"]),
-          "br": set([]),
-          "cite": set(["style"]),
-          "em": set([]),
-          "img": set(["alt", "height", "src", "style", "width"]),
-          "li": set(["style"]),
-          "ol": set(["style"]),
-          "p": set(["style"]),
-          "span": set(["style"]),
-          "strong": set([]),
-          "ul": set(["style"]),
-          }
+    "a": set(["href", "style", "type"]),
+    "blockquote": set(["style"]),
+    "body": set(["style"]),
+    "br": set([]),
+    "cite": set(["style"]),
+    "em": set([]),
+    "img": set(["alt", "height", "src", "style", "width"]),
+    "li": set(["style"]),
+    "ol": set(["style"]),
+    "p": set(["style"]),
+    "span": set(["style"]),
+    "strong": set([]),
+    "ul": set(["style"]),
+}
 
-styles_allowed = ["background-color", "color", "font-family", "font-size", "font-style", "font-weight", "margin-left", "margin-right", "text-align", "text-decoration"]
+styles_allowed = [
+    "background-color",
+    "color",
+    "font-family",
+    "font-size",
+    "font-style",
+    "font-weight",
+    "margin-left",
+    "margin-right",
+    "text-align",
+    "text-decoration",
+]
 
-blacklist = ['script'] # tag that we have to kill (we don't keep content)
+blacklist = ["script"]  # tag that we have to kill (we don't keep content)
 
 
 class XEP_0071(object):
@@ -79,7 +94,12 @@
         log.info(_("XHTML-IM plugin initialization"))
         self.host = host
         self._s = self.host.plugins["TEXT-SYNTAXES"]
-        self._s.addSyntax(self.SYNTAX_XHTML_IM, lambda xhtml: xhtml, self.XHTML2XHTML_IM, [self._s.OPT_HIDDEN])
+        self._s.addSyntax(
+            self.SYNTAX_XHTML_IM,
+            lambda xhtml: xhtml,
+            self.XHTML2XHTML_IM,
+            [self._s.OPT_HIDDEN],
+        )
         host.trigger.add("MessageReceived", self.messageReceivedTrigger)
         host.trigger.add("sendMessage", self.sendMessageTrigger)
 
@@ -97,17 +117,23 @@
         # TODO: check if text only body is empty, then try to convert XHTML-IM to pure text and show a warning message
         def converted(xhtml, lang):
             if lang:
-                data['extra']['xhtml_{}'.format(lang)] = xhtml
+                data["extra"]["xhtml_{}".format(lang)] = xhtml
             else:
-                data['extra']['xhtml'] = xhtml
+                data["extra"]["xhtml"] = xhtml
 
         defers = []
         for body_elt in body_elts:
-            lang = body_elt.getAttribute((C.NS_XML, 'lang'), '')
-            treat_d = defer.succeed(None)  # deferred used for treatments
-            if self.host.trigger.point("xhtml_post_treat", client, message_elt, body_elt, lang, treat_d):
+            lang = body_elt.getAttribute((C.NS_XML, "lang"), "")
+            treat_d = defer.succeed(None)  #  deferred used for treatments
+            if self.host.trigger.point(
+                "xhtml_post_treat", client, message_elt, body_elt, lang, treat_d
+            ):
                 continue
-            treat_d.addCallback(lambda dummy: self._s.convert(body_elt.toXml(), self.SYNTAX_XHTML_IM, safe=True))
+            treat_d.addCallback(
+                lambda dummy: self._s.convert(
+                    body_elt.toXml(), self.SYNTAX_XHTML_IM, safe=True
+                )
+            )
             treat_d.addCallback(converted, lang)
             defers.append(treat_d)
 
@@ -116,15 +142,15 @@
         return d_list
 
     def _fill_body_text(self, text, data, lang):
-        data['message'][lang or ''] = text
-        message_elt = data['xml']
+        data["message"][lang or ""] = text
+        message_elt = data["xml"]
         body_elt = message_elt.addElement("body", content=text)
         if lang:
-            body_elt[(C.NS_XML, 'lang')] = lang
+            body_elt[(C.NS_XML, "lang")] = lang
 
     def _check_body_text(self, data, lang, markup, syntax, defers):
         """check if simple text message exists, and fill if needed"""
-        if not (lang or '') in data['message']:
+        if not (lang or "") in data["message"]:
             d = self._s.convert(markup, syntax, self._s.SYNTAX_TEXT)
             d.addCallback(self._fill_body_text, data, lang)
             defers.append(d)
@@ -136,30 +162,30 @@
         """
         # at this point, either ['extra']['rich'] or ['extra']['xhtml'] exists
         # but both can't exist at the same time
-        message_elt = data['xml']
-        html_elt = message_elt.addElement((NS_XHTML_IM, 'html'))
+        message_elt = data["xml"]
+        html_elt = message_elt.addElement((NS_XHTML_IM, "html"))
 
         def syntax_converted(xhtml_im, lang):
-            body_elt = html_elt.addElement((NS_XHTML, 'body'))
+            body_elt = html_elt.addElement((NS_XHTML, "body"))
             if lang:
-                body_elt[(C.NS_XML, 'lang')] = lang
-                data['extra']['xhtml_{}'.format(lang)] = xhtml_im
+                body_elt[(C.NS_XML, "lang")] = lang
+                data["extra"]["xhtml_{}".format(lang)] = xhtml_im
             else:
-                data['extra']['xhtml'] = xhtml_im
+                data["extra"]["xhtml"] = xhtml_im
             body_elt.addRawXml(xhtml_im)
 
         syntax = self._s.getCurrentSyntax(client.profile)
         defers = []
-        if u'xhtml' in data['extra']:
+        if u"xhtml" in data["extra"]:
             # we have directly XHTML
-            for lang, xhtml in data_format.getSubDict('xhtml', data['extra']):
+            for lang, xhtml in data_format.getSubDict("xhtml", data["extra"]):
                 self._check_body_text(data, lang, xhtml, self._s.SYNTAX_XHTML, defers)
                 d = self._s.convert(xhtml, self._s.SYNTAX_XHTML, self.SYNTAX_XHTML_IM)
                 d.addCallback(syntax_converted, lang)
                 defers.append(d)
-        elif u'rich' in data['extra']:
+        elif u"rich" in data["extra"]:
             # we have rich syntax to convert
-            for lang, rich_data in data_format.getSubDict('rich', data['extra']):
+            for lang, rich_data in data_format.getSubDict("rich", data["extra"]):
                 self._check_body_text(data, lang, rich_data, syntax, defers)
                 d = self._s.convert(rich_data, syntax, self.SYNTAX_XHTML_IM)
                 d.addCallback(syntax_converted, lang)
@@ -174,12 +200,12 @@
         """ Check presence of XHTML-IM in message
         """
         try:
-            html_elt = message.elements(NS_XHTML_IM, 'html').next()
+            html_elt = message.elements(NS_XHTML_IM, "html").next()
         except StopIteration:
             # No XHTML-IM
             pass
         else:
-            body_elts = html_elt.elements(NS_XHTML, 'body')
+            body_elts = html_elt.elements(NS_XHTML, "body")
             post_treat.addCallback(self._messagePostTreat, message, body_elts, client)
         return True
 
@@ -187,18 +213,20 @@
         """ Check presence of rich text in extra """
         rich = {}
         xhtml = {}
-        for key, value in data['extra'].iteritems():
-            if key.startswith('rich'):
+        for key, value in data["extra"].iteritems():
+            if key.startswith("rich"):
                 rich[key[5:]] = value
-            elif key.startswith('xhtml'):
+            elif key.startswith("xhtml"):
                 xhtml[key[6:]] = value
         if rich and xhtml:
-            raise exceptions.DataError(_(u"Can't have XHTML and rich content at the same time"))
+            raise exceptions.DataError(
+                _(u"Can't have XHTML and rich content at the same time")
+            )
         if rich or xhtml:
             if rich:
-                data['rich'] = rich
+                data["rich"] = rich
             else:
-                data['xhtml'] = xhtml
+                data["xhtml"] = xhtml
             post_xml_treatments.addCallback(self._sendMessageAddRich, client)
         return True
 
@@ -208,7 +236,7 @@
         """
         purged = []
 
-        styles = [style.strip().split(':') for style in styles_raw.split(';')]
+        styles = [style.strip().split(":") for style in styles_raw.split(";")]
 
         for style_tuple in styles:
             if len(style_tuple) != 2:
@@ -219,7 +247,7 @@
                 continue
             purged.append((name, value.strip()))
 
-        return u'; '.join([u"%s: %s" % data for data in purged])
+        return u"; ".join([u"%s: %s" % data for data in purged])
 
     def XHTML2XHTML_IM(self, xhtml):
         """ Convert XHTML document to XHTML_IM subset
@@ -227,12 +255,12 @@
         """
         # TODO: more clever tag replacement (replace forbidden tags with equivalents when possible)
 
-        parser = html.HTMLParser(remove_comments=True, encoding='utf-8')
+        parser = html.HTMLParser(remove_comments=True, encoding="utf-8")
         root = html.fromstring(xhtml, parser=parser)
-        body_elt = root.find('body')
+        body_elt = root.find("body")
         if body_elt is None:
             # we use the whole XML as body if no body element is found
-            body_elt = html.Element('body')
+            body_elt = html.Element("body")
             body_elt.append(root)
         else:
             body_elt.attrib.clear()
@@ -247,24 +275,25 @@
                 attrib = elem.attrib
                 att_to_remove = set(attrib).difference(allowed[elem.tag])
                 for att in att_to_remove:
-                    del(attrib[att])
+                    del (attrib[att])
                 if "style" in attrib:
                     attrib["style"] = self._purgeStyle(attrib["style"])
 
         for elem in to_strip:
             if elem.tag in blacklist:
-                #we need to remove the element and all descendants
+                # we need to remove the element and all descendants
                 log.debug(u"removing black listed tag: %s" % (elem.tag))
                 elem.drop_tree()
             else:
                 elem.drop_tag()
-        if len(body_elt) !=1:
+        if len(body_elt) != 1:
             root_elt = body_elt
             body_elt.tag = "p"
         else:
             root_elt = body_elt[0]
 
-        return html.tostring(root_elt, encoding='unicode', method='xml')
+        return html.tostring(root_elt, encoding="unicode", method="xml")
+
 
 class XEP_0071_handler(XMPPHandler):
     implements(iwokkel.IDisco)
@@ -273,8 +302,8 @@
         self.plugin_parent = plugin_parent
         self.host = plugin_parent.host
 
-    def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
+    def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
         return [disco.DiscoFeature(NS_XHTML_IM)]
 
-    def getDiscoItems(self, requestor, target, nodeIdentifier=''):
+    def getDiscoItems(self, requestor, target, nodeIdentifier=""):
         return []