upload_image_as_cs.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """
  2. Script to load an actual image file as concrete syntax model.
  3. """
  4. import sys
  5. import os
  6. import argparse
  7. import base64
  8. import imghdr
  9. import commons
  10. import wrappers.modelverse as mv
  11. from sketchUI.mvops import new_concrete_syntax_model
  12. from sketchUI.graphics_node_item import IconType
  13. def query_yes_no(question, default="yes"):
  14. """Ask a yes/no question via raw_input() and return their answer.
  15. "question" is a string that is presented to the user.
  16. "default" is the presumed answer if the user just hits <Enter>.
  17. It must be "yes" (the default), "no" or None (meaning
  18. an answer is required of the user).
  19. The "answer" return value is True for "yes" or False for "no".
  20. """
  21. valid = {"yes": True, "y": True, "ye": True,
  22. "no": False, "n": False}
  23. if default is None:
  24. prompt = " [y/n] "
  25. elif default == "yes":
  26. prompt = " [Y/n] "
  27. elif default == "no":
  28. prompt = " [y/N] "
  29. else:
  30. raise ValueError("invalid default answer: {}".format(default))
  31. while True:
  32. sys.stdout.write(question + prompt)
  33. choice = raw_input().lower()
  34. if default is not None and choice == '':
  35. return valid[default]
  36. elif choice in valid:
  37. return valid[choice]
  38. else:
  39. sys.stdout.write("Please respond with 'yes' or 'no' "
  40. "(or 'y' or 'n').\n")
  41. def main(img_file, node_type):
  42. csm = new_concrete_syntax_model(node_type, IconType.IMAGE)
  43. img_class = mv.instantiate(csm, "Image")
  44. img = open(img_file, "rb").read()
  45. img_b64 = base64.b64encode(img)
  46. mv.attr_assign(csm, img_class, "data", img_b64)
  47. print("Verify ...")
  48. mv.verify(csm, "formalisms/consynMM")
  49. print("Done")
  50. if __name__ == "__main__":
  51. parser = argparse.ArgumentParser()
  52. parser.add_argument("-f", help="The image file to upload")
  53. parser.add_argument("-t", help="The type this image file represents")
  54. args = parser.parse_args()
  55. img_file = args.f
  56. node_type = args.t
  57. if not img_file or not node_type:
  58. parser.print_help()
  59. sys.exit()
  60. if not os.path.isfile(img_file):
  61. print("No such file: {}".format(img_file))
  62. sys.exit()
  63. if not imghdr.what(img_file):
  64. print("No image file: {}".format(img_file))
  65. sys.exit()
  66. mv.init()
  67. mv.login("admin", "admin")
  68. all_consyn_models = commons.all_consyn_models()
  69. if node_type in [x.split("/")[-1] for x in all_consyn_models]:
  70. ans = query_yes_no("Already a concrete syntax for type {}. Overwrite?".format(node_type))
  71. if ans:
  72. mv.model_delete("models/consyn/"+node_type)
  73. else:
  74. print("bye")
  75. sys.exit(0)
  76. main(img_file, node_type)