# HG changeset patch # User RhodeCode Admin # Date 2023-07-18 09:45:54 # Node ID 62af941ab9856ade732f2afb1638b7c7d3498b7f # Parent 4eab4aa8132134f08933d4d4f23b334c14df483c apps: base app fixes for python3 diff --git a/rhodecode/__init__.py b/rhodecode/__init__.py --- a/rhodecode/__init__.py +++ b/rhodecode/__init__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright (C) 2010-2020 RhodeCode GmbH # @@ -22,7 +21,8 @@ import os import datetime import collections -now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") +now = datetime.datetime.now() +now = now.strftime("%Y-%m-%d %H:%M:%S") + '.' + "{:03d}".format(int(now.microsecond/1000)) print(f'{now} Starting RhodeCode imports...') @@ -42,6 +42,35 @@ CELERY_EAGER = False # link to config for pyramid CONFIG = {} + +class ConfigGet: + NotGiven = object() + + def _get_val_or_missing(self, key, missing): + if key not in CONFIG: + if missing == self.NotGiven: + return missing + # we don't get key, we don't get missing value, return nothing similar as config.get(key) + return None + else: + val = CONFIG[key] + return val + + def get_str(self, key, missing=NotGiven): + from rhodecode.lib.str_utils import safe_str + val = self._get_val_or_missing(key, missing) + return safe_str(val) + + def get_int(self, key, missing=NotGiven): + from rhodecode.lib.str_utils import safe_int + val = self._get_val_or_missing(key, missing) + return safe_int(val) + + def get_bool(self, key, missing=NotGiven): + from rhodecode.lib.type_utils import str2bool + val = self._get_val_or_missing(key, missing) + return str2bool(val) + # Populated with the settings dictionary from application init in # rhodecode.conf.environment.load_pyramid_environment PYRAMID_SETTINGS = {} diff --git a/rhodecode/apps/_base/__init__.py b/rhodecode/apps/_base/__init__.py --- a/rhodecode/apps/_base/__init__.py +++ b/rhodecode/apps/_base/__init__.py @@ -25,9 +25,10 @@ import operator from pyramid.httpexceptions import HTTPFound, HTTPForbidden, HTTPBadRequest from rhodecode.lib import helpers as h, diffs, rc_cache +from rhodecode.lib.str_utils import safe_str from rhodecode.lib.utils import repo_name_slug from rhodecode.lib.utils2 import ( - StrictAttributeDict, str2bool, safe_int, datetime_to_time, safe_unicode) + StrictAttributeDict, str2bool, safe_int, datetime_to_time) from rhodecode.lib.markup_renderer import MarkupRenderer, relative_links from rhodecode.lib.vcs.backends.base import EmptyCommit from rhodecode.lib.vcs.exceptions import RepositoryRequirementError @@ -73,7 +74,7 @@ def add_route_requirements(route_path, r add_route_requirements('{repo_name}/settings') """ requirements = requirements or URL_NAME_REQUIREMENTS - for key, regex in requirements.items(): + for key, regex in list(requirements.items()): route_path = route_path.replace('{%s}' % key, '{%s:%s}' % (key, regex)) return route_path @@ -240,7 +241,7 @@ class RepoAppView(BaseAppView): def _handle_missing_requirements(self, error): log.error( 'Requirements are missing for repository %s: %s', - self.db_repo_name, safe_unicode(error)) + self.db_repo_name, safe_str(error)) def _prepare_and_set_clone_url(self, c): username = '' @@ -352,8 +353,8 @@ class RepoAppView(BaseAppView): return None, None landing_commit_id = landing_commit.raw_id - cache_namespace_uid = 'cache_repo.{}'.format(db_repo.repo_id) - region = rc_cache.get_or_create_region('cache_repo', cache_namespace_uid) + cache_namespace_uid = 'repo.{}'.format(db_repo.repo_id) + region = rc_cache.get_or_create_region('cache_repo', cache_namespace_uid, use_async_runner=True) start = time.time() @region.conditional_cache_on_arguments(namespace=cache_namespace_uid) @@ -377,7 +378,7 @@ class RepoAppView(BaseAppView): } readme_data = self._render_readme_or_none(commit, readme_node, relative_urls) - readme_filename = readme_node.unicode_path + readme_filename = readme_node.str_path return readme_data, readme_filename @@ -394,13 +395,12 @@ class RepoAppView(BaseAppView): renderer = MarkupRenderer() try: html_source = renderer.render( - readme_node.content, filename=readme_node.path) + readme_node.str_content, filename=readme_node.path) if relative_urls: return relative_links(html_source, relative_urls) return html_source except Exception: - log.exception( - "Exception while trying to render the README") + log.exception("Exception while trying to render the README") def get_recache_flag(self): for flag_name in ['force_recache', 'force-recache', 'no-cache']: @@ -455,6 +455,7 @@ class PathFilter(object): return patchset, False def render_patchset_filtered(self, diffset, patchset, source_ref=None, target_ref=None): + filtered_patchset, has_hidden_changes = self.filter_patchset(patchset) result = diffset.render_patchset( filtered_patchset, source_ref=source_ref, target_ref=target_ref) @@ -488,12 +489,9 @@ class RepoGroupAppView(BaseAppView): return c def _revoke_perms_on_yourself(self, form_result): - _updates = filter(lambda u: self._rhodecode_user.user_id == int(u[0]), - form_result['perm_updates']) - _additions = filter(lambda u: self._rhodecode_user.user_id == int(u[0]), - form_result['perm_additions']) - _deletions = filter(lambda u: self._rhodecode_user.user_id == int(u[0]), - form_result['perm_deletions']) + _updates = [u for u in form_result['perm_updates'] if self._rhodecode_user.user_id == int(u[0])] + _additions = [u for u in form_result['perm_additions'] if self._rhodecode_user.user_id == int(u[0])] + _deletions = [u for u in form_result['perm_deletions'] if self._rhodecode_user.user_id == int(u[0])] admin_perm = 'group.admin' if _updates and _updates[0][1] != admin_perm or \ _additions and _additions[0][1] != admin_perm or \ @@ -627,7 +625,7 @@ class RepoRoutePredicate(object): self.val = val def text(self): - return 'repo_route = %s' % self.val + return f'repo_route = {self.val}' phash = text @@ -639,7 +637,7 @@ class RepoRoutePredicate(object): repo_name = info['match']['repo_name'] repo_name_parts = repo_name.split('/') - repo_slugs = [x for x in map(lambda x: repo_name_slug(x), repo_name_parts)] + repo_slugs = [x for x in (repo_name_slug(x) for x in repo_name_parts)] if repo_name_parts != repo_slugs: # short-skip if the repo-name doesn't follow slug rule @@ -666,12 +664,15 @@ class RepoRoutePredicate(object): if by_name_match: # register this as request object we can re-use later request.db_repo = by_name_match + request.db_repo_name = request.db_repo.repo_name + redirect_if_creating(info, by_name_match) return True by_id_match = repo_model.get_repo_by_id(repo_name) if by_id_match: request.db_repo = by_id_match + request.db_repo_name = request.db_repo.repo_name redirect_if_creating(info, by_id_match) return True @@ -683,7 +684,7 @@ class RepoForbidArchivedRoutePredicate(o self.val = val def text(self): - return 'repo_forbid_archived = %s' % self.val + return f'repo_forbid_archived = {self.val}' phash = text @@ -713,7 +714,7 @@ class RepoTypeRoutePredicate(object): self.val = val or ['hg', 'git', 'svn'] def text(self): - return 'repo_accepted_type = %s' % self.val + return f'repo_accepted_type = {self.val}' phash = text @@ -741,7 +742,7 @@ class RepoGroupRoutePredicate(object): self.val = val def text(self): - return 'repo_group_route = %s' % self.val + return f'repo_group_route = {self.val}' phash = text @@ -753,7 +754,7 @@ class RepoGroupRoutePredicate(object): repo_group_name = info['match']['repo_group_name'] repo_group_name_parts = repo_group_name.split('/') - repo_group_slugs = [x for x in map(lambda x: repo_name_slug(x), repo_group_name_parts)] + repo_group_slugs = [x for x in [repo_name_slug(x) for x in repo_group_name_parts]] if repo_group_name_parts != repo_group_slugs: # short-skip if the repo-name doesn't follow slug rule log.warning('repo_group_name: %s is different than slug %s', repo_group_name_parts, repo_group_slugs) @@ -765,6 +766,7 @@ class RepoGroupRoutePredicate(object): if by_name_match: # register this as request object we can re-use later request.db_repo_group = by_name_match + request.db_repo_group_name = request.db_repo_group.group_name return True return False @@ -775,7 +777,7 @@ class UserGroupRoutePredicate(object): self.val = val def text(self): - return 'user_group_route = %s' % self.val + return f'user_group_route = {self.val}' phash = text @@ -827,7 +829,7 @@ class UserRoutePredicate(UserRoutePredic supports_default = False def text(self): - return 'user_route = %s' % self.val + return f'user_route = {self.val}' phash = text @@ -836,7 +838,7 @@ class UserRouteWithDefaultPredicate(User supports_default = True def text(self): - return 'user_with_default_route = %s' % self.val + return f'user_with_default_route = {self.val}' phash = text