comparison libervia/cli/cmd_profile.py @ 4270:0d7bb4df2343

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents 47401850dec6
children
comparison
equal deleted inserted replaced
4269:64a85ce8be70 4270:0d7bb4df2343
28 log = getLogger(__name__) 28 log = getLogger(__name__)
29 29
30 30
31 __commands__ = ["Profile"] 31 __commands__ = ["Profile"]
32 32
33 PROFILE_HELP = _('The name of the profile') 33 PROFILE_HELP = _("The name of the profile")
34 34
35 35
36 class ProfileConnect(base.CommandBase): 36 class ProfileConnect(base.CommandBase):
37 """Dummy command to use profile_session parent, i.e. to be able to connect without doing anything else""" 37 """Dummy command to use profile_session parent, i.e. to be able to connect without doing anything else"""
38 38
39 def __init__(self, host): 39 def __init__(self, host):
40 # it's weird to have a command named "connect" with need_connect=False, but it can be handy to be able 40 # it's weird to have a command named "connect" with need_connect=False, but it can be handy to be able
41 # to launch just the session, so some paradoxes don't hurt 41 # to launch just the session, so some paradoxes don't hurt
42 super(ProfileConnect, self).__init__(host, 'connect', need_connect=False, help=('connect a profile')) 42 super(ProfileConnect, self).__init__(
43 host, "connect", need_connect=False, help=("connect a profile")
44 )
43 45
44 def add_parser_options(self): 46 def add_parser_options(self):
45 pass 47 pass
46 48
47 async def start(self): 49 async def start(self):
49 # so we just need to check arguments and quit 51 # so we just need to check arguments and quit
50 if not self.args.connect and not self.args.start_session: 52 if not self.args.connect and not self.args.start_session:
51 self.parser.error(_("You need to use either --connect or --start-session")) 53 self.parser.error(_("You need to use either --connect or --start-session"))
52 self.host.quit() 54 self.host.quit()
53 55
56
54 class ProfileDisconnect(base.CommandBase): 57 class ProfileDisconnect(base.CommandBase):
55 58
56 def __init__(self, host): 59 def __init__(self, host):
57 super(ProfileDisconnect, self).__init__(host, 'disconnect', need_connect=False, help=('disconnect a profile')) 60 super(ProfileDisconnect, self).__init__(
61 host, "disconnect", need_connect=False, help=("disconnect a profile")
62 )
58 63
59 def add_parser_options(self): 64 def add_parser_options(self):
60 pass 65 pass
61 66
62 async def start(self): 67 async def start(self):
70 75
71 76
72 class ProfileCreate(base.CommandBase): 77 class ProfileCreate(base.CommandBase):
73 def __init__(self, host): 78 def __init__(self, host):
74 super(ProfileCreate, self).__init__( 79 super(ProfileCreate, self).__init__(
75 host, 'create', use_profile=False, help=('create a new profile')) 80 host, "create", use_profile=False, help=("create a new profile")
76 81 )
77 def add_parser_options(self): 82
78 self.parser.add_argument('profile', type=str, help=_('the name of the profile')) 83 def add_parser_options(self):
79 self.parser.add_argument( 84 self.parser.add_argument("profile", type=str, help=_("the name of the profile"))
80 '-p', '--password', type=str, default='', 85 self.parser.add_argument(
81 help=_('the password of the profile')) 86 "-p",
82 self.parser.add_argument( 87 "--password",
83 '-j', '--jid', type=str, help=_('the jid of the profile')) 88 type=str,
84 self.parser.add_argument( 89 default="",
85 '-x', '--xmpp-password', type=str, 90 help=_("the password of the profile"),
91 )
92 self.parser.add_argument(
93 "-j", "--jid", type=str, help=_("the jid of the profile")
94 )
95 self.parser.add_argument(
96 "-x",
97 "--xmpp-password",
98 type=str,
86 help=_( 99 help=_(
87 'the password of the XMPP account (use profile password if not specified)' 100 "the password of the XMPP account (use profile password if not specified)"
88 ), 101 ),
89 metavar='PASSWORD') 102 metavar="PASSWORD",
90 self.parser.add_argument( 103 )
91 '-A', '--autoconnect', choices=[C.BOOL_TRUE, C.BOOL_FALSE], nargs='?', 104 self.parser.add_argument(
105 "-A",
106 "--autoconnect",
107 choices=[C.BOOL_TRUE, C.BOOL_FALSE],
108 nargs="?",
92 const=C.BOOL_TRUE, 109 const=C.BOOL_TRUE,
93 help=_('connect this profile automatically when backend starts') 110 help=_("connect this profile automatically when backend starts"),
94 ) 111 )
95 self.parser.add_argument( 112 self.parser.add_argument(
96 '-C', '--component', default='', 113 "-C",
97 help=_('set to component import name (entry point) if this is a component')) 114 "--component",
115 default="",
116 help=_("set to component import name (entry point) if this is a component"),
117 )
98 118
99 async def start(self): 119 async def start(self):
100 """Create a new profile""" 120 """Create a new profile"""
101 if self.args.profile in await self.host.bridge.profiles_list_get(): 121 if self.args.profile in await self.host.bridge.profiles_list_get():
102 self.disp(f"Profile {self.args.profile} already exists.", error=True) 122 self.disp(f"Profile {self.args.profile} already exists.", error=True)
103 self.host.quit(C.EXIT_BRIDGE_ERROR) 123 self.host.quit(C.EXIT_BRIDGE_ERROR)
104 try: 124 try:
105 await self.host.bridge.profile_create( 125 await self.host.bridge.profile_create(
106 self.args.profile, self.args.password, self.args.component) 126 self.args.profile, self.args.password, self.args.component
127 )
107 except Exception as e: 128 except Exception as e:
108 self.disp(f"can't create profile: {e}", error=True) 129 self.disp(f"can't create profile: {e}", error=True)
109 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 130 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
110 131
111 try: 132 try:
112 await self.host.bridge.profile_start_session( 133 await self.host.bridge.profile_start_session(
113 self.args.password, self.args.profile) 134 self.args.password, self.args.profile
135 )
114 except Exception as e: 136 except Exception as e:
115 self.disp(f"can't start profile session: {e}", error=True) 137 self.disp(f"can't start profile session: {e}", error=True)
116 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 138 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
117 139
118 if self.args.jid: 140 if self.args.jid:
119 await self.host.bridge.param_set( 141 await self.host.bridge.param_set(
120 "JabberID", self.args.jid, "Connection", profile_key=self.args.profile) 142 "JabberID", self.args.jid, "Connection", profile_key=self.args.profile
143 )
121 xmpp_pwd = self.args.password or self.args.xmpp_password 144 xmpp_pwd = self.args.password or self.args.xmpp_password
122 if xmpp_pwd: 145 if xmpp_pwd:
123 await self.host.bridge.param_set( 146 await self.host.bridge.param_set(
124 "Password", xmpp_pwd, "Connection", profile_key=self.args.profile) 147 "Password", xmpp_pwd, "Connection", profile_key=self.args.profile
148 )
125 149
126 if self.args.autoconnect is not None: 150 if self.args.autoconnect is not None:
127 await self.host.bridge.param_set( 151 await self.host.bridge.param_set(
128 "autoconnect_backend", self.args.autoconnect, "Connection", 152 "autoconnect_backend",
129 profile_key=self.args.profile) 153 self.args.autoconnect,
130 154 "Connection",
131 self.disp(f'profile {self.args.profile} created successfully', 1) 155 profile_key=self.args.profile,
156 )
157
158 self.disp(f"profile {self.args.profile} created successfully", 1)
132 self.host.quit() 159 self.host.quit()
133 160
134 161
135 class ProfileDefault(base.CommandBase): 162 class ProfileDefault(base.CommandBase):
136 def __init__(self, host): 163 def __init__(self, host):
137 super(ProfileDefault, self).__init__( 164 super(ProfileDefault, self).__init__(
138 host, 'default', use_profile=False, help=('print default profile')) 165 host, "default", use_profile=False, help=("print default profile")
166 )
139 167
140 def add_parser_options(self): 168 def add_parser_options(self):
141 pass 169 pass
142 170
143 async def start(self): 171 async def start(self):
144 print(await self.host.bridge.profile_name_get('@DEFAULT@')) 172 print(await self.host.bridge.profile_name_get("@DEFAULT@"))
145 self.host.quit() 173 self.host.quit()
146 174
147 175
148 class ProfileDelete(base.CommandBase): 176 class ProfileDelete(base.CommandBase):
149 def __init__(self, host): 177 def __init__(self, host):
150 super(ProfileDelete, self).__init__(host, 'delete', use_profile=False, help=('delete a profile')) 178 super(ProfileDelete, self).__init__(
151 179 host, "delete", use_profile=False, help=("delete a profile")
152 def add_parser_options(self): 180 )
153 self.parser.add_argument('profile', type=str, help=PROFILE_HELP) 181
154 self.parser.add_argument('-f', '--force', action='store_true', help=_('delete profile without confirmation')) 182 def add_parser_options(self):
183 self.parser.add_argument("profile", type=str, help=PROFILE_HELP)
184 self.parser.add_argument(
185 "-f",
186 "--force",
187 action="store_true",
188 help=_("delete profile without confirmation"),
189 )
155 190
156 async def start(self): 191 async def start(self):
157 if self.args.profile not in await self.host.bridge.profiles_list_get(): 192 if self.args.profile not in await self.host.bridge.profiles_list_get():
158 log.error(f"Profile {self.args.profile} doesn't exist.") 193 log.error(f"Profile {self.args.profile} doesn't exist.")
159 self.host.quit(C.EXIT_NOT_FOUND) 194 self.host.quit(C.EXIT_NOT_FOUND)
168 203
169 class ProfileInfo(base.CommandBase): 204 class ProfileInfo(base.CommandBase):
170 205
171 def __init__(self, host): 206 def __init__(self, host):
172 super(ProfileInfo, self).__init__( 207 super(ProfileInfo, self).__init__(
173 host, 'info', need_connect=False, use_output=C.OUTPUT_DICT, 208 host,
174 help=_('get information about a profile')) 209 "info",
175 self.to_show = [(_("jid"), "Connection", "JabberID"),] 210 need_connect=False,
176 211 use_output=C.OUTPUT_DICT,
177 def add_parser_options(self): 212 help=_("get information about a profile"),
178 self.parser.add_argument( 213 )
179 '--show-password', action='store_true', 214 self.to_show = [
180 help=_('show the XMPP password IN CLEAR TEXT')) 215 (_("jid"), "Connection", "JabberID"),
216 ]
217
218 def add_parser_options(self):
219 self.parser.add_argument(
220 "--show-password",
221 action="store_true",
222 help=_("show the XMPP password IN CLEAR TEXT"),
223 )
181 224
182 async def start(self): 225 async def start(self):
183 if self.args.show_password: 226 if self.args.show_password:
184 self.to_show.append((_("XMPP password"), "Connection", "Password")) 227 self.to_show.append((_("XMPP password"), "Connection", "Password"))
185 self.to_show.append((_("autoconnect (backend)"), "Connection", 228 self.to_show.append(
186 "autoconnect_backend")) 229 (_("autoconnect (backend)"), "Connection", "autoconnect_backend")
230 )
187 data = {} 231 data = {}
188 for label, category, name in self.to_show: 232 for label, category, name in self.to_show:
189 try: 233 try:
190 value = await self.host.bridge.param_get_a_async( 234 value = await self.host.bridge.param_get_a_async(
191 name, category, profile_key=self.host.profile) 235 name, category, profile_key=self.host.profile
236 )
192 except Exception as e: 237 except Exception as e:
193 self.disp(f"can't get {name}/{category} param: {e}", error=True) 238 self.disp(f"can't get {name}/{category} param: {e}", error=True)
194 else: 239 else:
195 data[label] = value 240 data[label] = value
196 241
199 244
200 245
201 class ProfileList(base.CommandBase): 246 class ProfileList(base.CommandBase):
202 def __init__(self, host): 247 def __init__(self, host):
203 super(ProfileList, self).__init__( 248 super(ProfileList, self).__init__(
204 host, 'list', use_profile=False, use_output='list', help=('list profiles')) 249 host, "list", use_profile=False, use_output="list", help=("list profiles")
250 )
205 251
206 def add_parser_options(self): 252 def add_parser_options(self):
207 group = self.parser.add_mutually_exclusive_group() 253 group = self.parser.add_mutually_exclusive_group()
208 group.add_argument( 254 group.add_argument(
209 '-c', '--clients', action='store_true', help=_('get clients profiles only')) 255 "-c", "--clients", action="store_true", help=_("get clients profiles only")
256 )
210 group.add_argument( 257 group.add_argument(
211 '-C', '--components', action='store_true', 258 "-C",
212 help=('get components profiles only')) 259 "--components",
260 action="store_true",
261 help=("get components profiles only"),
262 )
213 263
214 async def start(self): 264 async def start(self):
215 if self.args.clients: 265 if self.args.clients:
216 clients, components = True, False 266 clients, components = True, False
217 elif self.args.components: 267 elif self.args.components:
224 274
225 class ProfileModify(base.CommandBase): 275 class ProfileModify(base.CommandBase):
226 276
227 def __init__(self, host): 277 def __init__(self, host):
228 super(ProfileModify, self).__init__( 278 super(ProfileModify, self).__init__(
229 host, 'modify', need_connect=False, help=_('modify an existing profile')) 279 host, "modify", need_connect=False, help=_("modify an existing profile")
280 )
230 281
231 def add_parser_options(self): 282 def add_parser_options(self):
232 profile_pwd_group = self.parser.add_mutually_exclusive_group() 283 profile_pwd_group = self.parser.add_mutually_exclusive_group()
233 profile_pwd_group.add_argument( 284 profile_pwd_group.add_argument(
234 '-w', '--password', help=_('change the password of the profile')) 285 "-w", "--password", help=_("change the password of the profile")
286 )
235 profile_pwd_group.add_argument( 287 profile_pwd_group.add_argument(
236 '--disable-password', action='store_true', 288 "--disable-password",
237 help=_('disable profile password (dangerous!)')) 289 action="store_true",
238 self.parser.add_argument('-j', '--jid', help=_('the jid of the profile')) 290 help=_("disable profile password (dangerous!)"),
239 self.parser.add_argument( 291 )
240 '-x', '--xmpp-password', help=_('change the password of the XMPP account'), 292 self.parser.add_argument("-j", "--jid", help=_("the jid of the profile"))
241 metavar='PASSWORD') 293 self.parser.add_argument(
242 self.parser.add_argument( 294 "-x",
243 '-D', '--default', action='store_true', help=_('set as default profile')) 295 "--xmpp-password",
244 self.parser.add_argument( 296 help=_("change the password of the XMPP account"),
245 '-A', '--autoconnect', choices=[C.BOOL_TRUE, C.BOOL_FALSE], nargs='?', 297 metavar="PASSWORD",
298 )
299 self.parser.add_argument(
300 "-D", "--default", action="store_true", help=_("set as default profile")
301 )
302 self.parser.add_argument(
303 "-A",
304 "--autoconnect",
305 choices=[C.BOOL_TRUE, C.BOOL_FALSE],
306 nargs="?",
246 const=C.BOOL_TRUE, 307 const=C.BOOL_TRUE,
247 help=_('connect this profile automatically when backend starts') 308 help=_("connect this profile automatically when backend starts"),
248 ) 309 )
249 310
250 async def start(self): 311 async def start(self):
251 if self.args.disable_password: 312 if self.args.disable_password:
252 self.args.password = '' 313 self.args.password = ""
253 if self.args.password is not None: 314 if self.args.password is not None:
254 await self.host.bridge.param_set( 315 await self.host.bridge.param_set(
255 "Password", self.args.password, "General", profile_key=self.host.profile) 316 "Password", self.args.password, "General", profile_key=self.host.profile
317 )
256 if self.args.jid is not None: 318 if self.args.jid is not None:
257 await self.host.bridge.param_set( 319 await self.host.bridge.param_set(
258 "JabberID", self.args.jid, "Connection", profile_key=self.host.profile) 320 "JabberID", self.args.jid, "Connection", profile_key=self.host.profile
321 )
259 if self.args.xmpp_password is not None: 322 if self.args.xmpp_password is not None:
260 await self.host.bridge.param_set( 323 await self.host.bridge.param_set(
261 "Password", self.args.xmpp_password, "Connection", 324 "Password",
262 profile_key=self.host.profile) 325 self.args.xmpp_password,
326 "Connection",
327 profile_key=self.host.profile,
328 )
263 if self.args.default: 329 if self.args.default:
264 await self.host.bridge.profile_set_default(self.host.profile) 330 await self.host.bridge.profile_set_default(self.host.profile)
265 if self.args.autoconnect is not None: 331 if self.args.autoconnect is not None:
266 await self.host.bridge.param_set( 332 await self.host.bridge.param_set(
267 "autoconnect_backend", self.args.autoconnect, "Connection", 333 "autoconnect_backend",
268 profile_key=self.host.profile) 334 self.args.autoconnect,
335 "Connection",
336 profile_key=self.host.profile,
337 )
269 338
270 self.host.quit() 339 self.host.quit()
271 340
272 341
273 class Profile(base.CommandBase): 342 class Profile(base.CommandBase):
274 subcommands = ( 343 subcommands = (
275 ProfileConnect, ProfileDisconnect, ProfileCreate, ProfileDefault, ProfileDelete, 344 ProfileConnect,
276 ProfileInfo, ProfileList, ProfileModify) 345 ProfileDisconnect,
346 ProfileCreate,
347 ProfileDefault,
348 ProfileDelete,
349 ProfileInfo,
350 ProfileList,
351 ProfileModify,
352 )
277 353
278 def __init__(self, host): 354 def __init__(self, host):
279 super(Profile, self).__init__( 355 super(Profile, self).__init__(
280 host, 'profile', use_profile=False, help=_('profile commands')) 356 host, "profile", use_profile=False, help=_("profile commands")
357 )