def create_block(self, diagram_control=None): if diagram_control is None: diagram_control = self.create_diagram_control() System() block_model = BlockModel() System.add_port(self.create_port()) block_model.ports = [{ "type": "Test", "label": "Click", "conn_type": "Input", "name": "0" }, { "type": "Test", "label": "Click", "conn_type": "Output", "name": "1" }, { "type": "Test", "label": "Click", "conn_type": "Input", "name": "2" }, { "type": "Test", "label": "Click", "conn_type": "Output", "name": "3" }] 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 result = BlockControl.load_ports(block_model, System.get_ports()) System.add_block(block_model) self.assertEquals(result[1], "Success") self.assertEquals(result[0], True) self.assertEquals(len(block_model.ports), 4) block = Block(diagram_control.diagram, block_model) self.assertEquals(len(block.ports), 4) return block
def load_xml(cls, file_name): """ This method loads the block from XML file. Returns: * **Types** (:class:`boolean<boolean>`) """ if os.path.exists(file_name) is False: return None parser = XMLParser(file_name) if parser.getTag(tag_name) is None: return None block = BlockModel() block.type = parser.getTagAttr(tag_name, "type") block.language = parser.getTagAttr(tag_name, "language") block.extension = parser.getTagAttr(tag_name, "extension") block.help = parser.getTagAttr(tag_name, "help") block.color = parser.getTagAttr(tag_name, "color") block.label = parser.getTagAttr(tag_name, "label") block.group = parser.getTagAttr(tag_name, "group") codes = parser.getTag(tag_name).getTag("codes") if codes: codes = codes.getChildTags("code") for code in codes: block.codes[code.getAttr("name_")] = code.getAttr("value") props = parser.getTag(tag_name).getTag("properties") if props: props = props.getChildTags("property") for prop in props: block.properties.append(ast.literal_eval(prop.getAttr("value"))) ports = parser.getTag(tag_name).getTag("ports") if ports: ports = ports.getChildTags("port") for port in ports: dict_port = {} dict_port["type"] = str(port.getAttr("type_")) dict_port["name"] = str(port.getAttr("name_")) dict_port["label"] = str(port.getAttr("label")) dict_port["conn_type"] = str(port.getAttr("conn_type")) block.ports.append(dict_port) block.file = file_name if block.type == "mosaicode.model.blockmodel": return None return block
def load(cls, file_name): """ This method loads the block from JSON file. Returns: * **Types** (:class:`boolean<boolean>`) """ if os.path.exists(file_name) is False: return None data = "" block = BlockModel() try: data_file = open(file_name, 'r') data = json.load(data_file) data_file.close() if data["data"] != "BLOCK": return None block.type = data["type"] block.language = data["language"] block.extension = data["extension"] block.help = data["help"] block.color = data["color"] block.label = data["label"] block.group = data["group"] codes = data["codes"] if codes: for code in codes: block.codes[code["name"]] = code["code"] props = data["properties"] for prop in props: block.properties.append(prop) ports = data["ports"] for port in ports: block.ports.append(port) block.file = file_name except: return None if block.type == "mosaicode.model.blockmodel": return None return block
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" port1 = Port() port1.label = "Test1" port1.conn_type = Port.INPUT port1.name = "Test1" port1.type = "Test" 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.maxIO = 2 block_model.language = "language" block_model.properties = [{"name": "test", "label": "Test", "type": MOSAICODE_FLOAT }] block_model.extension = "Test" block_model.file = None block = Block(diagram_control.diagram, block_model) return block
def test_load_save(self): # Create, save and load a empty block block = BlockModel() block.label = "Empty block" persistence = BlockPersistence.save_xml(block, "/tmp/") file_name = "/tmp/" + block.label + ".xml" assert persistence result = BlockPersistence.load_xml(file_name) assert result is None os.remove(file_name) # Create, save and load a block # save(path is None) and block.file = "/tmp/" + block.label + ".xml" diagram_control = self.create_diagram_control() block = self.create_block(diagram_control) block.file = "/tmp/" + block.label + ".xml" persistence = BlockPersistence.save_xml(block) assert persistence result = BlockPersistence.load_xml(block.file) os.remove(block.file) assert isinstance(result, BlockModel) # Create e save # save(path is None) and block.file = Nove diagram_control = self.create_diagram_control() block = self.create_block(diagram_control) block.file = None persistence = BlockPersistence.save_xml(block) assert not persistence # Create, save and load a block # save(path not None) diagram_control = self.create_diagram_control() block = self.create_block(diagram_control) persistence = BlockPersistence.save_xml(block, "/tmp/") file_name = "/tmp/" + block.label + ".xml" assert persistence result = BlockPersistence.load_xml(file_name) os.remove(file_name) assert isinstance(result, BlockModel) assert result.type == block.type assert result.language == block.language assert result.extension == block.extension assert result.help == block.help assert result.color == block.color assert result.label == block.label assert result.group == block.group assert result.file is not None #assert result.maxOI == block.maxOI # assert codes for key, value in result.codes.items(): assert key in block.codes.keys() assert result.codes[key] == block.codes[key] # assert properties for result_prop in result.properties: match_prop = False for block_prop in block.properties: match_attr = False for result_key, result_value in result_prop.items(): if result_key in block_prop: if result_value == block_prop[result_key]: match_attr = True continue else: match_attr = False break else: match_attr = False break if match_attr: match_prop = True break assert match_prop # assert ports for result_port in result.ports: assert isinstance(result_port, dict) match = False for block_port in block.ports: if result_port["type"] == block_port.type and \ result_port["name"] == block_port.name and \ result_port["label"] == block_port.label and \ result_port["conn_type"] == block_port.conn_type: match = True assert match