Mercurial > libervia-backend
comparison sat_frontends/jp/cmd_invitation.py @ 2624:56f94936df1e
code style reformatting using black
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 27 Jun 2018 20:14:46 +0200 |
parents | 26edcf3a30eb |
children | 378188abe941 |
comparison
equal
deleted
inserted
replaced
2623:49533de4540b | 2624:56f94936df1e |
---|---|
27 | 27 |
28 __commands__ = ["Invitation"] | 28 __commands__ = ["Invitation"] |
29 | 29 |
30 | 30 |
31 class Create(base.CommandBase): | 31 class Create(base.CommandBase): |
32 | 32 def __init__(self, host): |
33 def __init__(self, host): | 33 base.CommandBase.__init__( |
34 base.CommandBase.__init__(self, host, 'create', use_profile=False, use_output=C.OUTPUT_DICT, help=_(u'create and send an invitation')) | 34 self, |
35 self.need_loop=True | 35 host, |
36 "create", | |
37 use_profile=False, | |
38 use_output=C.OUTPUT_DICT, | |
39 help=_(u"create and send an invitation"), | |
40 ) | |
41 self.need_loop = True | |
36 | 42 |
37 def add_parser_options(self): | 43 def add_parser_options(self): |
38 self.parser.add_argument("-j", "--jid", type=base.unicode_decoder, default='', help='jid of the invitee (default: generate one)') | 44 self.parser.add_argument( |
39 self.parser.add_argument("-P", "--password", type=base.unicode_decoder, default='', help='password of the invitee profile/XMPP account (default: generate one)') | 45 "-j", |
40 self.parser.add_argument("-n", "--name", type=base.unicode_decoder, default='', help='name of the invitee') | 46 "--jid", |
41 self.parser.add_argument("-N", "--host-name", type=base.unicode_decoder, default='', help='name of the host') | 47 type=base.unicode_decoder, |
42 self.parser.add_argument("-e", "--email", action="append", type=base.unicode_decoder, default=[], help='email(s) to send the invitation to (if --no-email is set, email will just be saved)') | 48 default="", |
43 self.parser.add_argument("--no-email", action="store_true", help='do NOT send invitation email') | 49 help="jid of the invitee (default: generate one)", |
44 self.parser.add_argument("-l", "--lang", type=base.unicode_decoder, default='', help='main language spoken by the invitee') | 50 ) |
45 self.parser.add_argument("-u", "--url", type=base.unicode_decoder, default='', help='template to construct the URL') | 51 self.parser.add_argument( |
46 self.parser.add_argument("-s", "--subject", type=base.unicode_decoder, default='', help='subject of the invitation email (default: generic subject)') | 52 "-P", |
47 self.parser.add_argument("-b", "--body", type=base.unicode_decoder, default='', help='body of the invitation email (default: generic body)') | 53 "--password", |
48 self.parser.add_argument("-x", "--extra", metavar=('KEY', 'VALUE'), type=base.unicode_decoder, action='append', nargs=2, default=[], help='extra data to associate with invitation/invitee') | 54 type=base.unicode_decoder, |
49 self.parser.add_argument("-p", "--profile", type=base.unicode_decoder, default='', help="profile doing the invitation (default: don't associate profile)") | 55 default="", |
56 help="password of the invitee profile/XMPP account (default: generate one)", | |
57 ) | |
58 self.parser.add_argument( | |
59 "-n", | |
60 "--name", | |
61 type=base.unicode_decoder, | |
62 default="", | |
63 help="name of the invitee", | |
64 ) | |
65 self.parser.add_argument( | |
66 "-N", | |
67 "--host-name", | |
68 type=base.unicode_decoder, | |
69 default="", | |
70 help="name of the host", | |
71 ) | |
72 self.parser.add_argument( | |
73 "-e", | |
74 "--email", | |
75 action="append", | |
76 type=base.unicode_decoder, | |
77 default=[], | |
78 help="email(s) to send the invitation to (if --no-email is set, email will just be saved)", | |
79 ) | |
80 self.parser.add_argument( | |
81 "--no-email", action="store_true", help="do NOT send invitation email" | |
82 ) | |
83 self.parser.add_argument( | |
84 "-l", | |
85 "--lang", | |
86 type=base.unicode_decoder, | |
87 default="", | |
88 help="main language spoken by the invitee", | |
89 ) | |
90 self.parser.add_argument( | |
91 "-u", | |
92 "--url", | |
93 type=base.unicode_decoder, | |
94 default="", | |
95 help="template to construct the URL", | |
96 ) | |
97 self.parser.add_argument( | |
98 "-s", | |
99 "--subject", | |
100 type=base.unicode_decoder, | |
101 default="", | |
102 help="subject of the invitation email (default: generic subject)", | |
103 ) | |
104 self.parser.add_argument( | |
105 "-b", | |
106 "--body", | |
107 type=base.unicode_decoder, | |
108 default="", | |
109 help="body of the invitation email (default: generic body)", | |
110 ) | |
111 self.parser.add_argument( | |
112 "-x", | |
113 "--extra", | |
114 metavar=("KEY", "VALUE"), | |
115 type=base.unicode_decoder, | |
116 action="append", | |
117 nargs=2, | |
118 default=[], | |
119 help="extra data to associate with invitation/invitee", | |
120 ) | |
121 self.parser.add_argument( | |
122 "-p", | |
123 "--profile", | |
124 type=base.unicode_decoder, | |
125 default="", | |
126 help="profile doing the invitation (default: don't associate profile)", | |
127 ) | |
50 | 128 |
51 def invitationCreateCb(self, invitation_data): | 129 def invitationCreateCb(self, invitation_data): |
52 self.output(invitation_data) | 130 self.output(invitation_data) |
53 self.host.quit(C.EXIT_OK) | 131 self.host.quit(C.EXIT_OK) |
54 | 132 |
55 def invitationCreateEb(self, failure_): | 133 def invitationCreateEb(self, failure_): |
56 self.disp(u"can't create invitation: {reason}".format( | 134 self.disp( |
57 reason=failure_), error=True) | 135 u"can't create invitation: {reason}".format(reason=failure_), error=True |
136 ) | |
58 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | 137 self.host.quit(C.EXIT_BRIDGE_ERRBACK) |
59 | 138 |
60 def start(self): | 139 def start(self): |
61 extra = dict(self.args.extra) | 140 extra = dict(self.args.extra) |
62 email = self.args.email[0] if self.args.email else None | 141 email = self.args.email[0] if self.args.email else None |
63 emails_extra = self.args.email[1:] | 142 emails_extra = self.args.email[1:] |
64 if self.args.no_email: | 143 if self.args.no_email: |
65 if email: | 144 if email: |
66 extra['email'] = email | 145 extra["email"] = email |
67 data_format.iter2dict(u'emails_extra', emails_extra) | 146 data_format.iter2dict(u"emails_extra", emails_extra) |
68 else: | 147 else: |
69 if not email: | 148 if not email: |
70 self.parser.error(_(u'you need to specify an email address to send email invitation')) | 149 self.parser.error( |
150 _(u"you need to specify an email address to send email invitation") | |
151 ) | |
71 | 152 |
72 self.host.bridge.invitationCreate( | 153 self.host.bridge.invitationCreate( |
73 email, | 154 email, |
74 emails_extra, | 155 emails_extra, |
75 self.args.jid, | 156 self.args.jid, |
81 self.args.subject, | 162 self.args.subject, |
82 self.args.body, | 163 self.args.body, |
83 extra, | 164 extra, |
84 self.args.profile, | 165 self.args.profile, |
85 callback=self.invitationCreateCb, | 166 callback=self.invitationCreateCb, |
86 errback=self.invitationCreateEb) | 167 errback=self.invitationCreateEb, |
168 ) | |
87 | 169 |
88 | 170 |
89 class Get(base.CommandBase): | 171 class Get(base.CommandBase): |
90 | 172 def __init__(self, host): |
91 def __init__(self, host): | 173 base.CommandBase.__init__( |
92 base.CommandBase.__init__(self, host, 'get', use_profile=False, use_output=C.OUTPUT_DICT, help=_(u'get invitation data')) | 174 self, |
93 self.need_loop=True | 175 host, |
176 "get", | |
177 use_profile=False, | |
178 use_output=C.OUTPUT_DICT, | |
179 help=_(u"get invitation data"), | |
180 ) | |
181 self.need_loop = True | |
94 | 182 |
95 def add_parser_options(self): | 183 def add_parser_options(self): |
96 self.parser.add_argument("id", type=base.unicode_decoder, | 184 self.parser.add_argument( |
97 help=_(u"invitation UUID")) | 185 "id", type=base.unicode_decoder, help=_(u"invitation UUID") |
98 self.parser.add_argument("-j", "--with-jid", action="store_true", help=_(u"start profile session and retrieve jid")) | 186 ) |
187 self.parser.add_argument( | |
188 "-j", | |
189 "--with-jid", | |
190 action="store_true", | |
191 help=_(u"start profile session and retrieve jid"), | |
192 ) | |
99 | 193 |
100 def output_data(self, data, jid_=None): | 194 def output_data(self, data, jid_=None): |
101 if jid_ is not None: | 195 if jid_ is not None: |
102 data['jid'] = jid_ | 196 data["jid"] = jid_ |
103 self.output(data) | 197 self.output(data) |
104 self.host.quit() | 198 self.host.quit() |
105 | 199 |
106 def invitationGetCb(self, invitation_data): | 200 def invitationGetCb(self, invitation_data): |
107 if self.args.with_jid: | 201 if self.args.with_jid: |
108 profile = invitation_data[u'guest_profile'] | 202 profile = invitation_data[u"guest_profile"] |
203 | |
109 def session_started(dummy): | 204 def session_started(dummy): |
110 self.host.bridge.asyncGetParamA( | 205 self.host.bridge.asyncGetParamA( |
111 u'JabberID', | 206 u"JabberID", |
112 u'Connection', | 207 u"Connection", |
113 profile_key=profile, | 208 profile_key=profile, |
114 callback=lambda jid_: self.output_data(invitation_data, jid_), | 209 callback=lambda jid_: self.output_data(invitation_data, jid_), |
115 errback=partial(self.errback, | 210 errback=partial( |
116 msg=_(u"can't retrieve jid: {}"), | 211 self.errback, |
117 exit_code=C.EXIT_BRIDGE_ERRBACK)) | 212 msg=_(u"can't retrieve jid: {}"), |
213 exit_code=C.EXIT_BRIDGE_ERRBACK, | |
214 ), | |
215 ) | |
118 | 216 |
119 self.host.bridge.profileStartSession( | 217 self.host.bridge.profileStartSession( |
120 invitation_data[u'password'], | 218 invitation_data[u"password"], |
121 profile, | 219 profile, |
122 callback=session_started, | 220 callback=session_started, |
123 errback=partial(self.errback, | 221 errback=partial( |
124 msg=_(u"can't start session: {}"), | 222 self.errback, |
125 exit_code=C.EXIT_BRIDGE_ERRBACK)) | 223 msg=_(u"can't start session: {}"), |
224 exit_code=C.EXIT_BRIDGE_ERRBACK, | |
225 ), | |
226 ) | |
126 else: | 227 else: |
127 self.output_data(invitation_data) | 228 self.output_data(invitation_data) |
128 | 229 |
129 def start(self): | 230 def start(self): |
130 self.host.bridge.invitationGet( | 231 self.host.bridge.invitationGet( |
131 self.args.id, | 232 self.args.id, |
132 callback=self.invitationGetCb, | 233 callback=self.invitationGetCb, |
133 errback=partial(self.errback, | 234 errback=partial( |
134 msg=_(u"can't get invitation data: {}"), | 235 self.errback, |
135 exit_code=C.EXIT_BRIDGE_ERRBACK)) | 236 msg=_(u"can't get invitation data: {}"), |
237 exit_code=C.EXIT_BRIDGE_ERRBACK, | |
238 ), | |
239 ) | |
136 | 240 |
137 | 241 |
138 class Modify(base.CommandBase): | 242 class Modify(base.CommandBase): |
139 | 243 def __init__(self, host): |
140 def __init__(self, host): | 244 base.CommandBase.__init__( |
141 base.CommandBase.__init__(self, host, 'modify', use_profile=False, help=_(u'modify existing invitation')) | 245 self, host, "modify", use_profile=False, help=_(u"modify existing invitation") |
142 self.need_loop=True | 246 ) |
247 self.need_loop = True | |
143 | 248 |
144 def add_parser_options(self): | 249 def add_parser_options(self): |
145 self.parser.add_argument("--replace", action='store_true', help='replace the whole data') | 250 self.parser.add_argument( |
146 self.parser.add_argument("-n", "--name", type=base.unicode_decoder, default='', help='name of the invitee') | 251 "--replace", action="store_true", help="replace the whole data" |
147 self.parser.add_argument("-N", "--host-name", type=base.unicode_decoder, default='', help='name of the host') | 252 ) |
148 self.parser.add_argument("-e", "--email", type=base.unicode_decoder, default='', help='email to send the invitation to (if --no-email is set, email will just be saved)') | 253 self.parser.add_argument( |
149 self.parser.add_argument("-l", "--lang", dest="language", type=base.unicode_decoder, default='', | 254 "-n", |
150 help='main language spoken by the invitee') | 255 "--name", |
151 self.parser.add_argument("-x", "--extra", metavar=('KEY', 'VALUE'), type=base.unicode_decoder, action='append', nargs=2, default=[], help='extra data to associate with invitation/invitee') | 256 type=base.unicode_decoder, |
152 self.parser.add_argument("-p", "--profile", type=base.unicode_decoder, default='', help="profile doing the invitation (default: don't associate profile") | 257 default="", |
153 self.parser.add_argument("id", type=base.unicode_decoder, | 258 help="name of the invitee", |
154 help=_(u"invitation UUID")) | 259 ) |
260 self.parser.add_argument( | |
261 "-N", | |
262 "--host-name", | |
263 type=base.unicode_decoder, | |
264 default="", | |
265 help="name of the host", | |
266 ) | |
267 self.parser.add_argument( | |
268 "-e", | |
269 "--email", | |
270 type=base.unicode_decoder, | |
271 default="", | |
272 help="email to send the invitation to (if --no-email is set, email will just be saved)", | |
273 ) | |
274 self.parser.add_argument( | |
275 "-l", | |
276 "--lang", | |
277 dest="language", | |
278 type=base.unicode_decoder, | |
279 default="", | |
280 help="main language spoken by the invitee", | |
281 ) | |
282 self.parser.add_argument( | |
283 "-x", | |
284 "--extra", | |
285 metavar=("KEY", "VALUE"), | |
286 type=base.unicode_decoder, | |
287 action="append", | |
288 nargs=2, | |
289 default=[], | |
290 help="extra data to associate with invitation/invitee", | |
291 ) | |
292 self.parser.add_argument( | |
293 "-p", | |
294 "--profile", | |
295 type=base.unicode_decoder, | |
296 default="", | |
297 help="profile doing the invitation (default: don't associate profile", | |
298 ) | |
299 self.parser.add_argument( | |
300 "id", type=base.unicode_decoder, help=_(u"invitation UUID") | |
301 ) | |
155 | 302 |
156 def invitationModifyCb(self): | 303 def invitationModifyCb(self): |
157 self.disp(_(u'invitations have been modified correctly')) | 304 self.disp(_(u"invitations have been modified correctly")) |
158 self.host.quit(C.EXIT_OK) | 305 self.host.quit(C.EXIT_OK) |
159 | 306 |
160 def invitationModifyEb(self, failure_): | 307 def invitationModifyEb(self, failure_): |
161 self.disp(u"can't create invitation: {reason}".format( | 308 self.disp( |
162 reason=failure_), error=True) | 309 u"can't create invitation: {reason}".format(reason=failure_), error=True |
310 ) | |
163 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | 311 self.host.quit(C.EXIT_BRIDGE_ERRBACK) |
164 | 312 |
165 def start(self): | 313 def start(self): |
166 extra = dict(self.args.extra) | 314 extra = dict(self.args.extra) |
167 for arg_name in ('name', 'host_name', 'email', 'language', 'profile'): | 315 for arg_name in ("name", "host_name", "email", "language", "profile"): |
168 value = getattr(self.args, arg_name) | 316 value = getattr(self.args, arg_name) |
169 if not value: | 317 if not value: |
170 continue | 318 continue |
171 if arg_name in extra: | 319 if arg_name in extra: |
172 self.parser.error(_(u"you can't set {arg_name} in both optional argument and extra").format(arg_name=arg_name)) | 320 self.parser.error( |
321 _( | |
322 u"you can't set {arg_name} in both optional argument and extra" | |
323 ).format(arg_name=arg_name) | |
324 ) | |
173 extra[arg_name] = value | 325 extra[arg_name] = value |
174 self.host.bridge.invitationModify( | 326 self.host.bridge.invitationModify( |
175 self.args.id, | 327 self.args.id, |
176 extra, | 328 extra, |
177 self.args.replace, | 329 self.args.replace, |
178 callback=self.invitationModifyCb, | 330 callback=self.invitationModifyCb, |
179 errback=self.invitationModifyEb) | 331 errback=self.invitationModifyEb, |
332 ) | |
180 | 333 |
181 | 334 |
182 class List(base.CommandBase): | 335 class List(base.CommandBase): |
183 | 336 def __init__(self, host): |
184 def __init__(self, host): | 337 extra_outputs = {"default": self.default_output} |
185 extra_outputs = {'default': self.default_output} | 338 base.CommandBase.__init__( |
186 base.CommandBase.__init__(self, host, 'list', use_profile=False, use_output=C.OUTPUT_COMPLEX, extra_outputs=extra_outputs, help=_(u'list invitations data')) | 339 self, |
187 self.need_loop=True | 340 host, |
341 "list", | |
342 use_profile=False, | |
343 use_output=C.OUTPUT_COMPLEX, | |
344 extra_outputs=extra_outputs, | |
345 help=_(u"list invitations data"), | |
346 ) | |
347 self.need_loop = True | |
188 | 348 |
189 def default_output(self, data): | 349 def default_output(self, data): |
190 for idx, datum in enumerate(data.iteritems()): | 350 for idx, datum in enumerate(data.iteritems()): |
191 if idx: | 351 if idx: |
192 self.disp(u"\n") | 352 self.disp(u"\n") |
193 key, invitation_data = datum | 353 key, invitation_data = datum |
194 self.disp(A.color(C.A_HEADER, key)) | 354 self.disp(A.color(C.A_HEADER, key)) |
195 indent = u' ' | 355 indent = u" " |
196 for k, v in invitation_data.iteritems(): | 356 for k, v in invitation_data.iteritems(): |
197 self.disp(indent + A.color(C.A_SUBHEADER, k + u':') + u' ' + unicode(v)) | 357 self.disp(indent + A.color(C.A_SUBHEADER, k + u":") + u" " + unicode(v)) |
198 | 358 |
199 def add_parser_options(self): | 359 def add_parser_options(self): |
200 self.parser.add_argument("-p", "--profile", default=C.PROF_KEY_NONE, help=_(u"return only invitations linked to this profile")) | 360 self.parser.add_argument( |
361 "-p", | |
362 "--profile", | |
363 default=C.PROF_KEY_NONE, | |
364 help=_(u"return only invitations linked to this profile"), | |
365 ) | |
201 | 366 |
202 def invitationListCb(self, data): | 367 def invitationListCb(self, data): |
203 self.output(data) | 368 self.output(data) |
204 self.host.quit() | 369 self.host.quit() |
205 | 370 |
206 def start(self): | 371 def start(self): |
207 self.host.bridge.invitationList( | 372 self.host.bridge.invitationList( |
208 self.args.profile, | 373 self.args.profile, |
209 callback=self.invitationListCb, | 374 callback=self.invitationListCb, |
210 errback=partial(self.errback, | 375 errback=partial( |
211 msg=_(u"can't list invitations: {}"), | 376 self.errback, |
212 exit_code=C.EXIT_BRIDGE_ERRBACK)) | 377 msg=_(u"can't list invitations: {}"), |
378 exit_code=C.EXIT_BRIDGE_ERRBACK, | |
379 ), | |
380 ) | |
213 | 381 |
214 | 382 |
215 class Invitation(base.CommandBase): | 383 class Invitation(base.CommandBase): |
216 subcommands = (Create, Get, Modify, List) | 384 subcommands = (Create, Get, Modify, List) |
217 | 385 |
218 def __init__(self, host): | 386 def __init__(self, host): |
219 super(Invitation, self).__init__(host, 'invitation', use_profile=False, help=_(u'invitation of user(s) without XMPP account')) | 387 super(Invitation, self).__init__( |
388 host, | |
389 "invitation", | |
390 use_profile=False, | |
391 help=_(u"invitation of user(s) without XMPP account"), | |
392 ) |