Mercurial > libervia-backend
comparison sat_frontends/quick_frontend/quick_profile_manager.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 |
---|---|
17 # You should have received a copy of the GNU Affero General Public License | 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/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 from sat.core.i18n import _ | 20 from sat.core.i18n import _ |
21 from sat.core import log as logging | 21 from sat.core import log as logging |
22 | |
22 log = logging.getLogger(__name__) | 23 log = logging.getLogger(__name__) |
23 from sat_frontends.primitivus.constants import Const as C | 24 from sat_frontends.primitivus.constants import Const as C |
24 | 25 |
25 | 26 |
26 class ProfileRecord(object): | 27 class ProfileRecord(object): |
83 """ | 84 """ |
84 if not profile_keys: | 85 if not profile_keys: |
85 log.warning("No profile given to autoconnect") | 86 log.warning("No profile given to autoconnect") |
86 return | 87 return |
87 self._autoconnect = True | 88 self._autoconnect = True |
88 self._autoconnect_profiles=[] | 89 self._autoconnect_profiles = [] |
89 self._do_autoconnect(profile_keys) | 90 self._do_autoconnect(profile_keys) |
90 | |
91 | 91 |
92 def _do_autoconnect(self, profile_keys): | 92 def _do_autoconnect(self, profile_keys): |
93 """Connect automatically given profiles | 93 """Connect automatically given profiles |
94 | 94 |
95 @param profile_kes(iterable): profiles to connect | 95 @param profile_kes(iterable): profiles to connect |
96 """ | 96 """ |
97 assert self._autoconnect | 97 assert self._autoconnect |
98 | 98 |
99 def authenticate_cb(data, cb_id, profile): | 99 def authenticate_cb(data, cb_id, profile): |
100 | 100 |
101 if C.bool(data.pop('validated', C.BOOL_FALSE)): | 101 if C.bool(data.pop("validated", C.BOOL_FALSE)): |
102 self._autoconnect_profiles.append(profile) | 102 self._autoconnect_profiles.append(profile) |
103 if len(self._autoconnect_profiles) == len(profile_keys): | 103 if len(self._autoconnect_profiles) == len(profile_keys): |
104 # all the profiles have been validated | 104 # all the profiles have been validated |
105 self.host.plug_profiles(self._autoconnect_profiles) | 105 self.host.plug_profiles(self._autoconnect_profiles) |
106 else: | 106 else: |
107 # a profile is not validated, we go to manual mode | 107 # a profile is not validated, we go to manual mode |
108 self._autoconnect=False | 108 self._autoconnect = False |
109 self.host.actionManager(data, callback=authenticate_cb, profile=profile) | 109 self.host.actionManager(data, callback=authenticate_cb, profile=profile) |
110 | 110 |
111 def getProfileNameCb(profile): | 111 def getProfileNameCb(profile): |
112 if not profile: | 112 if not profile: |
113 # FIXME: this method is not handling manual mode correclty anymore | 113 # FIXME: this method is not handling manual mode correclty anymore |
114 # must be thought to be handled asynchronously | 114 # must be thought to be handled asynchronously |
115 self._autoconnect = False # manual mode | 115 self._autoconnect = False # manual mode |
116 msg = _("Trying to plug an unknown profile key ({})".format(profile_key)) | 116 msg = _("Trying to plug an unknown profile key ({})".format(profile_key)) |
117 log.warning(msg) | 117 log.warning(msg) |
118 self.host.showDialog(_("Profile plugging in error"), msg, 'error') | 118 self.host.showDialog(_("Profile plugging in error"), msg, "error") |
119 else: | 119 else: |
120 self.host.launchAction(C.AUTHENTICATE_PROFILE_ID, callback=authenticate_cb, profile=profile) | 120 self.host.launchAction( |
121 C.AUTHENTICATE_PROFILE_ID, callback=authenticate_cb, profile=profile | |
122 ) | |
121 | 123 |
122 def getProfileNameEb(failure): | 124 def getProfileNameEb(failure): |
123 log.error(u"Can't retrieve profile name: {}".format(failure)) | 125 log.error(u"Can't retrieve profile name: {}".format(failure)) |
124 | 126 |
125 for profile_key in profile_keys: | 127 for profile_key in profile_keys: |
126 self.host.bridge.profileNameGet(profile_key, callback=getProfileNameCb, errback=getProfileNameEb) | 128 self.host.bridge.profileNameGet( |
127 | 129 profile_key, callback=getProfileNameCb, errback=getProfileNameEb |
130 ) | |
128 | 131 |
129 def getParamError(self, dummy): | 132 def getParamError(self, dummy): |
130 self.host.showDialog(_(u"Error"), _("Can't get profile parameter"), 'error') | 133 self.host.showDialog(_(u"Error"), _("Can't get profile parameter"), "error") |
131 | 134 |
132 ## Helping methods ## | 135 ## Helping methods ## |
133 | 136 |
134 def _getErrorMessage(self, reason): | 137 def _getErrorMessage(self, reason): |
135 """Return an error message corresponding to profile creation error | 138 """Return an error message corresponding to profile creation error |
140 if reason == "ConflictError": | 143 if reason == "ConflictError": |
141 message = _("A profile with this name already exists") | 144 message = _("A profile with this name already exists") |
142 elif reason == "CancelError": | 145 elif reason == "CancelError": |
143 message = _("Profile creation cancelled by backend") | 146 message = _("Profile creation cancelled by backend") |
144 elif reason == "ValueError": | 147 elif reason == "ValueError": |
145 message = _("You profile name is not valid") # TODO: print a more informative message (empty name, name starting with '@') | 148 message = _( |
149 "You profile name is not valid" | |
150 ) # TODO: print a more informative message (empty name, name starting with '@') | |
146 else: | 151 else: |
147 message = _("Can't create profile ({})").format(reason) | 152 message = _("Can't create profile ({})").format(reason) |
148 return message | 153 return message |
149 | 154 |
150 def _deleteProfile(self): | 155 def _deleteProfile(self): |
151 """Delete the currently selected profile""" | 156 """Delete the currently selected profile""" |
152 if self.current.profile: | 157 if self.current.profile: |
153 self.host.bridge.asyncDeleteProfile(self.current.profile, callback=self.refillProfiles) | 158 self.host.bridge.asyncDeleteProfile( |
159 self.current.profile, callback=self.refillProfiles | |
160 ) | |
154 self.resetFields() | 161 self.resetFields() |
155 | 162 |
156 ## workflow methods (events occuring during the profiles selection) ## | 163 ## workflow methods (events occuring during the profiles selection) ## |
157 | 164 |
158 # These methods must be called by the frontend at some point | 165 # These methods must be called by the frontend at some point |
159 | 166 |
160 def _onConnectProfiles(self): | 167 def _onConnectProfiles(self): |
161 """Connect the profiles and start the main widget""" | 168 """Connect the profiles and start the main widget""" |
162 if self._autoconnect: | 169 if self._autoconnect: |
163 self.host.showDialog(_('Internal error'), _("You can't connect manually and automatically at the same time"), 'error') | 170 self.host.showDialog( |
171 _("Internal error"), | |
172 _("You can't connect manually and automatically at the same time"), | |
173 "error", | |
174 ) | |
164 return | 175 return |
165 self.updateConnectionParams() | 176 self.updateConnectionParams() |
166 profiles = self.getProfiles() | 177 profiles = self.getProfiles() |
167 if not profiles: | 178 if not profiles: |
168 self.host.showDialog(_('No profile selected'), _('You need to create and select at least one profile before connecting'), 'error') | 179 self.host.showDialog( |
180 _("No profile selected"), | |
181 _("You need to create and select at least one profile before connecting"), | |
182 "error", | |
183 ) | |
169 else: | 184 else: |
170 # All profiles in the list are already validated, so we can plug them directly | 185 # All profiles in the list are already validated, so we can plug them directly |
171 self.host.plug_profiles(profiles) | 186 self.host.plug_profiles(profiles) |
172 | 187 |
173 def getConnectionParams(self, profile): | 188 def getConnectionParams(self, profile): |
174 """Get login and password and display them | 189 """Get login and password and display them |
175 | 190 |
176 @param profile: %(doc_profile)s | 191 @param profile: %(doc_profile)s |
177 """ | 192 """ |
178 self.host.bridge.asyncGetParamA("JabberID", "Connection", profile_key=profile, callback=self.setJID, errback=self.getParamError) | 193 self.host.bridge.asyncGetParamA( |
179 self.host.bridge.asyncGetParamA("Password", "Connection", profile_key=profile, callback=self.setPassword, errback=self.getParamError) | 194 "JabberID", |
195 "Connection", | |
196 profile_key=profile, | |
197 callback=self.setJID, | |
198 errback=self.getParamError, | |
199 ) | |
200 self.host.bridge.asyncGetParamA( | |
201 "Password", | |
202 "Connection", | |
203 profile_key=profile, | |
204 callback=self.setPassword, | |
205 errback=self.getParamError, | |
206 ) | |
180 | 207 |
181 def updateConnectionParams(self): | 208 def updateConnectionParams(self): |
182 """Check if connection parameters have changed, and update them if so""" | 209 """Check if connection parameters have changed, and update them if so""" |
183 if self.current.profile: | 210 if self.current.profile: |
184 login = self.getJID() | 211 login = self.getJID() |
185 password = self.getPassword() | 212 password = self.getPassword() |
186 if login != self.current.login and self.current.login is not None: | 213 if login != self.current.login and self.current.login is not None: |
187 self.current.login = login | 214 self.current.login = login |
188 self.host.bridge.setParam("JabberID", login, "Connection", profile_key=self.current.profile) | 215 self.host.bridge.setParam( |
216 "JabberID", login, "Connection", profile_key=self.current.profile | |
217 ) | |
189 log.info(u"login updated for profile [{}]".format(self.current.profile)) | 218 log.info(u"login updated for profile [{}]".format(self.current.profile)) |
190 if password != self.current.password and self.current.password is not None: | 219 if password != self.current.password and self.current.password is not None: |
191 self.current.password = password | 220 self.current.password = password |
192 self.host.bridge.setParam("Password", password, "Connection", profile_key=self.current.profile) | 221 self.host.bridge.setParam( |
193 log.info(u"password updated for profile [{}]".format(self.current.profile)) | 222 "Password", password, "Connection", profile_key=self.current.profile |
223 ) | |
224 log.info( | |
225 u"password updated for profile [{}]".format(self.current.profile) | |
226 ) | |
194 | 227 |
195 ## graphic updates (should probably be overriden in frontends) ## | 228 ## graphic updates (should probably be overriden in frontends) ## |
196 | 229 |
197 def resetFields(self): | 230 def resetFields(self): |
198 """Set profile to None, and reset fields""" | 231 """Set profile to None, and reset fields""" |
199 self.current.profile=None | 232 self.current.profile = None |
200 self.setJID("") | 233 self.setJID("") |
201 self.setPassword("") | 234 self.setPassword("") |
202 | 235 |
203 def refillProfiles(self): | 236 def refillProfiles(self): |
204 """Rebuild the list of profiles""" | 237 """Rebuild the list of profiles""" |
220 | 253 |
221 def setProfiles(self, profiles): | 254 def setProfiles(self, profiles): |
222 """Update the list of profiles""" | 255 """Update the list of profiles""" |
223 raise NotImplementedError | 256 raise NotImplementedError |
224 | 257 |
225 | |
226 def getJID(self): | 258 def getJID(self): |
227 """Get current jid | 259 """Get current jid |
228 | 260 |
229 Must be implemented by frontends | 261 Must be implemented by frontends |
230 @return (unicode): current jabber id | 262 @return (unicode): current jabber id |