def export_xml(cls): from mosaicode.system import System as System System() code_templates = System.get_code_templates() for code_template in code_templates: path = System.get_user_dir() + "/extensions/" path = path + code_template.language + "/" CodeTemplatePersistence.save_xml(code_templates[code_template])
def delete_code_template(cls, code_template_key): from mosaicode.system import System code_templates = System.get_code_templates() if code_template_key not in code_templates: return False code_template = code_templates[code_template_key] if code_template.file is not None: os.remove(code_template.file) return code_template.file
def export_xml(cls): from mosaicode.system import System as System System() code_templates = System.get_code_templates() for key in code_templates: path = System.get_user_dir() path = os.path.join(path, 'extensions', code_templates[key].language, 'codetemplates') CodeTemplatePersistence.save_xml(code_templates[key], path)
def __update(self): System() code_template_list = [] code_templates = System.get_code_templates() for x in code_templates: code_template_list.append([x]) code_template_list.sort() self.tree_store.clear() for x in code_template_list: self.tree_store.append(None, x)
def delete_code_template(cls, code_template_key): from mosaicode.system import System code_templates = System.get_code_templates() if code_template_key not in code_templates: System.log("Error: This code template does not exist") return False code_template = code_templates[code_template_key] if code_template.file is not None: os.remove(code_template.file) else: System.log("Error: This code template does not have a file.") return code_template.file
def export_code_template(cls, output): from mosaicode.system import System as System System() code_templates = System.get_code_templates() for code_template in code_templates: path = System.get_user_dir() + "/extensions/" path = path + code_template.language + "/" if output == "xml": CodeTemplatePersistence.save_xml(code_templates[code_template]) else: CodeTemplatePersistence.save_python( code_templates[code_template], path)
def delete_code_template(cls, code_template_key): from mosaicode.system import System System() code_templates = System.get_code_templates() if code_template_key not in code_templates: return False, "This code template does not exist in System" code_template = code_templates[code_template_key] if code_template.file is not None: os.remove(code_template.file) return True, "File " + code_template.file + " deleted" else: return False, "Could not remove " + code_template.file
def __get_code_generator(self, diagram): if diagram.language is None: message = "You shall not generate code of an empty diagram!" Dialog().message_dialog("Error", message, self.main_window) return None template_list = [] code_templates = System.get_code_templates() for key in code_templates: if code_templates[key].language == diagram.language: template_list.append(code_templates[key]) if len(template_list) == 0: message = "Generator not available for the language " + diagram.language + "." Dialog().message_dialog("Error", message, self.main_window) return None if len(template_list) == 1: return CodeGenerator(diagram, template_list[0]) select = SelectCodeTemplate(self.main_window, template_list) code_template = select.get_value() return CodeGenerator(diagram, code_template)
def load(cls, diagram): """ This method load the xml file that represents the diagram. :param diagram: diagram to load. :return: operation status (True or False) """ from mosaicode.control.diagramcontrol import DiagramControl dc = DiagramControl(diagram) # load the diagram parser = XMLParser(diagram.file_name) # Loading from tags zoom = parser.getTag(tag_name).getTag("zoom") if zoom is not None: zoom = zoom.getAttr("value") else: zoom = parser.getTagAttr(tag_name, "zoom") diagram.zoom = float(zoom) language = parser.getTag(tag_name).getTag("language") if language is not None: language = language.getAttr("value") else: language = parser.getTagAttr(tag_name, "language") diagram.language = language code_template = parser.getTag(tag_name).getTag("code_template") if code_template is not None and hasattr(code_template, "value"): code_template = code_template.getAttr("value") if code_template not in System.get_code_templates(): System.log("Code Template " + code_template + " not found") else: code_template = System.get_code_templates()[code_template] diagram.code_template = deepcopy(code_template) parser = parser.getTag(tag_name) # new version load blocks = parser.getTag("blocks").getChildTags("block") system_blocks = System.get_blocks() for block in blocks: block_type = block.getAttr("type") if block_type not in system_blocks: System.log("Block " + block_type + " not found") continue block_id = int(block.getAttr("id")) collapsed = False if hasattr(block, "collapsed"): collapsed = block.collapsed == "True" if hasattr(block, "x"): x = block.x if hasattr(block, "y"): y = block.y position = block.getTag("position") if position is not None: x = position.getAttr("x") y = position.getAttr("y") properties = block.getChildTags("property") props = {} for prop in properties: if hasattr(prop, 'key') and hasattr(prop, 'value'): props[prop.key] = prop.value new_block = deepcopy(system_blocks[block_type]) new_block.set_properties(props) new_block.id = block_id new_block.x = float(x) new_block.y = float(y) new_block.is_collapsed = collapsed dc.add_block(new_block) connections = parser.getTag("connections") connections = connections.getChildTags("connection") for conn in connections: if not hasattr(conn, 'from_block'): continue elif not hasattr(conn, 'to_block'): continue try: from_block = diagram.blocks[int(conn.from_block)] to_block = diagram.blocks[int(conn.to_block)] from_block_out = from_block.ports[int( conn.getAttr("from_out"))] to_block_in = to_block.ports[int(conn.getAttr("to_in"))] except: continue connection = ConnectionModel(diagram, from_block, from_block_out, to_block, to_block_in) dc.add_connection(connection) comments = parser.getTag("comments") if comments is not None: comments = comments.getChildTags("comment") for com in comments: comment = CommentModel() comment.x = float(com.getAttr("x")) comment.y = float(com.getAttr("y")) properties = com.getChildTags("property") props = {} for prop in properties: if hasattr(prop, 'key') and hasattr(prop, 'value'): props[prop.key] = prop.value comment.set_properties(props) dc.add_comment(comment) authors = parser.getTag("authors") if authors is not None: authors = authors.getChildTags("author") for author in authors: auth = AuthorModel() auth.name = author.getAttr("author") auth.license = author.getAttr("license") auth.date = author.getAttr("date") diagram.authors.append(auth) diagram.redraw() return True
def load(cls, diagram): """ This method load the JSON file that represents the diagram. :param diagram: diagram to load. :return: operation status (True or False) """ if os.path.exists(diagram.file_name) is False: System.log("Problem loading the diagram. File does not exist.") return None if not isinstance(diagram, DiagramModel): System.log("Problem loading the diagram. Is this a Diagram?") return False from mosaicode.control.diagramcontrol import DiagramControl dc = DiagramControl(diagram) # load the diagram data = "" try: data_file = open(diagram.file_name, 'r') data = json.load(data_file) data_file.close() if data["data"] != "DIAGRAM": System.log("Problem loading the diagram. Are you sure this is a valid file?") return False if "zoom" in data: diagram.zoom = float(data["zoom"]) if "language" in data: diagram.language = data["language"] # Loading Code Template if "code_template" in data: code_template_data = data["code_template"] if "type" in code_template_data: code_template = code_template_data["type"] if code_template not in System.get_code_templates(): System.log("Code Template " + code_template + " not found") else: code_template = System.get_code_templates()[code_template] diagram.code_template = deepcopy(code_template) if "properties" in code_template_data: properties = code_template_data["properties"] props = {} for prop in properties: props[prop["key"]] = prop["value"] diagram.code_template.set_properties(props) # Loading Blocks if "blocks" in data: blocks = data["blocks"] system_blocks = System.get_blocks() for block in blocks: block_type = block["type"] if block_type not in system_blocks: System.log("Block " + block_type + " not found") continue block_id = int(block["id"]) collapsed = block["collapsed"] x = block["x"] y = block["y"] properties = block["properties"] props = {} for prop in properties: props[prop["key"]] = prop["value"] new_block = deepcopy(system_blocks[block_type]) new_block.set_properties(props) new_block.id = block_id new_block.x = float(x) new_block.y = float(y) new_block.is_collapsed = collapsed dc.add_block(new_block) # Loading connections connections = data["connections"] for conn in connections: try: from_block = diagram.blocks[int(conn["from_block"])] to_block = diagram.blocks[int(conn["to_block"])] port_index = int(conn["from_out"]) if port_index >= 0 and port_index < len(from_block.ports): from_block_out = from_block.ports[port_index] if from_block_out.is_input(): System.log("Diagram error: Output port is an input port") continue else: System.log("Diagram error: invalid output port index " + str(port_index)) continue port_index = int(conn["to_in"]) if port_index >= 0 and port_index < len(to_block.ports): to_block_in = to_block.ports[port_index] if not to_block_in.is_input(): System.log("Diagram error: Input port is an output port") continue else: System.log("Diagram error: invalid input port index " + str(port_index)) continue except Exception as e: System.log("Diagram error:" + str(e)) continue connection = ConnectionModel(diagram, from_block, from_block_out, to_block, to_block_in) dc.add_connection(connection) # Loading comments comments = data["comments"] for com in comments: comment = CommentModel() comment.x = float(com["x"]) comment.y = float(com["y"]) properties = com["properties"] props = {} for prop in properties: props[prop["key"]] = prop["value"] comment.set_properties(props) dc.add_comment(comment) # Loading authors authors = data["authors"] for author in authors: auth = AuthorModel() auth.name = author["author"] auth.license = author["license"] auth.date = author["date"] diagram.authors.append(auth) diagram.redraw() except Exception as e: System.log(e) return False return True
def test_get_code_templates(self): System.get_code_templates()
def __init__(self, code_template_manager, code_template_name): Gtk.Dialog.__init__(self, _("Code Template Editor"), code_template_manager, 0, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_SAVE, Gtk.ResponseType.OK)) self.code_template_manager = code_template_manager system_code_template = System.get_code_templates() if code_template_name is not None: self.code_template = system_code_template[code_template_name] else: self.code_template = CodeTemplate() self.main_control = self self.set_default_size(800, 300) self.tabs = Gtk.Notebook() self.tabs.set_scrollable(True) box = self.get_content_area() box.pack_start(self.tabs, True, True, 0) common_tab = Gtk.VBox() code_tab = Gtk.VBox() command_tab = Gtk.VBox() self.tabs.append_page(common_tab, Gtk.Label(_("Common"))) self.tabs.append_page(code_tab, Gtk.Label(_("Code"))) self.tabs.append_page(command_tab, Gtk.Label(_("Command"))) # First Tab: Common properties self.name = StringField({"label": _("Name")}, self.__edit) self.language = StringField({"label": _("Language")}, self.__edit) self.extension = StringField({"label": _("Extension")}, self.__edit) self.type = StringField({"label": _("Type")}, None) self.description = StringField({"label": _("Description")}, None) self.code_parts = StringField({"label": _("Code Parts")}, None) common_tab.pack_start(self.language, False, False, 0) common_tab.pack_start(self.name, False, False, 0) common_tab.pack_start(self.extension, False, False, 0) common_tab.pack_start(self.type, False, False, 0) common_tab.pack_start(self.description, False, False, 0) common_tab.pack_start(self.code_parts, False, False, 0) # Second Tab: Code properties # Button bar button_bar = Gtk.HBox() code_tab.pack_start(button_bar, False, False, 0) self.__populate_combos(button_bar) self.code = CodeField({"label": _("")}, None) code_tab.pack_end(self.code, True, True, 0) # Third Tab: Command properties self.command = CodeField({"label": _("")}, None) command_tab.pack_start(self.command, True, True, 0) if code_template_name is not None: System() self.name.set_value(self.code_template.name) self.type.set_value(self.code_template.type) self.description.set_value(self.code_template.description) self.language.set_value(self.code_template.language) self.command.set_value(self.code_template.command) self.extension.set_value(self.code_template.extension) self.code.set_value(self.code_template.code) code_parts_string = ', '.join(self.code_template.code_parts) self.code_parts.set_value(code_parts_string) self.show_all() result = self.run() if result == Gtk.ResponseType.OK: self.__save() self.close() self.destroy()
def print_templates(cls): # This method is used by the launcher class code_templates = System.get_code_templates() for template in code_templates: print "--------------------- " CodeTemplateControl.print_template(code_templates[template])
def create_code_template(self): code_template = CodeTemplate() code_template.name = "webaudio" code_template.type = "Test" code_template.language = "javascript" code_template.command = "python -m webbrowser -t $dir_name$index.html\n" code_template.description = "Javascript / webaudio code template" code_template.code_parts = [ "onload", "function", "declaration", "execution", "html" ] code_template.properties = [{ "name": "title", "label": "Title", "value": "Title", "type": MOSAICODE_STRING }] code_template.files["index.html"] = r""" <html> <head> <meta http-equiv="Cache-Control" content="no-store" /> <!-- $author$ $license$ --> <title>$prop[title]$</title> <link rel="stylesheet" type="text/css" href="theme.css"> <script src="functions.js"></script> <script> $single_code[function]$ function loadme(){ $single_code[onload]$ return; } var context = new (window.AudioContext || window.webkitAudioContext)(); //declaration block $code[declaration]$ //execution $code[execution]$ //connections $connections$ </script> </head> <body onload='loadme();'> $code[html]$ </body> </html> """ code_template.files["theme.css"] = r""" /* Developed by: $author$ */ html, body { background: #ffeead; color: #ff6f69; } h1, p { color: #ff6f69; } #navbar a { color: #ff6f69; } .item { background: #ffcc5c; } button { background: #ff6f69; color: #ffcc5c; } """ code_template.files["functions.js"] = r""" /* Developed by: $author$ */ $single_code[function]$ """ System.get_code_templates()[code_template.type] = code_template return code_template
def export_extensions(self): from mosaicode.system import System as System System() result = True folder = "extension-" + datetime.datetime.now().strftime("%Y-%m-%d") # Export ports ports = System.get_ports() for key in ports: path = System.get_user_dir() path = os.path.join(path, folder, ports[key].language, 'ports') result = result and PortPersistence.save(ports[key], path) # Export Blocks blocks = System.get_blocks() result = True for key in blocks: path = System.get_user_dir() path = os.path.join(path, folder, blocks[key].language, 'blocks', blocks[key].extension, blocks[key].group) result = result and BlockPersistence.save(blocks[key], path) # Export Code Templates code_templates = System.get_code_templates() result = True for key in code_templates: path = System.get_user_dir() path = os.path.join(path, folder, code_templates[key].language, 'codetemplates') result = result and CodeTemplatePersistence.save( code_templates[key], path) # Export examples path = System.get_user_dir() path = os.path.join(path, "extensions") examples = System.get_examples() for example in examples: relpath = os.path.relpath(example, path) path = System.get_user_dir() path = os.path.join(path, folder, relpath) os.makedirs(os.path.dirname(path), exist_ok=True) shutil.copy2(example, path) # Create a zip file to the extension path = System.get_user_dir() path = os.path.join(path, folder+".zip") zip_file = zipfile.ZipFile(path, 'w') path = System.get_user_dir() path = os.path.join(path, folder) for folderName, subfolders, filenames in os.walk(path): for filename in filenames: filePath = os.path.join(folderName, filename) #create complete filepath of file in directory # Add file to zip zip_file.write(filePath, os.path.relpath(filePath, path)) zip_file.close() path = System.get_user_dir() path = os.path.join(path, folder) shutil.rmtree(path) # create a publish file filename = "resource.txt" path = System.get_user_dir() path = os.path.join(path, filename) f = open(path, "w") f.write(folder + ".zip") f.close() if result: MessageDialog( "Success", "File " + folder + ".zip created successfully!", self.main_window).run() else: MessageDialog( "Error", "Could not export extension", self.main_window).run() return result
def export_python(cls): from mosaicode.system import System as System System() code_templates = System.get_code_templates() for code_template in code_templates: CodeTemplatePersistence.save_python(code_templates[code_template])