Skip to content

Commit

Permalink
More use of session instead of application
Browse files Browse the repository at this point in the history
  • Loading branch information
amolenaar committed Jan 26, 2020
1 parent f2426b4 commit 86096bc
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 305 deletions.
4 changes: 2 additions & 2 deletions examples/list_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import sys

import gaphor.UML as UML
from gaphor.application import Application
from gaphor.application import Session

# Setup command line options.
usage = "usage: %prog [options] file.gaphor"
Expand All @@ -33,7 +33,7 @@
model = args[0]

# Create the Gaphor application object.
session = Application.new_session()
session = Session()

# Get services we need.
element_factory = session.get_service("element_factory")
Expand Down
1 change: 0 additions & 1 deletion gaphor/UML/tests/test_properties.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest

from gaphor.application import Application
from gaphor.core import event_handler
from gaphor.UML.element import Element
from gaphor.UML.event import AssociationUpdated
Expand Down
19 changes: 11 additions & 8 deletions gaphor/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ def __call__(self, appservices=None):
assert not self._services_by_name
uninitialized_services = load_services("gaphor.appservices", appservices)
self._services_by_name = init_services(uninitialized_services, application=self)

transaction.subscribers.add(self._transaction_proxy)

return self

def new_session(self, services=None):
Expand All @@ -74,6 +77,7 @@ def new_session(self, services=None):
session = Session()
self.sessions.add(session)
self.active_session = session

return session

def has_sessions(self):
Expand All @@ -92,6 +96,8 @@ def shutdown(self):
This is mainly for testing purposes.
"""
transaction.subscribers.discard(self._transaction_proxy)

while self.sessions:
self.shutdown_session(self.sessions.pop())

Expand All @@ -112,13 +118,18 @@ def quit(self):
if self.active_session == session:
logger.info("Window not closed, abort quit operation")
return
self.shutdown()

# def all(self, base: Type[T]) -> Iterator[Tuple[T, str]]:
def all(self, base):
return (
(n, c) for n, c in self._services_by_name.items() if isinstance(c, base)
)

def _transaction_proxy(self, event):
if self.active_session:
self.active_session.event_manager.handle(event)


class Session:
"""
Expand All @@ -141,21 +152,13 @@ def __init__(self, services=None):
self.component_registry.register(name, srv)
self.event_manager.handle(ServiceInitializedEvent(name, srv))

transaction.subscribers.add(self._transaction_proxy)

def _transaction_proxy(self, event):
if self is Application.active_session:
self.event_manager.handle(event)

def get_service(self, name):
if not self.component_registry:
raise NotInitializedError("Session is no longer alive")

return self.component_registry.get_service(name)

def shutdown(self):
transaction.subscribers.discard(self._transaction_proxy)

if self.component_registry:
for name, _srv in self.component_registry.all(Service):
self.shutdown_service(name)
Expand Down
1 change: 0 additions & 1 deletion gaphor/services/session.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from gaphor.abc import Service
from gaphor.application import Application


class Session(Service):
Expand Down
5 changes: 3 additions & 2 deletions gaphor/transaction.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Transaction support is located in module gaphor.transaction:

Do some basic initialization, so event emission will work:

>>> session = Application.new_session(services=['event_manager'])
>>> application = Application()
>>> session = application.new_session(services=['event_manager'])
>>> event_manager = session.get_service('event_manager')

The Transaction class is used mainly to signal the begin and end of a transaction. This is done by the TransactionBegin, TransactionCommit and TransactionRollback events:
Expand Down Expand Up @@ -115,4 +116,4 @@ All transactions are marked for rollback once an exception is raised:

Cleanup:

>>> Application.shutdown()
>>> application.shutdown()
2 changes: 1 addition & 1 deletion gaphor/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import gi

from gaphor.application import Application, Session, _Application
from gaphor.application import Application, _Application
from gaphor.core import event_handler
from gaphor.event import ActiveSessionChanged, SessionShutdown
from gaphor.ui.actiongroup import apply_application_actions
Expand Down
124 changes: 68 additions & 56 deletions gaphor/ui/tests/test_diagrampage.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,75 @@
import unittest

import pytest
from gaphas.examples import Box

from gaphor import UML
from gaphor.application import Application
from gaphor.application import Session
from gaphor.diagram.general.comment import CommentItem
from gaphor.ui.mainwindow import DiagramPage


class DiagramPageTestCase(unittest.TestCase):
def setUp(self):
session = Application.new_session(
services=[
"event_manager",
"component_registry",
"element_factory",
"main_window",
"properties",
"namespace",
"diagrams",
"toolbox",
"elementeditor",
"export_menu",
"tools_menu",
]
)
main_window = session.get_service("main_window")
main_window.open()
self.element_factory = session.get_service("element_factory")
self.diagram = self.element_factory.create(UML.Diagram)
self.page = DiagramPage(
self.diagram,
session.get_service("event_manager"),
self.element_factory,
session.get_service("properties"),
)
self.page.construct()
assert self.page.diagram == self.diagram
assert self.page.view.canvas == self.diagram.canvas
assert len(self.element_factory.lselect()) == 1

def tearDown(self):
self.page.close()
del self.page
self.diagram.unlink()
del self.diagram
Application.shutdown()
assert len(self.element_factory.lselect()) == 0

def test_creation(self):
pass

def test_placement(self):
box = Box()
self.diagram.canvas.add(box)
self.diagram.canvas.update_now()
self.page.view.request_update([box])

self.diagram.create(
CommentItem, subject=self.element_factory.create(UML.Comment)
)
assert len(self.element_factory.lselect()) == 2
@pytest.fixture
def session():
session = Session(
services=[
"event_manager",
"component_registry",
"element_factory",
"main_window",
"properties",
"namespace",
"diagrams",
"toolbox",
"elementeditor",
"export_menu",
"tools_menu",
]
)
yield session
session.shutdown()


@pytest.fixture
def main_window(session):
main_window = session.get_service("main_window")
main_window.open()


@pytest.fixture
def element_factory(session):
return session.get_service("element_factory")


@pytest.fixture
def diagram(element_factory):
diagram = element_factory.create(UML.Diagram)
yield diagram
diagram.unlink()


@pytest.fixture
def page(session, diagram, element_factory):
page = DiagramPage(
diagram,
session.get_service("event_manager"),
element_factory,
session.get_service("properties"),
)
page.construct()
assert page.diagram == diagram
assert page.view.canvas == diagram.canvas
yield page
page.close()


def test_creation(page, element_factory):
assert len(element_factory.lselect()) == 1


def test_placement(diagram, page, element_factory):
box = Box()
diagram.canvas.add(box)
diagram.canvas.update_now()
page.view.request_update([box])

diagram.create(CommentItem, subject=element_factory.create(UML.Comment))
assert len(element_factory.lselect()) == 2

0 comments on commit 86096bc

Please sign in to comment.