def test_elements_ns_1(): w = Writer() xml_w = XMLWriter(w) xml_w.startDocument() xml_w.startPrefixMapping("g", "http://gaphor.devjavu.com/schema") xml_w.startElementNS(("http://gaphor.devjavu.com/schema", "foo"), "qn", {}) xml_w.endElementNS(("http://gaphor.devjavu.com/schema", "foo"), "qn") xml = ( """<?xml version="1.0" encoding="%s"?>\n<g:foo xmlns:g="http://gaphor.devjavu.com/schema"/>""" % sys.getdefaultencoding()) assert w.s == xml, w.s
def test_elements_test(): w = Writer() xml_w = XMLWriter(w) xml_w.startDocument() xml_w.startElement("foo", {}) xml_w.startElement("bar", {}) xml_w.characters("hello") xml_w.endElement("bar") xml_w.endElement("foo") xml = ( """<?xml version="1.0" encoding="%s"?>\n<foo>\n<bar>hello</bar>\n</foo>""" % sys.getdefaultencoding()) assert w.s == xml, w.s
def test_load_and_save_of_a_model(self): path = distribution().locate_file("test-models/simple-items.gaphor") with open(path, "r") as ifile: storage.load( ifile, factory=self.element_factory, modeling_language=self.modeling_language, ) pf = PseudoFile() storage.save(XMLWriter(pf), factory=self.element_factory) with open(path, "r") as ifile: orig = ifile.read() copy = pf.data expr = re.compile('gaphor-version="[^"]*"') orig = expr.sub("%VER%", orig) copy = expr.sub("%VER%", copy) self.maxDiff = None assert copy == orig, "Saved model does not match copy"
def test_elements_1(): w = Writer() xml_w = XMLWriter(w) xml_w.startDocument() xml_w.startElement("foo", {}) xml_w.endElement("foo") xml = """<?xml version="1.0" encoding="%s"?>\n<foo/>""" % sys.getdefaultencoding( ) assert w.s == xml, w.s + " != " + xml
def save(): """Save diagram into string.""" f = StringIO() storage.save(XMLWriter(f), element_factory) data = f.getvalue() f.close() return data
def export(self, filename): out = open(filename, "w") xmi = XMLWriter(out) attributes = { "xmi.version": self.XMI_VERSION, "xmlns:xmi": self.XMI_NAMESPACE, "xmlns:UML": self.UML_NAMESPACE, } xmi.startElement("XMI", attrs=attributes) for package in self.element_factory.select(self.select_package): self.handle(xmi, package) for generalization in self.element_factory.select( self.select_generalization): self.handle(xmi, generalization) for realization in self.element_factory.select( self.select_realization): self.handle(xmi, realization) xmi.endElement("XMI") logger.debug(self.handled_ids)
def save(self): """Save diagram into string.""" from gaphor.storage import storage from gaphor.storage.xmlwriter import XMLWriter f = StringIO() storage.save(XMLWriter(f), factory=self.element_factory) data = f.getvalue() f.close() self.element_factory.flush() assert not list(self.element_factory.select()) assert not list(self.element_factory.lselect()) return data
def test_save_item(self): """Save a diagranm item too. """ diagram = self.element_factory.create(UML.Diagram) diagram.create(CommentItem, subject=self.element_factory.create(UML.Comment)) out = PseudoFile() storage.save(XMLWriter(out), factory=self.element_factory) out.close() assert "<Diagram " in out.data assert "<Comment " in out.data assert "<canvas>" in out.data assert ' type="CommentItem"' in out.data, out.data
def test_save_uml(self): """Saving gaphor.UML model elements.""" self.element_factory.create(UML.Package) self.element_factory.create(UML.Diagram) self.element_factory.create(UML.Comment) self.element_factory.create(UML.Class) out = PseudoFile() storage.save(XMLWriter(out), factory=self.element_factory) out.close() assert "<Package " in out.data assert "<Diagram " in out.data assert "<Comment " in out.data assert "<Class " in out.data
def test_save_and_load_of_association_with_two_connected_classes( self, case): c1 = case.create(ClassItem, UML.Class) c2 = case.create(ClassItem, UML.Class) c2.matrix.translate(200, 200) case.diagram.request_update(c2) case.diagram.update_now((c1, c2)) assert tuple(c2.matrix_i2c) == (1, 0, 0, 1, 200, 200) a = case.create(AssociationItem) case.connect(a, a.head, c1) case.connect(a, a.tail, c2) case.diagram.update_now((c1, c2, a)) assert a.head.pos.y == 0, a.head.pos assert a.tail.pos.x == 200, a.tail.pos assert a.tail.pos.y == 200, a.tail.pos assert a.subject fd = StringIO() storage.save(XMLWriter(fd), factory=case.element_factory) data = fd.getvalue() fd.close() old_a_subject_id = a.subject.id case.element_factory.flush() assert not list(case.element_factory.select()) fd = StringIO(data) storage.load(fd, factory=case.element_factory, modeling_language=case.modeling_language) fd.close() diagrams = list(case.kindof(UML.Diagram)) assert len(diagrams) == 1 d = diagrams[0] a = next(d.select(lambda e: isinstance(e, AssociationItem))) assert a.subject is not None assert old_a_subject_id == a.subject.id cinfo_head = a.diagram.connections.get_connection(a.head) assert cinfo_head.connected is not None cinfo_tail = a.diagram.connections.get_connection(a.tail) assert cinfo_tail.connected is not None assert cinfo_head.connected is not cinfo_tail.connected
def test_save_and_load_of_association_with_two_connected_classes(self): c1 = self.create(ClassItem, UML.Class) c2 = self.create(ClassItem, UML.Class) c2.matrix.translate(200, 200) self.diagram.canvas.update_matrix(c2) assert tuple(self.diagram.canvas.get_matrix_i2c(c2)) == (1, 0, 0, 1, 200, 200) a = self.create(AssociationItem) self.connect(a, a.head, c1) self.connect(a, a.tail, c2) self.diagram.canvas.update_now() assert a.head.pos.y == 0, a.head.pos assert a.tail.pos.x == 10, a.tail.pos assert a.tail.pos.y == 200, a.tail.pos assert a.subject fd = StringIO() storage.save(XMLWriter(fd), factory=self.element_factory) data = fd.getvalue() fd.close() old_a_subject_id = a.subject.id self.element_factory.flush() assert not list(self.element_factory.select()) fd = StringIO(data) storage.load(fd, factory=self.element_factory, modeling_language=self.modeling_language) fd.close() diagrams = list(self.kindof(UML.Diagram)) assert 1 == len(diagrams) d = diagrams[0] a = d.canvas.select(lambda e: isinstance(e, AssociationItem))[0] assert a.subject is not None assert old_a_subject_id == a.subject.id cinfo_head = a.canvas.get_connection(a.head) assert cinfo_head.connected is not None cinfo_tail = a.canvas.get_connection(a.tail) assert cinfo_tail.connected is not None assert cinfo_head.connected is not cinfo_tail.connected
def save(self, filename): """Save the current UML model to the specified file name. Before writing the model file, this will verify that there are no orphan references. It will also verify that the filename has the correct extension. A status window is displayed while the GIdleThread is executed. This thread actually saves the model. """ if not (filename and len(filename)): return self.verify_orphans() main_window = self.main_window queue = Queue() status_window = StatusWindow( gettext("Saving..."), gettext("Saving model to {filename}").format(filename=filename), parent=main_window.window, queue=queue, ) try: with open(filename.encode("utf-8"), "w") as out: saver = storage.save_generator(XMLWriter(out), self.element_factory) worker = GIdleThread(saver, queue) worker.start() worker.wait() if worker.error: worker.reraise() self.filename = filename self.event_manager.handle(FileSaved(self, filename)) except Exception as e: error_handler( message=gettext("Unable to save model “{filename}”.").format( filename=filename ), secondary_message=error_message(e), window=self.main_window.window, ) raise finally: status_window.destroy()