twister: Refactor python module to follow PEP8 rules

Incremental refactoring.
Fix PEP8 issues to make ruff green for config_parser.py module.
Add __init__.py to twister directory to make it a proper python package,
and make modules importable.

Signed-off-by: Lukasz Fundakowski <lukasz.fundakowski@nordicsemi.no>
This commit is contained in:
Lukasz Fundakowski 2025-02-13 13:03:42 +01:00 committed by Carles Cufí
commit c5e8664089
5 changed files with 64 additions and 63 deletions

View file

@ -828,14 +828,6 @@
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports "I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
"UP026", # https://docs.astral.sh/ruff/rules/deprecated-mock-import "UP026", # https://docs.astral.sh/ruff/rules/deprecated-mock-import
] ]
"./scripts/tests/twister/test_config_parser.py" = [
"B017", # https://docs.astral.sh/ruff/rules/assert-raises-exception
"B033", # https://docs.astral.sh/ruff/rules/duplicate-value
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
"SIM117", # https://docs.astral.sh/ruff/rules/multiple-with-statements
"UP026", # https://docs.astral.sh/ruff/rules/deprecated-mock-import
]
"./scripts/tests/twister/test_data/mixins/test_to_ignore.py" = [ "./scripts/tests/twister/test_data/mixins/test_to_ignore.py" = [
"B011", # https://docs.astral.sh/ruff/rules/assert-false "B011", # https://docs.astral.sh/ruff/rules/assert-false
] ]

View file

@ -0,0 +1,2 @@
# Copyright (c) 2025 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

View file

@ -0,0 +1,2 @@
# Copyright (c) 2025 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

View file

@ -5,18 +5,21 @@
import copy import copy
import warnings import warnings
from typing import Any
import scl import scl
from twisterlib.error import ConfigurationError from twisterlib.error import ConfigurationError
def extract_fields_from_arg_list(target_fields: set, arg_list: str | list): def extract_fields_from_arg_list(
target_fields: set, arg_list: str | list
) -> tuple[dict[str, list[str]], list[str]]:
""" """
Given a list of "FIELD=VALUE" args, extract values of args with a Given a list of "FIELD=VALUE" args, extract values of args with a
given field name and return the remaining args separately. given field name and return the remaining args separately.
""" """
extracted_fields = {f : list() for f in target_fields} extracted_fields: dict[str, list[str]] = {f: list() for f in target_fields}
other_fields = [] other_fields: list[str] = []
if isinstance(arg_list, str): if isinstance(arg_list, str):
args = arg_list.strip().split() args = arg_list.strip().split()
@ -39,16 +42,18 @@ def extract_fields_from_arg_list(target_fields: set, arg_list: str | list):
return extracted_fields, other_fields return extracted_fields, other_fields
class TwisterConfigParser: class TwisterConfigParser:
"""Class to read testsuite yaml files with semantic checking """Class to read testsuite yaml files with semantic checking
""" """
testsuite_valid_keys = {"tags": {"type": "set", "required": False}, testsuite_valid_keys: dict[str, dict[str, Any]] = {
"tags": {"type": "set", "required": False},
"type": {"type": "str", "default": "integration"}, "type": {"type": "str", "default": "integration"},
"extra_args": {"type": "list"}, "extra_args": {"type": "list"},
"extra_configs": {"type": "list"}, "extra_configs": {"type": "list"},
"extra_conf_files": {"type": "list", "default": []}, "extra_conf_files": {"type": "list", "default": []},
"extra_overlay_confs" : {"type": "list", "default": []}, "extra_overlay_confs": {"type": "list", "default": []},
"extra_dtc_overlay_files": {"type": "list", "default": []}, "extra_dtc_overlay_files": {"type": "list", "default": []},
"required_snippets": {"type": "list"}, "required_snippets": {"type": "list"},
"build_only": {"type": "bool", "default": False}, "build_only": {"type": "bool", "default": False},
@ -67,8 +72,8 @@ class TwisterConfigParser:
"extra_sections": {"type": "list", "default": []}, "extra_sections": {"type": "list", "default": []},
"integration_platforms": {"type": "list", "default": []}, "integration_platforms": {"type": "list", "default": []},
"integration_toolchains": {"type": "list", "default": []}, "integration_toolchains": {"type": "list", "default": []},
"ignore_faults": {"type": "bool", "default": False }, "ignore_faults": {"type": "bool", "default": False},
"ignore_qemu_crash": {"type": "bool", "default": False }, "ignore_qemu_crash": {"type": "bool", "default": False},
"testcases": {"type": "list", "default": []}, "testcases": {"type": "list", "default": []},
"platform_type": {"type": "list", "default": []}, "platform_type": {"type": "list", "default": []},
"platform_exclude": {"type": "set"}, "platform_exclude": {"type": "set"},
@ -85,18 +90,18 @@ class TwisterConfigParser:
"sysbuild": {"type": "bool", "default": False} "sysbuild": {"type": "bool", "default": False}
} }
def __init__(self, filename, schema): def __init__(self, filename: str, schema: dict[str, Any]) -> None:
"""Instantiate a new TwisterConfigParser object """Instantiate a new TwisterConfigParser object
@param filename Source .yaml file to read @param filename Source .yaml file to read
""" """
self.data = {}
self.schema = schema self.schema = schema
self.filename = filename self.filename = filename
self.scenarios = {} self.data: dict[str, Any] = {}
self.common = {} self.scenarios: dict[str, Any] = {}
self.common: dict[str, Any] = {}
def load(self): def load(self) -> dict[str, Any]:
data = scl.yaml_load_verify(self.filename, self.schema) data = scl.yaml_load_verify(self.filename, self.schema)
self.data = data self.data = data
@ -106,7 +111,7 @@ class TwisterConfigParser:
self.common = self.data['common'] self.common = self.data['common']
return data return data
def _cast_value(self, value, typestr): def _cast_value(self, value: Any, typestr: str) -> Any:
if typestr == "str": if typestr == "str":
return value.strip() return value.strip()
@ -142,7 +147,7 @@ class TwisterConfigParser:
else: else:
raise ConfigurationError(self.filename, f"unknown type '{value}'") raise ConfigurationError(self.filename, f"unknown type '{value}'")
def get_scenario(self, name): def get_scenario(self, name: str) -> dict[str, Any]:
"""Get a dictionary representing the keys/values within a scenario """Get a dictionary representing the keys/values within a scenario
@param name The scenario in the .yaml file to retrieve data from @param name The scenario in the .yaml file to retrieve data from
@ -152,10 +157,10 @@ class TwisterConfigParser:
# "CONF_FILE", "OVERLAY_CONFIG", and "DTC_OVERLAY_FILE" fields from each # "CONF_FILE", "OVERLAY_CONFIG", and "DTC_OVERLAY_FILE" fields from each
# of the extra_args lines # of the extra_args lines
extracted_common = {} extracted_common: dict = {}
extracted_testsuite = {} extracted_testsuite: dict = {}
d = {} d: dict[str, Any] = {}
for k, v in self.common.items(): for k, v in self.common.items():
if k == "extra_args": if k == "extra_args":
# Pull out these fields and leave the rest # Pull out these fields and leave the rest