Mercurial > libervia-backend
diff sat/plugins/plugin_misc_account.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 | 943de4f9ce50 |
children |
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_account.py Fri Apr 07 15:18:39 2023 +0200 +++ b/sat/plugins/plugin_misc_account.py Sat Apr 08 13:54:42 2023 +0200 @@ -108,72 +108,72 @@ def __init__(self, host): log.info(_("Plugin Account initialization")) self.host = host - host.bridge.addMethod( - "registerSatAccount", + host.bridge.add_method( + "libervia_account_register", ".plugin", in_sign="sss", out_sign="", - method=self._registerAccount, + method=self._register_account, async_=True, ) - host.bridge.addMethod( - "getNewAccountDomain", + host.bridge.add_method( + "account_domain_new_get", ".plugin", in_sign="", out_sign="s", - method=self.getNewAccountDomain, + method=self.account_domain_new_get, async_=False, ) - host.bridge.addMethod( - "getAccountDialogUI", + host.bridge.add_method( + "account_dialog_ui_get", ".plugin", in_sign="s", out_sign="s", - method=self._getAccountDialogUI, + method=self._get_account_dialog_ui, async_=False, ) - host.bridge.addMethod( - "asyncConnectWithXMPPCredentials", + host.bridge.add_method( + "credentials_xmpp_connect", ".plugin", in_sign="ss", out_sign="b", - method=self.asyncConnectWithXMPPCredentials, + method=self.credentials_xmpp_connect, async_=True, ) - self.fixEmailAdmins() + self.fix_email_admins() self._sessions = Sessions() - self.__account_cb_id = host.registerCallback( - self._accountDialogCb, with_data=True + self.__account_cb_id = host.register_callback( + self._account_dialog_cb, with_data=True ) - self.__change_password_id = host.registerCallback( - self.__changePasswordCb, with_data=True + self.__change_password_id = host.register_callback( + self.__change_password_cb, with_data=True ) - def deleteBlogCallback(posts, comments): - return lambda data, profile: self.__deleteBlogPostsCb( + def delete_blog_callback(posts, comments): + return lambda data, profile: self.__delete_blog_posts_cb( posts, comments, data, profile ) - self.__delete_posts_id = host.registerCallback( - deleteBlogCallback(True, False), with_data=True + self.__delete_posts_id = host.register_callback( + delete_blog_callback(True, False), with_data=True ) - self.__delete_comments_id = host.registerCallback( - deleteBlogCallback(False, True), with_data=True + self.__delete_comments_id = host.register_callback( + delete_blog_callback(False, True), with_data=True ) - self.__delete_posts_comments_id = host.registerCallback( - deleteBlogCallback(True, True), with_data=True + self.__delete_posts_comments_id = host.register_callback( + delete_blog_callback(True, True), with_data=True ) - self.__delete_account_id = host.registerCallback( - self.__deleteAccountCb, with_data=True + self.__delete_account_id = host.register_callback( + self.__delete_account_cb, with_data=True ) # FIXME: remove this after some time, when the deprecated parameter is really abandoned - def fixEmailAdmins(self): + def fix_email_admins(self): """Handle deprecated config option "admin_email" to fix the admin emails list""" - admin_email = self.getConfig("admin_email") + admin_email = self.config_get("admin_email") if not admin_email: return log.warning( @@ -182,10 +182,10 @@ param_name = "email_admins_list" try: section = "" - value = self.host.memory.getConfig(section, param_name, Exception) + value = self.host.memory.config_get(section, param_name, Exception) except (configparser.NoOptionError, configparser.NoSectionError): section = CONFIG_SECTION - value = self.host.memory.getConfig( + value = self.host.memory.config_get( section, param_name, default_conf[param_name] ) @@ -193,13 +193,13 @@ value.add(admin_email) self.host.memory.config.set(section, param_name, ",".join(value)) - def getConfig(self, name, section=CONFIG_SECTION): + def config_get(self, name, section=CONFIG_SECTION): if name.startswith("email_"): # XXX: email_ parameters were first in [plugin account] section # but as it make more sense to have them in common with other plugins, # they can now be in [DEFAULT] section try: - value = self.host.memory.getConfig(None, name, Exception) + value = self.host.memory.config_get(None, name, Exception) except (configparser.NoOptionError, configparser.NoSectionError): pass else: @@ -209,9 +209,9 @@ default = default_conf[name] else: default = None - return self.host.memory.getConfig(section, name, default) + return self.host.memory.config_get(section, name, default) - def _registerAccount(self, email, password, profile): + def _register_account(self, email, password, profile): return self.registerAccount(email, password, None, profile) def registerAccount(self, email, password, jid_s, profile): @@ -226,11 +226,11 @@ @param profile @return Deferred """ - d = self.createProfile(password, jid_s, profile) - d.addCallback(lambda __: self.sendEmails(email, profile)) + d = self.create_profile(password, jid_s, profile) + d.addCallback(lambda __: self.send_emails(email, profile)) return d - def createProfile(self, password, jid_s, profile): + def create_profile(self, password, jid_s, profile): """Register a new profile and its associated XMPP account. @param password (unicode): password chosen by the user @@ -244,14 +244,14 @@ if not password or not profile: raise exceptions.DataError - if profile.lower() in self.getConfig("reserved_list"): + if profile.lower() in self.config_get("reserved_list"): return defer.fail(Failure(exceptions.ConflictError)) - d = self.host.memory.createProfile(profile, password) - d.addCallback(lambda __: self.profileCreated(password, jid_s, profile)) + d = self.host.memory.create_profile(profile, password) + d.addCallback(lambda __: self.profile_created(password, jid_s, profile)) return d - def profileCreated(self, password, jid_s, profile): + def profile_created(self, password, jid_s, profile): """Create the XMPP account and set the profile connection parameters. @param password (unicode): password chosen by the user @@ -265,30 +265,30 @@ d = defer.succeed(None) jid_ = jid.JID(jid_s) else: - jid_s = profile + "@" + self.getNewAccountDomain() + jid_s = profile + "@" + self.account_domain_new_get() jid_ = jid.JID(jid_s) - d = self.host.plugins["XEP-0077"].registerNewAccount(jid_, password) + d = self.host.plugins["XEP-0077"].register_new_account(jid_, password) def setParams(__): - self.host.memory.setParam( + self.host.memory.param_set( "JabberID", jid_s, "Connection", profile_key=profile ) - d = self.host.memory.setParam( + d = self.host.memory.param_set( "Password", password, "Connection", profile_key=profile ) return d - def removeProfile(failure): - self.host.memory.asyncDeleteProfile(profile) + def remove_profile(failure): + self.host.memory.profile_delete_async(profile) return failure - d.addCallback(lambda __: self.host.memory.startSession(password, profile)) + d.addCallback(lambda __: self.host.memory.start_session(password, profile)) d.addCallback(setParams) - d.addCallback(lambda __: self.host.memory.stopSession(profile)) - d.addErrback(removeProfile) + d.addCallback(lambda __: self.host.memory.stop_session(profile)) + d.addErrback(remove_profile) return d - def _sendEmailEb(self, failure_, email): + def _send_email_eb(self, failure_, email): # TODO: return error code to user log.error( _("Failed to send account creation confirmation to {email}: {msg}").format( @@ -296,13 +296,13 @@ ) ) - def sendEmails(self, email, profile): + def send_emails(self, email, profile): # time to send the email - domain = self.getNewAccountDomain() + domain = self.account_domain_new_get() # email to the administrators - admins_emails = self.getConfig("email_admins_list") + admins_emails = self.config_get("email_admins_list") if not admins_emails: log.warning( "No known admin email, we can't send email to administrator(s).\n" @@ -313,7 +313,7 @@ subject = _("New Libervia account created") # there is no email when an existing XMPP account is used body = f"New account created on {domain}: {profile} [{email or '<no email>'}]" - d_admin = sat_email.sendEmail( + d_admin = sat_email.send_email( self.host.memory.config, admins_emails, subject, body) admins_emails_txt = ", ".join(["<" + addr + ">" for addr in admins_emails]) @@ -333,7 +333,7 @@ # TODO: if use register with an existing account, an XMPP message should be sent return d_admin - jid_s = self.host.memory.getParamA( + jid_s = self.host.memory.param_get_a( "JabberID", "Connection", profile_key=profile ) subject = _("Your Libervia account has been created") @@ -342,20 +342,20 @@ # XXX: this will not fail when the email address doesn't exist # FIXME: check email reception to validate email given by the user # FIXME: delete the profile if the email could not been sent? - d_user = sat_email.sendEmail(self.host.memory.config, [email], subject, body) + d_user = sat_email.send_email(self.host.memory.config, [email], subject, body) d_user.addCallbacks( lambda __: log.debug( "Account creation confirmation sent to <{}>".format(email) ), - self._sendEmailEb, + self._send_email_eb, errbackArgs=[email] ) return defer.DeferredList([d_user, d_admin]) - def getNewAccountDomain(self): + def account_domain_new_get(self): """get the domain that will be set to new account""" - domain = self.getConfig("new_account_domain") or self.getConfig( + domain = self.config_get("new_account_domain") or self.config_get( "xmpp_domain", None ) if not domain: @@ -367,7 +367,7 @@ return DEFAULT_DOMAIN return domain - def _getAccountDialogUI(self, profile): + def _get_account_dialog_ui(self, profile): """Get the main dialog to manage your account @param menu_data @param profile: %(doc_profile)s @@ -381,7 +381,7 @@ ) tab_container = form_ui.current_container - tab_container.addTab( + tab_container.add_tab( "update", D_("Change your password"), container=xml_tools.PairsContainer ) form_ui.addLabel(D_("Current profile password")) @@ -394,7 +394,7 @@ # FIXME: uncomment and fix these features """ if 'GROUPBLOG' in self.host.plugins: - tab_container.addTab("delete_posts", D_("Delete your posts"), container=xml_tools.PairsContainer) + tab_container.add_tab("delete_posts", D_("Delete your posts"), container=xml_tools.PairsContainer) form_ui.addLabel(D_("Current profile password")) form_ui.addPassword("delete_posts_passwd", value="") form_ui.addLabel(D_("Delete all your posts and their comments")) @@ -402,7 +402,7 @@ form_ui.addLabel(D_("Delete all your comments on other's posts")) form_ui.addBool("delete_comments_checkbox", "false") - tab_container.addTab("delete", D_("Delete your account"), container=xml_tools.PairsContainer) + tab_container.add_tab("delete", D_("Delete your account"), container=xml_tools.PairsContainer) form_ui.addLabel(D_("Current profile password")) form_ui.addPassword("delete_passwd", value="") form_ui.addLabel(D_("Delete your account")) @@ -412,12 +412,12 @@ return form_ui.toXml() @defer.inlineCallbacks - def _accountDialogCb(self, data, profile): + def _account_dialog_cb(self, data, profile): """Called when the user submits the main account dialog @param data @param profile """ - sat_cipher = yield self.host.memory.asyncGetParamA( + sat_cipher = yield self.host.memory.param_get_a_async( C.PROFILE_PASS_PATH[1], C.PROFILE_PASS_PATH[0], profile_key=profile ) @@ -442,7 +442,7 @@ verified = yield verify(delete_passwd) assert isinstance(verified, bool) if verified: - defer.returnValue(self.__deleteAccount(profile)) + defer.returnValue(self.__delete_account(profile)) defer.returnValue(error_ui()) # check for blog posts deletion @@ -456,7 +456,7 @@ verified = yield verify(delete_posts_passwd) assert isinstance(verified, bool) if verified: - defer.returnValue(self.__deleteBlogPosts(posts, comments, profile)) + defer.returnValue(self.__delete_blog_posts(posts, comments, profile)) defer.returnValue(error_ui()) """ @@ -469,7 +469,7 @@ assert isinstance(verified, bool) if verified: if new_passwd1 == new_passwd2: - data = yield self.__changePassword(new_passwd1, profile=profile) + data = yield self.__change_password(new_passwd1, profile=profile) defer.returnValue(data) else: defer.returnValue( @@ -481,13 +481,13 @@ defer.returnValue({}) - def __changePassword(self, password, profile): + def __change_password(self, password, profile): """Ask for a confirmation before changing the XMPP account and SàT profile passwords. @param password (str): the new password @param profile (str): %(doc_profile)s """ - session_id, __ = self._sessions.newSession( + session_id, __ = self._sessions.new_session( {"new_password": password}, profile=profile ) form_ui = xml_tools.XMLUI( @@ -504,24 +504,24 @@ form_ui.addText(D_("Continue with changing the password?")) return {"xmlui": form_ui.toXml()} - def __changePasswordCb(self, data, profile): + def __change_password_cb(self, data, profile): """Actually change the user XMPP account and SàT profile password @param data (dict) @profile (str): %(doc_profile)s """ - client = self.host.getClient(profile) - password = self._sessions.profileGet(data["session_id"], profile)["new_password"] + client = self.host.get_client(profile) + password = self._sessions.profile_get(data["session_id"], profile)["new_password"] del self._sessions[data["session_id"]] - def passwordChanged(__): - d = self.host.memory.setParam( + def password_changed(__): + d = self.host.memory.param_set( C.PROFILE_PASS_PATH[1], password, C.PROFILE_PASS_PATH[0], profile_key=profile, ) d.addCallback( - lambda __: self.host.memory.setParam( + lambda __: self.host.memory.param_set( "Password", password, "Connection", profile_key=profile ) ) @@ -536,11 +536,11 @@ ) return defer.succeed({"xmlui": error_ui.toXml()}) - d = self.host.plugins["XEP-0077"].changePassword(client, password) - d.addCallbacks(passwordChanged, errback) + d = self.host.plugins["XEP-0077"].change_password(client, password) + d.addCallbacks(password_changed, errback) return d - def __deleteAccount(self, profile): + def __delete_account(self, profile): """Ask for a confirmation before deleting the XMPP account and SàT profile @param profile """ @@ -561,7 +561,7 @@ D_( "All your data stored on %(server)s, including your %(target)s will be erased." ) - % {"server": self.getNewAccountDomain(), "target": target} + % {"server": self.account_domain_new_get(), "target": target} ) form_ui.addText( D_( @@ -570,26 +570,26 @@ ) return {"xmlui": form_ui.toXml()} - def __deleteAccountCb(self, data, profile): + def __delete_account_cb(self, data, profile): """Actually delete the XMPP account and SàT profile @param data @param profile """ - client = self.host.getClient(profile) + client = self.host.get_client(profile) - def userDeleted(__): + def user_deleted(__): # FIXME: client should be disconnected at this point, so 2 next loop should be removed (to be confirmed) for jid_ in client.roster._jids: # empty roster client.presence.unsubscribe(jid_) - for jid_ in self.host.memory.getWaitingSub( + for jid_ in self.host.memory.sub_waiting_get( profile ): # delete waiting subscriptions - self.host.memory.delWaitingSub(jid_) + self.host.memory.del_waiting_sub(jid_) - delete_profile = lambda: self.host.memory.asyncDeleteProfile( + delete_profile = lambda: self.host.memory.profile_delete_async( profile, force=True ) if "GROUPBLOG" in self.host.plugins: @@ -611,10 +611,10 @@ return defer.succeed({"xmlui": error_ui.toXml()}) d = self.host.plugins["XEP-0077"].unregister(client, jid.JID(client.jid.host)) - d.addCallbacks(userDeleted, errback) + d.addCallbacks(user_deleted, errback) return d - def __deleteBlogPosts(self, posts, comments, profile): + def __delete_blog_posts(self, posts, comments, profile): """Ask for a confirmation before deleting the blog posts @param posts: delete all posts of the user (and their comments) @param comments: delete all the comments of the user on other's posts @@ -678,7 +678,7 @@ return {"xmlui": form_ui.toXml()} - def __deleteBlogPostsCb(self, posts, comments, data, profile): + def __delete_blog_posts_cb(self, posts, comments, data, profile): """Actually delete the XMPP account and SàT profile @param posts: delete all posts of the user (and their comments) @param comments: delete all the comments of the user on other's posts @@ -723,7 +723,7 @@ d.addCallbacks(deleted, errback) return d - def asyncConnectWithXMPPCredentials(self, jid_s, password): + def credentials_xmpp_connect(self, jid_s, password): """Create and connect a new SàT profile using the given XMPP credentials. Re-use given JID and XMPP password for the profile name and profile password. @@ -733,34 +733,34 @@ @raise exceptions.PasswordError, exceptions.ConflictError """ try: # be sure that the profile doesn't exist yet - self.host.memory.getProfileName(jid_s) + self.host.memory.get_profile_name(jid_s) except exceptions.ProfileUnknownError: pass else: raise exceptions.ConflictError - d = self.createProfile(password, jid_s, jid_s) + d = self.create_profile(password, jid_s, jid_s) d.addCallback( - lambda __: self.host.memory.getProfileName(jid_s) + lambda __: self.host.memory.get_profile_name(jid_s) ) # checks if the profile has been successfuly created d.addCallback(lambda profile: defer.ensureDeferred( self.host.connect(profile, password, {}, 0))) def connected(result): - self.sendEmails(None, profile=jid_s) + self.send_emails(None, profile=jid_s) return result - def removeProfile( + def remove_profile( failure ): # profile has been successfully created but the XMPP credentials are wrong! log.debug( "Removing previously auto-created profile: %s" % failure.getErrorMessage() ) - self.host.memory.asyncDeleteProfile(jid_s) + self.host.memory.profile_delete_async(jid_s) raise failure # FIXME: we don't catch the case where the JID host is not an XMPP server, and the user # has to wait until the DBUS timeout ; as a consequence, emails are sent to the admins - # and the profile is not deleted. When the host exists, removeProfile is well called. - d.addCallbacks(connected, removeProfile) + # and the profile is not deleted. When the host exists, remove_profile is well called. + d.addCallbacks(connected, remove_profile) return d