""" Script to load an actual image file as concrete syntax model. """ import sys import os import argparse import base64 import imghdr import commons import wrappers.modelverse as mv from sketchUI.mvops import new_concrete_syntax_model from sketchUI.graphics_node_item import IconType def query_yes_no(question, default="yes"): """Ask a yes/no question via raw_input() and return their answer. "question" is a string that is presented to the user. "default" is the presumed answer if the user just hits . It must be "yes" (the default), "no" or None (meaning an answer is required of the user). The "answer" return value is True for "yes" or False for "no". """ valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False} if default is None: prompt = " [y/n] " elif default == "yes": prompt = " [Y/n] " elif default == "no": prompt = " [y/N] " else: raise ValueError("invalid default answer: {}".format(default)) while True: sys.stdout.write(question + prompt) choice = raw_input().lower() if default is not None and choice == '': return valid[default] elif choice in valid: return valid[choice] else: sys.stdout.write("Please respond with 'yes' or 'no' " "(or 'y' or 'n').\n") def main(img_file, node_type): csm = new_concrete_syntax_model(node_type, IconType.IMAGE) img_class = mv.instantiate(csm, "Image") img = open(img_file, "rb").read() img_b64 = base64.b64encode(img) mv.attr_assign(csm, img_class, "data", img_b64) print("Verify ...") mv.verify(csm, "formalisms/consynMM") print("Done") if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("-f", help="The image file to upload") parser.add_argument("-t", help="The type this image file represents") args = parser.parse_args() img_file = args.f node_type = args.t if not img_file or not node_type: parser.print_help() sys.exit() if not os.path.isfile(img_file): print("No such file: {}".format(img_file)) sys.exit() if not imghdr.what(img_file): print("No image file: {}".format(img_file)) sys.exit() mv.init() mv.login("admin", "admin") all_consyn_models = commons.all_consyn_models() if node_type in [x.split("/")[-1] for x in all_consyn_models]: ans = query_yes_no("Already a concrete syntax for type {}. Overwrite?".format(node_type)) if ans: mv.model_delete("models/consyn/"+node_type) else: print("bye") sys.exit(0) main(img_file, node_type)