modernize: updates for python3

This commit is contained in:
RhodeCode Admin 2023-07-18 14:45:20 +02:00
parent a6e9ccf9e0
commit 4bbb30fa06
155 changed files with 232 additions and 455 deletions

View file

@ -1,4 +1,3 @@
# Copyright (C) 2010-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
@ -50,8 +49,8 @@ SOCIAL_PLUGINS_LIST = ['github', 'bitbucket', 'twitter', 'google']
class SettingNotFound(Exception):
def __init__(self, setting_id):
msg = 'Setting `{}` is not found'.format(setting_id)
super(SettingNotFound, self).__init__(msg)
msg = f'Setting `{setting_id}` is not found'
super().__init__(msg)
class SettingsModel(BaseModel):
@ -67,7 +66,7 @@ class SettingsModel(BaseModel):
self.UiDbModel = RepoRhodeCodeUi if repo else RhodeCodeUi
self.SettingsDbModel = (
RepoRhodeCodeSetting if repo else RhodeCodeSetting)
super(SettingsModel, self).__init__(sa)
super().__init__(sa)
def get_ui_by_key(self, key):
q = self.UiDbModel.query()
@ -362,7 +361,7 @@ class IssueTrackerSettingsModel(object):
Session().add(settings)
def _get_keyname(self, key, uid, prefix=''):
return '{0}{1}{2}_{3}'.format(
return '{}{}{}_{}'.format(
prefix, self.SETTINGS_PREFIX, key, uid)
def _make_dict_for_settings(self, qs):
@ -585,7 +584,7 @@ class VcsSettingsModel(object):
data_key = self._get_form_ui_key(section, key)
if data_key not in data:
raise ValueError(
'The given data does not contain {} key'.format(data_key))
f'The given data does not contain {data_key} key')
active = data.get(data_key)
repo_setting = self.repo_settings.get_ui_by_section_and_key(
@ -604,7 +603,7 @@ class VcsSettingsModel(object):
data_key = self._get_form_ui_key(section, key)
if data_key not in data:
raise ValueError(
'The given data does not contain {} key'.format(data_key))
f'The given data does not contain {data_key} key')
active = data.get(data_key)
repo_setting = self.global_settings.get_ui_by_section_and_key(
section, key)
@ -825,7 +824,7 @@ class VcsSettingsModel(object):
return keep
def _filter_general_settings(self, settings):
keys = ['rhodecode_{}'.format(key) for key in self.GENERAL_SETTINGS]
keys = [f'rhodecode_{key}' for key in self.GENERAL_SETTINGS]
return {
k: settings[k]
for k in settings if k in keys}
@ -849,7 +848,7 @@ class VcsSettingsModel(object):
for name in self.GENERAL_SETTINGS:
setting = settings.get_setting_by_name(name)
if setting:
result_key = 'rhodecode_{}'.format(name)
result_key = f'rhodecode_{name}'
result[result_key] = setting.app_settings_value
return result
@ -883,10 +882,10 @@ class VcsSettingsModel(object):
def _create_or_update_general_settings(self, settings, data):
for name in self.GENERAL_SETTINGS:
data_key = 'rhodecode_{}'.format(name)
data_key = f'rhodecode_{name}'
if data_key not in data:
raise ValueError(
'The given data does not contain {} key'.format(data_key))
f'The given data does not contain {data_key} key')
setting = settings.create_or_update_setting(
name, data[data_key], 'bool')
Session().add(setting)
@ -896,7 +895,7 @@ class VcsSettingsModel(object):
for data_key in data_keys:
if data_key not in data:
raise ValueError(
'The given data does not contain {} key'.format(data_key))
f'The given data does not contain {data_key} key')
return data_keys
def create_largeobjects_dirs_if_needed(self, repo_store_path):

View file

@ -1,5 +1,3 @@
# Copyright (C) 2013-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify

View file

@ -1,5 +1,3 @@
# Copyright (C) 2013-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify

View file

@ -1,4 +1,3 @@
# Copyright (C) 2010-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
@ -577,8 +576,8 @@ class UserModel(BaseModel):
try:
if user.username == User.DEFAULT_USER:
raise DefaultUserException(
u"You can't remove this user since it's"
u" crucial for entire application")
"You can't remove this user since it's"
" crucial for entire application")
handle_user = handle_new_owner or self.cls.get_first_super_admin()
log.debug('New detached objects owner %s', handle_user)
@ -587,8 +586,8 @@ class UserModel(BaseModel):
if left_overs and user.repositories:
repos = [x.repo_name for x in user.repositories]
raise UserOwnsReposException(
u'user "%(username)s" still owns %(len_repos)s repositories and cannot be '
u'removed. Switch owners or remove those repositories:%(list_repos)s'
'user "%(username)s" still owns %(len_repos)s repositories and cannot be '
'removed. Switch owners or remove those repositories:%(list_repos)s'
% {'username': user.username, 'len_repos': len(repos),
'list_repos': ', '.join(repos)})
@ -597,8 +596,8 @@ class UserModel(BaseModel):
if left_overs and user.repository_groups:
repo_groups = [x.group_name for x in user.repository_groups]
raise UserOwnsRepoGroupsException(
u'user "%(username)s" still owns %(len_repo_groups)s repository groups and cannot be '
u'removed. Switch owners or remove those repository groups:%(list_repo_groups)s'
'user "%(username)s" still owns %(len_repo_groups)s repository groups and cannot be '
'removed. Switch owners or remove those repository groups:%(list_repo_groups)s'
% {'username': user.username, 'len_repo_groups': len(repo_groups),
'list_repo_groups': ', '.join(repo_groups)})
@ -607,17 +606,17 @@ class UserModel(BaseModel):
if left_overs and user.user_groups:
user_groups = [x.users_group_name for x in user.user_groups]
raise UserOwnsUserGroupsException(
u'user "%s" still owns %s user groups and cannot be '
u'removed. Switch owners or remove those user groups:%s'
'user "%s" still owns %s user groups and cannot be '
'removed. Switch owners or remove those user groups:%s'
% (user.username, len(user_groups), ', '.join(user_groups)))
left_overs = self._handle_user_pull_requests(
user.username, user.user_pull_requests, handle_user, handle_pull_requests)
if left_overs and user.user_pull_requests:
pull_requests = ['!{}'.format(x.pull_request_id) for x in user.user_pull_requests]
pull_requests = [f'!{x.pull_request_id}' for x in user.user_pull_requests]
raise UserOwnsPullRequestsException(
u'user "%s" still owns %s pull requests and cannot be '
u'removed. Switch owners or remove those pull requests:%s'
'user "%s" still owns %s pull requests and cannot be '
'removed. Switch owners or remove those pull requests:%s'
% (user.username, len(pull_requests), ', '.join(pull_requests)))
left_overs = self._handle_user_artifacts(
@ -625,8 +624,8 @@ class UserModel(BaseModel):
if left_overs and user.artifacts:
artifacts = [x.file_uid for x in user.artifacts]
raise UserOwnsArtifactsException(
u'user "%s" still owns %s artifacts and cannot be '
u'removed. Switch owners or remove those artifacts:%s'
'user "%s" still owns %s artifacts and cannot be '
'removed. Switch owners or remove those artifacts:%s'
% (user.username, len(artifacts), ', '.join(artifacts)))
user_data = user.get_dict() # fetch user data before expire
@ -941,7 +940,7 @@ class UserModel(BaseModel):
auth_token_role = AuthTokenModel.cls
def add_auth_token(self, user, lifetime_minutes, role, description=u'',
def add_auth_token(self, user, lifetime_minutes, role, description='',
scope_callback=None):
"""
Add AuthToken for user.

View file

@ -1,5 +1,3 @@
# Copyright (C) 2011-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
@ -298,7 +296,7 @@ class UserGroupModel(BaseModel):
assigned_to_repo+assigned_to_repo_group))
raise UserGroupAssignedException(
'UserGroup assigned to %s' % (assigned,))
'UserGroup assigned to {}'.format(assigned))
self.sa.delete(user_group)
except Exception:
log.error(traceback.format_exc())
@ -710,7 +708,7 @@ class UserGroupModel(BaseModel):
query = query.filter(UserGroup.users_group_active == true())
if name_contains:
ilike_expression = u'%{}%'.format(safe_str(name_contains))
ilike_expression = f'%{safe_str(name_contains)}%'
query = query.filter(
UserGroup.users_group_name.ilike(ilike_expression))\
.order_by(func.length(UserGroup.users_group_name))\

View file

@ -1,5 +1,3 @@
# Copyright (C) 2016-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify

View file

@ -1,5 +1,3 @@
# Copyright (C) 2016-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify

View file

@ -1,5 +1,3 @@
# Copyright (C) 2016-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify

View file

@ -1,5 +1,3 @@
# Copyright (C) 2017-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
@ -39,13 +37,13 @@ def unique_gist_validator(node, value):
from rhodecode.model.db import Gist
existing = Gist.get_by_access_id(value)
if existing:
msg = _(u'Gist with name {} already exists').format(value)
msg = _('Gist with name {} already exists').format(value)
raise colander.Invalid(node, msg)
def filename_validator(node, value):
if value != os.path.basename(value):
msg = _(u'Filename {} cannot be inside a directory').format(value)
msg = _('Filename {} cannot be inside a directory').format(value)
raise colander.Invalid(node, msg)

View file

@ -1,5 +1,3 @@
# Copyright (C) 2016-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
@ -54,7 +52,7 @@ def nodes_to_sequence(nodes, colander_node=None):
def sequence_to_nodes(nodes, colander_node=None):
if not isinstance(nodes, list):
msg = 'Nodes needs to be a list, got {}'.format(type(nodes))
msg = f'Nodes needs to be a list, got {type(nodes)}'
raise colander.Invalid(colander_node, msg)
nodes = Nodes().deserialize(nodes)
@ -71,7 +69,7 @@ def sequence_to_nodes(nodes, colander_node=None):
out[filename].update(file_data_skip)
except Exception as e:
msg = 'Invalid data format org_exc:`{}`'.format(repr(e))
msg = f'Invalid data format org_exc:`{repr(e)}`'
raise colander.Invalid(colander_node, msg)
return out

View file

@ -1,5 +1,3 @@
# Copyright (C) 2016-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify

View file

@ -1,5 +1,3 @@
# Copyright (C) 2016-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
@ -50,15 +48,15 @@ def deferred_can_write_to_group_validator(node, kw):
messages = {
'invalid_parent_repo_group':
_(u"Parent repository group `{}` does not exist"),
_("Parent repository group `{}` does not exist"),
# permissions denied we expose as not existing, to prevent
# resource discovery
'permission_denied_parent_group':
_(u"You do not have the permissions to store "
u"repository groups inside repository group `{}`"),
_("You do not have the permissions to store "
"repository groups inside repository group `{}`"),
'permission_denied_root':
_(u"You do not have the permission to store "
u"repository groups in the root location.")
_("You do not have the permission to store "
"repository groups in the root location.")
}
value = value['repo_group_name']
@ -124,7 +122,7 @@ def deferred_repo_group_owner_validator(node, kw):
value = username_converter(value)
existing = User.get_by_username(value)
if not existing:
msg = _(u'Repo group owner with id `{}` does not exists').format(
msg = _('Repo group owner with id `{}` does not exists').format(
value)
raise colander.Invalid(node, msg)
@ -142,12 +140,12 @@ def deferred_unique_name_validator(node, kw):
existing = Repository.get_by_repo_name(value)
if name_changed and existing:
msg = _(u'Repository with name `{}` already exists').format(value)
msg = _('Repository with name `{}` already exists').format(value)
raise colander.Invalid(node, msg)
existing_group = RepoGroup.get_by_group_name(value)
if name_changed and existing_group:
msg = _(u'Repository group with name `{}` already exists').format(
msg = _('Repository group with name `{}` already exists').format(
value)
raise colander.Invalid(node, msg)
return unique_name_validator
@ -184,7 +182,7 @@ class GroupType(colander.Mapping):
if cstruct is colander.null:
return cstruct
appstruct = super(GroupType, self).deserialize(node, cstruct)
appstruct = super().deserialize(node, cstruct)
validated_name = appstruct['repo_group_name']
# inject group based on once deserialized data
@ -253,7 +251,7 @@ class RepoGroupSchema(colander.Schema):
permissions, and as last step uniqueness
"""
appstruct = super(RepoGroupSchema, self).deserialize(cstruct)
appstruct = super().deserialize(cstruct)
validated_name = appstruct['repo_group_name']
# second pass to validate permissions to repo_group

View file

@ -1,5 +1,3 @@
# Copyright (C) 2016-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
@ -53,7 +51,7 @@ def deferred_repo_owner_validator(node, kw):
value = username_converter(value)
existing = User.get_by_username(value)
if not existing:
msg = _(u'Repo owner with id `{}` does not exists').format(value)
msg = _('Repo owner with id `{}` does not exists').format(value)
raise colander.Invalid(node, msg)
return repo_owner_validator
@ -96,11 +94,11 @@ def deferred_fork_of_validator(node, kw):
from rhodecode.model.db import Repository, RepoGroup
existing = Repository.get_by_repo_name(value)
if not existing:
msg = _(u'Fork with id `{}` does not exists').format(value)
msg = _('Fork with id `{}` does not exists').format(value)
raise colander.Invalid(node, msg)
elif old_values['repo_name'] == existing.repo_name:
msg = _(u'Cannot set fork of '
u'parameter of this repository to itself').format(value)
msg = _('Cannot set fork of '
'parameter of this repository to itself').format(value)
raise colander.Invalid(node, msg)
return fork_of_validator
@ -124,14 +122,14 @@ def deferred_can_write_to_group_validator(node, kw):
messages = {
'invalid_repo_group':
_(u"Repository group `{}` does not exist"),
_("Repository group `{}` does not exist"),
# permissions denied we expose as not existing, to prevent
# resource discovery
'permission_denied':
_(u"Repository group `{}` does not exist"),
_("Repository group `{}` does not exist"),
'permission_denied_root':
_(u"You do not have the permission to store "
u"repositories in the root location.")
_("You do not have the permission to store "
"repositories in the root location.")
}
value = value['repo_group_name']
@ -215,12 +213,12 @@ def deferred_unique_name_validator(node, kw):
existing = Repository.get_by_repo_name(value)
if name_changed and existing:
msg = _(u'Repository with name `{}` already exists').format(value)
msg = _('Repository with name `{}` already exists').format(value)
raise colander.Invalid(node, msg)
existing_group = RepoGroup.get_by_group_name(value)
if name_changed and existing_group:
msg = _(u'Repository group with name `{}` already exists').format(
msg = _('Repository group with name `{}` already exists').format(
value)
raise colander.Invalid(node, msg)
return unique_name_validator
@ -271,7 +269,7 @@ class GroupType(colander.Mapping):
if cstruct is colander.null:
return cstruct
appstruct = super(GroupType, self).deserialize(node, cstruct)
appstruct = super().deserialize(node, cstruct)
validated_name = appstruct['repo_group_name']
# inject group based on once deserialized data
@ -380,7 +378,7 @@ class RepoSchema(colander.MappingSchema):
"""
# first pass, to validate given data
appstruct = super(RepoSchema, self).deserialize(cstruct)
appstruct = super().deserialize(cstruct)
validated_name = appstruct['repo_name']
# second pass to validate permissions to repo_group

View file

@ -1,5 +1,3 @@
# Copyright (C) 2016-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify

View file

@ -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 @@ def sort_validator(node, value):
if value.startswith('desc:'):
return value
msg = u'Invalid search sort, must be `oldfirst`, `newfirst`, or start with asc: or desc:'
msg = 'Invalid search sort, must be `oldfirst`, `newfirst`, or start with asc: or desc:'
raise colander.Invalid(node, msg)

View file

@ -1,5 +1,3 @@
# Copyright (C) 2016-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
@ -47,7 +45,7 @@ def deferred_user_group_owner_validator(node, kw):
value = username_converter(value)
existing = User.get_by_username(value)
if not existing:
msg = _(u'User group owner with id `{}` does not exists').format(value)
msg = _('User group owner with id `{}` does not exists').format(value)
raise colander.Invalid(node, msg)
return owner_validator
@ -76,5 +74,5 @@ class UserGroupSchema(colander.Schema):
permissions, and as last step uniqueness
"""
appstruct = super(UserGroupSchema, self).deserialize(cstruct)
appstruct = super().deserialize(cstruct)
return appstruct

View file

@ -1,5 +1,3 @@
# Copyright (C) 2016-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
@ -69,9 +67,9 @@ def deferred_username_validator(node, kw):
def name_validator(node, value):
msg = _(
u'Username may only contain alphanumeric characters '
u'underscores, periods or dashes and must begin with '
u'alphanumeric character or underscore')
'Username may only contain alphanumeric characters '
'underscores, periods or dashes and must begin with '
'alphanumeric character or underscore')
if not re.match(r'^[\w]{1}[\w\-\.]{0,254}$', value):
raise colander.Invalid(node, msg)
@ -126,7 +124,7 @@ class UserSchema(colander.Schema):
permissions, and as last step uniqueness
"""
appstruct = super(UserSchema, self).deserialize(cstruct)
appstruct = super().deserialize(cstruct)
return appstruct
@ -145,7 +143,7 @@ def deferred_additional_email_validator(node, kw):
raise colander.Invalid(node, msg)
user = User.get_by_email(value, case_insensitive=True)
if user:
msg = _(u'This e-mail address is already taken')
msg = _('This e-mail address is already taken')
raise colander.Invalid(node, msg)
c = colander.Email()
return c(node, value)

View file

@ -1,5 +1,3 @@
# Copyright (C) 2016-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
@ -62,7 +60,7 @@ class RepoNameType(colander.String):
SEPARATOR = '/'
def deserialize(self, node, cstruct):
result = super(RepoNameType, self).deserialize(node, cstruct)
result = super().deserialize(node, cstruct)
if cstruct is colander.null:
return colander.null
return self._normalize(result)
@ -78,7 +76,7 @@ class GroupNameType(colander.String):
if cstruct is RootLocation:
return cstruct
result = super(GroupNameType, self).deserialize(node, cstruct)
result = super().deserialize(node, cstruct)
if cstruct is colander.null:
return colander.null
return self._normalize(result)
@ -116,7 +114,7 @@ class StringBooleanType(colander.String):
return False
else:
raise colander.Invalid(
node, '{} value cannot be translated to bool'.format(value))
node, f'{value} value cannot be translated to bool')
class UserOrUserGroupType(colander.SchemaType):
@ -144,7 +142,7 @@ class UserOrUserGroupType(colander.SchemaType):
return appstruct.users_group_name
raise colander.Invalid(
node, '%s is not a valid %s' % (appstruct, ' or '.join(self.scopes)))
node, '{} is not a valid {}'.format(appstruct, ' or '.join(self.scopes)))
def deserialize(self, node, cstruct):
if cstruct is colander.null:
@ -177,7 +175,7 @@ class UserOrUserGroupType(colander.SchemaType):
return usergroup
raise colander.Invalid(
node, '%s is not a valid %s' % (cstruct, ' or '.join(self.scopes)))
node, '{} is not a valid {}'.format(cstruct, ' or '.join(self.scopes)))
class UserType(UserOrUserGroupType):
@ -191,6 +189,6 @@ class UserGroupType(UserOrUserGroupType):
class StrOrIntType(colander.String):
def deserialize(self, node, cstruct):
if isinstance(cstruct, str):
return super(StrOrIntType, self).deserialize(node, cstruct)
return super().deserialize(node, cstruct)
else:
return colander.Integer().deserialize(node, cstruct)

View file

@ -1,5 +1,3 @@
# Copyright (C) 2016-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify

View file

@ -1,5 +1,3 @@
# Copyright (C) 2011-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
@ -120,7 +118,7 @@ def url_validator(url, repo_type, config):
# no validation for SVN yet
return
raise InvalidCloneUrl('Invalid repo type specified: `{}`'.format(repo_type))
raise InvalidCloneUrl(f'Invalid repo type specified: `{repo_type}`')
class CloneUriValidator(object):
@ -157,5 +155,5 @@ def json_validator_with_exc(node, value):
try:
json.loads(value)
except (Exception,) as e:
msg = _('Please enter a valid json object: `{}`'.format(e))
msg = _(f'Please enter a valid json object: `{e}`')
raise colander.Invalid(node, msg)

View file

@ -1,5 +1,3 @@
# Copyright (C) 2011-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify

View file

@ -1,4 +1,3 @@
# Copyright (C) 2010-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify