# HG changeset patch # User Mads Kiilerich # Date 2019-12-16 01:35:41 # Node ID d4ea298c3ec437a17e4bbdc561f913ad058a2422 # Parent 82b1eaec25f552d33b02ebf2b31a7d2c3b7b094b cleanup: minor refactorings and simplification of dict usage Makes it more py3 compatible. diff --git a/kallithea/config/conf.py b/kallithea/config/conf.py --- a/kallithea/config/conf.py +++ b/kallithea/config/conf.py @@ -35,7 +35,7 @@ LANGUAGES_EXTENSIONS_MAP = pygmentsutils # Whoosh index targets # Extensions we want to index content of using whoosh -INDEX_EXTENSIONS = LANGUAGES_EXTENSIONS_MAP.keys() +INDEX_EXTENSIONS = list(LANGUAGES_EXTENSIONS_MAP) # Filenames we want to index content of using whoosh INDEX_FILENAMES = pygmentsutils.get_index_filenames() diff --git a/kallithea/controllers/files.py b/kallithea/controllers/files.py --- a/kallithea/controllers/files.py +++ b/kallithea/controllers/files.py @@ -163,7 +163,7 @@ class FilesController(BaseRepoController c.load_full_history = False # determine if we're on branch head _branches = c.db_repo_scm_instance.branches - c.on_branch_head = revision in _branches.keys() + _branches.values() + c.on_branch_head = revision in _branches or revision in _branches.values() _hist = [] c.file_history = [] if c.load_full_history: @@ -292,7 +292,7 @@ class FilesController(BaseRepoController # create multiple heads via file editing _branches = repo.scm_instance.branches # check if revision is a branch name or branch hash - if revision not in _branches.keys() + _branches.values(): + if revision not in _branches and revision not in _branches.values(): h.flash(_('You can only delete files with revision ' 'being a valid branch'), category='warning') raise HTTPFound(location=h.url('files_home', @@ -346,7 +346,7 @@ class FilesController(BaseRepoController # create multiple heads via file editing _branches = repo.scm_instance.branches # check if revision is a branch name or branch hash - if revision not in _branches.keys() + _branches.values(): + if revision not in _branches and revision not in _branches.values(): h.flash(_('You can only edit files with revision ' 'being a valid branch'), category='warning') raise HTTPFound(location=h.url('files_home', diff --git a/kallithea/controllers/pullrequests.py b/kallithea/controllers/pullrequests.py --- a/kallithea/controllers/pullrequests.py +++ b/kallithea/controllers/pullrequests.py @@ -173,7 +173,7 @@ class PullrequestsController(BaseRepoCon if 'master' in repo.branches: selected = 'branch:master:%s' % repo.branches['master'] else: - k, v = repo.branches.items()[0] + k, v = list(repo.branches.items())[0] selected = 'branch:%s:%s' % (k, v) groups = [(specials, _("Special")), diff --git a/kallithea/lib/base.py b/kallithea/lib/base.py --- a/kallithea/lib/base.py +++ b/kallithea/lib/base.py @@ -408,7 +408,7 @@ class BaseController(TGController): # END CONFIG VARS c.repo_name = get_repo_slug(request) # can be empty - c.backends = BACKENDS.keys() + c.backends = list(BACKENDS) self.cut_off_limit = safe_int(config.get('cut_off_limit')) diff --git a/kallithea/lib/celerylib/tasks.py b/kallithea/lib/celerylib/tasks.py --- a/kallithea/lib/celerylib/tasks.py +++ b/kallithea/lib/celerylib/tasks.py @@ -489,7 +489,7 @@ def __get_codes_stats(repo_name): for _topnode, _dirnodes, filenodes in tip.walk('/'): for filenode in filenodes: ext = filenode.extension.lower() - if ext in LANGUAGES_EXTENSIONS_MAP.keys() and not filenode.is_binary: + if ext in LANGUAGES_EXTENSIONS_MAP and not filenode.is_binary: if ext in code_stats: code_stats[ext] += 1 else: diff --git a/kallithea/lib/helpers.py b/kallithea/lib/helpers.py --- a/kallithea/lib/helpers.py +++ b/kallithea/lib/helpers.py @@ -1164,7 +1164,7 @@ def urlify_issues(newtext, repo_name): tmp_urlify_issues_f = lambda s: s issue_pat_re = re.compile(r'issue_pat(.*)') - for k in CONFIG.keys(): + for k in CONFIG: # Find all issue_pat* settings that also have corresponding server_link and prefix configuration m = issue_pat_re.match(k) if m is None: diff --git a/kallithea/lib/vcs/backends/git/changeset.py b/kallithea/lib/vcs/backends/git/changeset.py --- a/kallithea/lib/vcs/backends/git/changeset.py +++ b/kallithea/lib/vcs/backends/git/changeset.py @@ -355,10 +355,10 @@ class GitChangeset(BaseChangeset): :raise ImproperArchiveTypeError: If given kind is wrong. :raise VcsError: If given stream is None """ - allowed_kinds = settings.ARCHIVE_SPECS.keys() + allowed_kinds = settings.ARCHIVE_SPECS if kind not in allowed_kinds: raise ImproperArchiveTypeError('Archive kind not supported use one' - 'of %s' % allowed_kinds) + 'of %s' % ' '.join(allowed_kinds)) if stream is None: raise VCSError('You need to pass in a valid stream for filling' diff --git a/kallithea/lib/vcs/backends/hg/changeset.py b/kallithea/lib/vcs/backends/hg/changeset.py --- a/kallithea/lib/vcs/backends/hg/changeset.py +++ b/kallithea/lib/vcs/backends/hg/changeset.py @@ -316,10 +316,10 @@ class MercurialChangeset(BaseChangeset): :raise ImproperArchiveTypeError: If given kind is wrong. :raise VcsError: If given stream is None """ - allowed_kinds = settings.ARCHIVE_SPECS.keys() + allowed_kinds = settings.ARCHIVE_SPECS if kind not in allowed_kinds: raise ImproperArchiveTypeError('Archive kind not supported use one' - 'of %s' % allowed_kinds) + 'of %s' % ' '.join(allowed_kinds)) if stream is None: raise VCSError('You need to pass in a valid stream for filling' diff --git a/kallithea/lib/vcs/backends/hg/repository.py b/kallithea/lib/vcs/backends/hg/repository.py --- a/kallithea/lib/vcs/backends/hg/repository.py +++ b/kallithea/lib/vcs/backends/hg/repository.py @@ -136,11 +136,11 @@ class MercurialRepository(BaseRepository if self._empty: return {} - sortkey = lambda ctx: ctx[0] # sort by name - _tags = [(safe_unicode(n), hex(h),) for n, h in - self._repo.tags().items()] - - return OrderedDict(sorted(_tags, key=sortkey, reverse=True)) + return OrderedDict(sorted( + ((safe_unicode(n), hex(h)) for n, h in self._repo.tags().items()), + reverse=True, + key=lambda x: x[0], # sort by name + )) def tag(self, name, user, revision=None, message=None, date=None, **kwargs): @@ -214,10 +214,11 @@ class MercurialRepository(BaseRepository if self._empty: return {} - sortkey = lambda ctx: ctx[0] # sort by name - _bookmarks = [(safe_unicode(n), hex(h),) for n, h in - self._repo._bookmarks.items()] - return OrderedDict(sorted(_bookmarks, key=sortkey, reverse=True)) + return OrderedDict(sorted( + ((safe_unicode(n), hex(h)) for n, h in self._repo._bookmarks.items()), + reverse=True, + key=lambda x: x[0], # sort by name + )) def _get_all_revisions(self): @@ -525,7 +526,7 @@ class MercurialRepository(BaseRepository raise RepositoryError("Start revision '%s' cannot be " "after end revision '%s'" % (start, end)) - if branch_name and branch_name not in self.allbranches.keys(): + if branch_name and branch_name not in self.allbranches: msg = ("Branch %s not found in %s" % (branch_name, self)) raise BranchDoesNotExistError(msg) if end_pos is not None: diff --git a/kallithea/model/db.py b/kallithea/model/db.py --- a/kallithea/model/db.py +++ b/kallithea/model/db.py @@ -233,7 +233,7 @@ class Setting(Base, BaseDbModel): def app_settings_type(self, val): if val not in self.SETTINGS_TYPES: raise Exception('type must be one of %s got %s' - % (self.SETTINGS_TYPES.keys(), val)) + % (list(self.SETTINGS_TYPES), val)) self._app_settings_type = val def __unicode__(self): diff --git a/kallithea/model/forms.py b/kallithea/model/forms.py --- a/kallithea/model/forms.py +++ b/kallithea/model/forms.py @@ -238,7 +238,7 @@ def PasswordResetConfirmationForm(): return _PasswordResetConfirmationForm -def RepoForm(edit=False, old_data=None, supported_backends=BACKENDS.keys(), +def RepoForm(edit=False, old_data=None, supported_backends=BACKENDS, repo_groups=None, landing_revs=None): old_data = old_data or {} repo_groups = repo_groups or [] @@ -315,7 +315,7 @@ def RepoFieldForm(): return _RepoFieldForm -def RepoForkForm(edit=False, old_data=None, supported_backends=BACKENDS.keys(), +def RepoForkForm(edit=False, old_data=None, supported_backends=BACKENDS, repo_groups=None, landing_revs=None): old_data = old_data or {} repo_groups = repo_groups or [] @@ -435,7 +435,7 @@ def CustomDefaultPermissionsForm(): return _CustomDefaultPermissionsForm -def DefaultsForm(edit=False, old_data=None, supported_backends=BACKENDS.keys()): +def DefaultsForm(edit=False, old_data=None, supported_backends=BACKENDS): class _DefaultsForm(formencode.Schema): allow_extra_fields = True filter_extra_fields = True diff --git a/kallithea/model/scm.py b/kallithea/model/scm.py --- a/kallithea/model/scm.py +++ b/kallithea/model/scm.py @@ -186,10 +186,10 @@ class ScmModel(object): klass = get_backend(path[0]) - if path[0] == 'hg' and path[0] in BACKENDS.keys(): + if path[0] == 'hg' and path[0] in BACKENDS: repos[name] = klass(safe_str(path[1]), baseui=baseui) - if path[0] == 'git' and path[0] in BACKENDS.keys(): + if path[0] == 'git' and path[0] in BACKENDS: repos[name] = klass(path[1]) except OSError: continue diff --git a/kallithea/model/validators.py b/kallithea/model/validators.py --- a/kallithea/model/validators.py +++ b/kallithea/model/validators.py @@ -556,8 +556,8 @@ def ValidPerms(type_='repo'): new_perms_group[pos][_key] = v # fill new permissions in order of how they were added - for k in sorted(map(int, new_perms_group.keys())): - perm_dict = new_perms_group[str(k)] + for k in sorted(new_perms_group, key=lambda k: int(k)): + perm_dict = new_perms_group[k] new_member = perm_dict.get('name') new_perm = perm_dict.get('perm') new_type = perm_dict.get('type') diff --git a/kallithea/tests/vcs/test_changesets.py b/kallithea/tests/vcs/test_changesets.py --- a/kallithea/tests/vcs/test_changesets.py +++ b/kallithea/tests/vcs/test_changesets.py @@ -69,7 +69,7 @@ class _ChangesetsWithCommitsTestCaseixin assert foobar_tip.branch == 'foobar' assert foobar_tip.branches == ['foobar'] # 'foobar' should be the only branch that contains the new commit - branch_tips = self.repo.branches.values() + branch_tips = list(self.repo.branches.values()) assert branch_tips.count(str(foobar_tip.raw_id)) == 1 def test_new_head_in_default_branch(self):