comparison sat_frontends/jp/cmd_adhoc.py @ 3040:fee60f17ebac

jp: jp asyncio port: /!\ this commit is huge. Jp is temporarily not working with `dbus` bridge /!\ This patch implements the port of jp to asyncio, so it is now correctly using the bridge asynchronously, and it can be used with bridges like `pb`. This also simplify the code, notably for things which were previously implemented with many callbacks (like pagination with RSM). During the process, some behaviours have been modified/fixed, in jp and backends, check diff for details.
author Goffi <goffi@goffi.org>
date Wed, 25 Sep 2019 08:56:41 +0200
parents ab2696e34d29
children 9d0df638c8b4
comparison
equal deleted inserted replaced
3039:a1bc34f90fa5 3040:fee60f17ebac
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from . import base 20 from . import base
21 from sat.core.i18n import _ 21 from sat.core.i18n import _
22 from functools import partial
23 from sat_frontends.jp.constants import Const as C 22 from sat_frontends.jp.constants import Const as C
24 from sat_frontends.jp import xmlui_manager 23 from sat_frontends.jp import xmlui_manager
25 24
26 __commands__ = ["AdHoc"] 25 __commands__ = ["AdHoc"]
27 26
65 ) 64 )
66 self.parser.add_argument( 65 self.parser.add_argument(
67 "-l", "--loop", action="store_true", help=_("loop on the commands") 66 "-l", "--loop", action="store_true", help=_("loop on the commands")
68 ) 67 )
69 68
70 def start(self): 69 async def start(self):
71 name = self.args.software.lower() 70 name = self.args.software.lower()
72 flags = [] 71 flags = []
73 magics = {jid for jid in self.args.jids if jid.count("@") > 1} 72 magics = {jid for jid in self.args.jids if jid.count("@") > 1}
74 magics.add(MAGIC_BAREJID) 73 magics.add(MAGIC_BAREJID)
75 jids = set(self.args.jids).difference(magics) 74 jids = set(self.args.jids).difference(magics)
76 if self.args.loop: 75 if self.args.loop:
77 flags.append(FLAG_LOOP) 76 flags.append(FLAG_LOOP)
78 bus_name, methods = self.host.bridge.adHocDBusAddAuto( 77 try:
79 name, 78 bus_name, methods = await self.host.bridge.adHocDBusAddAuto(
80 jids, 79 name,
81 self.args.groups, 80 list(jids),
82 magics, 81 self.args.groups,
83 self.args.forbidden_jids, 82 magics,
84 self.args.forbidden_groups, 83 self.args.forbidden_jids,
85 flags, 84 self.args.forbidden_groups,
86 self.profile, 85 flags,
87 ) 86 self.profile,
88 if not bus_name:
89 self.disp(_("No bus name found"), 1)
90 return
91 self.disp(_("Bus name found: [%s]" % bus_name), 1)
92 for method in methods:
93 path, iface, command = method
94 self.disp(
95 _(
96 "Command found: (path:%(path)s, iface: %(iface)s) [%(command)s]"
97 % {"path": path, "iface": iface, "command": command}
98 ),
99 1,
100 ) 87 )
88 except Exception as e:
89 self.disp(f"can't create remote control: {e}", error=True)
90 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
91 else:
92 if not bus_name:
93 self.disp(_("No bus name found"), 1)
94 self.host.quit(C.EXIT_NOT_FOUND)
95 else:
96 self.disp(_("Bus name found: [%s]" % bus_name), 1)
97 for method in methods:
98 path, iface, command = method
99 self.disp(
100 _(f"Command found: (path:{path}, iface: {iface}) [{command}]"),
101 1,
102 )
103 self.host.quit()
101 104
102 105
103 class Run(base.CommandBase): 106 class Run(base.CommandBase):
104 """Run an Ad-Hoc command""" 107 """Run an Ad-Hoc command"""
105 108
106 def __init__(self, host): 109 def __init__(self, host):
107 super(Run, self).__init__( 110 super(Run, self).__init__(
108 host, "run", use_verbose=True, help=_("run an Ad-Hoc command") 111 host, "run", use_verbose=True, help=_("run an Ad-Hoc command")
109 ) 112 )
110 self.need_loop = True
111 113
112 def add_parser_options(self): 114 def add_parser_options(self):
113 self.parser.add_argument( 115 self.parser.add_argument(
114 "-j", 116 "-j",
115 "--jid", 117 "--jid",
138 nargs="?", 140 nargs="?",
139 default="", 141 default="",
140 help=_("node of the command (default: list commands)"), 142 help=_("node of the command (default: list commands)"),
141 ) 143 )
142 144
143 def adHocRunCb(self, xmlui_raw): 145 async def start(self):
144 xmlui = xmlui_manager.create(self.host, xmlui_raw) 146 try:
145 workflow = self.args.workflow 147 xmlui_raw = await self.host.bridge.adHocRun(
146 xmlui.show(workflow) 148 self.args.jid,
147 if not workflow: 149 self.args.node,
148 if xmlui.type == "form": 150 self.profile,
149 xmlui.submitForm() 151 )
150 else: 152 except Exception as e:
151 self.host.quit() 153 self.disp(f"can't get ad-hoc commands list: {e}", error=True)
152 154 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
153 def start(self): 155 else:
154 self.host.bridge.adHocRun( 156 xmlui = xmlui_manager.create(self.host, xmlui_raw)
155 self.args.jid, 157 workflow = self.args.workflow
156 self.args.node, 158 await xmlui.show(workflow)
157 self.profile, 159 if not workflow:
158 callback=self.adHocRunCb, 160 if xmlui.type == "form":
159 errback=partial( 161 await xmlui.submitForm()
160 self.errback, 162 self.host.quit()
161 msg=_("can't get ad-hoc commands list: {}"),
162 exit_code=C.EXIT_BRIDGE_ERRBACK,
163 ),
164 )
165 163
166 164
167 class List(base.CommandBase): 165 class List(base.CommandBase):
168 """Run an Ad-Hoc command""" 166 """Run an Ad-Hoc command"""
169 167
170 def __init__(self, host): 168 def __init__(self, host):
171 super(List, self).__init__( 169 super(List, self).__init__(
172 host, "list", use_verbose=True, help=_("list Ad-Hoc commands of a service") 170 host, "list", use_verbose=True, help=_("list Ad-Hoc commands of a service")
173 ) 171 )
174 self.need_loop = True
175 172
176 def add_parser_options(self): 173 def add_parser_options(self):
177 self.parser.add_argument( 174 self.parser.add_argument(
178 "-j", 175 "-j",
179 "--jid", 176 "--jid",
180 default="", 177 default="",
181 help=_("jid of the service (default: profile's server"), 178 help=_("jid of the service (default: profile's server"),
182 ) 179 )
183 180
184 def adHocListCb(self, xmlui_raw): 181 async def start(self):
185 xmlui = xmlui_manager.create(self.host, xmlui_raw) 182 try:
186 xmlui.readonly = True 183 xmlui_raw = await self.host.bridge.adHocList(
187 xmlui.show() 184 self.args.jid,
188 self.host.quit() 185 self.profile,
189 186 )
190 def start(self): 187 except Exception as e:
191 self.host.bridge.adHocList( 188 self.disp(f"can't get ad-hoc commands list: {e}", error=True)
192 self.args.jid, 189 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
193 self.profile, 190 else:
194 callback=self.adHocListCb, 191 xmlui = xmlui_manager.create(self.host, xmlui_raw)
195 errback=partial( 192 await xmlui.show(read_only=True)
196 self.errback, 193 self.host.quit()
197 msg=_("can't get ad-hoc commands list: {}"),
198 exit_code=C.EXIT_BRIDGE_ERRBACK,
199 ),
200 )
201 194
202 195
203 class AdHoc(base.CommandBase): 196 class AdHoc(base.CommandBase):
204 subcommands = (Run, List, Remote) 197 subcommands = (Run, List, Remote)
205 198