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_xml(cls, file_name): """ This method loads the code_template from XML file. Returns: * **Types** (:class:`boolean<boolean>`) """ # load the code_template if os.path.exists(file_name) is False: return None if os.path and os.path.isdir(file_name): return None parser = XMLParser(file_name) if parser.getTag(tag_name) is None: return None ct = parser.getTag(tag_name) code_template = CodeTemplate() code_template.name = parser.getTagAttr(tag_name, "name") code_template.type = parser.getTagAttr(tag_name, "type") code_template.description = parser.getTagAttr(tag_name, "description") code_template.language = parser.getTagAttr(tag_name, "language") code_template.command = parser.getTagAttr(tag_name, "command") parts = parser.getTag(tag_name) parts = parts.getTag("code_parts") if parts: parts = parts.getChildTags("code_part") for part in parts: code_template.code_parts.append(part.getAttr("value")) parts = parser.getTag(tag_name) parts = parts.getTag("files") if parts: parts = parts.getChildTags("file") for part in parts: code_template.files[part.getAttr("name_")] = part.getAttr( "value") parts = parser.getTag(tag_name) parts = parts.getTag("properties") if parts: parts = parts.getChildTags("property") for part in parts: code_template.properties.append( ast.literal_eval(part.getAttr("value"))) if code_template.name == "": return None return code_template
def load(cls, diagram): # load the diagram parser = XMLParser(diagram.file_name) zoom = parser.getTag("mosaicode").getTag("zoom").getAttr("value") diagram.zoom = float(zoom) try: language = parser.getTag("mosaicode").getTag("language").getAttr( "value") diagram.language = language except: pass # new version load blocks = parser.getTag("mosaicode").getTag("blocks").getChildTags( "block") for block in blocks: block_type = block.getAttr("type") if block_type not in System.plugins: continue block_id = int(block.getAttr("id")) position = block.getTag("position") x = position.getAttr("x") y = position.getAttr("y") properties = block.getChildTags("property") props = {} for prop in properties: try: props[prop.key] = prop.value except: pass new_block = System.plugins[block_type] new_block.set_properties(props) new_block.id = block_id new_block.x = float(x) new_block.y = float(y) diagram.add_block(new_block) connections = parser.getTag("mosaicode").getTag( "connections").getChildTags("connection") for conn in connections: try: from_block = diagram.blocks[int(conn.getAttr("from_block"))] to_block = diagram.blocks[int(conn.getAttr("to_block"))] except: continue from_block_out = int(conn.getAttr("from_out")) to_block_in = int(conn.getAttr("to_in")) diagram.start_connection(from_block, int(from_block_out) - 1) diagram.end_connection(to_block, int(to_block_in) - 1) return True
def load(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 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.framework = parser.getTagAttr(tag_name, "framework") block.label = parser.getTagAttr(tag_name, "label") block.group = parser.getTagAttr(tag_name, "group") block.color = parser.getTagAttr(tag_name, "color") block.help = parser.getTagAttr(tag_name, "help") block.source = parser.getTagAttr(tag_name, "source") count = 0 for code in block.codes: block.codes[count] = parser.getTag(tag_name).getTag( "code" + str(count)).getText() count = count + 1 props = parser.getTag(tag_name).getTag("properties").getChildTags( "property") for prop in props: block.properties.append(ast.literal_eval(prop.getAttr("value"))) in_ports = parser.getTag(tag_name).getTag("in_ports").getChildTags( "port") for port in in_ports: block.in_ports.append(ast.literal_eval(port.getAttr("value"))) out_ports = parser.getTag(tag_name).getTag("out_ports").getChildTags( "port") for port in out_ports: block.out_ports.append(ast.literal_eval(port.getAttr("value"))) if block.type == "mosaicode.model.blockmodel": return None return block
def load(cls): """ This method loads the diagram. Returns: * **Types** (:class:`boolean<boolean>`) """ prefs = Preferences() from mosaicode.system import System file_name = System.get_user_dir() + "/" + prefs.conf_file_path file_name = os.path.expanduser(file_name) if os.path.exists(file_name) is False: return prefs parser = XMLParser(file_name) if parser.getTag("MosaicodeProperties") is None: return prefs try: prefs.default_directory = parser.getTagAttr("MosaicodeProperties", "default_directory") prefs.default_filename = parser.getTagAttr("MosaicodeProperties", "default_filename") prefs.grid = int(parser.getTagAttr("MosaicodeProperties","grid")) prefs.width = int(parser.getTagAttr("MosaicodeProperties","width")) prefs.height = int(parser.getTagAttr("MosaicodeProperties", "height")) prefs.hpaned_work_area = int(parser.getTagAttr("MosaicodeProperties", "hpaned_work_area")) prefs.vpaned_bottom = int(parser.getTagAttr("MosaicodeProperties", "vpaned_bottom")) prefs.vpaned_left = int(parser.getTagAttr("MosaicodeProperties", "vpaned_left")) files = parser.getTag("MosaicodeProperties").getTag( "recent_files").getChildTags("name") for file_name in files: prefs.recent_files.append(file_name.getAttr("value")) except: pass return prefs
def load(cls, file_name): """ This method loads the code_template from XML file. Returns: * **Types** (:class:`boolean<boolean>`) """ # load the code_template if os.path.exists(file_name) is False: return None parser = XMLParser(file_name) if parser.getTag(tag_name) is None: return None try: code_template = CodeTemplate() code_template.name = parser.getTagAttr(tag_name, "name") code_template.type = parser.getTagAttr(tag_name, "type") code_template.description = parser.getTagAttr( tag_name, "description") code_template.language = parser.getTagAttr(tag_name, "language") code_template.command = parser.getTag(tag_name).getTag( "command").getText() code_template.extension = parser.getTagAttr(tag_name, "extension") code_template.code = parser.getTag(tag_name).getTag( "code").getText() code_parts = parser.getTag(tag_name).getTag( "code_parts").getChildTags("code_part") for code_part in code_parts: code_template.code_parts.append(code_part.getAttr("value")) except Exception as e: print e return None if code_template.name == "": return None return code_template
def load(cls, file_name): """ This method loads the code_template from XML file. Returns: * **Types** (:class:`boolean<boolean>`) """ # load the code_template if os.path.exists(file_name) is False: return parser = XMLParser(file_name) if parser.getTag("MosaicodeCodeTemplate") is None: return None try: code_template = CodeTemplate() code_template.name = parser.getTagAttr("MosaicodeCodeTemplate", "name") code_template.type = parser.getTagAttr("MosaicodeCodeTemplate", "type") code_template.description = parser.getTagAttr( "MosaicodeCodeTemplate", "description") code_template.language = parser.getTagAttr("MosaicodeCodeTemplate", "language") code_template.extension = parser.getTagAttr( "MosaicodeCodeTemplate", "extension") code_template.source = parser.getTagAttr("MosaicodeCodeTemplate", "source") code_template.command = parser.getTag( "MosaicodeCodeTemplate").getTag("command").getText() code_template.code = parser.getTag("MosaicodeCodeTemplate").getTag( "code").getText() except: return None if code_template.name == "": return None return code_template
def load(cls, path): """ This method loads the diagram. Returns: * **Types** (:class:`boolean<boolean>`) """ prefs = Preferences() file_name = path + "/" + prefs.conf_file_path file_name = os.path.expanduser(file_name) if os.path.exists(file_name) is False: return prefs parser = XMLParser(file_name) if parser.getTag(tag_name) is None: return prefs prefs.author = parser.getTagAttr(tag_name, "author") prefs.license = parser.getTagAttr(tag_name, "license") prefs.default_directory = parser.getTagAttr(tag_name, "default_directory") prefs.default_filename = parser.getTagAttr(tag_name, "default_filename") prefs.grid = int(parser.getTagAttr(tag_name, "grid")) prefs.port = int(parser.getTagAttr(tag_name, "network_port")) prefs.width = int(parser.getTagAttr(tag_name, "width")) prefs.height = int(parser.getTagAttr(tag_name, "height")) prefs.hpaned_work_area = int( parser.getTagAttr(tag_name, "hpaned_work_area")) prefs.vpaned_bottom = int(parser.getTagAttr(tag_name, "vpaned_bottom")) prefs.vpaned_left = int(parser.getTagAttr(tag_name, "vpaned_left")) files = parser.getTag(tag_name).getTag("recent_files").getChildTags( "name") for file_name in files: prefs.recent_files.append(file_name.getAttr("value")) return prefs
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
class TestXMLParser(TestCase): def setUp(self): """Do the test basic setup.""" source = None fromString = False fromTag = False self.xml_parser = XMLParser(source, fromString, fromTag) source = FILES_INPUT + "And.mscd" fromString = False fromTag = False self.xml_parser = XMLParser(source, fromString, fromTag) # source = FILES_INPUT + "And.mscd" # fromString = True # fromTag = False # self.xml_parser = XMLParser(source, fromString, fromTag) # # source = "TESTE" # fromString = True # fromTag = False # self.xml_parser = XMLParser(source, fromString, fromTag) # # source = "TESTE" # fromString = False # fromTag = True # self.xml_parser = XMLParser(source, fromString, fromTag) # # source = "TESTE" # fromString = True # fromTag = True # self.xml_parser = XMLParser(source, fromString, fromTag) # ---------------------------------------------------------------------- def test_getTagAttr(self): self.xml_parser.tag = "property" self.xml_parser.attr = "key" self.assertIsNotNone( self.xml_parser.getTagAttr(self.xml_parser.tag, self.xml_parser.attr)) #tag = None #attr = None #self.assertIsNotNone(self.xml_parser.getTagAttr(tag, attr)) # ---------------------------------------------------------------------- def test_setTagAttr(self): tag = "position" attr = "x" self.assertIsNone(self.xml_parser.setTagAttr(tag, attr, 10)) # ---------------------------------------------------------------------- def test_getAttr(self): #tag = "position" attr = "position" self.assertIsNotNone(self.xml_parser.getAttr(attr)) #self.assertEqual(10, self.xml_parser.setTagAttr(attr)) # ---------------------------------------------------------------------- def test_setAttr(self): attr = "to_block" self.assertIsNone(self.xml_parser.setAttr(attr, 10)) # ---------------------------------------------------------------------- def test_getChildTagAttr(self): parent = "block" child = "position" attr = "x" self.assertIsNotNone( self.xml_parser.getChildTagAttr(parent, child, attr)) # ---------------------------------------------------------------------- def test_setChildTagAttr(self): parent = "block" child = "position" attr = "x" value = 10 self.assertIsNone( self.xml_parser.setChildTagAttr(parent, child, attr, value)) # ---------------------------------------------------------------------- def test_getChildTags(self): child = "position" self.assertIsNotNone(self.xml_parser.getChildTags(child)) child = "block" self.assertIsNotNone(self.xml_parser.getChildTags(child)) # ---------------------------------------------------------------------- def test_addTag(self): tagName = "position" self.assertIsNotNone(self.xml_parser.addTag(tagName)) # ---------------------------------------------------------------------- def test_appendToTag(self): tagParent = "position" tagChild = "x" self.assertIsNotNone(self.xml_parser.appendToTag(tagParent, tagChild)) # ---------------------------------------------------------------------- def test_appendToLastTag(self): tagParent = "position" tagChild = "x" self.assertIsNotNone( self.xml_parser.appendToLastTag(tagParent, tagChild)) # ---------------------------------------------------------------------- def test_getXML(self): self.assertIsNotNone(self.xml_parser.getXML()) # ---------------------------------------------------------------------- def test_getTagXML(self): tagName = "position" tag = self.xml_parser.addTag(tagName) self.assertIsNotNone(self.xml_parser.getTagXML(tag)) # ---------------------------------------------------------------------- def test_getTag(self): tagName = "position" tag = self.xml_parser.addTag(tagName) self.assertIsNone(self.xml_parser.getTag(tag)) tag = "ABC" self.assertIsNone(self.xml_parser.getTag(tag)) tag = "position" self.assertIsNotNone(self.xml_parser.getTag(tag)) # ---------------------------------------------------------------------- def test_getTagChild(self): parent = "block" child = "position" self.assertIsNotNone(self.xml_parser.getTagChild(parent, child)) # ---------------------------------------------------------------------- def test_getTagContent(self): self.assertIsNotNone(self.xml_parser.getTagContent()) # ---------------------------------------------------------------------- def test_getTagChildren(self): self.assertIsNotNone(self.xml_parser.getTagChildren()) # ---------------------------------------------------------------------- def test_getText(self): self.assertIsNotNone(self.xml_parser.getText()) # ---------------------------------------------------------------------- def test_prettify(self): self.assertIsNotNone(self.xml_parser.prettify())