Skip to content

Commit

Permalink
autotailor: Refactor import_json_tailoring
Browse files Browse the repository at this point in the history
  • Loading branch information
evgenyz committed Feb 26, 2024
1 parent 73e6fea commit dbab0f7
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions utils/autotailor
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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():
Expand Down

0 comments on commit dbab0f7

Please sign in to comment.