Skip to content

Commit

Permalink
Only recover sessions in GUI mode
Browse files Browse the repository at this point in the history
  • Loading branch information
amolenaar committed May 31, 2024
1 parent e5b16de commit 051d3e8
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 34 deletions.
2 changes: 1 addition & 1 deletion gaphor/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def run(args) -> int:
run_argv += ["--gapplication-service"]
run_argv.extend(args.model)

return gaphor.ui.run(run_argv)
return gaphor.ui.run(run_argv, recover=True)

parser = argparse.ArgumentParser(
description="Launch the GUI.", parents=[version_parser()]
Expand Down
28 changes: 15 additions & 13 deletions gaphor/storage/recovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ def all_sessions() -> Iterator[tuple[str, Path | None, Path | None]]:
preamble = ast.literal_eval(preamble_line)
path = preamble.get("path")
is_template = preamble.get("template")
yield (
(session_file.stem, None, Path(path))
if is_template
else (session_file.stem, Path(path), None)
)
if path:
yield (
(session_file.stem, None, Path(path))
if is_template
else (session_file.stem, Path(path), None)
)


class Recovery(Service):
Expand Down Expand Up @@ -115,7 +116,8 @@ def on_transaction_rollback(self, _event):
@event_handler(SessionCreated)
def on_model_loaded(self, event: SessionCreated):
self.session_id = event.session.session_id # type: ignore[attr-defined]
self.event_log = EventLog(self.session_id, event.filename, event.template)
if event.filename or event.template:
self.event_log = EventLog(self.session_id, event.filename, event.template)
self.recorder.truncate()

@event_handler(ModelReady)
Expand Down Expand Up @@ -185,19 +187,21 @@ def clear(self):
self._log_name.unlink(missing_ok=True)

def write(self, event):
if not (self._filename or self._template):
return

f = self._file
if not f or f.closed:
f = self._file = self._log_name.open(mode="a", encoding="utf-8")

if f.tell() == 0:
if self._filename:
filename = self._filename.absolute()
is_template = False
elif self._template:
if self._template:
filename = self._template.absolute()
is_template = True
else:
filename = None
assert self._filename
filename = self._filename.absolute()
is_template = False

f.write(
repr(
Expand All @@ -206,8 +210,6 @@ def write(self, event):
"sha256": sha256sum(filename),
"template": is_template,
}
if filename
else {}
)
)
f.write("\n")
Expand Down
2 changes: 1 addition & 1 deletion gaphor/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
def mock_gaphor_ui_run(monkeypatch):
_argv = []

def run(argv):
def run(argv, recover=False):
_argv[:] = argv
return 0

Expand Down
7 changes: 4 additions & 3 deletions gaphor/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
)


def run(argv: list[str], launch_service="greeter") -> int:
def run(argv: list[str], *, launch_service="greeter", recover=False) -> int:
application: Application | None = None

def app_startup(gtk_app):
Expand Down Expand Up @@ -72,7 +72,8 @@ def on_quit(_event):
event_manager.subscribe(on_session_created)
event_manager.subscribe(on_quit)
application.get_service(launch_service).init(gtk_app)
recover_sessions(application)
if recover:
recover_sessions(application)
except Exception:
gtk_app.exit_code = 1
gtk_app.quit()
Expand All @@ -81,7 +82,7 @@ def on_quit(_event):
def app_activate(gtk_app):
assert application
if not application.has_sessions():
application.get_service("greeter").open()
application.get_service(launch_service).open()

def app_open(gtk_app, files, n_files, hint):
# appfilemanager should take care of this:
Expand Down
7 changes: 2 additions & 5 deletions gaphor/ui/selftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,6 @@ def test_library_versions(self, status):

@test
def test_new_session(self, status):
with (importlib.resources.files("gaphor") / "templates" / "uml.gaphor").open(
encoding="utf-8"
) as f:
session = self.application.new_session(template=f)

def check_new_session(session):
main_window = session.get_service("main_window")

Expand All @@ -120,6 +115,8 @@ def check_new_session(session):
return GLib.SOURCE_REMOVE
return GLib.SOURCE_CONTINUE

template = importlib.resources.files("gaphor") / "templates" / "uml.gaphor"
session = self.application.new_session(template=template)
GLib.idle_add(check_new_session, session, priority=GLib.PRIORITY_LOW)

@test
Expand Down
23 changes: 12 additions & 11 deletions tests/test_session_recovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,20 +182,21 @@ def test_recover_from_session_files(application: Application, test_models):
session_id = "1234"
class_id = "9876"

recovery_statements = [
{
"path": str(test_models / "all-elements.gaphor"),
"sha256": sha256sum(test_models / "all-elements.gaphor"),
"template": True,
},
[("c", "Class", class_id, None)],
]

with (sessions_dir() / f"{session_id}.recovery").open("w", encoding="utf-8") as f:
f.writelines(
[
f"{{'path': '{test_models}/all-elements.gaphor', 'sha256': '{sha256sum(test_models / 'all-elements.gaphor')}', 'template': True}}\n",
f"[('c', 'Class', '{class_id}', None)]\n",
]
)
f.writelines(f"{repr(stmt)}\n" for stmt in recovery_statements)

recover_sessions(application)

session = next(s for s in application.sessions if s.session_id == session_id)
element_factory = session.get_service("element_factory")

assert session.session_id == session_id

# element_factory = session.get_service("element_factory")

# assert element_factory.lookup(class_id)
assert element_factory.lookup(class_id)

0 comments on commit 051d3e8

Please sign in to comment.