comparison sat/plugins/plugin_exp_command_export.py @ 4037:524856bd7b19

massive refactoring to switch from camelCase to snake_case: historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a pre-PEP8 code, to use the same coding style as in Twisted. However, snake_case is more readable and it's better to follow PEP8 best practices, so it has been decided to move on full snake_case. Because Libervia has a huge codebase, this ended with a ugly mix of camelCase and snake_case. To fix that, this patch does a big refactoring by renaming every function and method (including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case. This is a massive change, and may result in some bugs.
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 13:54:42 +0200
parents be6d91572633
children c23cad65ae99
comparison
equal deleted inserted replaced
4036:c4464d7ae97b 4037:524856bd7b19
70 self.parent.removeProcess(self.target, self) 70 self.parent.removeProcess(self.target, self)
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 boolOption(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, "")
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("messageReceived", self.messageReceivedTrigger, priority=10000) 95 host.trigger.add("messageReceived", self.message_received_trigger, priority=10000)
96 host.bridge.addMethod( 96 host.bridge.add_method(
97 "exportCommand", 97 "command_export",
98 ".plugin", 98 ".plugin",
99 in_sign="sasasa{ss}s", 99 in_sign="sasasa{ss}s",
100 out_sign="", 100 out_sign="",
101 method=self._exportCommand, 101 method=self._export_command,
102 ) 102 )
103 103
104 def removeProcess(self, entity, process): 104 def removeProcess(self, entity, process):
105 """ Called when the process is finished 105 """ Called when the process is finished
106 @param entity: jid.JID attached to the process 106 @param entity: jid.JID attached to the process
111 if not processes_set: 111 if not processes_set:
112 del (self.spawned[(entity, process.client.profile)]) 112 del (self.spawned[(entity, process.client.profile)])
113 except ValueError: 113 except ValueError:
114 pass 114 pass
115 115
116 def messageReceivedTrigger(self, client, message_elt, post_treat): 116 def message_received_trigger(self, client, message_elt, post_treat):
117 """ Check if source is linked and repeat message, else do nothing """ 117 """ Check if source is linked and repeat message, else do nothing """
118 from_jid = jid.JID(message_elt["from"]) 118 from_jid = jid.JID(message_elt["from"])
119 spawned_key = (from_jid.userhostJID(), client.profile) 119 spawned_key = (from_jid.userhostJID(), client.profile)
120 120
121 if spawned_key in self.spawned: 121 if spawned_key in self.spawned:
129 processes_set = self.spawned[spawned_key] 129 processes_set = self.spawned[spawned_key]
130 _continue = False 130 _continue = False
131 exclusive = False 131 exclusive = False
132 for process in processes_set: 132 for process in processes_set:
133 process.write(mess_data) 133 process.write(mess_data)
134 _continue &= process.boolOption("continue") 134 _continue &= process.bool_option("continue")
135 exclusive |= process.boolOption("exclusive") 135 exclusive |= process.bool_option("exclusive")
136 if exclusive: 136 if exclusive:
137 raise trigger.SkipOtherTriggers 137 raise trigger.SkipOtherTriggers
138 return _continue 138 return _continue
139 139
140 return True 140 return True
141 141
142 def _exportCommand(self, command, args, targets, options, profile_key): 142 def _export_command(self, command, args, targets, options, profile_key):
143 """ Export a commands to authorised targets 143 """ Export a commands to authorised targets
144 @param command: full path of the command to execute 144 @param command: full path of the command to execute
145 @param args: list of arguments, with command name as first one 145 @param args: list of arguments, with command name as first one
146 @param targets: list of allowed entities 146 @param targets: list of allowed entities
147 @param options: export options, a dict which can have the following keys ("true" to set booleans): 147 @param options: export options, a dict which can have the following keys ("true" to set booleans):
148 - exclusive: if set, skip all other triggers 148 - exclusive: if set, skip all other triggers
149 - loop: if set, restart the command once terminated #TODO 149 - loop: if set, restart the command once terminated #TODO
150 - pty: if set, launch in a pseudo terminal 150 - pty: if set, launch in a pseudo terminal
151 - continue: continue normal messageReceived handling 151 - continue: continue normal messageReceived handling
152 """ 152 """
153 client = self.host.getClient(profile_key) 153 client = self.host.get_client(profile_key)
154 for target in targets: 154 for target in targets:
155 try: 155 try:
156 _jid = jid.JID(target) 156 _jid = jid.JID(target)
157 if not _jid.user or not _jid.host: 157 if not _jid.user or not _jid.host:
158 raise jid.InvalidFormat 158 raise jid.InvalidFormat
161 log.info("invalid target ignored: %s" % (target,)) 161 log.info("invalid target ignored: %s" % (target,))
162 continue 162 continue
163 process_prot = ExportCommandProtocol(self, client, _jid, options) 163 process_prot = ExportCommandProtocol(self, client, _jid, options)
164 self.spawned.setdefault((_jid, client.profile), set()).add(process_prot) 164 self.spawned.setdefault((_jid, client.profile), set()).add(process_prot)
165 reactor.spawnProcess( 165 reactor.spawnProcess(
166 process_prot, command, args, usePTY=process_prot.boolOption("pty") 166 process_prot, command, args, usePTY=process_prot.bool_option("pty")
167 ) 167 )