From 62ba15ec20611de96295892c076c2ac1a32d3942 Mon Sep 17 00:00:00 2001 From: danieleades <33452915+danieleades@users.noreply.github.com> Date: Tue, 30 Nov 2021 08:11:57 +0000 Subject: [PATCH] lint and fix using pyupgrade (#418) --- .pre-commit-config.yaml | 7 ++++ docs/conf.py | 8 ++--- performance/performance_test.py | 2 +- sphinxcontrib/needs/api/configuration.py | 4 +-- sphinxcontrib/needs/api/need.py | 36 +++++++++---------- sphinxcontrib/needs/builder.py | 2 +- sphinxcontrib/needs/diagrams_common.py | 8 ++--- sphinxcontrib/needs/directives/need.py | 11 +++--- sphinxcontrib/needs/directives/needextend.py | 2 +- sphinxcontrib/needs/directives/needfilter.py | 4 +-- sphinxcontrib/needs/directives/needflow.py | 12 +++---- sphinxcontrib/needs/directives/needgantt.py | 4 +-- sphinxcontrib/needs/directives/needimport.py | 15 +++----- sphinxcontrib/needs/directives/needlist.py | 2 +- sphinxcontrib/needs/directives/needpie.py | 6 ++-- sphinxcontrib/needs/directives/needservice.py | 2 +- sphinxcontrib/needs/directives/needtable.py | 2 +- sphinxcontrib/needs/environment.py | 12 +++---- sphinxcontrib/needs/external_needs.py | 2 +- sphinxcontrib/needs/filter_common.py | 6 ++-- sphinxcontrib/needs/functions/common.py | 4 +-- sphinxcontrib/needs/functions/functions.py | 16 ++++----- sphinxcontrib/needs/layout.py | 18 +++++----- sphinxcontrib/needs/needs.py | 6 ++-- sphinxcontrib/needs/needsfile.py | 10 +++--- sphinxcontrib/needs/roles/need_count.py | 2 +- sphinxcontrib/needs/roles/need_outgoing.py | 2 +- sphinxcontrib/needs/roles/need_part.py | 2 +- sphinxcontrib/needs/roles/need_ref.py | 2 +- sphinxcontrib/needs/services/config/github.py | 2 +- sphinxcontrib/needs/services/github.py | 4 +-- sphinxcontrib/needs/services/manager.py | 2 +- sphinxcontrib/needs/warnings.py | 4 +-- tests/doc_test/add_sections/conf.py | 1 - tests/doc_test/api_doc/conf.py | 1 - tests/doc_test/api_doc_awesome/conf.py | 1 - tests/doc_test/broken_doc/conf.py | 1 - tests/doc_test/broken_links/conf.py | 1 - tests/doc_test/broken_statuses/conf.py | 1 - tests/doc_test/broken_syntax_doc/conf.py | 1 - tests/doc_test/broken_tags/conf.py | 1 - tests/doc_test/broken_tags_2/conf.py | 1 - tests/doc_test/doc_build_latex/conf.py | 1 - tests/doc_test/doc_df_calc_sum/conf.py | 1 - .../doc_df_check_linked_values/conf.py | 1 - tests/doc_test/doc_df_user_functions/conf.py | 1 - tests/doc_test/doc_dynamic_functions/conf.py | 1 - tests/doc_test/doc_export_id/conf.py | 1 - tests/doc_test/doc_extra_links/conf.py | 1 - tests/doc_test/doc_github_issue_160/conf.py | 1 - tests/doc_test/doc_github_issue_21/conf.py | 1 - tests/doc_test/doc_github_issue_44/conf.py | 1 - tests/doc_test/doc_github_issue_61/conf.py | 1 - tests/doc_test/doc_global_options/conf.py | 1 - tests/doc_test/doc_layout/conf.py | 1 - tests/doc_test/doc_need_count/conf.py | 1 - tests/doc_test/doc_need_parts/conf.py | 1 - tests/doc_test/doc_needextend/conf.py | 1 - tests/doc_test/doc_needflow/conf.py | 1 - tests/doc_test/doc_needlist/conf.py | 1 - tests/doc_test/doc_needpie/conf.py | 1 - tests/doc_test/doc_needs_builder/conf.py | 1 - .../doc_needs_builder_negative_tests/conf.py | 1 - .../doc_test/doc_needs_external_needs/conf.py | 1 - tests/doc_test/doc_needs_filter_data/conf.py | 1 - tests/doc_test/doc_needs_warnings/conf.py | 1 - .../conf.py | 1 - tests/doc_test/doc_needsfile/conf.py | 1 - tests/doc_test/doc_needtable/conf.py | 1 - .../doc_role_need_max_title_length/conf.py | 1 - .../conf.py | 1 - tests/doc_test/doc_role_need_template/conf.py | 1 - tests/doc_test/doc_style_blank/conf.py | 1 - tests/doc_test/doc_style_custom/conf.py | 1 - tests/doc_test/doc_style_modern/conf.py | 1 - tests/doc_test/doc_style_unknown/conf.py | 1 - tests/doc_test/external_doc/conf.py | 1 - tests/doc_test/extra_options/conf.py | 1 - tests/doc_test/filter_doc/conf.py | 1 - tests/doc_test/generic_doc/conf.py | 1 - tests/doc_test/import_doc/conf.py | 1 - tests/doc_test/multiple_link_backs/conf.py | 1 - tests/doc_test/non_exists_file_import/conf.py | 1 - tests/doc_test/parallel_doc/conf.py | 1 - tests/doc_test/service_doc/conf.py | 1 - tests/doc_test/title_from_content/conf.py | 1 - tests/doc_test/title_optional/conf.py | 1 - tests/doc_test/unicode_support/conf.py | 1 - tests/test_needflow.py | 2 -- tests/test_needpie.py | 2 -- tests/test_needs_builder.py | 2 +- tests/test_needtable.py | 2 -- tests/test_official_doc.py | 3 +- tests/test_title_from_content.py | 1 - tests/test_title_optional.py | 1 - tests/test_unicode.py | 6 ++-- 96 files changed, 112 insertions(+), 183 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index de2c49584..3603eb620 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,3 +17,10 @@ repos: rev: 5.9.3 hooks: - id: isort + + - repo: https://github.com/asottile/pyupgrade + rev: v2.29.1 + hooks: + - id: pyupgrade + args: + - --py36-plus diff --git a/docs/conf.py b/docs/conf.py index a34daa432..f2e1ad1d5 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -257,9 +257,9 @@ if on_rtd: # Deactivated using rtd plantuml version as it looks quite old. # plantuml = 'java -Djava.awt.headless=true -jar /usr/share/plantuml/plantuml.jar' - plantuml = "java -Djava.awt.headless=true -jar {}".format(local_plantuml_path) + plantuml = f"java -Djava.awt.headless=true -jar {local_plantuml_path}" else: - plantuml = "java -jar {}".format(local_plantuml_path) + plantuml = f"java -jar {local_plantuml_path}" # plantuml_output_format = 'png' plantuml_output_format = "svg_img" @@ -343,7 +343,7 @@ github_token = os.environ.get("GITHUB_TOKEN", "") if github_username != "" and github_token != "": - print("GITHUB: Using as username: {}. lenth token: {}".format(github_username, len(github_token))) + print(f"GITHUB: Using as username: {github_username}. lenth token: {len(github_token)}") for service in ["github-issues", "github-prs", "github-commits"]: needs_services[service]["username"] = github_username needs_services[service]["token"] = github_token @@ -365,7 +365,7 @@ # General information about the project. project = "Sphinx-Needs" now = datetime.datetime.now() -copyright = '2017-{year}, team useblocks'.format(year=now.year) +copyright = f'2017-{now.year}, team useblocks' author = "team useblocks" # The language for content autogenerated by Sphinx. Refer to documentation diff --git a/performance/performance_test.py b/performance/performance_test.py index e82a0806d..5a8f438c3 100644 --- a/performance/performance_test.py +++ b/performance/performance_test.py @@ -249,7 +249,7 @@ def series( ] print(tabulate(result_table, headers=headers)) - overall_runtime = sum([x[1] for x in results]) + overall_runtime = sum(x[1] for x in results) print(f"\nOverall runtime: {overall_runtime:.2f} seconds.") if snakeviz: diff --git a/sphinxcontrib/needs/api/configuration.py b/sphinxcontrib/needs/api/configuration.py index 8a8f52c99..1aab1cd60 100644 --- a/sphinxcontrib/needs/api/configuration.py +++ b/sphinxcontrib/needs/api/configuration.py @@ -59,7 +59,7 @@ def add_need_type(app: Sphinx, directive, title, prefix, color="#ffffff", style= type_names = [x["directive"] for x in needs_types] if directive in type_names: - raise NeedsApiConfigException("{} already exists as need type".format(directive)) + raise NeedsApiConfigException(f"{directive} already exists as need type") needs_types.append({"directive": directive, "title": title, "prefix": prefix, "color": color, "style": style}) app.add_directive(directive, sphinxcontrib.needs.directives.need.NeedDirective) @@ -85,7 +85,7 @@ def add_extra_option(app, name): extra_options = NEEDS_CONFIG.create_or_get("extra_options", dict) if name in extra_options: - raise NeedsApiConfigWarning("Option {} already registered.".format(name)) + raise NeedsApiConfigWarning(f"Option {name} already registered.") NEEDS_CONFIG.add("extra_options", {name: directives.unchanged}, dict, append=True) # extra_options[name] = directives.unchanged diff --git a/sphinxcontrib/needs/api/need.py b/sphinxcontrib/needs/api/need.py index 71148e5b1..d7757046c 100644 --- a/sphinxcontrib/needs/api/need.py +++ b/sphinxcontrib/needs/api/need.py @@ -50,7 +50,7 @@ def add_need( is_external=False, external_url=None, external_css="external_link", - **kwargs + **kwargs, ): """ Creates a new need and returns its node. @@ -180,7 +180,7 @@ def run(): # Check if status is in needs_statuses. If not raise an error. if env.app.config.needs_statuses and status not in [stat["name"] for stat in env.app.config.needs_statuses]: raise NeedsStatusNotAllowed( - "Status {0} of need id {1} is not allowed " "by config value 'needs_statuses'.".format(status, need_id) + f"Status {status} of need id {need_id} is not allowed " "by config value 'needs_statuses'." ) if tags is None: @@ -193,9 +193,7 @@ def run(): new_tags = [] # Shall contain only valid tags for i in range(len(tags)): if len(tags[i]) == 0 or tags[i].isspace(): - logger.warning( - "Scruffy tag definition found in need {}. " "Defined tag contains spaces only.".format(need_id) - ) + logger.warning(f"Scruffy tag definition found in need {need_id}. " "Defined tag contains spaces only.") else: new_tags.append(tags[i]) @@ -205,7 +203,7 @@ def run(): for tag in tags: if tag not in [tag["name"] for tag in env.app.config.needs_tags]: raise NeedsTagNotAllowed( - "Tag {0} of need id {1} is not allowed " "by config value 'needs_tags'.".format(tag, need_id) + f"Tag {tag} of need id {need_id} is not allowed " "by config value 'needs_tags'." ) # This may have cut also dynamic function strings, as they can contain , as well. # So let put them together again @@ -222,8 +220,8 @@ def run(): if need_id in env.needs_all_needs: if id: raise NeedsDuplicatedId( - "A need with ID {} already exists! " - "This is not allowed. Document {}[{}] Title: {}.".format(need_id, docname, lineno, title) + f"A need with ID {need_id} already exists! " + f"This is not allowed. Document {docname}[{lineno}] Title: {title}." ) else: # this is a generated ID raise NeedsDuplicatedId( @@ -361,7 +359,7 @@ def run(): # Title and meta data information gets added alter during event handling via process_need_nodes() # We just add a basic need node and render the rst-based content, because this can not be done later. # style_classes = ['need', type_name, 'need-{}'.format(type_name.lower())] # Used < 0.4.4 - style_classes = ["need", "need-{}".format(need_type.lower())] + style_classes = ["need", f"need-{need_type.lower()}"] if style: style_classes.append(style) @@ -403,7 +401,7 @@ def del_need(app, id): if id in app.env.needs_all_needs: del app.env.needs_all_needs[id] else: - logger.warning("Given need id {} not exists!".format(id)) + logger.warning(f"Given need id {id} not exists!") def add_external_need( @@ -417,7 +415,7 @@ def add_external_need( status=None, tags=None, links_string=None, - **kwargs + **kwargs, ): """ Adds an external need from an external source. @@ -465,14 +463,14 @@ def _prepare_template(app, needs_info, template_key): template_folder = os.path.join(app.confdir, template_folder) if not os.path.isdir(template_folder): - raise NeedsTemplateException("Template folder does not exist: {}".format(template_folder)) + raise NeedsTemplateException(f"Template folder does not exist: {template_folder}") template_file_name = needs_info[template_key] + ".need" template_path = os.path.join(template_folder, template_file_name) if not os.path.isfile(template_path): - raise NeedsTemplateException("Template does not exist: {}".format(template_path)) + raise NeedsTemplateException(f"Template does not exist: {template_path}") - with open(template_path, "r") as template_file: + with open(template_path) as template_file: template_content = "".join(template_file.readlines()) template_obj = Template(template_content) new_content = template_obj.render(**needs_info) @@ -502,9 +500,7 @@ def _read_in_links(links_string: Union[str, List[str]]): link_list = links_string for link in link_list: if link.isspace(): - logger.warning( - "Grubby link definition found in need {}. " "Defined link contains spaces only.".format(id) - ) + logger.warning(f"Grubby link definition found in need {id}. " "Defined link contains spaces only.") else: links.append(link.strip()) @@ -536,10 +532,10 @@ def make_hashed_id(app, need_type, full_title, content, id_length=None): type_prefix = ntype["prefix"] break if type_prefix is None: - raise NeedsInvalidException("Given need_type {} is unknown. File {}".format(need_type, app.env.docname)) + raise NeedsInvalidException(f"Given need_type {need_type} is unknown. File {app.env.docname}") hashable_content = full_title or "\n".join(content) - return "%s%s" % (type_prefix, hashlib.sha1(hashable_content.encode("UTF-8")).hexdigest().upper()[:id_length]) + return "{}{}".format(type_prefix, hashlib.sha1(hashable_content.encode("UTF-8")).hexdigest().upper()[:id_length]) def _fix_list_dyn_func(list): @@ -621,7 +617,7 @@ def _merge_global_options(app, needs_info, global_options): for single_value in values: if len(single_value) < 2 or len(single_value) > 3: - raise NeedsInvalidException("global option tuple has wrong amount of parameters: {}".format(key)) + raise NeedsInvalidException(f"global option tuple has wrong amount of parameters: {key}") if filter_single_need(app, needs_info, single_value[1]): # Set value, if filter has matched needs_info[key] = single_value[0] diff --git a/sphinxcontrib/needs/builder.py b/sphinxcontrib/needs/builder.py index 98f232f24..2f23fccf6 100644 --- a/sphinxcontrib/needs/builder.py +++ b/sphinxcontrib/needs/builder.py @@ -53,7 +53,7 @@ def finish(self): try: needs_list.write_json() except Exception as e: - log.error("Error during writing json file: {0}".format(e)) + log.error(f"Error during writing json file: {e}") else: log.info("Needs successfully exported") diff --git a/sphinxcontrib/needs/diagrams_common.py b/sphinxcontrib/needs/diagrams_common.py index b7e61c069..225ad0c00 100644 --- a/sphinxcontrib/needs/diagrams_common.py +++ b/sphinxcontrib/needs/diagrams_common.py @@ -50,7 +50,7 @@ def prepare_env(self, env_var): def create_target(self, target_name): env = self.state.document.settings.env id = env.new_serialno(target_name) - targetid = "{targetname}-{docname}-{id}".format(targetname=target_name, docname=env.docname, id=id) + targetid = f"{target_name}-{env.docname}-{id}" targetnode = nodes.target("", "", ids=[targetid]) return id, targetid, targetnode @@ -78,9 +78,9 @@ def collect_diagram_attributes(self): scale = self.options.get("scale", "100").replace("%", "") if not scale.isdigit(): - raise Exception('Needsequence scale value must be a number. "{}" found'.format(scale)) + raise Exception(f'Needsequence scale value must be a number. "{scale}" found') if int(scale) < 1 or int(scale) > 300: - raise Exception('Needsequence scale value must be between 1 and 300. "{}" found'.format(scale)) + raise Exception(f'Needsequence scale value must be between 1 and 300. "{scale}" found') highlight = self.options.get("highlight", "") @@ -159,7 +159,7 @@ def get_debug_container(puml_node): else: data = puml_node["uml"] data = "\n".join([html.escape(line) for line in data.split("\n")]) - debug_para = nodes.raw("", "
{}".format(data), format="html") + debug_para = nodes.raw("", f"
{data}", format="html") debug_container += debug_para return debug_container diff --git a/sphinxcontrib/needs/directives/need.py b/sphinxcontrib/needs/directives/need.py index 3d48d9466..a0591f397 100644 --- a/sphinxcontrib/needs/directives/need.py +++ b/sphinxcontrib/needs/directives/need.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- import hashlib import re @@ -130,7 +129,9 @@ def read_in_links(self, name): def make_hashed_id(self, type_prefix, id_length): hashable_content = self.full_title or "\n".join(self.content) - return "%s%s" % (type_prefix, hashlib.sha1(hashable_content.encode("UTF-8")).hexdigest().upper()[:id_length]) + return "{}{}".format( + type_prefix, hashlib.sha1(hashable_content.encode("UTF-8")).hexdigest().upper()[:id_length] + ) @property def env(self): @@ -345,8 +346,8 @@ def create_back_links(env, option): :param env: sphinx enviroment :return: None """ - option_back = "{}_back".format(option) - if env.needs_workflow["backlink_creation_{}".format(option)]: + option_back = f"{option}_back" + if env.needs_workflow[f"backlink_creation_{option}"]: return needs = env.needs_all_needs @@ -368,7 +369,7 @@ def create_back_links(env, option): needs[link_main]["parts"][link_part][option_back] = [] needs[link_main]["parts"][link_part][option_back].append(key) - env.needs_workflow["backlink_creation_{}".format(option)] = True + env.needs_workflow[f"backlink_creation_{option}"] = True def _fix_list_dyn_func(list): diff --git a/sphinxcontrib/needs/directives/needextend.py b/sphinxcontrib/needs/directives/needextend.py index cd7634298..e9995e122 100644 --- a/sphinxcontrib/needs/directives/needextend.py +++ b/sphinxcontrib/needs/directives/needextend.py @@ -37,7 +37,7 @@ def run(self): env.needs_all_needs = {} id = env.new_serialno("needextend") - targetid = "needextend-{docname}-{id}".format(docname=env.docname, id=id) + targetid = f"needextend-{env.docname}-{id}" targetnode = nodes.target("", "", ids=[targetid]) extend_filter = self.arguments[0] if self.arguments else None diff --git a/sphinxcontrib/needs/directives/needfilter.py b/sphinxcontrib/needs/directives/needfilter.py index 8d62d3b21..e93f7284a 100644 --- a/sphinxcontrib/needs/directives/needfilter.py +++ b/sphinxcontrib/needs/directives/needfilter.py @@ -151,7 +151,7 @@ def process_needfilters(app, doctree, fromdocname): for need_info in found_needs: if current_needfilter["layout"] == "list": para = nodes.line() - description = "%s: %s" % (need_info["id"], need_info["title"]) + description = "{}: {}".format(need_info["id"], need_info["title"]) if current_needfilter["show_status"] and need_info["status"]: description += " (%s)" % need_info["status"] @@ -192,7 +192,7 @@ def process_needfilters(app, doctree, fromdocname): link = ( "../" + app.builder.get_target_uri(need_info["docname"]) - + "?highlight={0}".format(urlparse(need_info["title"])) + + "?highlight={}".format(urlparse(need_info["title"])) + "#" + need_info["target_node"]["refid"] ) # Gets mostly called during latex generation diff --git a/sphinxcontrib/needs/directives/needflow.py b/sphinxcontrib/needs/directives/needflow.py index 9e8380ee4..d9d46f14a 100644 --- a/sphinxcontrib/needs/directives/needflow.py +++ b/sphinxcontrib/needs/directives/needflow.py @@ -57,7 +57,7 @@ def run(self): env.needs_all_needs = {} id = env.new_serialno("needflow") - targetid = "needflow-{docname}-{id}".format(docname=env.docname, id=id) + targetid = f"needflow-{env.docname}-{id}" targetnode = nodes.target("", "", ids=[targetid]) all_link_types = ",".join(x["option"] for x in env.config.needs_extra_links) @@ -73,9 +73,9 @@ def run(self): scale = self.options.get("scale", "100").replace("%", "") if not scale.isdigit(): - raise Exception('Needflow scale value must be a number. "{}" found'.format(scale)) + raise Exception(f'Needflow scale value must be a number. "{scale}" found') if int(scale) < 1 or int(scale) > 300: - raise Exception('Needflow scale value must be between 1 and 300. "{}" found'.format(scale)) + raise Exception(f'Needflow scale value must be between 1 and 300. "{scale}" found') highlight = self.options.get("highlight", "") @@ -249,7 +249,7 @@ def process_needflow(app, doctree, fromdocname): # Only add subelements and their {...} container, if we really need them. # Otherwise plantuml may not set style correctly, if {..} is empty if node_part_code: - node_part_code = "{{\n {} }}".format(node_part_code) + node_part_code = f"{{\n {node_part_code} }}" style = need_info["type_style"] @@ -277,7 +277,7 @@ def process_needflow(app, doctree, fromdocname): final_link = link if current_needflow["show_link_names"] or env.config.needs_flow_show_links: desc = link_type["outgoing"] + "\\n" - comment = ": {desc}".format(desc=desc) + comment = f": {desc}" else: comment = "" @@ -399,7 +399,7 @@ def process_needflow(app, doctree, fromdocname): else: data = puml_node["uml"] data = "\n".join([html.escape(line) for line in data.split("\n")]) - debug_para = nodes.raw("", "
{}".format(data), format="html") + debug_para = nodes.raw("", f"
{data}", format="html") debug_container += debug_para content += debug_container diff --git a/sphinxcontrib/needs/directives/needgantt.py b/sphinxcontrib/needs/directives/needgantt.py index 039ef8e78..e6c4b190b 100644 --- a/sphinxcontrib/needs/directives/needgantt.py +++ b/sphinxcontrib/needs/directives/needgantt.py @@ -180,9 +180,9 @@ def process_needgantt(app, doctree, fromdocname): ) month = MONTH_NAMES[int(start_date.strftime("%-m"))] - start_date_plantuml = start_date.strftime("%dth of {} %Y".format(month)) + start_date_plantuml = start_date.strftime(f"%dth of {month} %Y") if start_date_plantuml: - puml_node["uml"] += "Project starts the {}\n".format(start_date_plantuml) + puml_node["uml"] += f"Project starts the {start_date_plantuml}\n" # Element handling puml_node["uml"] += "\n' Elements definition \n\n" diff --git a/sphinxcontrib/needs/directives/needimport.py b/sphinxcontrib/needs/directives/needimport.py index 55533e54d..423498b69 100644 --- a/sphinxcontrib/needs/directives/needimport.py +++ b/sphinxcontrib/needs/directives/needimport.py @@ -2,7 +2,6 @@ import os import re -import six from docutils import nodes from docutils.parsers.rst import Directive, directives @@ -73,9 +72,9 @@ def run(self): correct_need_import_path = os.path.join(env.app.confdir, need_import_path[1:]) if not os.path.exists(correct_need_import_path): - raise ReferenceError("Could not load needs import file {0}".format(correct_need_import_path)) + raise ReferenceError(f"Could not load needs import file {correct_need_import_path}") - with open(correct_need_import_path, "r") as needs_file: + with open(correct_need_import_path) as needs_file: needs_file_content = needs_file.read() try: needs_import_list = json.loads(needs_file_content) @@ -86,16 +85,12 @@ def run(self): if version is None: try: version = needs_import_list["current_version"] - if not isinstance(version, six.string_types): + if not isinstance(version, str): raise KeyError except KeyError: - raise CorruptedNeedsFile( - "Key 'current_version' missing or corrupted in {0}".format(correct_need_import_path) - ) + raise CorruptedNeedsFile(f"Key 'current_version' missing or corrupted in {correct_need_import_path}") if version not in needs_import_list["versions"].keys(): - raise VersionNotFound( - "Version {0} not found in needs import file {1}".format(version, correct_need_import_path) - ) + raise VersionNotFound(f"Version {version} not found in needs import file {correct_need_import_path}") needs_list = needs_import_list["versions"][version]["needs"] diff --git a/sphinxcontrib/needs/directives/needlist.py b/sphinxcontrib/needs/directives/needlist.py index b4932922a..60f9ebe32 100644 --- a/sphinxcontrib/needs/directives/needlist.py +++ b/sphinxcontrib/needs/directives/needlist.py @@ -88,7 +88,7 @@ def process_needlist(app, doctree, fromdocname): line_block = nodes.line_block() for need_info in found_needs: para = nodes.line() - description = "%s: %s" % (need_info["id"], need_info["title"]) + description = "{}: {}".format(need_info["id"], need_info["title"]) if current_needfilter["show_status"] and need_info["status"]: description += " (%s)" % need_info["status"] diff --git a/sphinxcontrib/needs/directives/needpie.py b/sphinxcontrib/needs/directives/needpie.py index 992ecbd1f..bde09d4fe 100644 --- a/sphinxcontrib/needs/directives/needpie.py +++ b/sphinxcontrib/needs/directives/needpie.py @@ -54,7 +54,7 @@ def run(self): env.needs_all_needs = {} id = env.new_serialno("needpie") - targetid = "needpie-{docname}-{id}".format(docname=env.docname, id=id) + targetid = f"needpie-{env.docname}-{id}" targetnode = nodes.target("", "", ids=[targetid]) content = self.content @@ -189,7 +189,7 @@ def process_needpie(app, doctree, fromdocname): os.mkdir(image_folder) # We need to calculate an unique pie-image file name hash_value = hashlib.sha256(id.encode()).hexdigest()[:5] - rel_file_path = os.path.join("_images", "need_pie_{}.png".format(hash_value)) + rel_file_path = os.path.join("_images", f"need_pie_{hash_value}.png") if rel_file_path not in env.images: fig.savefig(os.path.join(env.app.srcdir, rel_file_path), format="png") env.images[rel_file_path] = ["_images", os.path.split(rel_file_path)[-1]] @@ -205,4 +205,4 @@ def process_needpie(app, doctree, fromdocname): def label_calc(pct, allvals): absolute = int(round(pct / 100.0 * sum(allvals))) - return "{:.1f}%\n({:d})".format(pct, absolute) + return f"{pct:.1f}%\n({absolute:d})" diff --git a/sphinxcontrib/needs/directives/needservice.py b/sphinxcontrib/needs/directives/needservice.py index 54b53cc9b..90a3c98f8 100644 --- a/sphinxcontrib/needs/directives/needservice.py +++ b/sphinxcontrib/needs/directives/needservice.py @@ -80,7 +80,7 @@ def run(self): if app.config.needs_service_all_data: for name, value in missing_options.items(): - content.append("\n:{}: {}".format(name, value)) + content.append(f"\n:{name}: {value}") # content.insert(0, '.. code-block:: text\n') options["content"] = "\n".join(content) diff --git a/sphinxcontrib/needs/directives/needtable.py b/sphinxcontrib/needs/directives/needtable.py index 9ccb2c71b..0aa98cf6b 100644 --- a/sphinxcontrib/needs/directives/needtable.py +++ b/sphinxcontrib/needs/directives/needtable.py @@ -142,7 +142,7 @@ def process_needtables(app, doctree, fromdocname): style = current_needtable["style"].upper() # Prepare table - classes = ["NEEDS_{style}".format(style=style)] + classes = [f"NEEDS_{style}"] # Only add the theme specific "do not touch this table" class, if we use a style which # care about table layout and styling. The normal "TABLE" style is using the Sphinx default table diff --git a/sphinxcontrib/needs/environment.py b/sphinxcontrib/needs/environment.py index 62f98ad3f..9dd422d35 100644 --- a/sphinxcontrib/needs/environment.py +++ b/sphinxcontrib/needs/environment.py @@ -37,7 +37,7 @@ def safe_add_file(filename: Path, app: Sphinx): if hasattr(app.builder, "css_files") and static_data_file not in app.builder.css_files: app.add_css_file(str(filename)) else: - raise NotImplementedError("File type {} not support by save_add_file".format(filename.suffix)) + raise NotImplementedError(f"File type {filename.suffix} not support by save_add_file") def safe_remove_file(filename: Path, app: Sphinx): @@ -83,7 +83,7 @@ def install_styles_static_files(app: Sphinx, env): def find_css_files() -> Iterable[Path]: for theme in ["modern", "dark", "blank"]: - if app.config.needs_css == "{}.css".format(theme): + if app.config.needs_css == f"{theme}.css": css_dir = css_root / theme return [f for f in css_dir.glob("**/*") if f.is_file()] return [app.config.needs_css] @@ -93,7 +93,7 @@ def find_css_files() -> Iterable[Path]: # Be sure no "old" css layout is already set for theme in ["common", "modern", "dark", "blank"]: - path = Path("sphinx-needs") / "{}.css".format(theme) + path = Path("sphinx-needs") / f"{theme}.css" safe_remove_file(path, app) if parse_version(sphinx_version) < parse_version("1.6"): @@ -113,7 +113,7 @@ def find_css_files() -> Iterable[Path]: if not source_file_path.exists(): source_file_path = css_root / "blank" / "blank.css" - logger.warning("{0} not found. Copying sphinx-internal blank.css".format(source_file_path)) + logger.warning(f"{source_file_path} not found. Copying sphinx-internal blank.css") dest_file = dest_dir / source_file_path.name dest_dir.mkdir(exist_ok=True) @@ -147,10 +147,10 @@ def install_static_files( source_file = Path(source_file_path) if not source_file.is_absolute(): - raise IOError("Path must be absolute. Got: {}".format(source_file)) + raise OSError(f"Path must be absolute. Got: {source_file}") if not source_file.exists(): - raise IOError("File not found: {}".format(source_file)) + raise OSError(f"File not found: {source_file}") relative_path = source_file.relative_to(source_dir) destination_file = destination_dir / relative_path diff --git a/sphinxcontrib/needs/external_needs.py b/sphinxcontrib/needs/external_needs.py index 259a5c21c..f5fc6009a 100644 --- a/sphinxcontrib/needs/external_needs.py +++ b/sphinxcontrib/needs/external_needs.py @@ -47,7 +47,7 @@ def load_external_needs(app, env, _docname): if not os.path.exists(json_path): raise NeedsExternalException(f"Given json_path {json_path} does not exist.") - with open(json_path, "r") as json_file: + with open(json_path) as json_file: needs_json = json.load(json_file) version = source.get("version", needs_json.get("current_version", None)) diff --git a/sphinxcontrib/needs/filter_common.py b/sphinxcontrib/needs/filter_common.py index ae51c827b..1e3963f7e 100644 --- a/sphinxcontrib/needs/filter_common.py +++ b/sphinxcontrib/needs/filter_common.py @@ -79,7 +79,7 @@ def process_filters(app, all_needs, current_needlist, include_external=True): try: all_needs = sorted(all_needs, key=lambda node: node[sort_key] or "") except KeyError as e: - log.warning("Sorting parameter {0} not valid: Error: {1}".format(sort_key, e)) + log.warning(f"Sorting parameter {sort_key} not valid: Error: {e}") # check if include external needs checked_all_needs = [] @@ -266,7 +266,7 @@ def filter_needs(app, needs, filter_string="", current_need=None): ): found_needs.append(filter_need) except Exception as e: - log.warning("Filter {0} not valid: Error: {1}".format(filter_string, e)) + log.warning(f"Filter {filter_string} not valid: Error: {e}") return found_needs @@ -304,5 +304,5 @@ def filter_single_need(app, need, filter_string="", needs=None, current_need=Non else: result = bool(eval(filter_string, filter_context)) except Exception as e: - raise NeedsInvalidFilter("Filter {0} not valid: Error: {1}".format(filter_string, e)) + raise NeedsInvalidFilter(f"Filter {filter_string} not valid: Error: {e}") return result diff --git a/sphinxcontrib/needs/functions/common.py b/sphinxcontrib/needs/functions/common.py index c6269967a..408db648b 100644 --- a/sphinxcontrib/needs/functions/common.py +++ b/sphinxcontrib/needs/functions/common.py @@ -291,7 +291,7 @@ def check_linked_values(app, need, needs, result, search_option, search_value, f if not filter_single_need(app, needs[link], filter_string): continue except Exception as e: - logger.warning("CheckLinkedValues: Filter {0} not valid: Error: {1}".format(filter_string, e)) + logger.warning(f"CheckLinkedValues: Filter {filter_string} not valid: Error: {e}") if not one_hit and not needs[link][search_option] in search_value: return None @@ -398,7 +398,7 @@ def calc_sum(app, need, needs, option, filter=None, links_only=False): except ValueError: pass except NeedsInvalidFilter as ex: - logger.warning("Given filter is not valid. Error: {}".format(ex)) + logger.warning(f"Given filter is not valid. Error: {ex}") try: calculated_sum += float(check_need[option]) except ValueError: diff --git a/sphinxcontrib/needs/functions/functions.py b/sphinxcontrib/needs/functions/functions.py index 1196715e0..125feb5f7 100644 --- a/sphinxcontrib/needs/functions/functions.py +++ b/sphinxcontrib/needs/functions/functions.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - """ Sphinx-needs functions module ============================= @@ -130,7 +128,7 @@ def find_and_replace_node_content(node, env, need): if isinstance(func_return, list): func_return = ", ".join(func_return) - new_text = new_text.replace("[[{}]]".format(func_string_org), func_return) + new_text = new_text.replace(f"[[{func_string_org}]]", func_return) if isinstance(node, nodes.reference): node.attributes["refuri"] = new_text @@ -192,9 +190,9 @@ def resolve_dynamic_values(env): continue # Replace original function string with return value of function call if func_return is None: - need[need_option] = need[need_option].replace("[[{}]]".format(func_call), "") + need[need_option] = need[need_option].replace(f"[[{func_call}]]", "") else: - need[need_option] = need[need_option].replace("[[{}]]".format(func_call), str(func_return)) + need[need_option] = need[need_option].replace(f"[[{func_call}]]", str(func_return)) if need[need_option] == "": need[need_option] = None @@ -216,7 +214,7 @@ def resolve_dynamic_values(env): else: # Replace original function string with return value of function call if isinstance(need[need_option], (str, int, float)): - new_values.append(element.replace("[[{}]]".format(func_call), str(func_return))) + new_values.append(element.replace(f"[[{func_call}]]", str(func_return))) else: if isinstance(need[need_option], (list, set)): if isinstance(func_return, (list, set)): @@ -255,7 +253,7 @@ def check_and_get_content(content, need, env): func_return = execute_func(env, need, func_call) # Execute function call and get return value # Replace the function_call with the calculated value - content = content.replace("[[{}]]".format(func_call), func_return) + content = content.replace(f"[[{func_call}]]", func_return) return content @@ -292,12 +290,12 @@ def _analyze_func_string(func_string, need): func = ast.parse(func_string) except SyntaxError as e: need_id = need["id"] or "UNKNOWN" - raise SphinxError("Parsing function string failed for need {}: {}. {}".format(need_id, func_string, e)) + raise SphinxError(f"Parsing function string failed for need {need_id}: {func_string}. {e}") try: func_call = func.body[0].value func_name = func_call.func.id except AttributeError: - raise SphinxError("Given dynamic function string is not a valid python call. Got: {}".format(func_string)) + raise SphinxError(f"Given dynamic function string is not a valid python call. Got: {func_string}") func_args = [] for arg in func_call.args: diff --git a/sphinxcontrib/needs/layout.py b/sphinxcontrib/needs/layout.py index 53127ea30..875e034ce 100644 --- a/sphinxcontrib/needs/layout.py +++ b/sphinxcontrib/needs/layout.py @@ -37,7 +37,7 @@ def create_need(need_id, app: Sphinx, layout=None, style=None, docname=None): needs = env.needs_all_needs if need_id not in needs.keys(): - raise SphinxNeedLayoutException("Given need id {} does not exist.".format(need_id)) + raise SphinxNeedLayoutException(f"Given need id {need_id} does not exist.") need_data = needs[need_id] node_container = nodes.container() @@ -288,7 +288,7 @@ def get_section(self, section): if len(lines) == 0: return [] - lines_container = nodes.line_block(classes=["needs_{}".format(section)]) + lines_container = nodes.line_block(classes=[f"needs_{section}"]) for line in lines: # line_block_node = nodes.line_block() @@ -400,7 +400,7 @@ def _func_replace(self, section_nodes): node_line += result else: raise SphinxNeedLayoutException( - "Error during layout line parsing. This looks strange: {}".format(line_element) + f"Error during layout line parsing. This looks strange: {line_element}" ) return_nodes.append(node_line) @@ -413,7 +413,7 @@ def _replace_place_holder(self, data): raise SphinxNeedLayoutException(item) # To escape { we need to use 2 of them. # So {{ becomes {{{{ - replace_string = "{{{{{}}}}}".format(item) + replace_string = f"{{{{{item}}}}}" data = data.replace(replace_string, self.need[item]) return data @@ -585,7 +585,7 @@ def meta_all(self, prefix="", postfix="", exclude=None, no_links=False, defaults continue data_line = nodes.line() - label = prefix + "{}:".format(data) + postfix + " " + label = prefix + f"{data}:" + postfix + " " result = self.meta(data, label, show_empty) if not (show_empty or result): continue @@ -609,7 +609,7 @@ def meta_links(self, name, incoming=False): """ data_container = nodes.inline(classes=[name]) if name not in [x["option"] for x in self.app.config.needs_extra_links]: - raise SphinxNeedLayoutException("Invalid link name {} for link-type".format(name)) + raise SphinxNeedLayoutException(f"Invalid link name {name} for link-type") # if incoming: # link_name = self.app.config.needs_extra_links[name]['incoming'] @@ -620,9 +620,9 @@ def meta_links(self, name, incoming=False): from sphinxcontrib.needs.roles.need_outgoing import NeedOutgoing if incoming: - node_links = NeedIncoming(reftarget=self.need["id"], link_type="{}_back".format(name)) + node_links = NeedIncoming(reftarget=self.need["id"], link_type=f"{name}_back") else: - node_links = NeedOutgoing(reftarget=self.need["id"], link_type="{}".format(name)) + node_links = NeedOutgoing(reftarget=self.need["id"], link_type=f"{name}") node_links.append(nodes.inline(self.need["id"], self.need["id"])) data_container.append(node_links) return data_container @@ -715,7 +715,7 @@ def image(self, url, height=None, width=None, align=None, no_link=False, prefix= url = os.path.join( needs_location, "images", - "feather_{}".format(builder_extension), + f"feather_{builder_extension}", "{}.{}".format(url.split(":")[1], builder_extension), ) diff --git a/sphinxcontrib/needs/needs.py b/sphinxcontrib/needs/needs.py index 256792877..0b9dd73ba 100644 --- a/sphinxcontrib/needs/needs.py +++ b/sphinxcontrib/needs/needs.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - from docutils import nodes from docutils.parsers.rst import directives from sphinx.application import Sphinx @@ -135,7 +133,7 @@ def setup(app): app.add_config_value("needs_id_required", False, "html", types=[bool]) app.add_config_value( "needs_id_regex", - "^[A-Z0-9_]{{{id_length},}}".format(id_length=app.config.needs_id_length), + f"^[A-Z0-9_]{{{app.config.needs_id_length},}}", "html", ) app.add_config_value("needs_show_link_type", False, "html", types=[bool]) @@ -551,7 +549,7 @@ def check_configuration(_app: Sphinx, config: Config): # Check if external filter values is really a string if not isinstance(value, str): raise NeedsConfigException( - "External filter value: {0} from needs_filter_data {1} is not a string.".format(value, external_filter) + f"External filter value: {value} from needs_filter_data {external_filter} is not a string." ) # Check if needs external filter and extra option are using the same name if extern_filter in extra_options: diff --git a/sphinxcontrib/needs/needsfile.py b/sphinxcontrib/needs/needsfile.py index 530d9a218..3d840a4f6 100644 --- a/sphinxcontrib/needs/needsfile.py +++ b/sphinxcontrib/needs/needsfile.py @@ -114,7 +114,7 @@ def load_json(self, file): file = os.path.join(self.confdir, file) if not os.path.exists(file): - self.log.warning("Could not load needs json file {0}".format(file)) + self.log.warning(f"Could not load needs json file {file}") else: errors = check_needs_file(file) # We only care for schema errors here, all other possible errors @@ -124,12 +124,12 @@ def load_json(self, file): for error in errors["schema"]: self.log.info(f' {error.message} -> {".".join(error.path)}') - with open(file, "r") as needs_file: + with open(file) as needs_file: needs_file_content = needs_file.read() try: needs_list = json.loads(needs_file_content) except json.JSONDecodeError: - self.log.warning("Could not decode json file {0}".format(file)) + self.log.warning(f"Could not decode json file {file}") else: self.needs_list = needs_list @@ -147,10 +147,10 @@ def check_needs_file(path): :return: Dict, with error reports """ schema_path = os.path.join(os.path.dirname(__file__), "needsfile.json") - with open(schema_path, "r") as schema_file: + with open(schema_path) as schema_file: needs_schema = json.load(schema_file) - with open(path, "r") as needs_file: + with open(path) as needs_file: needs_data = json.load(needs_file) validator = Draft7Validator(needs_schema) diff --git a/sphinxcontrib/needs/roles/need_count.py b/sphinxcontrib/needs/roles/need_count.py index d14e6480e..8b9561270 100644 --- a/sphinxcontrib/needs/roles/need_count.py +++ b/sphinxcontrib/needs/roles/need_count.py @@ -32,7 +32,7 @@ def process_need_count(app, doctree, fromdocname): need_list = prepare_need_list(all_needs) # adds parts to need_list amount_1 = len(filter_needs(app, need_list, filters[0])) amount_2 = len(filter_needs(app, need_list, filters[1])) - amount = "{:2.1f}".format(amount_1 / amount_2 * 100) + amount = f"{amount_1 / amount_2 * 100:2.1f}" elif len(filters) > 2: raise NeedsInvalidFilter( "Filter not valid. Got too many filter elements. Allowed are 1 or 2. " diff --git a/sphinxcontrib/needs/roles/need_outgoing.py b/sphinxcontrib/needs/roles/need_outgoing.py index bd91afab7..907ce1ace 100644 --- a/sphinxcontrib/needs/roles/need_outgoing.py +++ b/sphinxcontrib/needs/roles/need_outgoing.py @@ -54,7 +54,7 @@ def process_need_outgoing(app, doctree, fromdocname): target_id = target_need["id"] if env.config.needs_show_link_title: - link_text = "{title} ({id})".format(title=target_title, id=target_id) + link_text = f"{target_title} ({target_id})" else: link_text = target_id if env.config.needs_show_link_type: diff --git a/sphinxcontrib/needs/roles/need_part.py b/sphinxcontrib/needs/roles/need_part.py index a4bd2c166..30437d36f 100644 --- a/sphinxcontrib/needs/roles/need_part.py +++ b/sphinxcontrib/needs/roles/need_part.py @@ -62,7 +62,7 @@ def update_need_with_parts(env, need, part_nodes): part_id_show = inline_id part_node["reftarget"] = part_id_ref - part_link_text = " {}".format(part_id_show) + part_link_text = f" {part_id_show}" part_link_node = nodes.Text(part_link_text, part_link_text) part_text_node = nodes.Text(part_content, part_content) diff --git a/sphinxcontrib/needs/roles/need_ref.py b/sphinxcontrib/needs/roles/need_ref.py index b2c055a27..2c73fef28 100644 --- a/sphinxcontrib/needs/roles/need_ref.py +++ b/sphinxcontrib/needs/roles/need_ref.py @@ -60,7 +60,7 @@ def process_need_ref(app, doctree, fromdocname): # Shorten title, if necessary max_length = app.config.needs_role_need_max_title_length if max_length > 3: - title = title if len(title) < max_length else u"{}...".format(title[: max_length - 3]) + title = title if len(title) < max_length else f"{title[: max_length - 3]}..." link_text = app.config.needs_role_need_template.format( title=title, diff --git a/sphinxcontrib/needs/services/config/github.py b/sphinxcontrib/needs/services/config/github.py index 3c55d1ecd..aa90d7303 100644 --- a/sphinxcontrib/needs/services/config/github.py +++ b/sphinxcontrib/needs/services/config/github.py @@ -28,7 +28,7 @@ ], "meta_right": [ '<>', - '<