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 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, 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 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, 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(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 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 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 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
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())