def __start_server(self): self.port = System.get_preferences().port while self.httpd is None: try: path = System.get_preferences().default_directory my_path = os.curdir os.chdir(path) System.log("Trying to run the server") handler = SimpleHTTPServer.SimpleHTTPRequestHandler SocketServer.ThreadingTCPServer.allow_reuse_address = True self.httpd = SocketServer.ThreadingTCPServer(('', self.port), handler) self.httpd_thread = threading.Thread(target=self.httpd.serve_forever) self.httpd_thread.setDaemon(True) self.httpd_thread.start() count = 0 ip_aux = 0 System.log("Server running on ip and port:") while self.ip != ip_aux: ip_aux = self.ip self.ip = self.get_ip(count) count = count + 1 if ip_aux != self.ip: System.log(str(self.ip) + ":" + str(self.port)) os.chdir(my_path) except: self.port = self.port + 1
def __resize(self, data): width, height = self.get_size() System.get_preferences().width = width System.get_preferences().height = height System.get_preferences().hpaned_work_area = self.hpaned_work_area.get_position() System.get_preferences().vpaned_bottom = self.vpaned_bottom.get_position() System.get_preferences().vpaned_left = self.vpaned_left.get_position() self.work_area.resize(data)
def set_recent_files(self, file_name): if file_name in System.get_preferences().recent_files: System.get_preferences().recent_files.remove(file_name) System.get_preferences().recent_files.insert(0, file_name) if len(System.get_preferences().recent_files) > 10: System.get_preferences().recent_files.pop() self.main_window.menu.update_recent_files( System.get_preferences().recent_files) PreferencesPersistence.save(System.get_preferences(), System.get_user_dir())
def move(self, x, y): """ This method move a block. Parameters: * **(x,y)** (:class:`float<float>`) Returns: * **Types** (:class:`float<float>`) """ new_x = x - (x % System.get_preferences().grid) new_y = y - (y % System.get_preferences().grid) self.translate(new_x, new_y)
def test_update_flow(self): self.connector.input = None self.connector.update_flow() point = (0,0) self.connector.update_flow(point) self.connector.input = Block(self.create_diagram(), self.create_block()) self.connector.input.move(100,100) self.connector.input_port = Port() System.get_preferences().connection = "Curve" self.connector.update_flow() System.get_preferences().connection = "Line" self.connector.update_flow() System.get_preferences().connection = "Square" self.connector.update_flow()
def __on_key_press(self, widget, event=None): grid = System.get_preferences().grid modifier_mask = Gtk.accelerator_get_default_mod_mask() event.state = event.state & modifier_mask if event.state == Gdk.ModifierType.CONTROL_MASK: if event.keyval == Gdk.KEY_Up: self.move_selected(0, -grid*5) return True if event.keyval == Gdk.KEY_Down: self.move_selected(0, grid*5) return True if event.keyval == Gdk.KEY_Left: self.move_selected(-grid*5, 0) return True if event.keyval == Gdk.KEY_Right: self.move_selected(grid*5, 0) return True if event.keyval == Gdk.KEY_Delete and self.focus: self.delete() return True if event.keyval == Gdk.KEY_Up: self.move_selected(0, -grid) return True if event.keyval == Gdk.KEY_Down: self.move_selected(0, grid) return True if event.keyval == Gdk.KEY_Left: self.move_selected(-grid, 0) return True if event.keyval == Gdk.KEY_Right: self.move_selected(grid, 0) return True
def init(self): # Load plugins self.update_blocks() for plugin in System.get_plugins(): plugin.load(self.main_window) self.main_window.menu.update_recent_files( System.get_preferences().recent_files) self.main_window.menu.update_examples(System.get_list_of_examples())
def open(self, file_name): """ This method open a file. """ diagram = Diagram(self.main_window) self.main_window.work_area.add_diagram(diagram) DiagramControl(diagram).load(file_name) diagram.redraw() diagram.set_modified(False) if file_name in System.get_preferences().recent_files: System.get_preferences().recent_files.remove(file_name) System.get_preferences().recent_files.insert(0, file_name) if len(System.get_preferences().recent_files) > 10: System.get_preferences().recent_files.pop() self.main_window.menu.update_recent_files( System.get_preferences().recent_files)
def __draw_grid(self): if self.show_grid: width = self.main_window.get_size()[0] height = self.main_window.get_size()[1] i = 0 while i < height: GooCanvas.CanvasPath(parent=self.get_root_item(), stroke_color="#F9F9F9", data="M 0 " + str(i) + " L " + str(width) + " " + str(i) + "", line_width=0.8) i = i + System.get_preferences().grid i = 0 while i < width: GooCanvas.CanvasPath(parent=self.get_root_item(), stroke_color="#F9F9F9", data="M " + str(i) + " 0 L " + str(i) + " " + str(height) + "", line_width=0.8) i = i + System.get_preferences().grid
def __init__(self, main_window): """ This method is the constructor. """ Gtk.Dialog.__init__(self, title=_("Preferences"), transient_for=main_window) self.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL) self.add_buttons(Gtk.STOCK_OK, Gtk.ResponseType.OK) self.main_window = main_window self.properties = System.get_preferences() box = self.get_content_area() box.set_border_width(3) self.tabs = Gtk.Notebook() box.add(self.tabs) # User preferences # ---------------------------------------------------------------------- self.user_preferences_tab = Gtk.Box() self.user_preferences_tab.set_border_width(10) label = Gtk.Label(label=_("User Preferences")) self.tabs.append_page(self.user_preferences_tab, label) self.__create_user_preferences_tab() # Default directory # ---------------------------------------------------------------------- self.default_directory_tab = Gtk.Box() self.default_directory_tab.set_border_width(10) label = Gtk.Label(label=_("Default Directory")) self.tabs.append_page(self.default_directory_tab, label) self.__create_default_directory_tab() # Grid Preferences # ---------------------------------------------------------------------- self.grid_preferences_tab = Gtk.Box() self.grid_preferences_tab.set_border_width(10) label = Gtk.Label(label=_("Grid Preferences")) self.tabs.append_page(self.grid_preferences_tab, label) self.__create_grid_preferences_tab() # Network Preferences # ---------------------------------------------------------------------- self.network_preferences_tab = Gtk.Box() self.network_preferences_tab.set_border_width(10) label = Gtk.Label(label=_("Network Preferences")) self.tabs.append_page(self.network_preferences_tab, label) self.__create_network_preferences_tab() self.show_all()
def exit(self, widget=None, data=None): """ This method close main window. Returns: * **Types** (:class:`boolean<boolean>`) """ PreferencesPersistence.save(System.get_preferences(), System.get_user_dir()) if self.main_window.work_area.close_tabs(): Gtk.main_quit() else: return True
def test_load_save(self): System() prefs = System.get_preferences() PreferencesPersistence.save(prefs, "/tmp") PreferencesPersistence.save(prefs, "/etc") PreferencesPersistence.load("/tmp") PreferencesPersistence.load("/not") os.remove("/tmp/" + prefs.conf_file_path) code_template = self.create_code_template() code_template.name = "configuration" CodeTemplatePersistence.save_xml(code_template, "/tmp/") result = PreferencesPersistence.load("/tmp") file_name = "/tmp/" + code_template.name + ".xml" os.remove(file_name)
def update_recent_files(self): """ This method update recent files. """ list_of_recent_files = System.get_preferences().recent_files for widget in self.recent_files_menu.get_children(): self.recent_files_menu.remove(widget) if list_of_recent_files is None or len(list_of_recent_files) == 0: menu_item = Gtk.MenuItem.new_with_label(_("<empty>")) self.recent_files_menu.append(menu_item) self.recent_files_menu.show_all() return False for index, recent_file in enumerate(list_of_recent_files): menu_item = Gtk.MenuItem.new_with_label(recent_file) self.recent_files_menu.append(menu_item) menu_item.connect("activate", self.__load_recent, None) self.recent_files_menu.show_all()
def __init__(self): """ This method is constructor. """ System() Gtk.Window.__init__(self, title="Mosaicode") self.resize(System.get_preferences().width, System.get_preferences().height) self.main_control = MainControl(self) # GUI components self.menu = Menu(self) self.toolbar = Toolbar(self) self.search = SearchBar(self) self.block_notebook = BlockNotebook(self) self.property_box = PropertyBox(self) self.work_area = WorkArea(self) self.status = Status(self) self.diagram_menu = DiagramMenu() self.menu.add_help() self.block_menu = BlockMenu() System.set_log(self.status) # vbox main # ----------------------------------------------------- # | Menu # ----------------------------------------------------- # | Toolbar # ----------------------------------------------------- # | V Paned bottom # ----------------------------------------------------- # First Vertical Box vbox_main = Gtk.VBox() self.add(vbox_main) vbox_main.pack_start(self.menu, False, True, 0) vbox_main.pack_start(self.toolbar, False, False, 0) self.vpaned_bottom = Gtk.Paned.new(Gtk.Orientation.VERTICAL) vbox_main.add(self.vpaned_bottom) # vpaned_bottom # ----------------------------------------------------- # | hpaned_work_area # ===================================================== # | status # ----------------------------------------------------- self.hpaned_work_area = Gtk.HPaned() self.hpaned_work_area.connect("accept-position", self.__resize) self.hpaned_work_area.set_position( System.get_preferences().hpaned_work_area) self.vpaned_bottom.add1(self.hpaned_work_area) self.vpaned_bottom.add2(self.__create_frame(self.status)) self.vpaned_bottom.set_position(System.get_preferences().vpaned_bottom) self.vpaned_bottom.set_size_request(50, 50) # hpaned_work_area # ----------------------------------------------------- # | vbox_left || work_area # ----------------------------------------------------- vbox_left = Gtk.VBox(homogeneous=False, spacing=0) self.hpaned_work_area.add1(vbox_left) self.hpaned_work_area.add2(self.work_area) # vbox_left # ----------------------------------------------------- # |search # ----------------------------------------------------- # |vpaned_left # ----------------------------------------------------- vbox_left.pack_start(self.search, False, False, 0) self.vpaned_left = Gtk.VPaned() vbox_left.pack_start(self.vpaned_left, True, True, 0) # vpaned_left # ----------------------------------------------------- # |blocks_tree_view # ===================================================== # |property_box # ----------------------------------------------------- self.vpaned_left.add1(self.__create_frame(self.block_notebook)) self.vpaned_left.add2(self.__create_frame(self.property_box)) self.vpaned_left.set_position(System.get_preferences().vpaned_left) self.connect("key-press-event", self.__on_key_press) self.connect("check-resize", self.__resize) self.connect("delete-event", self.main_control.exit) self.main_control.init() # Load the plugin from mosaicode.plugins.extensionsmanager.extensionsmanager \ import ExtensionsManager as em em().load(self)
def save(cls, diagram): """ This method save a file. Returns: * **Types** (:class:`boolean<boolean>`) """ x = { "source": "JSON", "data": "DIAGRAM", "version": System.VERSION, "zoom": diagram.zoom, "language": diagram.language, "code_template": {}, "blocks": [], "connections": [], "comments": [], "authors": [] } if diagram.code_template: code_template_data = { "type": diagram.code_template.type, "properties": [] } props = diagram.code_template.properties for prop in props: code_template_data["properties"].append({ "key": str(prop["name"]), "value": str(prop["value"]) }) x["code_template"] = code_template_data for block_id in diagram.blocks: block = diagram.blocks[block_id] pos = block.get_position() block_data = { "type": block.type, "id": block.id, "collapsed": block.is_collapsed, "x": pos[0], "y": pos[1], "properties": [] } props = block.get_properties() for prop in props: block_data["properties"].append({ "key": str(prop["name"]), "value": str(prop["value"]) } ) x["blocks"].append(block_data) for connector in diagram.connectors: x["connections"].append({ "from_block": connector.output.id, "from_out": int(connector.output_port.index), "to_block": connector.input.id, "to_in": int(connector.input_port.index) }) for comment in diagram.comments: pos = comment.get_position() comment_data = { "x": pos[0], "y": pos[1], "properties": [] } props = comment.get_properties() for prop in props: comment_data["properties"].append({ "key": str(prop["name"]), "value": str(prop["value"]) }) x["comments"].append(comment_data) auth = AuthorModel() auth.name = System.get_preferences().author auth.license = System.get_preferences().license auth.date = str(datetime.now()) diagram.authors.insert(0,auth) for author in diagram.authors: x["authors"].append({ "author": author.name, "license": author.license, "date": author.date }) try: save_file = open(str(diagram.file_name), "w") save_file.write(json.dumps(x, indent=4)) save_file.close() except IOError as e: System.log(e.strerror) return False, e.strerror diagram.set_modified(False) return True, "Success"
def test_get_preferences(self): System.get_preferences()
def init(self): self.update_blocks() self.main_window.menu.update_recent_files( System.get_preferences().recent_files) self.main_window.menu.update_examples(System.get_examples())
def add_recent_files(self, file_name): System.add_recent_files(file_name) self.main_window.menu.update_recent_files() PreferencesPersistence.save(System.get_preferences(), System.get_user_dir())
def __generate_file_code(self, code): """ This method generate the block code. """ # We first substitute data from the code template itself code = code.replace("$author$", System.get_preferences().author) code = code.replace("$license$", System.get_preferences().license) code = code.replace("$dir_name$", System.get_dir_name(self.diagram)) code = code.replace("$command$", self.diagram.code_template.command) code = code.replace("$name$", self.diagram.code_template.name) code = code.replace("$description$", self.diagram.code_template.description) for prop in self.diagram.code_template.properties: my_key = "$prop[" + prop.get("name") + "]$" value = str(prop.get("value")) code = code.replace(my_key, value) # Then we substitute the code parts with blocks for key in self.codes: # Check for single_code generation code_name = "$single_code["+ key + "]$" if code_name in code: temp_header = [] temp_code = "" for header_code in self.codes[key]: if header_code not in temp_header: temp_header.append(header_code) for header_code in temp_header: temp_code += header_code code = code.replace(code_name, temp_code) # Check for code generation code_name = "$code["+ key + "]$" if code_name in code: temp_code = "" for x in self.codes[key]: temp_code += x code = code.replace(code_name, temp_code) # Check for code + connections generation code_name = "$code["+ key + ", connection]$" if code_name in code: temp_code = "" for x,y in zip(self.codes[key], self.connections): temp_code += x temp_code += y code = code.replace(code_name, temp_code) # Check for connections + code generation code_name = "$code[connection, "+ key + "]$" if code_name in code: temp_code = "" for x,y in zip(self.connections, self.codes[key]): temp_code += x temp_code += y code = code.replace(code_name, temp_code) # Replace only connection connection_block = "" for conn in self.connections: connection_block += conn + "\n" code = code.replace("$connections$", connection_block) return code
def test_add_recent_files(self): self.main_control.add_recent_files("Test") self.main_control.add_recent_files("Test") for i in range(0, 20): System.get_preferences().recent_files.append("Some File") self.main_control.add_recent_files("Test")
def __update_draw(self): """ This method update draw. """ path = "" x0 = self.__from_point[0] y0 = self.__from_point[1] x1 = self.__to_point[0] y1 = self.__to_point[1] if System.get_preferences().connection == "Curve": c1x = x1 c1y = y0 c2x = x0 c2y = y1 path += "M " + str(x0) + " " + str(y0) path += " C" if x0 + 25 > x1: c1x = x0 + x0 - x1 c2x = x1 - x0 + x1 path += " " + str(c1x) + " " + str(c1y) path += " " + str(c2x) + " " + str(c2y) path += " " + str(x1) + " " + str(y1) elif System.get_preferences().connection == "Line": path += "M " + str(x0) + " " + str(y0) path += " L " + str(x1) + " " + str(y1) else: # System.get_preferences().connection == "Square": x0_shift = (self.output_port.type_index * 4) x1_shift = 0 if self.input_port is not None: x1_shift = self.input_port.type_index * 4 # svg M L bezier curve # Move to output port starting point / first horizontal line path += "M " + str(x0) + " " + str(y0) # Line to start point + 25 on x + output type index -- path += " L " + str(x0 + 25 + x0_shift) + " " + str(y0) # First vertical line path += " L " + str(x0 + 25 + x0_shift) + " " + str((y0 + y1) / 2) # Middle horizontal line if second block is on the left if x1 - 25 - x1_shift < x0 + 25 + x0_shift: path += " L " + str((x1 + x0) / 2 - x1_shift) + " " + str( (y0 + y1) / 2) path += " L " + str(x1 - 25 - x1_shift) + " " + str( (y0 + y1) / 2) else: path += " L " + str(x0 + 25 + x0_shift) + " " + str(y1) path += " L " + str(x1 - 25 - x1_shift) + " " + str(y1) # End Point path += " L " + str(x1) + " " + str(y1) if "Line" not in self.__widgets: color = self.output_port.color widget = GooCanvas.CanvasPath(parent=self, stroke_color=color, data=path) self.__widgets["Line"] = widget else: self.__widgets["Line"].set_property("data", path) self.__update_state()
def adjust_position(self): position = self.get_position() grid = System.get_preferences().grid new_x = position[0] - position[0] % grid new_y = position[1] - position[1] % grid self.translate(new_x - position[0], new_y - position[1])
def __init__(self): self.httpd = None self.ip = None self.port = System.get_preferences().port self.httpd_thread = None
def save(cls, diagram): """ This method save a file. Returns: * **Types** (:class:`boolean<boolean>`) """ parser = XMLParser() parser.addTag(tag_name) parser.setTagAttr(tag_name, 'version', value=System.VERSION) parser.setTagAttr(tag_name, 'zoom', value=diagram.zoom) parser.setTagAttr(tag_name, 'language', value=diagram.language) parser.appendToTag(tag_name, 'code_template', value=diagram.code_template) parser.appendToTag(tag_name, 'blocks') for block_id in diagram.blocks: block = diagram.blocks[block_id] pos = block.get_position() parser.appendToTag('blocks', 'block', type=block.type, id=block.id, collapsed=block.is_collapsed, x=pos[0], y=pos[1]) props = block.get_properties() for prop in props: parser.appendToLastTag('block', 'property', key=str(prop["name"]), value=str(prop["value"])) parser.appendToTag(tag_name, 'connections') for connector in diagram.connectors: parser.appendToTag('connections', 'connection', from_block=connector.output.id, from_out=int(connector.output_port.index), to_block=connector.input.id, to_in=int(connector.input_port.index)) parser.appendToTag(tag_name, 'comments') for comment in diagram.comments: pos = comment.get_position() parser.appendToTag('comments', 'comment', x=pos[0], y=pos[1]) props = comment.get_properties() for prop in props: parser.appendToLastTag('comment', 'property', key=str(prop["name"]), value=str(prop["value"])) auth = AuthorModel() auth.name = System.get_preferences().author auth.license = System.get_preferences().license auth.date = datetime.now() diagram.authors.insert(0, auth) parser.appendToTag(tag_name, 'authors') for author in diagram.authors: parser.appendToTag('authors', 'author', author=author.name, license=author.license, date=author.date) try: save_file = open(str(diagram.file_name), "w") save_file.write(parser.prettify()) save_file.close() except IOError as e: System.log(e.strerror) return False, e.strerror diagram.set_modified(False) return True, "Success"
def test_update_recent_files(self): self.menu.update_recent_files() System.get_preferences().recent_files = None self.assertEquals(System.get_preferences().recent_files, None) self.menu.update_recent_files()