#!/usr/bin/python -tt """ The eventual plan is to have several steps for data to be transformed: * Application originates the data * The playbook/script formats the data for sending to the TASKS backend * A application-specific plugin in the Transformation Layer in the TASKS backend formats the data for the TASKS frontend * The TASKS backend performs templating on some of the fields * The TASKS frontend uses the data to display a nice UI for the user. For convert2rhel, this looks like: [convert2rhel run generates convert2rhel-pre-conversion.json] [playbook calls something like format_for_sending() to get the data ready for the TASKS backend.] [The plugin in the TASKS backend calls something like transform_for_frontend() to get the data ready for the frontend consumption.] [The TASKS backend templates fields] [The TASKS front end renders the data] For the MVP, there is no TASKS backend Transformation Layer so we are doing that in the playbook. """ import copy import json import sys def format_for_sending(convert2rhel_data): envelope = { "status": "ERROR", "report": "", "message": "urlopen error [Errno -2] Name or service not known", "backend_format_version": "1.0" } envelope["report_json"] = { "entries": convert2rhel_data } return envelope def reformat_message(message, action_id): new_message = copy.deepcopy(message) # Construct key new_message["key"] = "%s::%s" % (action_id, message["id"]) del new_message["id"] # Modifiers (for LEAPP status modifiers) new_message["modifiers"] = [] return new_message def transform_for_frontend(sent_data): # Note: Rodolfo, I'm making up the fields here. We do need something # to identify the format to the front end but I don't care what their names # are frontend_formatted = { "report_json": { "tasks_format_id": "andy-2023", "tasks_format_version": "1.0", "results": [] } } for field_name in ("status", "report", "message"): frontend_formatted["report_json"][field_name] = sent_data[field_name] for action_id, result in sent_data["report_json"]["entries"]["actions"].items(): # Format the resuls as a single list for message in result["messages"]: new_message = reformat_message(message, action_id) frontend_formatted["report_json"]["results"].append(new_message) frontend_formatted["report_json"]["results"].append(reformat_message(result["result"], action_id)) return frontend_formatted def main(): raw_convert2rhel_data = json.load(open(sys.argv[1])) data_to_send = format_for_sending(raw_convert2rhel_data) frontend_ready_data = transform_for_frontend(data_to_send) with open(sys.argv[2], "w") as f: frontend_ready_data = json.dump(frontend_ready_data, f) if __name__ == '__main__': main()