comparison libervia/backend/plugins/plugin_exp_command_export.py @ 4270:0d7bb4df2343

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents 4b842c1fb686
children
comparison
equal deleted inserted replaced
4269:64a85ce8be70 4270:0d7bb4df2343
39 C.PI_DESCRIPTION: _("""Implementation of command export"""), 39 C.PI_DESCRIPTION: _("""Implementation of command export"""),
40 } 40 }
41 41
42 42
43 class ExportCommandProtocol(protocol.ProcessProtocol): 43 class ExportCommandProtocol(protocol.ProcessProtocol):
44 """ Try to register an account with prosody """ 44 """Try to register an account with prosody"""
45 45
46 def __init__(self, parent, client, target, options): 46 def __init__(self, parent, client, target, options):
47 self.parent = parent 47 self.parent = parent
48 self.target = target 48 self.target = target
49 self.options = options 49 self.options = options
71 71
72 def write(self, message): 72 def write(self, message):
73 self.transport.write(message.encode("utf-8")) 73 self.transport.write(message.encode("utf-8"))
74 74
75 def bool_option(self, key): 75 def bool_option(self, key):
76 """ Get boolean value from options 76 """Get boolean value from options
77 @param key: name of the option 77 @param key: name of the option
78 @return: True if key exists and set to "true" (case insensitive), 78 @return: True if key exists and set to "true" (case insensitive),
79 False in all other cases """ 79 False in all other cases"""
80 value = self.options.get(key, "") 80 value = self.options.get(key, "")
81 return value.lower() == "true" 81 return value.lower() == "true"
82 82
83 83
84 class CommandExport(object): 84 class CommandExport(object):
90 90
91 def __init__(self, host): 91 def __init__(self, host):
92 log.info(_("Plugin command export initialization")) 92 log.info(_("Plugin command export initialization"))
93 self.host = host 93 self.host = host
94 self.spawned = {} # key = entity 94 self.spawned = {} # key = entity
95 host.trigger.add("message_received", self.message_received_trigger, priority=10000) 95 host.trigger.add(
96 "message_received", self.message_received_trigger, priority=10000
97 )
96 host.bridge.add_method( 98 host.bridge.add_method(
97 "command_export", 99 "command_export",
98 ".plugin", 100 ".plugin",
99 in_sign="sasasa{ss}s", 101 in_sign="sasasa{ss}s",
100 out_sign="", 102 out_sign="",
101 method=self._export_command, 103 method=self._export_command,
102 ) 104 )
103 105
104 def removeProcess(self, entity, process): 106 def removeProcess(self, entity, process):
105 """ Called when the process is finished 107 """Called when the process is finished
106 @param entity: jid.JID attached to the process 108 @param entity: jid.JID attached to the process
107 @param process: process to remove""" 109 @param process: process to remove"""
108 try: 110 try:
109 processes_set = self.spawned[(entity, process.client.profile)] 111 processes_set = self.spawned[(entity, process.client.profile)]
110 processes_set.discard(process) 112 processes_set.discard(process)
111 if not processes_set: 113 if not processes_set:
112 del (self.spawned[(entity, process.client.profile)]) 114 del self.spawned[(entity, process.client.profile)]
113 except ValueError: 115 except ValueError:
114 pass 116 pass
115 117
116 def message_received_trigger(self, client, message_elt, post_treat): 118 def message_received_trigger(self, client, message_elt, post_treat):
117 """ Check if source is linked and repeat message, else do nothing """ 119 """Check if source is linked and repeat message, else do nothing"""
118 from_jid = jid.JID(message_elt["from"]) 120 from_jid = jid.JID(message_elt["from"])
119 spawned_key = (from_jid.userhostJID(), client.profile) 121 spawned_key = (from_jid.userhostJID(), client.profile)
120 122
121 if spawned_key in self.spawned: 123 if spawned_key in self.spawned:
122 try: 124 try:
138 return _continue 140 return _continue
139 141
140 return True 142 return True
141 143
142 def _export_command(self, command, args, targets, options, profile_key): 144 def _export_command(self, command, args, targets, options, profile_key):
143 """ Export a commands to authorised targets 145 """Export a commands to authorised targets
144 @param command: full path of the command to execute 146 @param command: full path of the command to execute
145 @param args: list of arguments, with command name as first one 147 @param args: list of arguments, with command name as first one
146 @param targets: list of allowed entities 148 @param targets: list of allowed entities
147 @param options: export options, a dict which can have the following keys ("true" to set booleans): 149 @param options: export options, a dict which can have the following keys ("true" to set booleans):
148 - exclusive: if set, skip all other triggers 150 - exclusive: if set, skip all other triggers