comparison sat_frontends/jp/cmd_profile.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 4486d72658b9
comparison
equal deleted inserted replaced
3039:a1bc34f90fa5 3040:fee60f17ebac
23 from sat_frontends.jp.constants import Const as C 23 from sat_frontends.jp.constants import Const as C
24 from sat.core.log import getLogger 24 from sat.core.log import getLogger
25 log = getLogger(__name__) 25 log = getLogger(__name__)
26 from sat.core.i18n import _ 26 from sat.core.i18n import _
27 from sat_frontends.jp import base 27 from sat_frontends.jp import base
28 from functools import partial
29 28
30 __commands__ = ["Profile"] 29 __commands__ = ["Profile"]
31 30
32 PROFILE_HELP = _('The name of the profile') 31 PROFILE_HELP = _('The name of the profile')
33 32
41 super(ProfileConnect, self).__init__(host, 'connect', need_connect=False, help=('connect a profile')) 40 super(ProfileConnect, self).__init__(host, 'connect', need_connect=False, help=('connect a profile'))
42 41
43 def add_parser_options(self): 42 def add_parser_options(self):
44 pass 43 pass
45 44
45 async def start(self):
46 # connection is already managed by profile common commands
47 # so we just need to check arguments and quit
48 if not self.args.connect and not self.args.start_session:
49 self.parser.error(_("You need to use either --connect or --start-session"))
50 self.host.quit()
46 51
47 class ProfileDisconnect(base.CommandBase): 52 class ProfileDisconnect(base.CommandBase):
48 53
49 def __init__(self, host): 54 def __init__(self, host):
50 super(ProfileDisconnect, self).__init__(host, 'disconnect', need_connect=False, help=('disconnect a profile')) 55 super(ProfileDisconnect, self).__init__(host, 'disconnect', need_connect=False, help=('disconnect a profile'))
51 self.need_loop = True
52 56
53 def add_parser_options(self): 57 def add_parser_options(self):
54 pass 58 pass
55 59
56 def start(self): 60 async def start(self):
57 self.host.bridge.disconnect(self.args.profile, callback=self.host.quit) 61 try:
58 62 await self.host.bridge.disconnect(self.args.profile)
59 63 except Exception as e:
60 class ProfileDefault(base.CommandBase): 64 self.disp(f"can't disconnect profile: {e}", error=True)
61 def __init__(self, host): 65 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
62 super(ProfileDefault, self).__init__(host, 'default', use_profile=False, help=('print default profile')) 66 else:
63
64 def add_parser_options(self):
65 pass
66
67 def start(self):
68 print(self.host.bridge.profileNameGet('@DEFAULT@'))
69
70
71 class ProfileDelete(base.CommandBase):
72 def __init__(self, host):
73 super(ProfileDelete, self).__init__(host, 'delete', use_profile=False, help=('delete a profile'))
74
75 def add_parser_options(self):
76 self.parser.add_argument('profile', type=str, help=PROFILE_HELP)
77 self.parser.add_argument('-f', '--force', action='store_true', help=_('delete profile without confirmation'))
78
79 def start(self):
80 if self.args.profile not in self.host.bridge.profilesListGet():
81 log.error("Profile %s doesn't exist." % self.args.profile)
82 self.host.quit(1)
83 if not self.args.force:
84 message = "Are you sure to delete profile [{}] ?".format(self.args.profile)
85 res = input("{} (y/N)? ".format(message))
86 if res not in ("y", "Y"):
87 self.disp(_("Profile deletion cancelled"))
88 self.host.quit(2)
89
90 self.host.bridge.asyncDeleteProfile(self.args.profile, callback=lambda __: None)
91
92
93 class ProfileInfo(base.CommandBase):
94 def __init__(self, host):
95 super(ProfileInfo, self).__init__(host, 'info', need_connect=False, help=_('get information about a profile'))
96 self.need_loop = True
97 self.to_show = [(_("jid"), "Connection", "JabberID"),]
98 self.largest = max([len(item[0]) for item in self.to_show])
99
100
101 def add_parser_options(self):
102 self.parser.add_argument('--show-password', action='store_true', help=_('show the XMPP password IN CLEAR TEXT'))
103
104 def showNextValue(self, label=None, category=None, value=None):
105 """Show next value from self.to_show and quit on last one"""
106 if label is not None:
107 print((("{label:<"+str(self.largest+2)+"}{value}").format(label=label+": ", value=value)))
108 try:
109 label, category, name = self.to_show.pop(0)
110 except IndexError:
111 self.host.quit() 67 self.host.quit()
112 else:
113 self.host.bridge.asyncGetParamA(name, category, profile_key=self.host.profile,
114 callback=lambda value: self.showNextValue(label, category, value))
115
116 def start(self):
117 if self.args.show_password:
118 self.to_show.append((_("XMPP password"), "Connection", "Password"))
119 self.showNextValue()
120
121
122 class ProfileList(base.CommandBase):
123 def __init__(self, host):
124 super(ProfileList, self).__init__(host, 'list', use_profile=False, use_output='list', help=('list profiles'))
125
126 def add_parser_options(self):
127 group = self.parser.add_mutually_exclusive_group()
128 group.add_argument('-c', '--clients', action='store_true', help=_('get clients profiles only'))
129 group.add_argument('-C', '--components', action='store_true', help=('get components profiles only'))
130
131
132 def start(self):
133 if self.args.clients:
134 clients, components = True, False
135 elif self.args.components:
136 clients, components = False, True
137 else:
138 clients, components = True, True
139 self.output(self.host.bridge.profilesListGet(clients, components))
140 68
141 69
142 class ProfileCreate(base.CommandBase): 70 class ProfileCreate(base.CommandBase):
143 def __init__(self, host): 71 def __init__(self, host):
144 super(ProfileCreate, self).__init__(host, 'create', use_profile=False, help=('create a new profile')) 72 super(ProfileCreate, self).__init__(host, 'create', use_profile=False, help=('create a new profile'))
145 self.need_loop = True
146 73
147 def add_parser_options(self): 74 def add_parser_options(self):
148 self.parser.add_argument('profile', type=str, help=_('the name of the profile')) 75 self.parser.add_argument('profile', type=str, help=_('the name of the profile'))
149 self.parser.add_argument('-p', '--password', type=str, default='', help=_('the password of the profile')) 76 self.parser.add_argument('-p', '--password', type=str, default='', help=_('the password of the profile'))
150 self.parser.add_argument('-j', '--jid', type=str, help=_('the jid of the profile')) 77 self.parser.add_argument('-j', '--jid', type=str, help=_('the jid of the profile'))
151 self.parser.add_argument('-x', '--xmpp-password', type=str, help=_('the password of the XMPP account (use profile password if not specified)'), 78 self.parser.add_argument('-x', '--xmpp-password', type=str, help=_('the password of the XMPP account (use profile password if not specified)'),
152 metavar='PASSWORD') 79 metavar='PASSWORD')
153 self.parser.add_argument('-C', '--component', default='', 80 self.parser.add_argument('-C', '--component', default='',
154 help=_('set to component import name (entry point) if this is a component')) 81 help=_('set to component import name (entry point) if this is a component'))
155 82
156 def _session_started(self, __): 83 async def start(self):
84 """Create a new profile"""
85 if self.args.profile in await self.host.bridge.profilesListGet():
86 self.disp(f"Profile {self.args.profile} already exists.", error=True)
87 self.host.quit(C.EXIT_BRIDGE_ERROR)
88 try:
89 await self.host.bridge.profileCreate(
90 self.args.profile, self.args.password, self.args.component)
91 except Exception as e:
92 self.disp(f"can't create profile: {e}", error=True)
93 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
94
95 try:
96 await self.host.bridge.profileStartSession(
97 self.args.password, self.args.profile)
98 except Exception as e:
99 self.disp(f"can't start profile session: {e}", error=True)
100 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
101
157 if self.args.jid: 102 if self.args.jid:
158 self.host.bridge.setParam("JabberID", self.args.jid, "Connection", profile_key=self.args.profile) 103 await self.host.bridge.setParam(
104 "JabberID", self.args.jid, "Connection", profile_key=self.args.profile)
159 xmpp_pwd = self.args.password or self.args.xmpp_password 105 xmpp_pwd = self.args.password or self.args.xmpp_password
160 if xmpp_pwd: 106 if xmpp_pwd:
161 self.host.bridge.setParam("Password", xmpp_pwd, "Connection", profile_key=self.args.profile) 107 await self.host.bridge.setParam(
162 self.host.quit() 108 "Password", xmpp_pwd, "Connection", profile_key=self.args.profile)
163 109
164 def _profile_created(self): 110 self.disp(f'profile {self.args.profile} created successfully', 1)
165 self.host.bridge.profileStartSession(self.args.password, self.args.profile, callback=self._session_started, errback=None) 111 self.host.quit()
166 112
167 def start(self): 113
168 """Create a new profile""" 114 class ProfileDefault(base.CommandBase):
169 if self.args.profile in self.host.bridge.profilesListGet(): 115 def __init__(self, host):
170 log.error("Profile %s already exists." % self.args.profile) 116 super(ProfileDefault, self).__init__(host, 'default', use_profile=False, help=('print default profile'))
171 self.host.quit(1) 117
172 self.host.bridge.profileCreate(self.args.profile, self.args.password, self.args.component, 118 def add_parser_options(self):
173 callback=self._profile_created, 119 pass
174 errback=partial(self.errback, 120
175 msg=_("can't create profile: {}"), 121 async def start(self):
176 exit_code=C.EXIT_BRIDGE_ERRBACK)) 122 print(await self.host.bridge.profileNameGet('@DEFAULT@'))
123 self.host.quit()
124
125
126 class ProfileDelete(base.CommandBase):
127 def __init__(self, host):
128 super(ProfileDelete, self).__init__(host, 'delete', use_profile=False, help=('delete a profile'))
129
130 def add_parser_options(self):
131 self.parser.add_argument('profile', type=str, help=PROFILE_HELP)
132 self.parser.add_argument('-f', '--force', action='store_true', help=_('delete profile without confirmation'))
133
134 async def start(self):
135 if self.args.profile not in await self.host.bridge.profilesListGet():
136 log.error(f"Profile {self.args.profile} doesn't exist.")
137 self.host.quit(C.EXIT_NOT_FOUND)
138 if not self.args.force:
139 message = f"Are you sure to delete profile [{self.args.profile}] ?"
140 cancel_message = "Profile deletion cancelled"
141 await self.host.confirmOrQuit(message, cancel_message)
142
143 await self.host.bridge.asyncDeleteProfile(self.args.profile)
144 self.host.quit()
145
146
147 class ProfileInfo(base.CommandBase):
148 def __init__(self, host):
149 super(ProfileInfo, self).__init__(host, 'info', need_connect=False, help=_('get information about a profile'))
150 self.to_show = [(_("jid"), "Connection", "JabberID"),]
151 self.largest = max([len(item[0]) for item in self.to_show])
152
153 def add_parser_options(self):
154 self.parser.add_argument('--show-password', action='store_true', help=_('show the XMPP password IN CLEAR TEXT'))
155
156 async def showNextValue(self, label=None, category=None, value=None):
157 """Show next value from self.to_show and quit on last one"""
158 if label is not None:
159 print((("{label:<"+str(self.largest+2)+"}{value}").format(
160 label=label+": ", value=value)))
161 try:
162 label, category, name = self.to_show.pop(0)
163 except IndexError:
164 self.host.quit()
165 else:
166 try:
167 value = await self.host.bridge.asyncGetParamA(
168 name, category, profile_key=self.host.profile)
169 except Exception as e:
170 self.disp(f"can't get {name}/{category} param: {e}", error=True)
171 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
172 else:
173 await self.showNextValue(label, category, value)
174
175 async def start(self):
176 if self.args.show_password:
177 self.to_show.append((_("XMPP password"), "Connection", "Password"))
178 await self.showNextValue()
179
180
181 class ProfileList(base.CommandBase):
182 def __init__(self, host):
183 super(ProfileList, self).__init__(host, 'list', use_profile=False, use_output='list', help=('list profiles'))
184
185 def add_parser_options(self):
186 group = self.parser.add_mutually_exclusive_group()
187 group.add_argument('-c', '--clients', action='store_true', help=_('get clients profiles only'))
188 group.add_argument('-C', '--components', action='store_true', help=('get components profiles only'))
189
190
191 async def start(self):
192 if self.args.clients:
193 clients, components = True, False
194 elif self.args.components:
195 clients, components = False, True
196 else:
197 clients, components = True, True
198 await self.output(await self.host.bridge.profilesListGet(clients, components))
199 self.host.quit()
177 200
178 201
179 class ProfileModify(base.CommandBase): 202 class ProfileModify(base.CommandBase):
203
180 def __init__(self, host): 204 def __init__(self, host):
181 super(ProfileModify, self).__init__(host, 'modify', need_connect=False, help=_('modify an existing profile')) 205 super(ProfileModify, self).__init__(host, 'modify', need_connect=False, help=_('modify an existing profile'))
182 206
183 def add_parser_options(self): 207 def add_parser_options(self):
184 profile_pwd_group = self.parser.add_mutually_exclusive_group() 208 profile_pwd_group = self.parser.add_mutually_exclusive_group()
187 self.parser.add_argument('-j', '--jid', help=_('the jid of the profile')) 211 self.parser.add_argument('-j', '--jid', help=_('the jid of the profile'))
188 self.parser.add_argument('-x', '--xmpp-password', help=_('change the password of the XMPP account'), 212 self.parser.add_argument('-x', '--xmpp-password', help=_('change the password of the XMPP account'),
189 metavar='PASSWORD') 213 metavar='PASSWORD')
190 self.parser.add_argument('-D', '--default', action='store_true', help=_('set as default profile')) 214 self.parser.add_argument('-D', '--default', action='store_true', help=_('set as default profile'))
191 215
192 def start(self): 216 async def start(self):
193 if self.args.disable_password: 217 if self.args.disable_password:
194 self.args.password = '' 218 self.args.password = ''
195 if self.args.password is not None: 219 if self.args.password is not None:
196 self.host.bridge.setParam("Password", self.args.password, "General", profile_key=self.host.profile) 220 await self.host.bridge.setParam(
221 "Password", self.args.password, "General", profile_key=self.host.profile)
197 if self.args.jid is not None: 222 if self.args.jid is not None:
198 self.host.bridge.setParam("JabberID", self.args.jid, "Connection", profile_key=self.host.profile) 223 await self.host.bridge.setParam(
224 "JabberID", self.args.jid, "Connection", profile_key=self.host.profile)
199 if self.args.xmpp_password is not None: 225 if self.args.xmpp_password is not None:
200 self.host.bridge.setParam("Password", self.args.xmpp_password, "Connection", profile_key=self.host.profile) 226 await self.host.bridge.setParam(
227 "Password", self.args.xmpp_password, "Connection", profile_key=self.host.profile)
201 if self.args.default: 228 if self.args.default:
202 self.host.bridge.profileSetDefault(self.host.profile) 229 await self.host.bridge.profileSetDefault(self.host.profile)
230
231 self.host.quit()
203 232
204 233
205 class Profile(base.CommandBase): 234 class Profile(base.CommandBase):
206 subcommands = (ProfileConnect, ProfileDisconnect, ProfileCreate, ProfileDefault, ProfileDelete, ProfileInfo, ProfileList, ProfileModify) 235 subcommands = (ProfileConnect, ProfileDisconnect, ProfileCreate, ProfileDefault, ProfileDelete, ProfileInfo, ProfileList, ProfileModify)
207 236