Skip to content

Commit

Permalink
feat: provide a list of parsed files
Browse files Browse the repository at this point in the history
  • Loading branch information
cuinixam committed Oct 30, 2023
1 parent 3ed0790 commit 4f63a50
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
22 changes: 21 additions & 1 deletion src/kspl/kconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,23 @@ def __init__(
"""
if not k_config_model_file.is_file():
raise FileNotFoundError(f"File {k_config_model_file} does not exist.")
with working_directory(k_config_root_directory or k_config_model_file.parent):
self.k_config_root_directory = (
k_config_root_directory or k_config_model_file.parent
)
with working_directory(self.k_config_root_directory):
self._config = kconfiglib.Kconfig(k_config_model_file.absolute().as_posix())
self.parsed_files: List[Path] = self._collect_parsed_files()
if k_config_file:
if not k_config_file.is_file():
raise FileNotFoundError(f"File {k_config_file} does not exist.")
self._config.load_config(k_config_file, replace=False)
self.parsed_files.append(k_config_file)
self.elements = self._collect_elements()
self._elements_dict = {element.id: element for element in self.elements}

def get_parsed_files(self) -> List[Path]:
return self.parsed_files

def collect_config_data(self) -> ConfigurationData:
"""- creates the ConfigurationData from the KConfig configuration"""
elements = self.elements
Expand Down Expand Up @@ -211,3 +219,15 @@ def create_elements_tree(

def find_element(self, name: str) -> Optional[EditableConfigElement]:
return self._elements_dict.get(name, None)

def _collect_parsed_files(self) -> List[Path]:
"""Collects all parsed files from the KConfig instance and returns them as a list of absolute paths"""
parsed_files: List[Path] = []
for file in self._config.kconfig_filenames:
file_path = Path(file)
parsed_files.append(
file_path
if file_path.is_absolute()
else self.k_config_root_directory / file_path
)
return parsed_files
33 changes: 23 additions & 10 deletions tests/test_kconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def test_create_configuration_data(tmp_path: Path) -> None:
default n
"""
)
config = KConfig(feature_model_file).collect_config_data()
kconfig = KConfig(feature_model_file)
assert set(kconfig.get_parsed_files()) == {feature_model_file}
config = kconfig.collect_config_data()
assert config.elements[0].type == ConfigElementType.STRING
assert config.elements[0].value == "John Smith"
assert config.elements[1].type == ConfigElementType.BOOL
Expand Down Expand Up @@ -282,7 +284,9 @@ def test_define_tristate_choices(tmp_path: Path) -> None:
),
)

config = KConfig(feature_model_file, user_config).collect_config_data()
kconfig = KConfig(feature_model_file, user_config)
assert set(kconfig.get_parsed_files()) == {feature_model_file, user_config}
config = kconfig.collect_config_data()
assert config.elements == [
ConfigElement(ConfigElementType.BOOL, "APP_VERSION_1", TriState.Y),
ConfigElement(ConfigElementType.BOOL, "APP_VERSION_2", TriState.N),
Expand All @@ -309,19 +313,19 @@ def test_config_including_other_config(tmp_path: Path) -> None:
source "common/common.txt"
""",
)
file = tmp_path / "common/common.txt"
file.parent.mkdir(parents=True)
file.write_text(
common_file = tmp_path / "common/common.txt"
common_file.parent.mkdir(parents=True)
common_file.write_text(
"""
config COMMON_BOOL
bool "You can select COMMON_BOOL"
default n
source "new/new.txt"
"""
)
file = tmp_path / "new/new.txt"
file.parent.mkdir(parents=True)
file.write_text(
new_file = tmp_path / "new/new.txt"
new_file.parent.mkdir(parents=True)
new_file.write_text(
"""
config NEW_BOOL
bool "You can select NEW_BOOL"
Expand All @@ -339,7 +343,14 @@ def test_config_including_other_config(tmp_path: Path) -> None:
"""
),
)
config = KConfig(feature_model_file, user_config).collect_config_data()
kconfig = KConfig(feature_model_file, user_config)
assert set(kconfig.get_parsed_files()) == {
feature_model_file,
common_file,
new_file,
user_config,
}
config = kconfig.collect_config_data()
assert config.elements == [
ConfigElement(ConfigElementType.BOOL, "FIRST_BOOL", TriState.Y),
ConfigElement(ConfigElementType.STRING, "FIRST_NAME", "Dude"),
Expand Down Expand Up @@ -384,7 +395,9 @@ def test_config_including_other_configs_based_on_env_vars(tmp_path: Path) -> Non
),
)
os.environ["COMMON_PATH"] = "common"
config = KConfig(feature_model_file, user_config).collect_config_data()
kconfig = KConfig(feature_model_file, user_config)
assert set(kconfig.get_parsed_files()) == {feature_model_file, file, user_config}
config = kconfig.collect_config_data()
assert config.elements == [
ConfigElement(ConfigElementType.BOOL, "FIRST_BOOL", TriState.Y),
ConfigElement(ConfigElementType.STRING, "FIRST_NAME", "Dude"),
Expand Down

0 comments on commit 4f63a50

Please sign in to comment.