Mercurial > libervia-backend
comparison sat_frontends/jp/cmd_account.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 |
---|---|
19 | 19 |
20 """This module permits to manage XMPP accounts using in-band registration (XEP-0077)""" | 20 """This module permits to manage XMPP accounts using in-band registration (XEP-0077)""" |
21 | 21 |
22 from sat_frontends.jp.constants import Const as C | 22 from sat_frontends.jp.constants import Const as C |
23 from sat.core.log import getLogger | 23 from sat.core.log import getLogger |
24 | |
24 log = getLogger(__name__) | 25 log = getLogger(__name__) |
25 from sat.core.i18n import _ | 26 from sat.core.i18n import _ |
26 from sat_frontends.jp import base | 27 from sat_frontends.jp import base |
27 from sat_frontends.tools import jid | 28 from sat_frontends.tools import jid |
28 | 29 |
29 __commands__ = ["Account"] | 30 __commands__ = ["Account"] |
30 | 31 |
31 | 32 |
32 class AccountCreate(base.CommandBase): | 33 class AccountCreate(base.CommandBase): |
33 | 34 def __init__(self, host): |
34 def __init__(self, host): | 35 super(AccountCreate, self).__init__( |
35 super(AccountCreate, self).__init__(host, 'create', use_profile=False, use_verbose=True, help=_(u'create a XMPP account')) | 36 host, |
37 "create", | |
38 use_profile=False, | |
39 use_verbose=True, | |
40 help=_(u"create a XMPP account"), | |
41 ) | |
36 self.need_loop = True | 42 self.need_loop = True |
37 | 43 |
38 def add_parser_options(self): | 44 def add_parser_options(self): |
39 self.parser.add_argument('jid', type=base.unicode_decoder, help=_(u'jid to create')) | 45 self.parser.add_argument( |
40 self.parser.add_argument('password', type=base.unicode_decoder, help=_(u'password of the account')) | 46 "jid", type=base.unicode_decoder, help=_(u"jid to create") |
41 self.parser.add_argument('-p', '--profile', type=base.unicode_decoder, help=_(u"create a profile to use this account (default: don't create profile)")) | 47 ) |
42 self.parser.add_argument('-e', '--email', type=base.unicode_decoder, default="", help=_(u"email (usage depends of XMPP server)")) | 48 self.parser.add_argument( |
43 self.parser.add_argument('-H', '--host', type=base.unicode_decoder, default="", help=_(u"server host (IP address or domain, default: use localhost)")) | 49 "password", type=base.unicode_decoder, help=_(u"password of the account") |
44 self.parser.add_argument('-P', '--port', type=int, default=0, help=_(u"server port (IP address or domain, default: use localhost)")) | 50 ) |
51 self.parser.add_argument( | |
52 "-p", | |
53 "--profile", | |
54 type=base.unicode_decoder, | |
55 help=_( | |
56 u"create a profile to use this account (default: don't create profile)" | |
57 ), | |
58 ) | |
59 self.parser.add_argument( | |
60 "-e", | |
61 "--email", | |
62 type=base.unicode_decoder, | |
63 default="", | |
64 help=_(u"email (usage depends of XMPP server)"), | |
65 ) | |
66 self.parser.add_argument( | |
67 "-H", | |
68 "--host", | |
69 type=base.unicode_decoder, | |
70 default="", | |
71 help=_(u"server host (IP address or domain, default: use localhost)"), | |
72 ) | |
73 self.parser.add_argument( | |
74 "-P", | |
75 "--port", | |
76 type=int, | |
77 default=0, | |
78 help=_(u"server port (IP address or domain, default: use localhost)"), | |
79 ) | |
45 | 80 |
46 def _setParamCb(self): | 81 def _setParamCb(self): |
47 self.host.bridge.setParam("Password", self.args.password, "Connection", profile_key=self.args.profile, callback=self.host.quit, errback=self.errback) | 82 self.host.bridge.setParam( |
83 "Password", | |
84 self.args.password, | |
85 "Connection", | |
86 profile_key=self.args.profile, | |
87 callback=self.host.quit, | |
88 errback=self.errback, | |
89 ) | |
48 | 90 |
49 def _session_started(self, dummy): | 91 def _session_started(self, dummy): |
50 self.host.bridge.setParam("JabberID", self.args.jid, "Connection", profile_key=self.args.profile, callback=self._setParamCb, errback=self.errback) | 92 self.host.bridge.setParam( |
93 "JabberID", | |
94 self.args.jid, | |
95 "Connection", | |
96 profile_key=self.args.profile, | |
97 callback=self._setParamCb, | |
98 errback=self.errback, | |
99 ) | |
51 | 100 |
52 def _profileCreateCb(self): | 101 def _profileCreateCb(self): |
53 self.disp(_(u"profile created"), 1) | 102 self.disp(_(u"profile created"), 1) |
54 self.host.bridge.profileStartSession(self.args.password, self.args.profile, callback=self._session_started, errback=self.errback) | 103 self.host.bridge.profileStartSession( |
104 self.args.password, | |
105 self.args.profile, | |
106 callback=self._session_started, | |
107 errback=self.errback, | |
108 ) | |
55 | 109 |
56 def _profileCreateEb(self, failure_): | 110 def _profileCreateEb(self, failure_): |
57 self.disp(_(u"Can't create profile {profile} to associate with jid {jid}: {msg}").format( | 111 self.disp( |
58 profile = self.args.profile, | 112 _( |
59 jid = self.args.jid, | 113 u"Can't create profile {profile} to associate with jid {jid}: {msg}" |
60 msg = failure_), error=True) | 114 ).format(profile=self.args.profile, jid=self.args.jid, msg=failure_), |
115 error=True, | |
116 ) | |
61 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | 117 self.host.quit(C.EXIT_BRIDGE_ERRBACK) |
62 | 118 |
63 def accountNewCb(self): | 119 def accountNewCb(self): |
64 self.disp(_(u"XMPP account created"), 1) | 120 self.disp(_(u"XMPP account created"), 1) |
65 if self.args.profile is not None: | 121 if self.args.profile is not None: |
66 self.disp(_(u"creating profile"), 2) | 122 self.disp(_(u"creating profile"), 2) |
67 self.host.bridge.profileCreate(self.args.profile, self.args.password, "", callback=self._profileCreateCb, errback=self._profileCreateEb) | 123 self.host.bridge.profileCreate( |
124 self.args.profile, | |
125 self.args.password, | |
126 "", | |
127 callback=self._profileCreateCb, | |
128 errback=self._profileCreateEb, | |
129 ) | |
68 else: | 130 else: |
69 self.host.quit() | 131 self.host.quit() |
70 | 132 |
71 def accountNewEb(self, failure_): | 133 def accountNewEb(self, failure_): |
72 self.disp(_(u"Can't create new account on server {host} with jid {jid}: {msg}").format( | 134 self.disp( |
73 host = self.args.host or u"localhost", | 135 _(u"Can't create new account on server {host} with jid {jid}: {msg}").format( |
74 jid = self.args.jid, | 136 host=self.args.host or u"localhost", jid=self.args.jid, msg=failure_ |
75 msg = failure_), error=True) | 137 ), |
138 error=True, | |
139 ) | |
76 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | 140 self.host.quit(C.EXIT_BRIDGE_ERRBACK) |
77 | 141 |
78 def start(self): | 142 def start(self): |
79 self.host.bridge.inBandAccountNew(self.args.jid, self.args.password, self.args.email, self.args.host, self.args.port, | 143 self.host.bridge.inBandAccountNew( |
80 callback=self.accountNewCb, errback=self.accountNewEb) | 144 self.args.jid, |
81 | 145 self.args.password, |
146 self.args.email, | |
147 self.args.host, | |
148 self.args.port, | |
149 callback=self.accountNewCb, | |
150 errback=self.accountNewEb, | |
151 ) | |
82 | 152 |
83 | 153 |
84 class AccountModify(base.CommandBase): | 154 class AccountModify(base.CommandBase): |
85 | 155 def __init__(self, host): |
86 def __init__(self, host): | 156 super(AccountModify, self).__init__( |
87 super(AccountModify, self).__init__(host, 'modify', help=_(u'change password for XMPP account')) | 157 host, "modify", help=_(u"change password for XMPP account") |
158 ) | |
88 self.need_loop = True | 159 self.need_loop = True |
89 | 160 |
90 def add_parser_options(self): | 161 def add_parser_options(self): |
91 self.parser.add_argument('password', type=base.unicode_decoder, help=_(u'new XMPP password')) | 162 self.parser.add_argument( |
163 "password", type=base.unicode_decoder, help=_(u"new XMPP password") | |
164 ) | |
92 | 165 |
93 def start(self): | 166 def start(self): |
94 self.host.bridge.inBandPasswordChange(self.args.password, self.args.profile, | 167 self.host.bridge.inBandPasswordChange( |
95 callback=self.host.quit, errback=self.errback) | 168 self.args.password, |
169 self.args.profile, | |
170 callback=self.host.quit, | |
171 errback=self.errback, | |
172 ) | |
96 | 173 |
97 | 174 |
98 class AccountDelete(base.CommandBase): | 175 class AccountDelete(base.CommandBase): |
99 | 176 def __init__(self, host): |
100 def __init__(self, host): | 177 super(AccountDelete, self).__init__( |
101 super(AccountDelete, self).__init__(host, 'delete', help=_(u'delete a XMPP account')) | 178 host, "delete", help=_(u"delete a XMPP account") |
179 ) | |
102 self.need_loop = True | 180 self.need_loop = True |
103 | 181 |
104 def add_parser_options(self): | 182 def add_parser_options(self): |
105 self.parser.add_argument('-f', '--force', action='store_true', help=_(u'delete account without confirmation')) | 183 self.parser.add_argument( |
184 "-f", | |
185 "--force", | |
186 action="store_true", | |
187 help=_(u"delete account without confirmation"), | |
188 ) | |
106 | 189 |
107 def _got_jid(self, jid_str): | 190 def _got_jid(self, jid_str): |
108 jid_ = jid.JID(jid_str) | 191 jid_ = jid.JID(jid_str) |
109 if not self.args.force: | 192 if not self.args.force: |
110 message = (u"You are about to delete the XMPP account with jid {jid_}\n" | 193 message = ( |
111 u"This is the XMPP account of profile \"{profile}\"\n" | 194 u"You are about to delete the XMPP account with jid {jid_}\n" |
112 u"Are you sure that you want to delete this account ?".format( | 195 u'This is the XMPP account of profile "{profile}"\n' |
113 jid_ = jid_, | 196 u"Are you sure that you want to delete this account ?".format( |
114 profile=self.profile | 197 jid_=jid_, profile=self.profile |
115 )) | 198 ) |
199 ) | |
116 res = raw_input("{} (y/N)? ".format(message)) | 200 res = raw_input("{} (y/N)? ".format(message)) |
117 if res not in ("y", "Y"): | 201 if res not in ("y", "Y"): |
118 self.disp(_(u"Account deletion cancelled")) | 202 self.disp(_(u"Account deletion cancelled")) |
119 self.host.quit(2) | 203 self.host.quit(2) |
120 self.host.bridge.inBandUnregister(jid_.domain, self.args.profile, | 204 self.host.bridge.inBandUnregister( |
121 callback=self.host.quit, errback=self.errback) | 205 jid_.domain, self.args.profile, callback=self.host.quit, errback=self.errback |
206 ) | |
122 | 207 |
123 def start(self): | 208 def start(self): |
124 self.host.bridge.asyncGetParamA("JabberID", "Connection", profile_key=self.profile, | 209 self.host.bridge.asyncGetParamA( |
125 callback=self._got_jid, errback=self.errback) | 210 "JabberID", |
211 "Connection", | |
212 profile_key=self.profile, | |
213 callback=self._got_jid, | |
214 errback=self.errback, | |
215 ) | |
126 | 216 |
127 | 217 |
128 class Account(base.CommandBase): | 218 class Account(base.CommandBase): |
129 subcommands = (AccountCreate, AccountModify, AccountDelete) | 219 subcommands = (AccountCreate, AccountModify, AccountDelete) |
130 | 220 |
131 def __init__(self, host): | 221 def __init__(self, host): |
132 super(Account, self).__init__(host, 'account', use_profile=False, help=(u'XMPP account management')) | 222 super(Account, self).__init__( |
223 host, "account", use_profile=False, help=(u"XMPP account management") | |
224 ) |