From dbab0f752d705d1fb823434b65441fcfe6c46339 Mon Sep 17 00:00:00 2001 From: Evgeny Kolesnikov Date: Mon, 26 Feb 2024 14:49:09 +0100 Subject: [PATCH] autotailor: Refactor import_json_tailoring --- utils/autotailor | 51 +++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/utils/autotailor b/utils/autotailor index 89890a7fb4..4bd751dd37 100755 --- a/utils/autotailor +++ b/utils/autotailor @@ -274,22 +274,7 @@ class Tailoring: with open(location, "w") if location != "-" else sys.stdout as f: f.write(pretty_xml) - def import_json_tailoring(self, json_tailoring): - with open(json_tailoring, "r") as jf: - all_tailorings = json.load(jf) - - if 'profiles' in all_tailorings and all_tailorings['profiles']: - if len(all_tailorings['profiles']) > 1: - raise ValueError("The autotailor tool currently does not support multi-profile JSON tailoring.") - tailoring = all_tailorings['profiles'][0] - else: - raise ValueError("JSON Tailoring does not define any profiles.") - - self.extends = tailoring["base_profile_id"] - - self.profile_id = tailoring.get("id", self.profile_id) - self.profile_title = tailoring.get("title", self.profile_title) - + def _import_groups_from_tailoring(self, tailoring): if "groups" in tailoring: for group_id, props in tailoring["groups"].items(): if "evaluate" in props: @@ -298,6 +283,15 @@ class Tailoring: else: self.groups_to_unselect.append(group_id) + def _import_variables_from_tailoring(self, tailoring): + if "variables" in tailoring: + for variable_id, props in tailoring["variables"].items(): + if "value" in props: + self.add_value_change(variable_id, props["value"]) + if "option_id" in props: + self.change_value_attribute(variable_id, "selector", props["option_id"]) + + def _import_rules_from_tailoring(self, tailoring): if "rules" in tailoring: for rule_id, props in tailoring["rules"].items(): if "evaluate" in props: @@ -309,12 +303,25 @@ class Tailoring: if attr in props: self.change_rule_attribute(rule_id, attr, props[attr]) - if "variables" in tailoring: - for variable_id, props in tailoring["variables"].items(): - if "value" in props: - self.add_value_change(variable_id, props["value"]) - if "option_id" in props: - self.change_value_attribute(variable_id, "selector", props["option_id"]) + def import_json_tailoring(self, json_tailoring): + with open(json_tailoring, "r") as jf: + all_tailorings = json.load(jf) + + if 'profiles' in all_tailorings and all_tailorings['profiles']: + if len(all_tailorings['profiles']) > 1: + raise ValueError("The autotailor tool currently does not support multi-profile JSON tailoring.") + tailoring = all_tailorings['profiles'][0] + else: + raise ValueError("JSON Tailoring does not define any profiles.") + + self.extends = tailoring["base_profile_id"] + + self.profile_id = tailoring.get("id", self.profile_id) + self.profile_title = tailoring.get("title", self.profile_title) + + self._import_groups_from_tailoring(tailoring) + self._import_rules_from_tailoring(tailoring) + self._import_variables_from_tailoring(tailoring) def get_parser():