Skip to content

Commit

Permalink
Implement function that translates QUARK 1 config to QUARK 2 config f…
Browse files Browse the repository at this point in the history
…ormat.
  • Loading branch information
Philipp Ross authored and Philipp Ross committed Nov 8, 2023
1 parent b25dc7d commit 3dd80e9
Showing 1 changed file with 82 additions and 1 deletion.
83 changes: 82 additions & 1 deletion src/ConfigManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,95 @@ def query_module(self, module: Core, module_friendly_name: str) -> ConfigModule:

}

@staticmethod
def is_legacy_config(config: dict) -> bool:
"""
Checks if a QUARK 1 config was provided
:param config: Valid config file
:type config: dict
:rtype: bool
:return: true if provided config is QUARK 1 config
"""
if "mapping" in config.keys():
logging.warning("Looks like you have provided a legacy config file")
return True

return False

@staticmethod
def translate_legacy_config(config: dict) -> BenchmarkConfig:
"""
Translates the QUARK 1 config format to QUARK 2 format
:param config: QUARK 1 config
:type config: dict
:return: Translated Config
:rtype: BenchmarkConfig
"""

logging.info("Trying to translate QUARK 1 config to QUARK 2 config format")
translated_config = {key: config[key] for key in ["application", "repetitions"]}
translated_config["application"]["submodules"] = []

for key, value in config["mapping"].items():
# Direct mapping does not exist anymore in QUARK 2.0
if key.lower() == "direct":
translated_config["application"]["submodules"].extend(
ConfigManager.translate_legacy_config_helper(value, "solver"))
else:
translated_config["application"]["submodules"].append(
{
"name": key,
"config": value["config"],
"submodules": ConfigManager.translate_legacy_config_helper(value, "solver")
}
)

return translated_config

@staticmethod
def translate_legacy_config_helper(config_part: dict, module_key: str) -> list:
"""
Helper function for translate_legacy_config, which translates the QUARK 1 config format to QUARK 2 format
:param config_part: part of a config
:type config_part: dict
:param module_key: Module key: mapping, solver or device
:type module_key: str
:return: translated config_part
:rtype: list
"""

next_module_key = None
if module_key.lower() == "solver":
next_module_key = "device"

result = []
for item in config_part[module_key]:
result.append(
{
"name": item["name"],
"config": item["config"],
"submodules": ConfigManager.translate_legacy_config_helper(item, next_module_key)
if next_module_key is not None else []
}
)

return result

def set_config(self, config: BenchmarkConfig) -> None:
"""
In case the user provides a config file, this function is used to set the config.
:param config: Valid config file
:param config: Valid config file
:type config: BenchmarkConfig
:rtype: None
"""

if ConfigManager.is_legacy_config(config):
config = ConfigManager.translate_legacy_config(config)

self.config = config

def load_config(self, app_modules: list[dict]):
Expand Down

0 comments on commit 3dd80e9

Please sign in to comment.