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)
def save(cls, prefs): """ This method save the diagram. Returns: * **Types** (:class:`boolean<boolean>`) """ parser = XMLParser() parser.addTag(tag_name) parser.setTagAttr(tag_name, 'default_directory', prefs.default_directory) parser.setTagAttr(tag_name, 'default_filename', prefs.default_filename) parser.setTagAttr(tag_name, 'grid', prefs.grid) parser.setTagAttr(tag_name, 'network_port', prefs.port) parser.setTagAttr(tag_name, 'width', prefs.width) parser.setTagAttr(tag_name, 'height', prefs.height) parser.setTagAttr(tag_name, 'hpaned_work_area', prefs.hpaned_work_area) parser.setTagAttr(tag_name, 'vpaned_bottom', prefs.vpaned_bottom) parser.setTagAttr(tag_name, 'vpaned_left', prefs.vpaned_left) parser.appendToTag(tag_name, 'recent_files') for key in prefs.recent_files: parser.appendToTag('recent_files', 'name', value=key) try: from mosaicode.system import System file_name = System.get_user_dir() + "/" + prefs.conf_file_path confFile = file(os.path.expanduser(file_name), 'w') confFile.write(parser.prettify()) confFile.close() except IOError as e: return False return True
def load_xml(cls, file_name): """ This method loads the port from XML file. Returns: * **Types** (:class:`boolean<boolean>`) """ # load the port if os.path.exists(file_name) is False: return None parser = XMLParser(file_name) if parser.getTag(tag_name) is None: return None port = Port() port.type = parser.getTagAttr(tag_name, "type") port.language = parser.getTagAttr(tag_name, "language") port.hint = parser.getTagAttr(tag_name, "hint") port.color = parser.getTagAttr(tag_name, "color") port.multiple = parser.getTagAttr(tag_name, "multiple") port.var_name = parser.getTagAttr(tag_name, "var_name") port.code = parser.getTag(tag_name).getTag("code").getText() if port.type == "": return None return port
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(CodeTemplatePersistence.tag_name) is None: return None ct = parser.getTag(CodeTemplatePersistence.tag_name) code_template = CodeTemplate() for prop in CodeTemplatePersistence.properties: if hasattr(ct, prop) and hasattr(code_template, prop): code_template.__dict__[prop] = parser.getTagAttr( CodeTemplatePersistence.tag_name, prop) code_parts = parser.getTag(CodeTemplatePersistence.tag_name).getTag( "code_parts").getChildTags("code_part") for code_part in code_parts: code_template.code_parts.append(code_part.getAttr("value")) if code_template.name == "": return None return code_template
def save_xml(cls, code_template, path): """ This method save the code_template in user space. Returns: * **Types** (:class:`boolean<boolean>`) """ code_template.source = "xml" parser = XMLParser() parser.addTag(CodeTemplatePersistence.tag_name) for prop in CodeTemplatePersistence.properties: if hasattr(code_template, prop): parser.setTagAttr(CodeTemplatePersistence.tag_name, prop, code_template.__dict__[prop]) parser.appendToTag(CodeTemplatePersistence.tag_name, 'code_parts') for key in code_template.code_parts: parser.appendToTag('code_parts', 'code_part', value=key.strip()) if not Persistence.create_dir(path): return False try: file_name = code_template.name code_template_file = file(os.path.join(path, file_name + '.xml'), 'w') code_template_file.write(parser.prettify()) code_template_file.close() except IOError as e: return False return True
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(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.extension = parser.getTagAttr(tag_name, "extension") code_template.source = parser.getTagAttr(tag_name, "source") code_template.command = parser.getTag(tag_name).getTag( "command").getText() code_template.code = parser.getTag(tag_name).getTag( "code").getText() except: return None if code_template.name == "": return None return code_template
def load(cls, file_name): """ This method loads the port from XML file. Returns: * **Types** (:class:`boolean<boolean>`) """ # load the port if os.path.exists(file_name) is False: return parser = XMLParser(file_name) if parser.getTag(tag_name) is None: return None port = Port() port.type = parser.getTagAttr(tag_name, "type") port.language = parser.getTagAttr(tag_name, "language") port.label = parser.getTagAttr(tag_name, "label") port.color = parser.getTagAttr(tag_name, "color") port.multiple = parser.getTagAttr(tag_name, "multiple") port.source = parser.getTagAttr(tag_name, "source") port.code = parser.getTag(tag_name).getTag("code").getText() count = 0 for code in port.input_codes: port.input_codes[count] = parser.getTag(tag_name).getTag('input_code' + str(count)).getText() port.output_codes[count] = parser.getTag(tag_name).getTag('output_code' + str(count)).getText() count = count + 1 if port.type == "": return None return port
def save_xml(cls, port, path): """ This method save the port in user space. Returns: * **Types** (:class:`boolean<boolean>`) """ port.source = "xml" parser = XMLParser() parser.addTag(tag_name) parser.setTagAttr(tag_name, 'type', port.type) parser.setTagAttr(tag_name, 'language', port.language) parser.setTagAttr(tag_name, 'hint', port.hint) parser.setTagAttr(tag_name, 'color', port.color) parser.setTagAttr(tag_name, 'multiple', port.multiple) parser.setTagAttr(tag_name, 'var_name', port.var_name) parser.appendToTag(tag_name, 'code').string = str(port.code) if not Persistence.create_dir(path): return False try: file_name = port.hint port_file = file(os.path.join(path, file_name + '.xml'), 'w') port_file.write(parser.prettify()) port_file.close() except IOError as e: return False return True
def save(cls, diagram): """ This method save a file. Returns: * **Types** (:class:`boolean<boolean>`) """ parser = XMLParser() parser.addTag(tag_name) parser.appendToTag(tag_name, 'version', value=System.VERSION) parser.appendToTag(tag_name, 'zoom', value=diagram.zoom) parser.appendToTag(tag_name, 'language', value=diagram.language) 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) parser.appendToLastTag('block', 'position', 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', text=comment.text, x=pos[0], y=pos[1]) 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 save_xml(cls, block, path=None): """ This method save the block in user space. Returns: * **Types** (:class:`boolean<boolean>`) """ block.source = "xml" parser = XMLParser() main = parser.addTag(tag_name) parser.setTagAttr(tag_name, 'type', block.type) parser.setTagAttr(tag_name, 'language', block.language) parser.setTagAttr(tag_name, 'extension', block.extension) parser.setTagAttr(tag_name, 'help', block.help) parser.setTagAttr(tag_name, 'label', block.label) parser.setTagAttr(tag_name, 'color', block.color) parser.setTagAttr(tag_name, 'group', block.group) parser.setTagAttr(tag_name, 'maxIO', block.maxIO) parser.setTagAttr(tag_name, 'codes', block.codes) parser.appendToTag(tag_name, 'properties') for key in block.properties: parser.appendToTag('properties', 'property', value=key) parser.appendToTag(tag_name, 'ports') for port in block.ports: parser.appendToTag('ports', 'port', conn_type=port.conn_type, name_=port.name, label=port.label, type_=port.type) if (path is not None) and not Persistence.create_dir(path): return False if (path is None) and (block.file is not None): path = block.file elif (path is not None): file_name = block.label path = os.path.join(path, file_name + '.xml') else: return False parser.setTagAttr(tag_name, 'file', path) try: block_file = file(path, 'w') block_file.write(parser.getXML()) block_file.close() except IOError as e: return False return True
def save(cls, block): """ This method save the block in user space. Returns: * **Types** (:class:`boolean<boolean>`) """ from mosaicode.system import System block.source = "xml" parser = XMLParser() main = parser.addTag(tag_name) parser.setTagAttr(tag_name, 'type', block.type) parser.setTagAttr(tag_name, 'language', block.language) parser.setTagAttr(tag_name, 'framework', block.framework) parser.setTagAttr(tag_name, 'label', block.label) parser.setTagAttr(tag_name, 'group', block.group) parser.setTagAttr(tag_name, 'color', block.color) parser.setTagAttr(tag_name, 'help', block.help) parser.setTagAttr(tag_name, 'source', block.source) count = 0 for code in block.codes: parser.appendToTag(tag_name, 'code' + str(count)).string = str( block.codes[count]) count = count + 1 parser.appendToTag(tag_name, 'properties') for key in block.properties: parser.appendToTag('properties', 'property', value=key) parser.appendToTag(tag_name, 'in_ports') for key in block.in_ports: parser.appendToTag('in_ports', 'port', value=key) parser.appendToTag(tag_name, 'out_ports') for key in block.out_ports: parser.appendToTag('out_ports', 'port', value=key) try: data_dir = System.get_user_dir() + "/extensions/" data_dir = data_dir + block.language + "/" + block.framework + "/" if not os.path.isdir(data_dir): try: os.makedirs(data_dir) except: pass file_name = data_dir + block.type + ".xml" block_file = file(os.path.expanduser(file_name), 'w') block_file.write(parser.getXML()) block_file.close() except IOError as e: return False return True
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 save_xml(cls, code_template, path): """ This method save the code_template in user space. Returns: * **Types** (:class:`boolean<boolean>`) """ code_template.source = "xml" parser = XMLParser() parser.addTag(tag_name) parser.setTagAttr(tag_name, 'name', code_template.name) parser.setTagAttr(tag_name, 'type', code_template.type) parser.setTagAttr(tag_name, 'description', code_template.description) parser.setTagAttr(tag_name, 'language', code_template.language) parser.setTagAttr(tag_name, 'command', code_template.command) parser.appendToTag(tag_name, 'code_parts') for key in code_template.code_parts: parser.appendToTag('code_parts', 'code_part', value=key.strip()) parser.appendToTag(tag_name, 'files') for key in code_template.files: parser.appendToTag('files', 'file', name_=key, value=code_template.files[key]) parser.appendToTag(tag_name, 'properties') for key in code_template.properties: parser.appendToTag('properties', 'property', value=key) if not Persistence.create_dir(path): return False try: file_name = code_template.name code_template_file = file(os.path.join(path, file_name + '.xml'), 'w') code_template_file.write(parser.prettify()) code_template_file.close() except IOError as e: from mosaicode.system import System System() System.log(e) return False return True
def save_xml(cls, block, path): """ This method save the block in user space. Returns: * **Types** (:class:`boolean<boolean>`) """ block.source = "xml" parser = XMLParser() main = parser.addTag(tag_name) parser.setTagAttr(tag_name,'type', block.type) parser.setTagAttr(tag_name,'language', block.language) parser.setTagAttr(tag_name,'framework', block.framework) parser.setTagAttr(tag_name,'label', block.label) parser.setTagAttr(tag_name,'group', block.group) parser.setTagAttr(tag_name,'color', block.color) parser.setTagAttr(tag_name,'help', block.help) for code in block.codes: parser.appendToTag(tag_name, code, value=block.codes[code]) parser.appendToTag(tag_name, 'properties') for key in block.properties: parser.appendToTag('properties', 'property', value=key) parser.appendToTag(tag_name, 'ports') for port in block.ports: parser.appendToTag('ports', 'port', conn_type=port.conn_type, name_=port.name, label=port.label, type_=port.type) if not Persistence.create_dir(path): return False try: file_name = path + block.type + ".xml" block_file = file(os.path.expanduser(file_name), 'w') block_file.write(parser.getXML()) block_file.close() except IOError as e: return False return True
def save(cls, port): """ This method save the port in user space. Returns: * **Types** (:class:`boolean<boolean>`) """ from mosaicode.system import System port.source = "xml" parser = XMLParser() parser.addTag(tag_name) parser.setTagAttr(tag_name, 'type', port.type) parser.setTagAttr(tag_name, 'language', port.language) parser.setTagAttr(tag_name, 'label', port.label) parser.setTagAttr(tag_name, 'color', port.color) parser.setTagAttr(tag_name, 'multiple', port.multiple) parser.setTagAttr(tag_name, 'source', port.source) parser.appendToTag(tag_name, 'code').string = str(port.code) count = 0 for code in port.input_codes: parser.appendToTag(tag_name, 'input_code' + \ str(count)).string = str(port.input_codes[count]) parser.appendToTag(tag_name, 'output_code' + \ str(count)).string = str(port.output_codes[count]) count = count + 1 try: data_dir = System.get_user_dir() + "/extensions/" data_dir = data_dir + port.language + "/ports/" if not os.path.isdir(data_dir): try: os.makedirs(data_dir) except: pass file_name = data_dir + port.type + ".xml" port_file = file(os.path.expanduser(file_name), 'w') port_file.write(parser.prettify()) port_file.close() except IOError as e: return False return True
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 save(cls, code_template): """ This method save the code_template in user space. Returns: * **Types** (:class:`boolean<boolean>`) """ from mosaicode.system import System code_template.source = "xml" parser = XMLParser() parser.addTag('MosaicodeCodeTemplate') parser.setTagAttr('MosaicodeCodeTemplate', 'name', code_template.name) parser.setTagAttr('MosaicodeCodeTemplate', 'type', code_template.type) parser.setTagAttr('MosaicodeCodeTemplate', 'description', code_template.description) parser.setTagAttr('MosaicodeCodeTemplate', 'language', code_template.language) parser.setTagAttr('MosaicodeCodeTemplate', 'extension', code_template.extension) parser.setTagAttr('MosaicodeCodeTemplate', 'source', code_template.source) parser.appendToTag('MosaicodeCodeTemplate', 'command').string = str(code_template.command) parser.appendToTag('MosaicodeCodeTemplate', 'code').string = str(code_template.code) try: data_dir = System.get_user_dir() + "/extensions/" data_dir = data_dir + code_template.language + "/" if not os.path.isdir(data_dir): try: os.makedirs(data_dir) except: pass file_name = data_dir + code_template.type + ".xml" code_template_file = file(os.path.expanduser(file_name), 'w') code_template_file.write(parser.prettify()) code_template_file.close() except IOError as e: return False return True
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, 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