generate_experiment_link.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # main imports
  2. import base64
  3. import json
  4. import re
  5. import argparse
  6. import sys, os
  7. import requests
  8. # modules imports
  9. sys.path.insert(0, '') # trick to enable import of main folder module
  10. # config imports
  11. import links.config as cfg
  12. def encode_data(data):
  13. json_data = json.dumps(data)
  14. link_data = base64.b64encode(str(json_data).encode('utf-8'))
  15. return link_data
  16. def main():
  17. # getting all scenes available name
  18. scenes_list_url = cfg.default_host + '/api/listScenes'
  19. res = requests.get(scenes_list_url)
  20. data = json.loads(res.content)
  21. scenes_name = data['data']
  22. # getting all params
  23. parser = argparse.ArgumentParser(description="Compute links for scenes of experiment")
  24. parser.add_argument('--host', type=str, help='hostname choosen', default=cfg.default_host)
  25. parser.add_argument('--experiment', type=str, help="experiment name to use", choices=cfg.experiment_list, required=True)
  26. parser.add_argument('--experimentId', type=str, help="experiment id to use")
  27. parser.add_argument('--scenes', type=str, help="scene name list from %s" % scenes_name)
  28. parser.add_argument('--output', type=str, help="output filename", required=True)
  29. args = parser.parse_args()
  30. p_host = args.host
  31. p_experiment = args.experiment
  32. p_experiment_id = args.experimentId
  33. p_scenes = args.scenes.split(',')
  34. p_output = args.output
  35. # generate link for each scene
  36. links = []
  37. for scene in p_scenes:
  38. data = {
  39. 'hostConfig': p_host,
  40. 'experimentId': p_experiment_id,
  41. 'experimentName': p_experiment,
  42. 'sceneName': scene
  43. }
  44. generated_link_info = encode_data(data)
  45. generated_link = p_host + '/#/?q=' + bytes(generated_link_info).decode("utf-8")
  46. links.append(generated_link)
  47. filename_path = os.path.join(cfg.links_data_folder, p_output)
  48. if not os.path.exists(cfg.links_data_folder):
  49. os.makedirs(cfg.links_data_folder)
  50. with open(filename_path, 'w') as f:
  51. for id, link in enumerate(links):
  52. f.write(p_scenes[id] + ';' + p_experiment + ';' + p_experiment_id + ';' + link + '\n')
  53. print("Links are saved into.. %s" % filename_path)
  54. if __name__== "__main__":
  55. main()