# HG changeset patch # User RhodeCode Admin # Date 2023-07-18 12:26:37 # Node ID 71df309f91b565ed72548bc8ad9ded400c95c31d # Parent 525812a83a8265794b43e077b9cac77a9af6862e authentication: run modernize for python3 diff --git a/rhodecode/authentication/__init__.py b/rhodecode/authentication/__init__.py --- a/rhodecode/authentication/__init__.py +++ b/rhodecode/authentication/__init__.py @@ -1,5 +1,3 @@ - - # Copyright (C) 2012-2023 RhodeCode GmbH # # This program is free software: you can redistribute it and/or modify diff --git a/rhodecode/authentication/base.py b/rhodecode/authentication/base.py --- a/rhodecode/authentication/base.py +++ b/rhodecode/authentication/base.py @@ -162,7 +162,7 @@ class RhodeCodeAuthPluginBase(object): # TODO: johbo: Using the name here is problematic. It would be good to # introduce either new models in the database to hold Plugin and # PluginSetting or to use the plugin id here. - return 'auth_{}_{}'.format(self.name, name) + return f'auth_{self.name}_{name}' def _get_setting_type(self, name): """ @@ -175,7 +175,7 @@ class RhodeCodeAuthPluginBase(object): db_type = self._settings_type_map.get( type(schema_node.typ), 'unicode') if name in self._settings_encrypted: - db_type = '{}.encrypted'.format(db_type) + db_type = f'{db_type}.encrypted' return db_type @classmethod @@ -249,7 +249,7 @@ class RhodeCodeAuthPluginBase(object): """ Returns a plugin setting by name. """ - full_name = 'rhodecode_{}'.format(self._get_setting_full_name(name)) + full_name = f'rhodecode_{self._get_setting_full_name(name)}' if plugin_cached_settings: plugin_settings = plugin_cached_settings else: @@ -504,7 +504,7 @@ class RhodeCodeExternalAuthPlugin(RhodeC def _authenticate(self, userobj, username, passwd, settings, **kwargs): # at this point _authenticate calls plugin's `auth()` function - auth = super(RhodeCodeExternalAuthPlugin, self)._authenticate( + auth = super()._authenticate( userobj, username, passwd, settings, **kwargs) if auth: @@ -602,14 +602,14 @@ class AuthLdapBase(object): """ host = host.strip() if not full_resolve: - return '{}:{}'.format(host, port) + return f'{host}:{port}' log.debug('LDAP: Resolving IP for LDAP host `%s`', host) try: ip = socket.gethostbyname(host) log.debug('LDAP: Got LDAP host `%s` ip %s', host, ip) except Exception: - raise LdapConnectionError('Failed to resolve host: `{}`'.format(host)) + raise LdapConnectionError(f'Failed to resolve host: `{host}`') log.debug('LDAP: Checking if IP %s is accessible', ip) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -619,9 +619,9 @@ class AuthLdapBase(object): log.debug('LDAP: connection to %s successful', ip) except Exception: raise LdapConnectionError( - 'Failed to connect to host: `{}:{}`'.format(host, port)) + f'Failed to connect to host: `{host}:{port}`') - return '{}:{}'.format(host, port) + return f'{host}:{port}' if len(ldap_server) == 1: # in case of single server use resolver to detect potential @@ -651,7 +651,7 @@ class AuthLdapBase(object): def validate_username(cls, username): if "," in username: raise LdapUsernameError( - "invalid character `,` in username: `{}`".format(username)) + f"invalid character `,` in username: `{username}`") @classmethod def validate_password(cls, username, password): @@ -751,7 +751,7 @@ def authenticate(username, password, env user_id = user.user_id if user else 'no-user' # don't cache for empty users plugin_cache_active = plugin_cache_active and user_id - cache_namespace_uid = 'cache_user_auth.{}'.format(user_id) + cache_namespace_uid = f'cache_user_auth.{user_id}' region = rc_cache.get_or_create_region('cache_perms', cache_namespace_uid) @region.conditional_cache_on_arguments(namespace=cache_namespace_uid, @@ -793,7 +793,7 @@ def authenticate(username, password, env elapsed_time_ms = round(1000.0 * auth_time) # use ms only statsd.incr('rhodecode_login_success_total') statsd.timing("rhodecode_login_timing.histogram", elapsed_time_ms, - tags=["plugin:{}".format(plugin.get_id())], + tags=[f"plugin:{plugin.get_id()}"], use_decimals=False ) return plugin_user diff --git a/rhodecode/authentication/interface.py b/rhodecode/authentication/interface.py --- a/rhodecode/authentication/interface.py +++ b/rhodecode/authentication/interface.py @@ -1,5 +1,3 @@ - - # Copyright (C) 2012-2023 RhodeCode GmbH # # This program is free software: you can redistribute it and/or modify diff --git a/rhodecode/authentication/plugins/__init__.py b/rhodecode/authentication/plugins/__init__.py --- a/rhodecode/authentication/plugins/__init__.py +++ b/rhodecode/authentication/plugins/__init__.py @@ -1,5 +1,3 @@ - - # Copyright (C) 2012-2023 RhodeCode GmbH # # This program is free software: you can redistribute it and/or modify diff --git a/rhodecode/authentication/plugins/auth_crowd.py b/rhodecode/authentication/plugins/auth_crowd.py --- a/rhodecode/authentication/plugins/auth_crowd.py +++ b/rhodecode/authentication/plugins/auth_crowd.py @@ -1,5 +1,3 @@ - - # Copyright (C) 2012-2023 RhodeCode GmbH # # This program is free software: you can redistribute it and/or modify @@ -141,7 +139,7 @@ class CrowdServer(object): _headers = {"Content-type": "application/json", "Accept": "application/json"} if self.user and self.passwd: - authstring = base64.b64encode("%s:%s" % (self.user, self.passwd)) + authstring = base64.b64encode("{}:{}".format(self.user, self.passwd)) _headers["Authorization"] = "Basic %s" % authstring if headers: _headers.update(headers) @@ -170,7 +168,7 @@ class CrowdServer(object): if not noformat: ret_val = {"status": False, "body": body, - "error": "{}\n{}".format(e, msg)} + "error": f"{e}\n{msg}"} else: ret_val = None return ret_val @@ -224,7 +222,7 @@ class RhodeCodeAuthPlugin(RhodeCodeExter @hybrid_property def name(self): - return u"crowd" + return "crowd" def use_fake_password(self): return True @@ -293,5 +291,5 @@ class RhodeCodeAuthPlugin(RhodeCodeExter def includeme(config): - plugin_id = 'egg:rhodecode-enterprise-ce#{}'.format(RhodeCodeAuthPlugin.uid) + plugin_id = f'egg:rhodecode-enterprise-ce#{RhodeCodeAuthPlugin.uid}' plugin_factory(plugin_id).includeme(config) diff --git a/rhodecode/authentication/plugins/auth_headers.py b/rhodecode/authentication/plugins/auth_headers.py --- a/rhodecode/authentication/plugins/auth_headers.py +++ b/rhodecode/authentication/plugins/auth_headers.py @@ -1,5 +1,3 @@ - - # Copyright (C) 2012-2023 RhodeCode GmbH # # This program is free software: you can redistribute it and/or modify @@ -104,7 +102,7 @@ class RhodeCodeAuthPlugin(RhodeCodeExter @hybrid_property def name(self): - return u"headers" + return "headers" @property def is_headers_auth(self): @@ -159,7 +157,7 @@ class RhodeCodeAuthPlugin(RhodeCodeExter settings = kwargs.get('settings') or {} username = self._get_username(environ, settings) # we got the username, so use default method now - return super(RhodeCodeAuthPlugin, self).get_user(username) + return super().get_user(username) def auth(self, userobj, username, password, settings, **kwargs): """ @@ -229,5 +227,5 @@ class RhodeCodeAuthPlugin(RhodeCodeExter def includeme(config): - plugin_id = 'egg:rhodecode-enterprise-ce#{}'.format(RhodeCodeAuthPlugin.uid) + plugin_id = f'egg:rhodecode-enterprise-ce#{RhodeCodeAuthPlugin.uid}' plugin_factory(plugin_id).includeme(config) diff --git a/rhodecode/authentication/plugins/auth_jasig_cas.py b/rhodecode/authentication/plugins/auth_jasig_cas.py --- a/rhodecode/authentication/plugins/auth_jasig_cas.py +++ b/rhodecode/authentication/plugins/auth_jasig_cas.py @@ -1,5 +1,3 @@ - - # Copyright (C) 2012-2023 RhodeCode GmbH # # This program is free software: you can redistribute it and/or modify @@ -96,7 +94,7 @@ class RhodeCodeAuthPlugin(RhodeCodeExter @hybrid_property def name(self): - return u"jasig-cas" + return "jasig-cas" @property def is_headers_auth(self): @@ -171,5 +169,5 @@ class RhodeCodeAuthPlugin(RhodeCodeExter def includeme(config): - plugin_id = 'egg:rhodecode-enterprise-ce#{}'.format(RhodeCodeAuthPlugin.uid) + plugin_id = f'egg:rhodecode-enterprise-ce#{RhodeCodeAuthPlugin.uid}' plugin_factory(plugin_id).includeme(config) diff --git a/rhodecode/authentication/plugins/auth_ldap.py b/rhodecode/authentication/plugins/auth_ldap.py --- a/rhodecode/authentication/plugins/auth_ldap.py +++ b/rhodecode/authentication/plugins/auth_ldap.py @@ -1,4 +1,3 @@ - # Copyright (C) 2010-2023 RhodeCode GmbH # # This program is free software: you can redistribute it and/or modify @@ -223,7 +222,7 @@ class AuthLdap(AuthLdapBase): break else: raise LdapPasswordError( - 'Failed to authenticate user `{}` with given password'.format(username)) + f'Failed to authenticate user `{username}` with given password') except ldap.NO_SUCH_OBJECT: log.debug("LDAP says no such user '%s' (%s), org_exc:", @@ -429,7 +428,7 @@ class RhodeCodeAuthPlugin(RhodeCodeExter @hybrid_property def name(self): - return u"ldap" + return "ldap" def use_fake_password(self): return True @@ -547,5 +546,5 @@ class RhodeCodeAuthPlugin(RhodeCodeExter def includeme(config): - plugin_id = 'egg:rhodecode-enterprise-ce#{}'.format(RhodeCodeAuthPlugin.uid) + plugin_id = f'egg:rhodecode-enterprise-ce#{RhodeCodeAuthPlugin.uid}' plugin_factory(plugin_id).includeme(config) diff --git a/rhodecode/authentication/plugins/auth_pam.py b/rhodecode/authentication/plugins/auth_pam.py --- a/rhodecode/authentication/plugins/auth_pam.py +++ b/rhodecode/authentication/plugins/auth_pam.py @@ -1,5 +1,3 @@ - - # Copyright (C) 2012-2023 RhodeCode GmbH # # This program is free software: you can redistribute it and/or modify @@ -104,7 +102,7 @@ class RhodeCodeAuthPlugin(RhodeCodeExter @hybrid_property def name(self): - return u"pam" + return "pam" def get_settings_schema(self): return PamSettingsSchema() @@ -126,7 +124,7 @@ class RhodeCodeAuthPlugin(RhodeCodeExter log.debug('Got PAM response %s', auth_result) # old attrs fetched from RhodeCode database - default_email = "%s@%s" % (username, socket.gethostname()) + default_email = "{}@{}".format(username, socket.gethostname()) admin = getattr(userobj, 'admin', False) active = getattr(userobj, 'active', True) email = getattr(userobj, 'email', '') or default_email @@ -168,5 +166,5 @@ class RhodeCodeAuthPlugin(RhodeCodeExter def includeme(config): - plugin_id = 'egg:rhodecode-enterprise-ce#{}'.format(RhodeCodeAuthPlugin.uid) + plugin_id = f'egg:rhodecode-enterprise-ce#{RhodeCodeAuthPlugin.uid}' plugin_factory(plugin_id).includeme(config) diff --git a/rhodecode/authentication/plugins/auth_rhodecode.py b/rhodecode/authentication/plugins/auth_rhodecode.py --- a/rhodecode/authentication/plugins/auth_rhodecode.py +++ b/rhodecode/authentication/plugins/auth_rhodecode.py @@ -1,5 +1,3 @@ - - # Copyright (C) 2012-2023 RhodeCode GmbH # # This program is free software: you can redistribute it and/or modify @@ -84,7 +82,7 @@ class RhodeCodeAuthPlugin(RhodeCodeAuthP @hybrid_property def name(self): - return u"rhodecode" + return "rhodecode" def user_activation_state(self): def_user_perms = User.get_default_user().AuthUser().permissions['global'] @@ -98,7 +96,7 @@ class RhodeCodeAuthPlugin(RhodeCodeAuthP We know that user exists in our database. """ allows_non_existing_user = False - return super(RhodeCodeAuthPlugin, self).allows_authentication_from( + return super().allows_authentication_from( user, allows_non_existing_user=allows_non_existing_user) def auth(self, userobj, username, password, settings, **kwargs): @@ -218,5 +216,5 @@ class RhodeCodeSettingsSchema(AuthnPlugi def includeme(config): - plugin_id = 'egg:rhodecode-enterprise-ce#{}'.format(RhodeCodeAuthPlugin.uid) + plugin_id = f'egg:rhodecode-enterprise-ce#{RhodeCodeAuthPlugin.uid}' plugin_factory(plugin_id).includeme(config) diff --git a/rhodecode/authentication/plugins/auth_token.py b/rhodecode/authentication/plugins/auth_token.py --- a/rhodecode/authentication/plugins/auth_token.py +++ b/rhodecode/authentication/plugins/auth_token.py @@ -1,5 +1,3 @@ - - # Copyright (C) 2016-2023 RhodeCode GmbH # # This program is free software: you can redistribute it and/or modify @@ -82,7 +80,7 @@ class RhodeCodeAuthPlugin(RhodeCodeAuthP @hybrid_property def name(self): - return u"authtoken" + return "authtoken" def user_activation_state(self): def_user_perms = User.get_default_user().AuthUser().permissions['global'] @@ -107,7 +105,7 @@ class RhodeCodeAuthPlugin(RhodeCodeAuthP # only for vcs operations allowed_auth_sources = [VCS_TYPE] - return super(RhodeCodeAuthPlugin, self).allows_authentication_from( + return super().allows_authentication_from( user, allows_non_existing_user=False, allowed_auth_plugins=allowed_auth_plugins, allowed_auth_sources=allowed_auth_sources) @@ -157,7 +155,7 @@ class RhodeCodeAuthPlugin(RhodeCodeAuthP def includeme(config): - plugin_id = 'egg:rhodecode-enterprise-ce#{}'.format(RhodeCodeAuthPlugin.uid) + plugin_id = f'egg:rhodecode-enterprise-ce#{RhodeCodeAuthPlugin.uid}' plugin_factory(plugin_id).includeme(config) diff --git a/rhodecode/authentication/registry.py b/rhodecode/authentication/registry.py --- a/rhodecode/authentication/registry.py +++ b/rhodecode/authentication/registry.py @@ -1,5 +1,3 @@ - - # Copyright (C) 2012-2023 RhodeCode GmbH # # This program is free software: you can redistribute it and/or modify diff --git a/rhodecode/authentication/routes.py b/rhodecode/authentication/routes.py --- a/rhodecode/authentication/routes.py +++ b/rhodecode/authentication/routes.py @@ -1,5 +1,3 @@ - - # Copyright (C) 2012-2023 RhodeCode GmbH # # This program is free software: you can redistribute it and/or modify @@ -121,7 +119,7 @@ class AuthnRootResource(AuthnResourceBas counter = 1 current = name while current in self._store.keys(): - current = '{}{}'.format(name, counter) + current = f'{name}{counter}' counter += 1 if counter > limit: raise ConfigurationError( diff --git a/rhodecode/authentication/schema.py b/rhodecode/authentication/schema.py --- a/rhodecode/authentication/schema.py +++ b/rhodecode/authentication/schema.py @@ -1,5 +1,3 @@ - - # Copyright (C) 2012-2023 RhodeCode GmbH # # This program is free software: you can redistribute it and/or modify diff --git a/rhodecode/authentication/tests/conftest.py b/rhodecode/authentication/tests/conftest.py --- a/rhodecode/authentication/tests/conftest.py +++ b/rhodecode/authentication/tests/conftest.py @@ -1,5 +1,3 @@ - - # Copyright (C) 2016-2023 RhodeCode GmbH # # This program is free software: you can redistribute it and/or modify diff --git a/rhodecode/authentication/tests/functional/test_settings.py b/rhodecode/authentication/tests/functional/test_settings.py --- a/rhodecode/authentication/tests/functional/test_settings.py +++ b/rhodecode/authentication/tests/functional/test_settings.py @@ -1,5 +1,3 @@ - - # Copyright (C) 2016-2023 RhodeCode GmbH # # This program is free software: you can redistribute it and/or modify @@ -30,7 +28,7 @@ from rhodecode.apps._base import ADMIN_P class TestAuthenticationSettings: def test_auth_settings_global_view_get(self, app): - url = '{prefix}/auth/'.format(prefix=ADMIN_PREFIX) + url = f'{ADMIN_PREFIX}/auth/' response = app.get(url) assert response.status_code == 200 diff --git a/rhodecode/authentication/views.py b/rhodecode/authentication/views.py --- a/rhodecode/authentication/views.py +++ b/rhodecode/authentication/views.py @@ -1,5 +1,3 @@ - - # Copyright (C) 2012-2023 RhodeCode GmbH # # This program is free software: you can redistribute it and/or modify