# Copyright 2023 (c) Anna Schumaker. """The `xfstestsdb gtk` command.""" import argparse import errno import sys from . import gsetup from gi.repository import GObject from gi.repository import Gio from gi.repository import Adw from . import model from . import sidebar from . import view from . import window from .. import commands from .. import sqlite class Application(Adw.Application): """Our Adw.Application for displaying xfstests results.""" runid = GObject.Property(type=int) show_sidebar = GObject.Property(type=bool, default=False) environment = GObject.Property(type=Gio.ListStore) properties = GObject.Property(type=model.PropertyList) summary = GObject.Property(type=model.SummaryList) model = GObject.Property(type=model.TestCaseList) win = GObject.Property(type=window.Window) sidebar = GObject.Property(type=sidebar.Sidebar) view = GObject.Property(type=view.XfstestsView) sql = GObject.Property(type=GObject.TYPE_PYOBJECT) def __init__(self, sql: sqlite.Connection): """Initialize the application.""" super().__init__(application_id=gsetup.APPLICATION_ID, resource_base_path=gsetup.RESOURCE_PATH, flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE, sql=sql) self.connect("notify::runid", self.__notify_runid) def __notify_runid(self, app: Adw.Application, param: GObject.ParamSpec) -> None: self.properties = model.PropertyList(self.sql, self.runid) self.environment = self.properties.environment self.model = model.TestCaseList(self.sql, self.runid) self.summary = model.SummaryList(self.sql, self.runid) def do_command_line(self, cmd_line: Gio.ApplicationCommandLine) -> int: """Handle the Adw.Application::command-line signal.""" Adw.Application.do_command_line(self, cmd_line) for arg in cmd_line.get_arguments(): split = arg.split("=") match split[0]: case "runid": self.runid = int(split[1]) case "show-sidebar": self.show_sidebar = True self.activate() return 0 def do_startup(self) -> None: """Handle the Adw.Application::startup signal.""" Adw.Application.do_startup(self) gsetup.add_style() self.sidebar = sidebar.Sidebar(self.sql) self.view = view.XfstestsView() self.win = window.Window(child=self.view, sidebar=self.sidebar) self.win.headerbar.pack_end(self.view.filterbuttons) self.sidebar.bind_property("runid", self, "runid") self.bind_property("runid", self.win, "runid") self.bind_property("show-sidebar", self.win, "show-sidebar") self.bind_property("environment", self.view, "environment") self.bind_property("properties", self.view, "properties") self.bind_property("model", self.view, "model") self.bind_property("summary", self.view, "summary") self.add_window(self.win) def do_activate(self) -> None: """Handle the Adw.Application::activate signal.""" Adw.Application.do_activate(self) self.win.present() def do_shutdown(self) -> None: """Handle the Adw.Application::shutdown signal.""" Adw.Application.do_shutdown(self) class Command(commands.Command): """The `xfstestsdb gtk` command.""" def __init__(self, subparser: argparse.Action, sql: sqlite.Connection) -> None: """Set up the gtk command.""" super().__init__(subparser, sql, "gtk", help="show a gtk-based ui") self.parser.add_argument("runid", metavar="runid", nargs='?', type=int, help="show a specific xfstests run") def do_command(self, args: argparse.Namespace) -> None: """Run the Gtk UI.""" app_args = [] if args.runid is not None: if self.sql("SELECT 1 FROM xfstests_runs WHERE runid=?", args.runid).fetchone(): app_args.append(f"runid={args.runid}") else: print(f"error: run #{args.runid} does not exist", file=sys.stderr) sys.exit(errno.ENOENT) else: app_args.append("show-sidebar") Application(self.sql).run(app_args)