def test_element_factory_undo(self): from gaphor.core.modeling import Element event_manager = EventManager() undo_manager = UndoManager(event_manager) element_factory = ElementFactory(event_manager) undo_manager.begin_transaction() p = element_factory.create(Element) assert undo_manager._current_transaction assert undo_manager._current_transaction._actions assert undo_manager.can_undo() undo_manager.commit_transaction() assert undo_manager.can_undo() assert element_factory.size() == 1 undo_manager.undo_transaction() assert not undo_manager.can_undo() assert undo_manager.can_redo() assert element_factory.size() == 0 undo_manager.redo_transaction() assert undo_manager.can_undo() assert not undo_manager.can_redo() assert element_factory.size() == 1 assert element_factory.lselect()[0] is p undo_manager.shutdown()
def test_redo_stack(self): from gaphor.core.modeling import Element event_manager = EventManager() undo_manager = UndoManager(event_manager) element_factory = ElementFactory(event_manager) undo_manager.begin_transaction() p = element_factory.create(Element) assert undo_manager._current_transaction assert undo_manager._current_transaction._actions assert undo_manager.can_undo() undo_manager.commit_transaction() assert undo_manager.can_undo() assert element_factory.size() == 1, element_factory.size() with Transaction(event_manager): element_factory.create(Element) assert undo_manager.can_undo() assert not undo_manager.can_redo() assert element_factory.size() == 2 undo_manager.undo_transaction() assert undo_manager.can_undo() assert 1 == len(undo_manager._undo_stack) assert 1 == len(undo_manager._redo_stack) assert undo_manager.can_redo() assert element_factory.size() == 1 undo_manager.undo_transaction() assert not undo_manager.can_undo() assert undo_manager.can_redo() assert 0 == len(undo_manager._undo_stack) assert 2 == len(undo_manager._redo_stack) # assert element_factory.size() == 0 undo_manager.redo_transaction() assert 1 == len(undo_manager._undo_stack) assert 1 == len(undo_manager._redo_stack) assert undo_manager.can_undo() assert undo_manager.can_redo() assert element_factory.size() == 1 undo_manager.redo_transaction() assert undo_manager.can_undo() assert not undo_manager.can_redo() assert element_factory.size() == 2 assert p in element_factory.lselect() undo_manager.shutdown()
def generate( filename: PathLike, outfile: PathLike, overridesfile: Optional[PathLike] = None, ) -> None: """Generates the Python data model. Opens the Gaphor model, generates the list of classes using the element factory, and then creates a new Python data model using a relationship search tree. """ element_factory = ElementFactory() modeling_language = UMLModelingLanguage() with open(filename): storage.load( filename, element_factory, modeling_language, ) classes: List = element_factory.lselect(UML.Class) classes, enumerations = find_enumerations(classes) classes = filter_out_gaphor_profile(classes) # Lambda key sort issue in mypy: https://github.com/python/mypy/issues/9656 classes = sorted( (cls for cls in classes if cls.name[0] != "~"), key=lambda c: c.name # type: ignore ) hierarchy = create_class_hierarchy(classes) uml_classes = filter_uml_classes(classes, modeling_language) with open(outfile, "w") as f: f.write(header) for cls in uml_classes: f.write(f"from gaphor.UML import {cls.name}\n") cls_written: Set[Element] = set(uml_classes) for cls in hierarchy.keys(): cls.attribute.sort(key=lambda a: a.name or "") # type: ignore[attr-defined] write_class_def(cls, hierarchy, f, cls_written) f.write("\n\n") for cls in hierarchy.keys(): write_properties(cls, f, enumerations) for cls in hierarchy.keys(): write_subsets(cls, f) element_factory.shutdown()
def generate( filename: PathLike, outfile: PathLike, overridesfile: Optional[PathLike] = None, ) -> None: """Generates the Python data model. Opens the Gaphor model, generates the list of classes using the element factory, and then creates a new Python data model using a relationship search tree. """ element_factory = ElementFactory() modeling_language = UMLModelingLanguage() with open(filename): storage.load( filename, element_factory, modeling_language, ) with open(outfile, "w") as f: f.write(header) classes: List = element_factory.lselect( lambda e: e.isKindOf(UML.Class)) classes, enumerations = find_enumerations(classes) classes = sorted((cls for cls in classes if cls.name[0] != "~"), key=lambda c: c.name) trees = create_class_trees(classes) create_referenced(classes) uml_classes = filter_uml_classes(classes, modeling_language) for cls in uml_classes: f.write(f"from gaphor.UML import {cls.name}\n") cls_written: Set[Element] = set(uml_classes) for cls in trees.keys(): cls.attribute.sort( key=lambda a: a.name or "") # type: ignore[attr-defined] write_class_def(cls, trees, f, cls_written) f.write("\n\n") for cls in trees.keys(): write_properties(cls, f, enumerations) element_factory.shutdown()