comparison sat_frontends/primitivus/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.quick_frontend.quick_profile_manager import QuickProfileManager 24 from sat_frontends.quick_frontend.quick_profile_manager import QuickProfileManager
24 from sat_frontends.primitivus.constants import Const as C 25 from sat_frontends.primitivus.constants import Const as C
25 from sat_frontends.primitivus.keys import action_key_map as a_key 26 from sat_frontends.primitivus.keys import action_key_map as a_key
26 from urwid_satext import sat_widgets 27 from urwid_satext import sat_widgets
27 import urwid 28 import urwid
28 29
29 30
30 class ProfileManager(QuickProfileManager, urwid.WidgetWrap): 31 class ProfileManager(QuickProfileManager, urwid.WidgetWrap):
31
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 onProfileChange
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.profilesListGet()
41 profiles.sort() 41 profiles.sort()
42 self.list_profile = sat_widgets.List(profiles, style=style, align='center', on_change=self.onProfileChange) 42 self.list_profile = sat_widgets.List(
43 43 profiles, style=style, align="center", on_change=self.onProfileChange
44 #new & delete buttons 44 )
45 buttons = [urwid.Button(_("New"), self.onNewProfile), 45
46 urwid.Button(_("Delete"), self.onDeleteProfile)] 46 # new & delete buttons
47 buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center') 47 buttons = [
48 48 urwid.Button(_("New"), self.onNewProfile),
49 #second part: login information: 49 urwid.Button(_("Delete"), self.onDeleteProfile),
50 divider = urwid.Divider('-') 50 ]
51 51 buttons_flow = urwid.GridFlow(
52 #connect button 52 buttons,
53 connect_button = sat_widgets.CustomButton(_("Connect"), self.onConnectProfiles, align='center') 53 max([len(button.get_label()) for button in buttons]) + 4,
54 54 1,
55 #we now build the widget 55 1,
56 list_walker = urwid.SimpleFocusListWalker([buttons_flow,self.list_profile, divider, self.login_wid, self.pass_wid, connect_button]) 56 "center",
57 )
58
59 # second part: login information:
60 divider = urwid.Divider("-")
61
62 # connect button
63 connect_button = sat_widgets.CustomButton(
64 _("Connect"), self.onConnectProfiles, align="center"
65 )
66
67 # we now build the widget
68 list_walker = urwid.SimpleFocusListWalker(
69 [
70 buttons_flow,
71 self.list_profile,
72 divider,
73 self.login_wid,
74 self.pass_wid,
75 connect_button,
76 ]
77 )
57 frame_body = urwid.ListBox(list_walker) 78 frame_body = urwid.ListBox(list_walker)
58 frame = urwid.Frame(frame_body,urwid.AttrMap(urwid.Text(_("Profile Manager"),align='center'),'title')) 79 frame = urwid.Frame(
80 frame_body,
81 urwid.AttrMap(urwid.Text(_("Profile Manager"), align="center"), "title"),
82 )
59 self.main_widget = urwid.LineBox(frame) 83 self.main_widget = urwid.LineBox(frame)
60 urwid.WidgetWrap.__init__(self, self.main_widget) 84 urwid.WidgetWrap.__init__(self, self.main_widget)
61 85
62 self.go(autoconnect) 86 self.go(autoconnect)
63 87
64
65 def keypress(self, size, key): 88 def keypress(self, size, key):
66 if key == a_key['APP_QUIT']: 89 if key == a_key["APP_QUIT"]:
67 self.host.onExit() 90 self.host.onExit()
68 raise urwid.ExitMainLoop() 91 raise urwid.ExitMainLoop()
69 elif key in (a_key['FOCUS_UP'], a_key['FOCUS_DOWN']): 92 elif key in (a_key["FOCUS_UP"], a_key["FOCUS_DOWN"]):
70 focus_diff = 1 if key==a_key['FOCUS_DOWN'] else -1 93 focus_diff = 1 if key == a_key["FOCUS_DOWN"] else -1
71 list_box = self.main_widget.base_widget.body 94 list_box = self.main_widget.base_widget.body
72 current_focus = list_box.body.get_focus()[1] 95 current_focus = list_box.body.get_focus()[1]
73 if current_focus is None: 96 if current_focus is None:
74 return 97 return
75 while True: 98 while True:
76 current_focus += focus_diff 99 current_focus += focus_diff
77 if current_focus < 0 or current_focus >= len(list_box.body): 100 if current_focus < 0 or current_focus >= len(list_box.body):
78 break 101 break
79 if list_box.body[current_focus].selectable(): 102 if list_box.body[current_focus].selectable():
80 list_box.set_focus(current_focus, 'above' if focus_diff == 1 else 'below') 103 list_box.set_focus(
104 current_focus, "above" if focus_diff == 1 else "below"
105 )
81 list_box._invalidate() 106 list_box._invalidate()
82 return 107 return
83 return super(ProfileManager, self).keypress(size, key) 108 return super(ProfileManager, self).keypress(size, key)
84 109
85 def cancelDialog(self, button): 110 def cancelDialog(self, button):
86 self.host.removePopUp() 111 self.host.removePopUp()
87 112
88 def newProfile(self, button, edit): 113 def newProfile(self, button, edit):
89 """Create the profile""" 114 """Create the profile"""
90 name = edit.get_edit_text() 115 name = edit.get_edit_text()
91 self.host.bridge.profileCreate(name, callback=lambda: self.newProfileCreated(name), errback=self.profileCreationFailure) 116 self.host.bridge.profileCreate(
117 name,
118 callback=lambda: self.newProfileCreated(name),
119 errback=self.profileCreationFailure,
120 )
92 121
93 def newProfileCreated(self, profile): 122 def newProfileCreated(self, profile):
94 # 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
95 self.host.bridge.profileStartSession('', profile, callback=lambda dummy: self.newProfileSessionStarted(profile), errback=self.profileCreationFailure) 124 self.host.bridge.profileStartSession(
125 "",
126 profile,
127 callback=lambda dummy: self.newProfileSessionStarted(profile),
128 errback=self.profileCreationFailure,
129 )
96 130
97 def newProfileSessionStarted(self, profile): 131 def newProfileSessionStarted(self, profile):
98 self.host.removePopUp() 132 self.host.removePopUp()
99 self.refillProfiles() 133 self.refillProfiles()
100 self.list_profile.selectValue(profile) 134 self.list_profile.selectValue(profile)
101 self.current.profile=profile 135 self.current.profile = profile
102 self.getConnectionParams(profile) 136 self.getConnectionParams(profile)
103 self.host.redraw() 137 self.host.redraw()
104 138
105 def profileCreationFailure(self, reason): 139 def profileCreationFailure(self, reason):
106 self.host.removePopUp() 140 self.host.removePopUp()
110 def deleteProfile(self, button): 144 def deleteProfile(self, button):
111 self._deleteProfile() 145 self._deleteProfile()
112 self.host.removePopUp() 146 self.host.removePopUp()
113 147
114 def onNewProfile(self, e): 148 def onNewProfile(self, e):
115 pop_up_widget = sat_widgets.InputDialog(_("New profile"), _("Please enter a new profile name"), cancel_cb=self.cancelDialog, ok_cb=self.newProfile) 149 pop_up_widget = sat_widgets.InputDialog(
150 _("New profile"),
151 _("Please enter a new profile name"),
152 cancel_cb=self.cancelDialog,
153 ok_cb=self.newProfile,
154 )
116 self.host.showPopUp(pop_up_widget) 155 self.host.showPopUp(pop_up_widget)
117 156
118 def onDeleteProfile(self, e): 157 def onDeleteProfile(self, e):
119 if self.current.profile: 158 if self.current.profile:
120 pop_up_widget = sat_widgets.ConfirmDialog(_("Are you sure you want to delete the profile {} ?").format(self.current.profile), no_cb=self.cancelDialog, yes_cb=self.deleteProfile) 159 pop_up_widget = sat_widgets.ConfirmDialog(
160 _("Are you sure you want to delete the profile {} ?").format(
161 self.current.profile
162 ),
163 no_cb=self.cancelDialog,
164 yes_cb=self.deleteProfile,
165 )
121 self.host.showPopUp(pop_up_widget) 166 self.host.showPopUp(pop_up_widget)
122 167
123 def onConnectProfiles(self, button): 168 def onConnectProfiles(self, button):
124 """Connect the profiles and start the main widget 169 """Connect the profiles and start the main widget
125 170
147 return self.pass_wid.get_edit_text() 192 return self.pass_wid.get_edit_text()
148 193
149 def setJID(self, jid_): 194 def setJID(self, jid_):
150 self.login_wid.set_edit_text(jid_) 195 self.login_wid.set_edit_text(jid_)
151 self.current.login = jid_ 196 self.current.login = jid_
152 self.host.redraw() # FIXME: redraw should be avoided 197 self.host.redraw() # FIXME: redraw should be avoided
153 198
154 def setPassword(self, password): 199 def setPassword(self, password):
155 self.pass_wid.set_edit_text(password) 200 self.pass_wid.set_edit_text(password)
156 self.current.password = password 201 self.current.password = password
157 self.host.redraw() 202 self.host.redraw()
162 @param list_wid: the List widget who sent the event 207 @param list_wid: the List widget who sent the event
163 """ 208 """
164 self.updateConnectionParams() 209 self.updateConnectionParams()
165 focused = list_wid.focus 210 focused = list_wid.focus
166 selected = focused.getState() if focused is not None else False 211 selected = focused.getState() if focused is not None else False
167 if not selected: # profile was just unselected 212 if not selected: # profile was just unselected
168 return 213 return
169 focused.setState(False, invisible=True) # we don't want the widget to be selected until we are sure we can access it 214 focused.setState(
215 False, invisible=True
216 ) # we don't want the widget to be selected until we are sure we can access it
217
170 def authenticate_cb(data, cb_id, profile): 218 def authenticate_cb(data, cb_id, profile):
171 if C.bool(data.pop('validated', C.BOOL_FALSE)): 219 if C.bool(data.pop("validated", C.BOOL_FALSE)):
172 self.current.profile = profile 220 self.current.profile = profile
173 focused.setState(True, invisible=True) 221 focused.setState(True, invisible=True)
174 self.getConnectionParams(profile) 222 self.getConnectionParams(profile)
175 self.host.redraw() 223 self.host.redraw()
176 self.host.actionManager(data, callback=authenticate_cb, profile=profile) 224 self.host.actionManager(data, callback=authenticate_cb, profile=profile)
177 225
178 self.host.launchAction(C.AUTHENTICATE_PROFILE_ID, callback=authenticate_cb, profile=focused.text) 226 self.host.launchAction(
179 227 C.AUTHENTICATE_PROFILE_ID, callback=authenticate_cb, profile=focused.text
228 )