diff --git a/src/ahriman/core/configuration/configuration.py b/src/ahriman/core/configuration/configuration.py index a861a1b9..d1aa9ebf 100644 --- a/src/ahriman/core/configuration/configuration.py +++ b/src/ahriman/core/configuration/configuration.py @@ -63,6 +63,7 @@ class Configuration(configparser.RawConfigParser): >>> path, repository_id = configuration.check_loaded() """ + _LEGACY_ARCHITECTURE_SPECIFIC_SECTIONS = ["web"] ARCHITECTURE_SPECIFIC_SECTIONS = ["alpm", "build", "sign"] SYSTEM_CONFIGURATION_PATH = Path(sys.prefix) / "share" / "ahriman" / "settings" / "ahriman.ini" converters: dict[str, Callable[[str], Any]] # typing guard @@ -160,25 +161,6 @@ class Configuration(configparser.RawConfigParser): configuration.merge_sections(repository_id) return configuration - @staticmethod - def override_sections(section: str, repository_id: RepositoryId) -> list[str]: - """ - extract override sections - - Args: - section(str): section name - repository_id(RepositoryId): repository unique identifier - - Returns: - list[str]: architecture and repository specific sections in correct order - """ - # the valid order is global < per architecture < per repository < per repository and architecture - return [ - Configuration.section_name(section, repository_id.architecture), # architecture specific override - Configuration.section_name(section, repository_id.name), # override with repository name - Configuration.section_name(section, repository_id.name, repository_id.architecture), # both - ] - @staticmethod def section_name(section: str, *suffixes: str | None) -> str: """ @@ -315,7 +297,7 @@ class Configuration(configparser.RawConfigParser): """ self.repository_id = repository_id - for section in self.ARCHITECTURE_SPECIFIC_SECTIONS: + for section in self.ARCHITECTURE_SPECIFIC_SECTIONS + self._LEGACY_ARCHITECTURE_SPECIFIC_SECTIONS: for specific in self.override_sections(section, repository_id): if self.has_section(specific): # if there is no such section it means that there is no overrides for this arch, @@ -330,6 +312,26 @@ class Configuration(configparser.RawConfigParser): continue self.remove_section(foreign) + def override_sections(self, section: str, repository_id: RepositoryId) -> list[str]: + """ + extract override sections + + Args: + section(str): section name + repository_id(RepositoryId): repository unique identifier + + Returns: + list[str]: architecture and repository specific sections in correct order + """ + if repository_id.is_empty: # special case, guess sections from configuration + return sorted(specific for specific in self.sections() if specific.startswith(f"{section}:")) + # the valid order is global < per architecture < per repository < per repository and architecture + return [ + Configuration.section_name(section, repository_id.architecture), # architecture specific override + Configuration.section_name(section, repository_id.name), # override with repository name + Configuration.section_name(section, repository_id.name, repository_id.architecture), # both + ] + def reload(self) -> None: """ reload configuration if possible or raise exception otherwise diff --git a/tests/ahriman/core/configuration/test_configuration.py b/tests/ahriman/core/configuration/test_configuration.py index 777d5c1b..61a1409b 100644 --- a/tests/ahriman/core/configuration/test_configuration.py +++ b/tests/ahriman/core/configuration/test_configuration.py @@ -61,17 +61,6 @@ def test_from_path_file_missing(repository_id: RepositoryId, mocker: MockerFixtu read_mock.assert_called_once_with(configuration.SYSTEM_CONFIGURATION_PATH) -def test_override_sections(repository_id: RepositoryId) -> None: - """ - must correctly generate override section names - """ - assert Configuration.override_sections("build", repository_id) == [ - "build:x86_64", - "build:aur-clone", - "build:aur-clone:x86_64", - ] - - def test_section_name(configuration: Configuration) -> None: """ must return architecture specific group @@ -384,6 +373,26 @@ def test_merge_sections_priority(configuration: Configuration) -> None: assert configuration.get("build", "key4") == "key4_value1" +def test_override_sections(configuration: Configuration, repository_id: RepositoryId) -> None: + """ + must correctly generate override section names + """ + assert configuration.override_sections("build", repository_id) == [ + "build:x86_64", + "build:aur-clone", + "build:aur-clone:x86_64", + ] + + +def test_override_sections_empty(configuration: Configuration) -> None: + """ + must look up for sections if repository identifier is empty + """ + configuration.set_option("web:x86_64", "port", "8080") + configuration.set_option("web:i686", "port", "8080") + assert configuration.override_sections("web", RepositoryId("", "")) == ["web:i686", "web:x86_64"] + + def test_reload(configuration: Configuration, mocker: MockerFixture) -> None: """ must reload configuration