comparison libervia/cli/cmd_adhoc.py @ 4075:47401850dec6

refactoring: rename `libervia.frontends.jp` to `libervia.cli`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 14:54:26 +0200
parents libervia/frontends/jp/cmd_adhoc.py@26b7ed2817da
children 319a0e47dc8b
comparison
equal deleted inserted replaced
4074:26b7ed2817da 4075:47401850dec6
1 #!/usr/bin/env python3
2
3
4 # Libervia CLI
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
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/>.
19
20 from . import base
21 from libervia.backend.core.i18n import _
22 from libervia.cli.constants import Const as C
23 from libervia.cli import xmlui_manager
24
25 __commands__ = ["AdHoc"]
26
27 FLAG_LOOP = "LOOP"
28 MAGIC_BAREJID = "@PROFILE_BAREJID@"
29
30
31 class Remote(base.CommandBase):
32 def __init__(self, host):
33 super(Remote, self).__init__(
34 host, "remote", use_verbose=True, help=_("remote control a software")
35 )
36
37 def add_parser_options(self):
38 self.parser.add_argument("software", type=str, help=_("software name"))
39 self.parser.add_argument(
40 "-j",
41 "--jids",
42 nargs="*",
43 default=[],
44 help=_("jids allowed to use the command"),
45 )
46 self.parser.add_argument(
47 "-g",
48 "--groups",
49 nargs="*",
50 default=[],
51 help=_("groups allowed to use the command"),
52 )
53 self.parser.add_argument(
54 "--forbidden-groups",
55 nargs="*",
56 default=[],
57 help=_("groups that are *NOT* allowed to use the command"),
58 )
59 self.parser.add_argument(
60 "--forbidden-jids",
61 nargs="*",
62 default=[],
63 help=_("jids that are *NOT* allowed to use the command"),
64 )
65 self.parser.add_argument(
66 "-l", "--loop", action="store_true", help=_("loop on the commands")
67 )
68
69 async def start(self):
70 name = self.args.software.lower()
71 flags = []
72 magics = {jid for jid in self.args.jids if jid.count("@") > 1}
73 magics.add(MAGIC_BAREJID)
74 jids = set(self.args.jids).difference(magics)
75 if self.args.loop:
76 flags.append(FLAG_LOOP)
77 try:
78 bus_name, methods = await self.host.bridge.ad_hoc_dbus_add_auto(
79 name,
80 list(jids),
81 self.args.groups,
82 magics,
83 self.args.forbidden_jids,
84 self.args.forbidden_groups,
85 flags,
86 self.profile,
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 _("Command found: (path:{path}, iface: {iface}) [{command}]")
101 .format(path=path, iface=iface, command=command),
102 1,
103 )
104 self.host.quit()
105
106
107 class Run(base.CommandBase):
108 """Run an Ad-Hoc command"""
109
110 def __init__(self, host):
111 super(Run, self).__init__(
112 host, "run", use_verbose=True, help=_("run an Ad-Hoc command")
113 )
114
115 def add_parser_options(self):
116 self.parser.add_argument(
117 "-j",
118 "--jid",
119 default="",
120 help=_("jid of the service (default: profile's server"),
121 )
122 self.parser.add_argument(
123 "-S",
124 "--submit",
125 action="append_const",
126 const=xmlui_manager.SUBMIT,
127 dest="workflow",
128 help=_("submit form/page"),
129 )
130 self.parser.add_argument(
131 "-f",
132 "--field",
133 action="append",
134 nargs=2,
135 dest="workflow",
136 metavar=("KEY", "VALUE"),
137 help=_("field value"),
138 )
139 self.parser.add_argument(
140 "node",
141 nargs="?",
142 default="",
143 help=_("node of the command (default: list commands)"),
144 )
145
146 async def start(self):
147 try:
148 xmlui_raw = await self.host.bridge.ad_hoc_run(
149 self.args.jid,
150 self.args.node,
151 self.profile,
152 )
153 except Exception as e:
154 self.disp(f"can't get ad-hoc commands list: {e}", error=True)
155 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
156 else:
157 xmlui = xmlui_manager.create(self.host, xmlui_raw)
158 workflow = self.args.workflow
159 await xmlui.show(workflow)
160 if not workflow:
161 if xmlui.type == "form":
162 await xmlui.submit_form()
163 self.host.quit()
164
165
166 class List(base.CommandBase):
167 """List Ad-Hoc commands available on a service"""
168
169 def __init__(self, host):
170 super(List, self).__init__(
171 host, "list", use_verbose=True, help=_("list Ad-Hoc commands of a service")
172 )
173
174 def add_parser_options(self):
175 self.parser.add_argument(
176 "-j",
177 "--jid",
178 default="",
179 help=_("jid of the service (default: profile's server)"),
180 )
181
182 async def start(self):
183 try:
184 xmlui_raw = await self.host.bridge.ad_hoc_list(
185 self.args.jid,
186 self.profile,
187 )
188 except Exception as e:
189 self.disp(f"can't get ad-hoc commands list: {e}", error=True)
190 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
191 else:
192 xmlui = xmlui_manager.create(self.host, xmlui_raw)
193 await xmlui.show(read_only=True)
194 self.host.quit()
195
196
197 class AdHoc(base.CommandBase):
198 subcommands = (Run, List, Remote)
199
200 def __init__(self, host):
201 super(AdHoc, self).__init__(
202 host, "ad-hoc", use_profile=False, help=_("Ad-hoc commands")
203 )