comparison sat/stdui/ui_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
comparison
equal deleted inserted replaced
4036:c4464d7ae97b 4037:524856bd7b19
36 36
37 def __init__(self, host): 37 def __init__(self, host):
38 self.host = host 38 self.host = host
39 self.profile_ciphers = {} 39 self.profile_ciphers = {}
40 self._sessions = ProfileSessions() 40 self._sessions = ProfileSessions()
41 host.registerCallback( 41 host.register_callback(
42 self._authenticateProfile, force_id=C.AUTHENTICATE_PROFILE_ID, with_data=True 42 self._authenticate_profile, force_id=C.AUTHENTICATE_PROFILE_ID, with_data=True
43 ) 43 )
44 host.registerCallback( 44 host.register_callback(
45 self._changeXMPPPassword, force_id=C.CHANGE_XMPP_PASSWD_ID, with_data=True 45 self._change_xmpp_password, force_id=C.CHANGE_XMPP_PASSWD_ID, with_data=True
46 ) 46 )
47 self.__new_xmpp_passwd_id = host.registerCallback( 47 self.__new_xmpp_passwd_id = host.register_callback(
48 self._changeXMPPPasswordCb, with_data=True 48 self._change_xmpp_password_cb, with_data=True
49 ) 49 )
50 50
51 def _startSessionEb(self, fail, first, profile): 51 def _start_session_eb(self, fail, first, profile):
52 """Errback method for startSession during profile authentication 52 """Errback method for start_session during profile authentication
53 53
54 @param first(bool): if True, this is the first try and we have tryied empty password 54 @param first(bool): if True, this is the first try and we have tryied empty password
55 in this case we ask for a password to the user. 55 in this case we ask for a password to the user.
56 @param profile(unicode, None): %(doc_profile)s 56 @param profile(unicode, None): %(doc_profile)s
57 must only be used if first is True 57 must only be used if first is True
60 # first call, we ask for the password 60 # first call, we ask for the password
61 form_ui = xml_tools.XMLUI( 61 form_ui = xml_tools.XMLUI(
62 "form", title=D_("Profile password for {}").format(profile), submit_id="" 62 "form", title=D_("Profile password for {}").format(profile), submit_id=""
63 ) 63 )
64 form_ui.addPassword("profile_password", value="") 64 form_ui.addPassword("profile_password", value="")
65 d = xml_tools.deferredUI(self.host, form_ui, chained=True) 65 d = xml_tools.deferred_ui(self.host, form_ui, chained=True)
66 d.addCallback(self._authenticateProfile, profile) 66 d.addCallback(self._authenticate_profile, profile)
67 return {"xmlui": form_ui.toXml()} 67 return {"xmlui": form_ui.toXml()}
68 68
69 assert profile is None 69 assert profile is None
70 70
71 if fail.check(exceptions.PasswordError): 71 if fail.check(exceptions.PasswordError):
75 log.error("Unexpected exceptions: {}".format(fail)) 75 log.error("Unexpected exceptions: {}".format(fail))
76 dialog = xml_tools.XMLUI("popup", title=D_("Internal error")) 76 dialog = xml_tools.XMLUI("popup", title=D_("Internal error"))
77 dialog.addText(D_("Internal error: {}".format(fail))) 77 dialog.addText(D_("Internal error: {}".format(fail)))
78 return {"xmlui": dialog.toXml(), "validated": C.BOOL_FALSE} 78 return {"xmlui": dialog.toXml(), "validated": C.BOOL_FALSE}
79 79
80 def _authenticateProfile(self, data, profile): 80 def _authenticate_profile(self, data, profile):
81 if C.bool(data.get("cancelled", "false")): 81 if C.bool(data.get("cancelled", "false")):
82 return {} 82 return {}
83 if self.host.memory.isSessionStarted(profile): 83 if self.host.memory.is_session_started(profile):
84 return {"validated": C.BOOL_TRUE} 84 return {"validated": C.BOOL_TRUE}
85 try: 85 try:
86 password = data[xml_tools.formEscape("profile_password")] 86 password = data[xml_tools.form_escape("profile_password")]
87 except KeyError: 87 except KeyError:
88 # first request, we try empty password 88 # first request, we try empty password
89 password = "" 89 password = ""
90 first = True 90 first = True
91 eb_profile = profile 91 eb_profile = profile
92 else: 92 else:
93 first = False 93 first = False
94 eb_profile = None 94 eb_profile = None
95 d = self.host.memory.startSession(password, profile) 95 d = self.host.memory.start_session(password, profile)
96 d.addCallback(lambda __: {"validated": C.BOOL_TRUE}) 96 d.addCallback(lambda __: {"validated": C.BOOL_TRUE})
97 d.addErrback(self._startSessionEb, first, eb_profile) 97 d.addErrback(self._start_session_eb, first, eb_profile)
98 return d 98 return d
99 99
100 def _changeXMPPPassword(self, data, profile): 100 def _change_xmpp_password(self, data, profile):
101 session_data = self._sessions.profileGetUnique(profile) 101 session_data = self._sessions.profile_get_unique(profile)
102 if not session_data: 102 if not session_data:
103 server = self.host.memory.getParamA( 103 server = self.host.memory.param_get_a(
104 C.FORCE_SERVER_PARAM, "Connection", profile_key=profile 104 C.FORCE_SERVER_PARAM, "Connection", profile_key=profile
105 ) 105 )
106 if not server: 106 if not server:
107 server = jid.parse( 107 server = jid.parse(
108 self.host.memory.getParamA( 108 self.host.memory.param_get_a(
109 "JabberID", "Connection", profile_key=profile 109 "JabberID", "Connection", profile_key=profile
110 ) 110 )
111 )[1] 111 )[1]
112 session_id, session_data = self._sessions.newSession( 112 session_id, session_data = self._sessions.new_session(
113 {"count": 0, "server": server}, profile=profile 113 {"count": 0, "server": server}, profile=profile
114 ) 114 )
115 if ( 115 if (
116 session_data["count"] > 2 116 session_data["count"] > 2
117 ): # 3 attempts with a new password after the initial try 117 ): # 3 attempts with a new password after the initial try
118 self._sessions.profileDelUnique(profile) 118 self._sessions.profile_del_unique(profile)
119 _dialog = xml_tools.XMLUI("popup", title=D_("Connection error")) 119 _dialog = xml_tools.XMLUI("popup", title=D_("Connection error"))
120 _dialog.addText( 120 _dialog.addText(
121 D_("Can't connect to %s. Please check your connection details.") 121 D_("Can't connect to %s. Please check your connection details.")
122 % session_data["server"] 122 % session_data["server"]
123 ) 123 )
138 % session_data["server"] 138 % session_data["server"]
139 ) 139 )
140 form_ui.addPassword("xmpp_password", value="") 140 form_ui.addPassword("xmpp_password", value="")
141 return {"xmlui": form_ui.toXml()} 141 return {"xmlui": form_ui.toXml()}
142 142
143 def _changeXMPPPasswordCb(self, data, profile): 143 def _change_xmpp_password_cb(self, data, profile):
144 xmpp_password = data[xml_tools.formEscape("xmpp_password")] 144 xmpp_password = data[xml_tools.form_escape("xmpp_password")]
145 d = self.host.memory.setParam( 145 d = self.host.memory.param_set(
146 "Password", xmpp_password, "Connection", profile_key=profile 146 "Password", xmpp_password, "Connection", profile_key=profile
147 ) 147 )
148 d.addCallback(lambda __: defer.ensureDeferred(self.host.connect(profile))) 148 d.addCallback(lambda __: defer.ensureDeferred(self.host.connect(profile)))
149 d.addCallback(lambda __: {}) 149 d.addCallback(lambda __: {})
150 d.addErrback(lambda __: self._changeXMPPPassword({}, profile)) 150 d.addErrback(lambda __: self._change_xmpp_password({}, profile))
151 return d 151 return d