Mercurial > libervia-backend
comparison sat_frontends/primitivus/profile_manager.py @ 4037:524856bd7b19
massive refactoring to switch from camelCase to snake_case:
historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a
pre-PEP8 code, to use the same coding style as in Twisted.
However, snake_case is more readable and it's better to follow PEP8 best practices, so it
has been decided to move on full snake_case. Because Libervia has a huge codebase, this
ended with a ugly mix of camelCase and snake_case.
To fix that, this patch does a big refactoring by renaming every function and method
(including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case.
This is a massive change, and may result in some bugs.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 08 Apr 2023 13:54:42 +0200 |
parents | be6d91572633 |
children | 4b842c1fb686 |
comparison
equal
deleted
inserted
replaced
4036:c4464d7ae97b | 4037:524856bd7b19 |
---|---|
30 | 30 |
31 class ProfileManager(QuickProfileManager, urwid.WidgetWrap): | 31 class ProfileManager(QuickProfileManager, urwid.WidgetWrap): |
32 def __init__(self, host, autoconnect=None): | 32 def __init__(self, host, autoconnect=None): |
33 QuickProfileManager.__init__(self, host, autoconnect) | 33 QuickProfileManager.__init__(self, host, autoconnect) |
34 | 34 |
35 # login & password box must be created before list because of onProfileChange | 35 # login & password box must be created before list because of on_profile_change |
36 self.login_wid = sat_widgets.AdvancedEdit(_("Login:"), align="center") | 36 self.login_wid = sat_widgets.AdvancedEdit(_("Login:"), align="center") |
37 self.pass_wid = sat_widgets.Password(_("Password:"), align="center") | 37 self.pass_wid = sat_widgets.Password(_("Password:"), align="center") |
38 | 38 |
39 style = ["no_first_select"] | 39 style = ["no_first_select"] |
40 profiles = host.bridge.profilesListGet() | 40 profiles = host.bridge.profiles_list_get() |
41 profiles.sort() | 41 profiles.sort() |
42 self.list_profile = sat_widgets.List( | 42 self.list_profile = sat_widgets.List( |
43 profiles, style=style, align="center", on_change=self.onProfileChange | 43 profiles, style=style, align="center", on_change=self.on_profile_change |
44 ) | 44 ) |
45 | 45 |
46 # new & delete buttons | 46 # new & delete buttons |
47 buttons = [ | 47 buttons = [ |
48 urwid.Button(_("New"), self.onNewProfile), | 48 urwid.Button(_("New"), self.on_new_profile), |
49 urwid.Button(_("Delete"), self.onDeleteProfile), | 49 urwid.Button(_("Delete"), self.on_delete_profile), |
50 ] | 50 ] |
51 buttons_flow = urwid.GridFlow( | 51 buttons_flow = urwid.GridFlow( |
52 buttons, | 52 buttons, |
53 max([len(button.get_label()) for button in buttons]) + 4, | 53 max([len(button.get_label()) for button in buttons]) + 4, |
54 1, | 54 1, |
59 # second part: login information: | 59 # second part: login information: |
60 divider = urwid.Divider("-") | 60 divider = urwid.Divider("-") |
61 | 61 |
62 # connect button | 62 # connect button |
63 connect_button = sat_widgets.CustomButton( | 63 connect_button = sat_widgets.CustomButton( |
64 _("Connect"), self.onConnectProfiles, align="center" | 64 _("Connect"), self.on_connect_profiles, align="center" |
65 ) | 65 ) |
66 | 66 |
67 # we now build the widget | 67 # we now build the widget |
68 list_walker = urwid.SimpleFocusListWalker( | 68 list_walker = urwid.SimpleFocusListWalker( |
69 [ | 69 [ |
85 | 85 |
86 self.go(autoconnect) | 86 self.go(autoconnect) |
87 | 87 |
88 def keypress(self, size, key): | 88 def keypress(self, size, key): |
89 if key == a_key["APP_QUIT"]: | 89 if key == a_key["APP_QUIT"]: |
90 self.host.onExit() | 90 self.host.on_exit() |
91 raise urwid.ExitMainLoop() | 91 raise urwid.ExitMainLoop() |
92 elif key in (a_key["FOCUS_UP"], a_key["FOCUS_DOWN"]): | 92 elif key in (a_key["FOCUS_UP"], a_key["FOCUS_DOWN"]): |
93 focus_diff = 1 if key == a_key["FOCUS_DOWN"] else -1 | 93 focus_diff = 1 if key == a_key["FOCUS_DOWN"] else -1 |
94 list_box = self.main_widget.base_widget.body | 94 list_box = self.main_widget.base_widget.body |
95 current_focus = list_box.body.get_focus()[1] | 95 current_focus = list_box.body.get_focus()[1] |
105 ) | 105 ) |
106 list_box._invalidate() | 106 list_box._invalidate() |
107 return | 107 return |
108 return super(ProfileManager, self).keypress(size, key) | 108 return super(ProfileManager, self).keypress(size, key) |
109 | 109 |
110 def cancelDialog(self, button): | 110 def cancel_dialog(self, button): |
111 self.host.removePopUp() | 111 self.host.remove_pop_up() |
112 | 112 |
113 def newProfile(self, button, edit): | 113 def new_profile(self, button, edit): |
114 """Create the profile""" | 114 """Create the profile""" |
115 name = edit.get_edit_text() | 115 name = edit.get_edit_text() |
116 self.host.bridge.profileCreate( | 116 self.host.bridge.profile_create( |
117 name, | 117 name, |
118 callback=lambda: self.newProfileCreated(name), | 118 callback=lambda: self.new_profile_created(name), |
119 errback=self.profileCreationFailure, | 119 errback=self.profile_creation_failure, |
120 ) | 120 ) |
121 | 121 |
122 def newProfileCreated(self, profile): | 122 def new_profile_created(self, profile): |
123 # new profile will be selected, and a selected profile assume the session is started | 123 # new profile will be selected, and a selected profile assume the session is started |
124 self.host.bridge.profileStartSession( | 124 self.host.bridge.profile_start_session( |
125 "", | 125 "", |
126 profile, | 126 profile, |
127 callback=lambda __: self.newProfileSessionStarted(profile), | 127 callback=lambda __: self.new_profile_session_started(profile), |
128 errback=self.profileCreationFailure, | 128 errback=self.profile_creation_failure, |
129 ) | 129 ) |
130 | 130 |
131 def newProfileSessionStarted(self, profile): | 131 def new_profile_session_started(self, profile): |
132 self.host.removePopUp() | 132 self.host.remove_pop_up() |
133 self.refillProfiles() | 133 self.refill_profiles() |
134 self.list_profile.selectValue(profile) | 134 self.list_profile.select_value(profile) |
135 self.current.profile = profile | 135 self.current.profile = profile |
136 self.getConnectionParams(profile) | 136 self.get_connection_params(profile) |
137 self.host.redraw() | 137 self.host.redraw() |
138 | 138 |
139 def profileCreationFailure(self, reason): | 139 def profile_creation_failure(self, reason): |
140 self.host.removePopUp() | 140 self.host.remove_pop_up() |
141 message = self._getErrorMessage(reason) | 141 message = self._get_error_message(reason) |
142 self.host.alert(_("Can't create profile"), message) | 142 self.host.alert(_("Can't create profile"), message) |
143 | 143 |
144 def deleteProfile(self, button): | 144 def delete_profile(self, button): |
145 self._deleteProfile() | 145 self._delete_profile() |
146 self.host.removePopUp() | 146 self.host.remove_pop_up() |
147 | 147 |
148 def onNewProfile(self, e): | 148 def on_new_profile(self, e): |
149 pop_up_widget = sat_widgets.InputDialog( | 149 pop_up_widget = sat_widgets.InputDialog( |
150 _("New profile"), | 150 _("New profile"), |
151 _("Please enter a new profile name"), | 151 _("Please enter a new profile name"), |
152 cancel_cb=self.cancelDialog, | 152 cancel_cb=self.cancel_dialog, |
153 ok_cb=self.newProfile, | 153 ok_cb=self.new_profile, |
154 ) | 154 ) |
155 self.host.showPopUp(pop_up_widget) | 155 self.host.show_pop_up(pop_up_widget) |
156 | 156 |
157 def onDeleteProfile(self, e): | 157 def on_delete_profile(self, e): |
158 if self.current.profile: | 158 if self.current.profile: |
159 pop_up_widget = sat_widgets.ConfirmDialog( | 159 pop_up_widget = sat_widgets.ConfirmDialog( |
160 _("Are you sure you want to delete the profile {} ?").format( | 160 _("Are you sure you want to delete the profile {} ?").format( |
161 self.current.profile | 161 self.current.profile |
162 ), | 162 ), |
163 no_cb=self.cancelDialog, | 163 no_cb=self.cancel_dialog, |
164 yes_cb=self.deleteProfile, | 164 yes_cb=self.delete_profile, |
165 ) | 165 ) |
166 self.host.showPopUp(pop_up_widget) | 166 self.host.show_pop_up(pop_up_widget) |
167 | 167 |
168 def onConnectProfiles(self, button): | 168 def on_connect_profiles(self, button): |
169 """Connect the profiles and start the main widget | 169 """Connect the profiles and start the main widget |
170 | 170 |
171 @param button: the connect button | 171 @param button: the connect button |
172 """ | 172 """ |
173 self._onConnectProfiles() | 173 self._on_connect_profiles() |
174 | 174 |
175 def resetFields(self): | 175 def reset_fields(self): |
176 """Set profile to None, and reset fields""" | 176 """Set profile to None, and reset fields""" |
177 super(ProfileManager, self).resetFields() | 177 super(ProfileManager, self).reset_fields() |
178 self.list_profile.unselectAll(invisible=True) | 178 self.list_profile.unselect_all(invisible=True) |
179 | 179 |
180 def setProfiles(self, profiles): | 180 def set_profiles(self, profiles): |
181 """Update the list of profiles""" | 181 """Update the list of profiles""" |
182 self.list_profile.changeValues(profiles) | 182 self.list_profile.change_values(profiles) |
183 self.host.redraw() | 183 self.host.redraw() |
184 | 184 |
185 def getProfiles(self): | 185 def get_profiles(self): |
186 return self.list_profile.getSelectedValues() | 186 return self.list_profile.get_selected_values() |
187 | 187 |
188 def getJID(self): | 188 def get_jid(self): |
189 return self.login_wid.get_edit_text() | 189 return self.login_wid.get_edit_text() |
190 | 190 |
191 def getPassword(self): | 191 def getPassword(self): |
192 return self.pass_wid.get_edit_text() | 192 return self.pass_wid.get_edit_text() |
193 | 193 |
194 def setJID(self, jid_): | 194 def set_jid(self, jid_): |
195 self.login_wid.set_edit_text(jid_) | 195 self.login_wid.set_edit_text(jid_) |
196 self.current.login = jid_ | 196 self.current.login = jid_ |
197 self.host.redraw() # FIXME: redraw should be avoided | 197 self.host.redraw() # FIXME: redraw should be avoided |
198 | 198 |
199 def setPassword(self, password): | 199 def set_password(self, password): |
200 self.pass_wid.set_edit_text(password) | 200 self.pass_wid.set_edit_text(password) |
201 self.current.password = password | 201 self.current.password = password |
202 self.host.redraw() | 202 self.host.redraw() |
203 | 203 |
204 def onProfileChange(self, list_wid, widget=None, selected=None): | 204 def on_profile_change(self, list_wid, widget=None, selected=None): |
205 """This is called when a profile is selected in the profile list. | 205 """This is called when a profile is selected in the profile list. |
206 | 206 |
207 @param list_wid: the List widget who sent the event | 207 @param list_wid: the List widget who sent the event |
208 """ | 208 """ |
209 self.updateConnectionParams() | 209 self.update_connection_params() |
210 focused = list_wid.focus | 210 focused = list_wid.focus |
211 selected = focused.getState() if focused is not None else False | 211 selected = focused.get_state() if focused is not None else False |
212 if not selected: # profile was just unselected | 212 if not selected: # profile was just unselected |
213 return | 213 return |
214 focused.setState( | 214 focused.set_state( |
215 False, invisible=True | 215 False, invisible=True |
216 ) # we don't want the widget to be selected until we are sure we can access it | 216 ) # we don't want the widget to be selected until we are sure we can access it |
217 | 217 |
218 def authenticate_cb(data, cb_id, profile): | 218 def authenticate_cb(data, cb_id, profile): |
219 if C.bool(data.pop("validated", C.BOOL_FALSE)): | 219 if C.bool(data.pop("validated", C.BOOL_FALSE)): |
220 self.current.profile = profile | 220 self.current.profile = profile |
221 focused.setState(True, invisible=True) | 221 focused.set_state(True, invisible=True) |
222 self.getConnectionParams(profile) | 222 self.get_connection_params(profile) |
223 self.host.redraw() | 223 self.host.redraw() |
224 self.host.actionManager(data, callback=authenticate_cb, profile=profile) | 224 self.host.action_manager(data, callback=authenticate_cb, profile=profile) |
225 | 225 |
226 self.host.launchAction( | 226 self.host.action_launch( |
227 C.AUTHENTICATE_PROFILE_ID, callback=authenticate_cb, profile=focused.text | 227 C.AUTHENTICATE_PROFILE_ID, callback=authenticate_cb, profile=focused.text |
228 ) | 228 ) |