def result(): ana = p() if request.method == 'POST': filename_str = "" output_dir = os.path.join(config.get('Output', 'output_path')) + "/" + session['username'] # check if the post request has the file part if 'file' not in request.files: flash('No file part') return redirect(request.url) file = request.files['file'] # if user does not select file, browser also # submit a empty part without filename if file.filename == '': flash('No selected file') return redirect(request.url) if file and allowed_file(file.filename): filename = secure_filename(file.filename) filename_str = filename.split(".")[0] all_content = file.read() # save file into database file_id = dbOps.saveFileToDB(mgInstance.mongo, current_user.username, filename_str, all_content) # save file to path output_dir = output_dir + "/" + filename_str + "/" + str(file_id) with fileOps.safe_open_w(output_dir + "/" + filename) as f: f.write(all_content) # Parse XML and generate JSON ana.DM_File_Analyze(output_dir, {'DM_Input_type': "Simple_XML"}, filename_str) # Parse JSON and generate code model_display_data, server_url = generate_code.generate_all(filename_str, output_dir) authen_key = dbOps.getAuthenKey(mgInstance.mongo, session['username']) # Pass required data to the template description_data = { "model_display_data": model_display_data, "server_url": server_url, "authen_key" : authen_key } # write description_data into json file generate_code.write_description_to_file(filename_str, output_dir, description_data) # Render the template return redirect(url_for('main_bp.index')) else: flash('File type is not allowed') return redirect(request.url) return redirect(request.url)
def generate_diagram(self, path, format='svg'): file_path = path + '/' + self.__title + '.txt' with fileOps.safe_open_w(file_path) as o: o.write(self.output()) # Bo: TODO: add more format support subprocess.call(['plantweb', file_path]) subprocess.call([ 'mv', self.__title + '.' + format, path + '/' + self.__title + '.' + format ])
def update_instance(): if request.method == 'POST': oldfile_id = request.form['fileId'] domain_model_name = request.form['domainModelName'] file = request.files['file'] filename_str = "" output_dir = os.path.join(config.get('Output', 'output_path')) + "/" + session['username'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) filename_str = filename.split(".")[0] all_content = file.read() b64content = base64.standard_b64encode(all_content) # get bson object bincontent = binary.Binary(b64content) #print all_content # save file into database #newfile_id = dbOps.saveFileToDB(mongo, current_user.username, filename_str, all_content) #print newfile_id # save file to path output_dir = output_dir + "/" + filename_str + "/" + str(oldfile_id) with fileOps.safe_open_w(output_dir + "/" + filename) as f: f.write(all_content) f.close() username = session['username'] dbOps.updateInstanceDb(mongo, username, domain_model_name, oldfile_id, bincontent) file_dir = os.path.join(config.get('Output', 'output_path')) + "/" + username + "/" + domain_model_name + "/" + str(oldfile_id) # Parse XML and generate JSON ana.DM_File_Analyze(output_dir, {'DM_Input_type': "Simple_XML"}, filename_str) return redirect(url_for('index')) #return redirect(url_for('index')) return redirect(url_for('update_instance'))
def write_description_to_file(dm_name, output_dir, meta_data): file_location = output_dir + "/" + dm_name + "Modeldata" + ".json" md = json.dumps(meta_data) with fileOps.safe_open_w(file_location) as json_input: json_input.write(md) json_input.close()
def generate_server(server_ip, port, output_path, dm_name, json_data): """ Creates the server code file for the REST services :param server_ip: the ip of the REST api server. :param port: the port of the REST api server. :param output_path: the output path :param dm_name: the domain model name :json_data: the domain model json structure """ server_file = open("code_templates/" + "Server", "r") class_file = open("code_templates/" + "class_template", "r") db_connection_file = open("code_templates/" + "db_connection_template", "r") db_ops_file = open("code_templates/" + "db_ops_template", "r") authen_file = open("code_templates/" + "authen_template", "r") behavior_file = open("code_templates/" + "behavior", "r") package_json_file = open("code_templates/" + "package.json", "r") server_template = server_file.read() class_template = class_file.read() db_connection_template = db_connection_file.read() db_ops_template = db_ops_file.read() authen_template = authen_file.read() behavior_template = behavior_file.read() package_json_template = package_json_file.read() output_path = output_path + "/Server/" if not os.path.isdir(output_path): os.makedirs(output_path) elements = None for model_name in json_data: elements = json_data[model_name].get("elements") # deliver template for each model elem_names = [str(element["elementName"]) for element in elements] data = { "server_ip": server_ip, "port": port, "db_name": dm_name, "collection_names": str(elem_names) } content = replace_words(server_template, data) server_code = fileOps.safe_open_w(output_path + "Server" + ".js") server_code.write(content) server_code.close() package_json = fileOps.safe_open_w(output_path + "package" + ".json") package_json.write(package_json_template) package_json.close() for elem_name in elem_names: output_location = output_path + elem_name + ".js" with fileOps.safe_open_w(output_location) as output_file: output_file.write(class_template) # generate behaviors behaviors = [element["Behaviors"] for element in elements] for element in elements: for behavior in element["Behaviors"]: output_location = output_path + "Behaviors/" + str( behavior['name']) + ".js" data = { "entity": str(element["elementName"]), "behavior": str(behavior['name']) } content = replace_words(behavior_template, data) with fileOps.safe_open_w(output_location) as output_file: output_file.write(content) # generate server db connection file output_location = output_path + "db_connection" + ".js" configure_db(dm_name) db_user, db_password = edm_utils.generate_user_credentials(dm_name) data = {"db_user": db_user, "db_password": db_password, "db_name": dm_name} with fileOps.safe_open_w(output_location) as output_file: content = replace_words(db_connection_template, data) output_file.write(content) # generate server db dbOps file output_location = output_path + "dbOps" + ".js" with fileOps.safe_open_w(output_location) as output_file: output_file.write(db_ops_template) # generate server authentication file output_location = output_path + "authen" + ".js" with fileOps.safe_open_w(output_location) as output_file: output_file.write(authen_template) server_file.close() class_file.close() db_connection_file.close() db_ops_file.close() authen_file.close() behavior_file.close()
def save_to_file(self, path): with fileOps.safe_open_w(path + self.__title + '.txt') as o: o.write(self.output())
def update_instance(): ana = p() if request.method == 'POST': oldfile_id = request.form['fileId'] domain_model_name = request.form['domainModelName'] file = request.files['file'] filename_str = "" output_dir = os.path.join(config.get('Output', 'output_path')) + "/" + session['username'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) filename_str = filename.split(".")[0] all_content = file.read() b64content = base64.standard_b64encode(all_content) # get bson object bincontent = binary.Binary(b64content) #print all_content # save file into database #newfile_id = dbOps.saveFileToDB(mongo, current_user.username, filename_str, all_content) #print newfile_id # save file to path output_dir = output_dir + "/" + filename_str + "/" + str(oldfile_id) with fileOps.safe_open_w(output_dir + "/" + filename) as f: f.write(all_content) f.close() username = session['username'] dbOps.updateInstanceDb(mgInstance.mongo, username, domain_model_name, oldfile_id, bincontent) file_dir = os.path.join(config.get('Output', 'output_path')) + "/" + username + "/" + domain_model_name + "/" + str(oldfile_id) # Parse XML and generate JSON ana.DM_File_Analyze(output_dir, {'DM_Input_type': "Simple_XML"}, filename_str) # Parse JSON and generate code model_display_data, server_url = generate_code.generate_all(filename_str, output_dir) authen_key = dbOps.getAuthenKey(mgInstance.mongo, session['username']) # Pass required data to the template description_data = { "model_display_data": model_display_data, "server_url": server_url, "authen_key" : authen_key } # write description_data into json file generate_code.write_description_to_file(filename_str, output_dir, description_data) return redirect(url_for('main_bp.index')) #return redirect(url_for('index')) return redirect(url_for('main_bp.update_instance'))
def generate_server(server_ip, port, output_path, dm_name, json_data): """ Creates the server code file for the REST services :param server_ip: the ip of the REST api server. :param port: the port of the REST api server. :param output_path: the output path :param dm_name: the domain model name :json_data: the domain model json structure """ server_file = open("code_templates/" + "Server", "r") class_file = open("code_templates/"+ "class_template", "r") db_schema_file = open("code_templates/"+ "db_schema_template", "r") db_connection_file = open("code_templates/"+ "db_connection_template", "r") db_ops_file = open("code_templates/"+ "db_ops_template", "r") authen_file = open("code_templates/"+ "authen_template", "r") behavior_file = open("code_templates/"+ "behavior", "r") package_json_file = open("code_templates/"+ "package.json", "r") type_converter_file = open("code_templates/"+ "typeConverter", "r") server_template = server_file.read() class_template = class_file.read() db_schema_template = db_schema_file.read() db_connection_template = db_connection_file.read() db_ops_template = db_ops_file.read() authen_template = authen_file.read() behavior_template = behavior_file.read() package_json_template = package_json_file.read() type_converter_template = type_converter_file.read() output_path = output_path + "/Server/" if not os.path.isdir(output_path): os.makedirs(output_path) elements = None for model_name in json_data: elements = json_data[model_name].get("elements") # deliver template for each model elem_names = [str(element["elementName"]) for element in elements] data = {"server_ip": server_ip, "port": port, "db_name": dm_name, "collection_names" : str(elem_names)} content = replace_words(server_template, data) server_code = fileOps.safe_open_w(output_path + "Server" + ".js") server_code.write(content) server_code.close() package_json = fileOps.safe_open_w(output_path + "package" + ".json") package_json.write(package_json_template) package_json.close() # generate entities jp = JSONParser(json_data, dm_name) for entity_id, entity_name in jp.entities().items(): attribute_names = set() attribute_schema = [] for attribute in jp.findEntityAttributes(entity_name): attribute_names.add(attribute['name'].encode('utf-8')) data = { "attribute": attribute['name'], "type": attribute['details']['type'] } content = replace_words(db_schema_template, data) attribute_schema.append(content) data = { "collection_name" : entity_name, "attribute_names" : str(list(attribute_names)), "attribute_schema" : ',\n'.join(attribute_schema) } content = replace_words(class_template, data) output_location = output_path + entity_name + ".js" with fileOps.safe_open_w(output_location) as output_file: output_file.write(content) # generate behaviors behaviors = [element["Behaviors"] for element in elements] for element in elements : for behavior in element["Behaviors"]: output_location = output_path + "Behaviors/" + str(behavior['name']) + ".js" data = { "entity" : str(element["elementName"]), "behavior": str(behavior['name']) } content = replace_words(behavior_template, data) with fileOps.safe_open_w(output_location) as output_file: output_file.write(content) # generate server db connection file output_location = output_path + "db_connection" + ".js" configure_db(dm_name) db_user, db_password = edm_utils.generate_user_credentials(dm_name) data = {"db_user": db_user, "db_password": db_password, "db_name" : dm_name} with fileOps.safe_open_w(output_location) as output_file: content = replace_words(db_connection_template, data) output_file.write(content) # generate server db dbOps file output_location = output_path + "dbOps" + ".js" with fileOps.safe_open_w(output_location) as output_file: output_file.write(db_ops_template) # generate server authentication file output_location = output_path + "authen" + ".js" with fileOps.safe_open_w(output_location) as output_file: output_file.write(authen_template) # generate type converter file output_location = output_path + "typeConverter" + ".js" with fileOps.safe_open_w(output_location) as output_file: output_file.write(type_converter_template) server_file.close() class_file.close() class_file.close() db_connection_file.close() db_ops_file.close() authen_file.close() behavior_file.close() type_converter_file.close()