changeset 8026:d4ea298c3ec4

cleanup: minor refactorings and simplification of dict usage Makes it more py3 compatible.
author Mads Kiilerich <mads@kiilerich.com>
date Mon, 16 Dec 2019 01:35:41 +0100
parents 82b1eaec25f5
children 38749d420fbd
files kallithea/config/conf.py kallithea/controllers/files.py kallithea/controllers/pullrequests.py kallithea/lib/base.py kallithea/lib/celerylib/tasks.py kallithea/lib/helpers.py kallithea/lib/vcs/backends/git/changeset.py kallithea/lib/vcs/backends/hg/changeset.py kallithea/lib/vcs/backends/hg/repository.py kallithea/model/db.py kallithea/model/forms.py kallithea/model/scm.py kallithea/model/validators.py kallithea/tests/vcs/test_changesets.py
diffstat 14 files changed, 32 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/config/conf.py	Mon Dec 16 00:53:11 2019 +0100
+++ b/kallithea/config/conf.py	Mon Dec 16 01:35:41 2019 +0100
@@ -35,7 +35,7 @@
 # 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()
--- a/kallithea/controllers/files.py	Mon Dec 16 00:53:11 2019 +0100
+++ b/kallithea/controllers/files.py	Mon Dec 16 01:35:41 2019 +0100
@@ -163,7 +163,7 @@
                 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 @@
         # 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 @@
         # 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',
--- a/kallithea/controllers/pullrequests.py	Mon Dec 16 00:53:11 2019 +0100
+++ b/kallithea/controllers/pullrequests.py	Mon Dec 16 01:35:41 2019 +0100
@@ -173,7 +173,7 @@
                 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")),
--- a/kallithea/lib/base.py	Mon Dec 16 00:53:11 2019 +0100
+++ b/kallithea/lib/base.py	Mon Dec 16 01:35:41 2019 +0100
@@ -408,7 +408,7 @@
         # 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'))
 
--- a/kallithea/lib/celerylib/tasks.py	Mon Dec 16 00:53:11 2019 +0100
+++ b/kallithea/lib/celerylib/tasks.py	Mon Dec 16 01:35:41 2019 +0100
@@ -489,7 +489,7 @@
     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:
--- a/kallithea/lib/helpers.py	Mon Dec 16 00:53:11 2019 +0100
+++ b/kallithea/lib/helpers.py	Mon Dec 16 01:35:41 2019 +0100
@@ -1164,7 +1164,7 @@
         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:
--- a/kallithea/lib/vcs/backends/git/changeset.py	Mon Dec 16 00:53:11 2019 +0100
+++ b/kallithea/lib/vcs/backends/git/changeset.py	Mon Dec 16 01:35:41 2019 +0100
@@ -355,10 +355,10 @@
         :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'
--- a/kallithea/lib/vcs/backends/hg/changeset.py	Mon Dec 16 00:53:11 2019 +0100
+++ b/kallithea/lib/vcs/backends/hg/changeset.py	Mon Dec 16 01:35:41 2019 +0100
@@ -316,10 +316,10 @@
         :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'
--- a/kallithea/lib/vcs/backends/hg/repository.py	Mon Dec 16 00:53:11 2019 +0100
+++ b/kallithea/lib/vcs/backends/hg/repository.py	Mon Dec 16 01:35:41 2019 +0100
@@ -136,11 +136,11 @@
         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 @@
         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 @@
             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:
--- a/kallithea/model/db.py	Mon Dec 16 00:53:11 2019 +0100
+++ b/kallithea/model/db.py	Mon Dec 16 01:35:41 2019 +0100
@@ -233,7 +233,7 @@
     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):
--- a/kallithea/model/forms.py	Mon Dec 16 00:53:11 2019 +0100
+++ b/kallithea/model/forms.py	Mon Dec 16 01:35:41 2019 +0100
@@ -238,7 +238,7 @@
     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 @@
     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 @@
     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
--- a/kallithea/model/scm.py	Mon Dec 16 00:53:11 2019 +0100
+++ b/kallithea/model/scm.py	Mon Dec 16 01:35:41 2019 +0100
@@ -186,10 +186,10 @@
 
                     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
--- a/kallithea/model/validators.py	Mon Dec 16 00:53:11 2019 +0100
+++ b/kallithea/model/validators.py	Mon Dec 16 01:35:41 2019 +0100
@@ -556,8 +556,8 @@
                         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')
--- a/kallithea/tests/vcs/test_changesets.py	Mon Dec 16 00:53:11 2019 +0100
+++ b/kallithea/tests/vcs/test_changesets.py	Mon Dec 16 01:35:41 2019 +0100
@@ -69,7 +69,7 @@
         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):