comparison sat_frontends/jp/cmd_invitation.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents frontends/src/jp/cmd_invitation.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # jp: a SàT command line tool
5 # Copyright (C) 2009-2018 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
21 import base
22 from sat.core.i18n import _
23 from sat_frontends.jp.constants import Const as C
24 from sat.tools.common.ansi import ANSI as A
25 from sat.tools.common import data_format
26 from functools import partial
27
28 __commands__ = ["Invitation"]
29
30
31 class Create(base.CommandBase):
32
33 def __init__(self, host):
34 base.CommandBase.__init__(self, host, 'create', use_profile=False, use_output=C.OUTPUT_DICT, help=_(u'create and send an invitation'))
35 self.need_loop=True
36
37 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)')
39 self.parser.add_argument("-P", "--password", type=base.unicode_decoder, default='', help='password of the invitee profile/XMPP account (default: generate one)')
40 self.parser.add_argument("-n", "--name", type=base.unicode_decoder, default='', help='name of the invitee')
41 self.parser.add_argument("-N", "--host-name", type=base.unicode_decoder, default='', help='name of the host')
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)')
43 self.parser.add_argument("--no-email", action="store_true", help='do NOT send invitation email')
44 self.parser.add_argument("-l", "--lang", type=base.unicode_decoder, default='', help='main language spoken by the invitee')
45 self.parser.add_argument("-u", "--url", type=base.unicode_decoder, default='', help='template to construct the URL')
46 self.parser.add_argument("-s", "--subject", type=base.unicode_decoder, default='', help='subject of the invitation email (default: generic subject)')
47 self.parser.add_argument("-b", "--body", type=base.unicode_decoder, default='', help='body of the invitation email (default: generic body)')
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')
49 self.parser.add_argument("-p", "--profile", type=base.unicode_decoder, default='', help="profile doing the invitation (default: don't associate profile)")
50
51 def invitationCreateCb(self, invitation_data):
52 self.output(invitation_data)
53 self.host.quit(C.EXIT_OK)
54
55 def invitationCreateEb(self, failure_):
56 self.disp(u"can't create invitation: {reason}".format(
57 reason=failure_), error=True)
58 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
59
60 def start(self):
61 extra = dict(self.args.extra)
62 email = self.args.email[0] if self.args.email else None
63 emails_extra = self.args.email[1:]
64 if self.args.no_email:
65 if email:
66 extra['email'] = email
67 data_format.iter2dict(u'emails_extra', emails_extra)
68 else:
69 if not email:
70 self.parser.error(_(u'you need to specify an email address to send email invitation'))
71
72 self.host.bridge.invitationCreate(
73 email,
74 emails_extra,
75 self.args.jid,
76 self.args.password,
77 self.args.name,
78 self.args.host_name,
79 self.args.lang,
80 self.args.url,
81 self.args.subject,
82 self.args.body,
83 extra,
84 self.args.profile,
85 callback=self.invitationCreateCb,
86 errback=self.invitationCreateEb)
87
88
89 class Get(base.CommandBase):
90
91 def __init__(self, host):
92 base.CommandBase.__init__(self, host, 'get', use_profile=False, use_output=C.OUTPUT_DICT, help=_(u'get invitation data'))
93 self.need_loop=True
94
95 def add_parser_options(self):
96 self.parser.add_argument("id", type=base.unicode_decoder,
97 help=_(u"invitation UUID"))
98 self.parser.add_argument("-j", "--with-jid", action="store_true", help=_(u"start profile session and retrieve jid"))
99
100 def output_data(self, data, jid_=None):
101 if jid_ is not None:
102 data['jid'] = jid_
103 self.output(data)
104 self.host.quit()
105
106 def invitationGetCb(self, invitation_data):
107 if self.args.with_jid:
108 profile = invitation_data[u'guest_profile']
109 def session_started(dummy):
110 self.host.bridge.asyncGetParamA(
111 u'JabberID',
112 u'Connection',
113 profile_key=profile,
114 callback=lambda jid_: self.output_data(invitation_data, jid_),
115 errback=partial(self.errback,
116 msg=_(u"can't retrieve jid: {}"),
117 exit_code=C.EXIT_BRIDGE_ERRBACK))
118
119 self.host.bridge.profileStartSession(
120 invitation_data[u'password'],
121 profile,
122 callback=session_started,
123 errback=partial(self.errback,
124 msg=_(u"can't start session: {}"),
125 exit_code=C.EXIT_BRIDGE_ERRBACK))
126 else:
127 self.output_data(invitation_data)
128
129 def start(self):
130 self.host.bridge.invitationGet(
131 self.args.id,
132 callback=self.invitationGetCb,
133 errback=partial(self.errback,
134 msg=_(u"can't get invitation data: {}"),
135 exit_code=C.EXIT_BRIDGE_ERRBACK))
136
137
138 class Modify(base.CommandBase):
139
140 def __init__(self, host):
141 base.CommandBase.__init__(self, host, 'modify', use_profile=False, help=_(u'modify existing invitation'))
142 self.need_loop=True
143
144 def add_parser_options(self):
145 self.parser.add_argument("--replace", action='store_true', help='replace the whole data')
146 self.parser.add_argument("-n", "--name", type=base.unicode_decoder, default='', help='name of the invitee')
147 self.parser.add_argument("-N", "--host-name", type=base.unicode_decoder, default='', help='name of the host')
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)')
149 self.parser.add_argument("-l", "--lang", dest="language", type=base.unicode_decoder, default='',
150 help='main language spoken by the invitee')
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')
152 self.parser.add_argument("-p", "--profile", type=base.unicode_decoder, default='', help="profile doing the invitation (default: don't associate profile")
153 self.parser.add_argument("id", type=base.unicode_decoder,
154 help=_(u"invitation UUID"))
155
156 def invitationModifyCb(self):
157 self.disp(_(u'invitations have been modified correctly'))
158 self.host.quit(C.EXIT_OK)
159
160 def invitationModifyEb(self, failure_):
161 self.disp(u"can't create invitation: {reason}".format(
162 reason=failure_), error=True)
163 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
164
165 def start(self):
166 extra = dict(self.args.extra)
167 for arg_name in ('name', 'host_name', 'email', 'language', 'profile'):
168 value = getattr(self.args, arg_name)
169 if not value:
170 continue
171 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))
173 extra[arg_name] = value
174 self.host.bridge.invitationModify(
175 self.args.id,
176 extra,
177 self.args.replace,
178 callback=self.invitationModifyCb,
179 errback=self.invitationModifyEb)
180
181
182 class List(base.CommandBase):
183
184 def __init__(self, host):
185 extra_outputs = {'default': self.default_output}
186 base.CommandBase.__init__(self, host, 'list', use_profile=False, use_output=C.OUTPUT_COMPLEX, extra_outputs=extra_outputs, help=_(u'list invitations data'))
187 self.need_loop=True
188
189 def default_output(self, data):
190 for idx, datum in enumerate(data.iteritems()):
191 if idx:
192 self.disp(u"\n")
193 key, invitation_data = datum
194 self.disp(A.color(C.A_HEADER, key))
195 indent = u' '
196 for k, v in invitation_data.iteritems():
197 self.disp(indent + A.color(C.A_SUBHEADER, k + u':') + u' ' + unicode(v))
198
199 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"))
201
202 def invitationListCb(self, data):
203 self.output(data)
204 self.host.quit()
205
206 def start(self):
207 self.host.bridge.invitationList(
208 self.args.profile,
209 callback=self.invitationListCb,
210 errback=partial(self.errback,
211 msg=_(u"can't list invitations: {}"),
212 exit_code=C.EXIT_BRIDGE_ERRBACK))
213
214
215 class Invitation(base.CommandBase):
216 subcommands = (Create, Get, Modify, List)
217
218 def __init__(self, host):
219 super(Invitation, self).__init__(host, 'invitation', use_profile=False, help=_(u'invitation of user(s) without XMPP account'))