generate_experiment_link_config.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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('--output', type=str, help="output filename", required=True)
  28. args = parser.parse_args()
  29. p_host = args.host
  30. p_experiment = args.experiment
  31. p_experiment_id = args.experimentId
  32. p_output = args.output
  33. p_scenes = None
  34. # generate link for each scene
  35. if p_experiment in cfg.scenes_list:
  36. p_scenes = cfg.scenes_list_calibration[p_experiment]
  37. else:
  38. print("Pas de scènes disponibles par défaut dans cette expérience")
  39. exit(0)
  40. links = []
  41. for scene in p_scenes:
  42. data = {
  43. 'hostConfig': p_host,
  44. 'experimentId': p_experiment_id,
  45. 'experimentName': p_experiment,
  46. 'sceneName': scene
  47. }
  48. generated_link_info = encode_data(data)
  49. generated_link = p_host + '/#/?q=' + bytes(generated_link_info).decode("utf-8")
  50. links.append(generated_link)
  51. filename_path = os.path.join(cfg.links_data_folder, p_output)
  52. if not os.path.exists(cfg.links_data_folder):
  53. os.makedirs(cfg.links_data_folder)
  54. with open(filename_path, 'w') as f:
  55. for id, link in enumerate(links):
  56. f.write(p_scenes[id] + ';' + p_experiment + ';' + p_experiment_id + ';' + link + '\n')
  57. print("Links are saved into.. %s" % filename_path)
  58. if __name__== "__main__":
  59. main()