code style reformatting using black

This commit is contained in:
Goffi 2018-06-27 20:14:46 +02:00
commit 744cb0c503

View file

@ -20,6 +20,7 @@
from sat.core.i18n import _
from sat.core.constants import Const as C
from sat.core.log import getLogger
log = getLogger(__name__)
from twisted.words.protocols.jabber import jid
from twisted.internet import reactor, protocol
@ -35,9 +36,10 @@ PLUGIN_INFO = {
C.PI_DEPENDENCIES: [],
C.PI_MAIN: "CommandExport",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _("""Implementation of command export""")
C.PI_DESCRIPTION: _("""Implementation of command export"""),
}
class ExportCommandProtocol(protocol.ProcessProtocol):
""" Try to register an account with prosody """
@ -49,26 +51,26 @@ class ExportCommandProtocol(protocol.ProcessProtocol):
def _clean(self, data):
if not data:
log.error ("data should not be empty !")
log.error("data should not be empty !")
return u""
decoded = data.decode('utf-8', 'ignore')[:-1 if data[-1] == '\n' else None]
decoded = data.decode("utf-8", "ignore")[: -1 if data[-1] == "\n" else None]
return clean_ustr(decoded)
def connectionMade(self):
log.info("connectionMade :)")
def outReceived(self, data):
self.client.sendMessage(self.target, {'': self._clean(data)}, no_trigger=True)
self.client.sendMessage(self.target, {"": self._clean(data)}, no_trigger=True)
def errReceived(self, data):
self.client.sendMessage(self.target, {'': self._clean(data)}, no_trigger=True)
self.client.sendMessage(self.target, {"": self._clean(data)}, no_trigger=True)
def processEnded(self, reason):
log.info (u"process finished: %d" % (reason.value.exitCode,))
log.info(u"process finished: %d" % (reason.value.exitCode,))
self.parent.removeProcess(self.target, self)
def write(self, message):
self.transport.write(message.encode('utf-8'))
self.transport.write(message.encode("utf-8"))
def boolOption(self, key):
""" Get boolean value from options
@ -81,6 +83,7 @@ class ExportCommandProtocol(protocol.ProcessProtocol):
class CommandExport(object):
"""Command export plugin: export a command to an entity"""
# XXX: This plugin can be potentially dangerous if we don't trust entities linked
# this is specially true if we have other triggers.
# FIXME: spawned should be a client attribute, not a class one
@ -88,9 +91,15 @@ class CommandExport(object):
def __init__(self, host):
log.info(_("Plugin command export initialization"))
self.host = host
self.spawned = {} # key = entity
self.spawned = {} # key = entity
host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=10000)
host.bridge.addMethod("exportCommand", ".plugin", in_sign='sasasa{ss}s', out_sign='', method=self._exportCommand)
host.bridge.addMethod(
"exportCommand",
".plugin",
in_sign="sasasa{ss}s",
out_sign="",
method=self._exportCommand,
)
def removeProcess(self, entity, process):
""" Called when the process is finished
@ -100,7 +109,7 @@ class CommandExport(object):
processes_set = self.spawned[(entity, process.client.profile)]
processes_set.discard(process)
if not processes_set:
del(self.spawned[(entity, process.client.profile)])
del (self.spawned[(entity, process.client.profile)])
except ValueError:
pass
@ -111,12 +120,12 @@ class CommandExport(object):
if spawned_key in self.spawned:
try:
body = message_elt.elements(C.NS_CLIENT, 'body').next()
body = message_elt.elements(C.NS_CLIENT, "body").next()
except StopIteration:
# do not block message without body (chat state notification...)
return True
mess_data = unicode(body) + '\n'
mess_data = unicode(body) + "\n"
processes_set = self.spawned[spawned_key]
_continue = False
exclusive = False
@ -152,5 +161,7 @@ class CommandExport(object):
log.info(u"invalid target ignored: %s" % (target,))
continue
process_prot = ExportCommandProtocol(self, client, _jid, options)
self.spawned.setdefault((_jid, client.profile),set()).add(process_prot)
reactor.spawnProcess(process_prot, command, args, usePTY = process_prot.boolOption('pty'))
self.spawned.setdefault((_jid, client.profile), set()).add(process_prot)
reactor.spawnProcess(
process_prot, command, args, usePTY=process_prot.boolOption("pty")
)