Пример #1
0
    def set_properties(self, data):
        """
        This method set properties of each block.

            Parameters:
                * **data**
        """
        CommentModel.set_properties(self, data)
Пример #2
0
    def test_new(self):
        comment = CommentModel()
        comment.set_properties({"text": "Novo Teste"})
        comment.set_properties({"Not here": "Novo Teste"})
        str(comment)

        comment.set_properties(None)
        str(comment)

        comment.properties = []
        str(comment)

        comment.properties = None
        str(comment)

        comment.properties = None
        comment.set_properties({"text": "Novo Teste"})
        str(comment)

        comment.properties = [{"test": "Test"}]
        str(comment)

        comment = CommentModel(comment)
Пример #3
0
    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
Пример #4
0
    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