def update_blocks(self): """ This methods update all blocks loaded for each library. :param blocks: blocks to update :return: None """ languages = [] while self.get_n_pages() > 0: self.remove_page(0) self.tabs.pop() blocks = System.get_blocks() for x in blocks: instance = blocks[x] name = instance.language name += "/" + instance.extension if name in languages: continue languages.append(name) for language in languages: treeview = BlocksTreeView(self.main_window, language, blocks) self.append_page(treeview, Gtk.Label.new(language)) self.tabs.append(treeview) self.show_all()
def export_python(cls): from mosaicode.system import System System() blocks = System.get_blocks() for block in blocks: print "Exporting block " + block BlockPersistence.save_python(blocks[block])
def export_python(cls): from mosaicode.system import System System() blocks = System.get_blocks() for block in blocks: path = System.get_user_dir() + "/extensions/" path = path + block.language + "/" + block.framework + "/" BlockPersistence.save_python(blocks[block], path)
def update_blocks(self): blocks = System.get_blocks() for widget in self.block_menu.get_children(): self.block_menu.remove(widget) block_list = [] # Copy the list to sort for key in blocks: instance = blocks[key] block_list.append(instance) # time to populate for block in sorted(block_list): # first, the language submenu language_menu_item = self.__get_child_by_name( self.block_menu, block.language) if language_menu_item is None: language_menu_item = Gtk.MenuItem.new_with_label( block.language) language_menu_item.set_name(block.language) self.block_menu.append(language_menu_item) language_menu = Gtk.Menu() language_menu_item.set_submenu(language_menu) else: language_menu = language_menu_item.get_submenu() extension_menu_item = self.__get_child_by_name( language_menu, block.extension) if extension_menu_item is None: extension_menu_item = Gtk.MenuItem.new_with_label( block.extension) extension_menu_item.set_name(block.extension) language_menu.append(extension_menu_item) extension_menu = Gtk.Menu() extension_menu_item.set_submenu(extension_menu) else: extension_menu = extension_menu_item.get_submenu() group_menu_item = self.__get_child_by_name(extension_menu, block.group) if group_menu_item is None: group_menu_item = Gtk.MenuItem.new_with_label(block.group) group_menu_item.set_name(block.group) extension_menu.append(group_menu_item) group_menu = Gtk.Menu() group_menu_item.set_submenu(group_menu) else: group_menu = group_menu_item.get_submenu() menu_item = Gtk.MenuItem.new_with_label(block.type) group_menu.append(menu_item) menu_item.connect("activate", self.__add_block, block) self.block_menu.show_all()
def export_block(cls, output): from mosaicode.system import System as System System() blocks = System.get_blocks() for block in blocks: path = System.get_user_dir() + "/extensions/" path = path + block.language + "/" + block.framework + "/" if output == "xml": BlockPersistence.save_xml(blocks[block], path) else: BlockPersistence.save_python(blocks[block], path)
def delete_block(cls, block_key): from mosaicode.system import System blocks = System.get_blocks() if block_key not in blocks: return False block = blocks[block_key] if block.file is not None: os.remove(block.file) return True else: return False
def export_xml(cls): from mosaicode.system import System as System System() blocks = System.get_blocks() for key in blocks: path = System.get_user_dir() path = os.path.join(path, 'extensions', blocks[key].language, 'block', blocks[key].extension, blocks[key].group) BlockPersistence.save_xml(blocks[key], path)
def test_load_save(self): file_name = get_temp_file() + ".mscd" diagram_control = self.create_diagram_control() diagram = diagram_control.diagram diagram.file_name = "." DiagramPersistence.save(diagram) comment = Comment(diagram) diagram.file_name = file_name DiagramControl.add_comment(diagram, comment) System() blocks = System.get_blocks() for key in blocks: block1 = Block(diagram, blocks[key]) DiagramControl.add_block(diagram, block1) source = None source_port = None for key in diagram.blocks: for port in diagram.blocks[key].ports: if not port.is_input(): source = diagram.blocks[key] source_port = port break sink = None sink_port = None for key in diagram.blocks: for port in diagram.blocks[key].ports: if port.is_input(): sink = diagram.blocks[key] sink_port = port break connection = ConnectionModel(diagram, source, source_port, sink, sink_port) DiagramControl.add_connection(diagram, connection) block0 = self.create_block(diagram_control) DiagramPersistence.save(diagram) System.remove_block(blocks.values()[0]) result = DiagramPersistence.load(diagram) os.remove(file_name)
def __edit_clicked(self, args): """ This method monitors if the button delete was clicked. Parameters: * **args** """ diagram = self.main_window.work_area.get_current_diagram() if diagram is None: return False for key in diagram.blocks: if not diagram.blocks[key].is_selected: continue BlockEditor(self.main_window, System.get_blocks()[diagram.blocks[key].type])
def create_block(self, diagram_control=None): if diagram_control is None: diagram_control = self.create_diagram_control() block_model = BlockModel() port0 = Port() port0.label = "Test0" port0.conn_type = Port.OUTPUT port0.name = "Test0" port0.type = "Test" port0.index = 0 port1 = Port() port1.label = "Test1" port1.conn_type = Port.INPUT port1.name = "Test1" port1.type = "Test" port1.index = 1 block_model.ports = [port0, port1] block_model.help = "Test" block_model.label = "Test" block_model.color = "200:200:25:150" block_model.group = "Test" block_model.codes = {"code0": "Test", "Code1": "Test", "Code2": "Test"} block_model.type = "Test" block_model.language = "Test" block_model.properties = [{ "name": "test", "label": "Test", "value": "0", "type": MOSAICODE_FLOAT }] block_model.extension = "Test" block_model.file = None block = Block(diagram_control.diagram, block_model) System.get_blocks()[block.type] = block return block
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 # load the diagram parser = XMLParser(diagram.file_name) zoom = parser.getTag(tag_name).getTag("zoom").getAttr("value") diagram.zoom = float(zoom) try: language = parser.getTag(tag_name).getTag("language").getAttr( "value") diagram.language = language except: pass # new version load blocks = parser.getTag(tag_name).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" position = block.getTag("position") 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 DiagramControl.add_block(diagram, new_block) connections = parser.getTag(tag_name).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) DiagramControl.add_connection(diagram, connection) comments = parser.getTag(tag_name).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")) comment.text = com.getAttr("text") diagram.comments.append(comment) return True
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_update_blocks(self): blocks = System.get_blocks() self.assertIsNone(self.block_notebook.update_blocks(blocks), "Failed to update blocks")
def print_blockmodels(cls): # This method is used by the launcher class blocks = System.get_blocks() for block in blocks: print "--------------------- " BlockControl.print_block(blocks[block])
def update_blocks(self): System.reload() blocks = System.get_blocks() self.main_window.menu.update_blocks(blocks) self.main_window.block_notebook.update_blocks(blocks)
def test_update_blocks(self): self.menu.update_blocks(System.get_blocks()) self.menu.update_blocks(System.get_blocks())
def setUp(self): self.blockstreeview = BlocksTreeView(self.create_main_window(), "javascript", System.get_blocks())
def update(self): System() self.block_notebook.update_blocks(System.get_blocks())
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 setUp(self): self.block_notebook = BlockNotebook(self.create_main_window()) blocks = System.get_blocks() self.block_notebook.update_blocks(blocks)