From 7be9861d562abc762d65a4efd7cf49b493d70206 Mon Sep 17 00:00:00 2001 From: Pawel Wieczorek Date: Thu, 30 Apr 2020 17:04:51 +0200 Subject: Exclude code location from code base URL Python method str.format allows easy templating using substitutions identified by braces ('{' and '}'). Using two levels of nested substitutions can be achieved by doubling braces ("{{" and "}}"). Hound configuration creator script was supposed to use two levels of nested substitutions and still leave braced components within the templated string for further substitution by the Hound tool. This would require using multitude of braces which would decrease readability significantly. This is why code location template is appended to the code base URL at the latest. If there will be need for more levels of nested templates, this code shall be further refactored to use only named fields, str.format_map() (available in Python 3.2+) and SafeDict, e.g. >>> class SafeDict(dict): ... def __missing__(self, key): ... return '{' + key + '}' ... which would leave braced components within the processed template in case given key is missing. Issue-ID: ONAPARC-579 Change-Id: I420d076867aa891edb2a945a8cd58e168c892ac3 Signed-off-by: Pawel Wieczorek --- bootstrap/codesearch/create_config.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'bootstrap') diff --git a/bootstrap/codesearch/create_config.py b/bootstrap/codesearch/create_config.py index 115b4672b..6d72f1725 100755 --- a/bootstrap/codesearch/create_config.py +++ b/bootstrap/codesearch/create_config.py @@ -14,6 +14,7 @@ API_PROJECTS = "/projects/" MAGIC_PREFIX = ")]}'" +CODE_LOCATION = "{path}{anchor}" GITWEB_ANCHOR = "#l{line}" GIT_ANCHOR = "#n{line}" @@ -33,20 +34,20 @@ def get_projects_list(gerrit): def create_repos_list(projects, gerrit, ssh, git): """Create a map of all projects to their repositories' URLs.""" gerrit_url = "https://{}{}".format(gerrit, API_PREFIX) - gerrit_project_url = "{}/{{}}.git".format(gerrit_url) - gitweb_code_url = "{}/gitweb?p={{}}.git;hb=HEAD;a=blob;f={{path}}{{anchor}}".format(gerrit_url) + gerrit_project_url_base = "{}/{{}}.git".format(gerrit_url) + gitweb_code_url_base = "{}/gitweb?p={{}}.git;hb=HEAD;a=blob;f=".format(gerrit_url) repos_list = {} for project in projects: - project_url = gerrit_project_url.format(project) - code_url = gitweb_code_url.format(project) + project_url = gerrit_project_url_base.format(project) + code_url = gitweb_code_url_base.format(project) + CODE_LOCATION anchor = GITWEB_ANCHOR if ssh and len(ssh) == 2: user, port = ssh[0], ssh[1] project_url = "ssh://{}@{}:{}/{}.git".format(user, gerrit, port, project) if git: - code_url = "https://{}/{}/tree/{{path}}{{anchor}}".format(git, project) + code_url = "https://{}/{}/tree/".format(git, project) + CODE_LOCATION anchor = GIT_ANCHOR repos_list[project] = { -- cgit 1.2.3-korg