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"
Exemple #2
0
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())