2023-11-25 00:55:01 +00:00
|
|
|
#!/usr/bin/env python3
|
2023-12-16 11:52:10 +00:00
|
|
|
import argparse
|
|
|
|
from dataclasses import dataclass
|
2023-12-16 12:03:10 +00:00
|
|
|
|
2023-11-25 00:55:01 +00:00
|
|
|
import gi
|
2023-12-16 12:03:10 +00:00
|
|
|
|
2023-11-25 00:55:01 +00:00
|
|
|
gi.require_version("Gtk", "3.0")
|
2023-12-16 11:52:10 +00:00
|
|
|
from clan_cli.clan_uri import ClanURI
|
2023-12-16 12:03:10 +00:00
|
|
|
from gi.repository import Gio, Gtk
|
|
|
|
|
2023-11-28 17:19:01 +00:00
|
|
|
from .constants import constants
|
2023-12-16 12:03:10 +00:00
|
|
|
from .interfaces import Callbacks, InitialJoinValues
|
2023-12-16 11:52:10 +00:00
|
|
|
from .windows.join import JoinWindow
|
|
|
|
from .windows.overview import OverviewWindow
|
2023-12-16 12:03:10 +00:00
|
|
|
|
2023-12-16 11:52:10 +00:00
|
|
|
|
|
|
|
@dataclass
|
2023-12-16 12:03:10 +00:00
|
|
|
class ClanWindows:
|
2023-12-16 12:05:17 +00:00
|
|
|
join: type[JoinWindow]
|
|
|
|
overview: type[OverviewWindow]
|
|
|
|
|
2023-11-28 17:19:01 +00:00
|
|
|
|
2023-12-16 11:52:10 +00:00
|
|
|
@dataclass
|
2023-12-16 12:03:10 +00:00
|
|
|
class ClanConfig:
|
2023-12-16 11:52:10 +00:00
|
|
|
initial_window: str
|
2023-12-16 12:04:50 +00:00
|
|
|
url: ClanURI | None
|
2023-12-16 12:05:17 +00:00
|
|
|
|
2023-11-28 18:55:40 +00:00
|
|
|
|
2023-11-28 17:19:01 +00:00
|
|
|
class Application(Gtk.Application):
|
2023-12-16 11:52:10 +00:00
|
|
|
def __init__(self, windows: ClanWindows, config: ClanConfig) -> None:
|
2023-11-28 17:19:01 +00:00
|
|
|
super().__init__(
|
|
|
|
application_id=constants["APPID"], flags=Gio.ApplicationFlags.FLAGS_NONE
|
|
|
|
)
|
|
|
|
self.init_style()
|
2023-12-16 11:52:10 +00:00
|
|
|
self.windows = windows
|
|
|
|
initial = windows.__dict__[config.initial_window]
|
2023-12-16 13:49:08 +00:00
|
|
|
self.cbs = Callbacks(show_list=self.show_list, show_join=self.show_join)
|
2023-12-16 12:05:17 +00:00
|
|
|
if issubclass(initial, JoinWindow):
|
2023-12-16 11:52:10 +00:00
|
|
|
# see JoinWindow constructor
|
2023-12-16 12:05:17 +00:00
|
|
|
self.window = initial(
|
|
|
|
initial_values=InitialJoinValues(url=config.url or ""),
|
2023-12-16 13:49:08 +00:00
|
|
|
cbs=self.cbs,
|
2023-12-16 12:05:17 +00:00
|
|
|
)
|
2023-12-16 11:52:10 +00:00
|
|
|
|
2023-12-16 12:05:17 +00:00
|
|
|
if issubclass(initial, OverviewWindow):
|
2023-12-16 11:52:10 +00:00
|
|
|
# see OverviewWindow constructor
|
2023-12-16 13:49:08 +00:00
|
|
|
self.window = initial(cbs=self.cbs)
|
2023-12-16 11:52:10 +00:00
|
|
|
|
2023-12-16 12:05:17 +00:00
|
|
|
def show_list(self) -> None:
|
2023-12-16 11:52:10 +00:00
|
|
|
prev = self.window
|
2023-12-16 13:49:08 +00:00
|
|
|
self.window = self.windows.__dict__["overview"](cbs=self.cbs)
|
2023-12-16 11:52:10 +00:00
|
|
|
prev.hide()
|
|
|
|
|
2023-12-16 13:49:08 +00:00
|
|
|
def show_join(self) -> None:
|
2023-12-16 11:52:10 +00:00
|
|
|
prev = self.window
|
2023-12-16 13:49:08 +00:00
|
|
|
self.window = self.windows.__dict__["join"](
|
|
|
|
cbs=self.cbs, initial_values=InitialJoinValues(url="")
|
|
|
|
)
|
2023-12-16 11:52:10 +00:00
|
|
|
prev.hide()
|
2023-11-28 17:19:01 +00:00
|
|
|
|
2023-11-30 12:42:15 +00:00
|
|
|
def do_startup(self) -> None:
|
2023-11-28 17:19:01 +00:00
|
|
|
Gtk.Application.do_startup(self)
|
2023-12-06 17:38:19 +00:00
|
|
|
Gtk.init()
|
2023-11-28 17:19:01 +00:00
|
|
|
|
2023-11-30 12:42:15 +00:00
|
|
|
def do_activate(self) -> None:
|
2023-11-28 17:19:01 +00:00
|
|
|
win = self.props.active_window
|
|
|
|
if not win:
|
2023-12-08 10:56:27 +00:00
|
|
|
win = self.window
|
|
|
|
win.set_application(self)
|
2023-11-28 17:19:01 +00:00
|
|
|
win.present()
|
|
|
|
|
|
|
|
# TODO: For css styling
|
2023-11-30 12:42:15 +00:00
|
|
|
def init_style(self) -> None:
|
2023-11-28 17:19:01 +00:00
|
|
|
pass
|
|
|
|
# css_provider = Gtk.CssProvider()
|
|
|
|
# css_provider.load_from_resource(constants['RESOURCEID'] + '/style.css')
|
|
|
|
# screen = Gdk.Screen.get_default()
|
|
|
|
# style_context = Gtk.StyleContext()
|
|
|
|
# style_context.add_provider_for_screen(screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
|
2023-12-16 11:52:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
def show_join(args: argparse.Namespace) -> None:
|
|
|
|
print(f"Joining clan {args.clan_uri}")
|
2023-12-16 12:05:17 +00:00
|
|
|
app = Application(
|
|
|
|
windows=ClanWindows(join=JoinWindow, overview=OverviewWindow),
|
|
|
|
config=ClanConfig(url=args.clan_uri, initial_window="join"),
|
|
|
|
)
|
2023-12-16 11:52:10 +00:00
|
|
|
return app.run()
|
|
|
|
|
|
|
|
|
|
|
|
def register_join_parser(parser: argparse.ArgumentParser) -> None:
|
|
|
|
parser.add_argument("clan_uri", type=ClanURI, help="clan URI to join")
|
|
|
|
parser.set_defaults(func=show_join)
|
|
|
|
|
|
|
|
|
|
|
|
def show_overview(args: argparse.Namespace) -> None:
|
2023-12-16 12:05:17 +00:00
|
|
|
app = Application(
|
|
|
|
windows=ClanWindows(join=JoinWindow, overview=OverviewWindow),
|
|
|
|
config=ClanConfig(url=None, initial_window="overview"),
|
|
|
|
)
|
2023-12-16 11:52:10 +00:00
|
|
|
return app.run()
|
|
|
|
|
|
|
|
|
|
|
|
def register_overview_parser(parser: argparse.ArgumentParser) -> None:
|
|
|
|
parser.set_defaults(func=show_overview)
|
2023-12-19 13:36:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Define a function that writes to the memfd
|
2023-12-19 15:44:26 +00:00
|
|
|
def dummy_f(msg: str) -> None:
|
2023-12-19 13:36:37 +00:00
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
2023-12-19 15:44:26 +00:00
|
|
|
print(f"Receeived message {msg}")
|
2023-12-19 13:36:37 +00:00
|
|
|
c = 0
|
|
|
|
while True: # Simulate a long running process
|
|
|
|
print(f"out: Hello from process c={c}", file=sys.stdout)
|
|
|
|
print(f"err: Hello from process c={c}", file=sys.stderr)
|
|
|
|
user = input("Enter to continue: \n")
|
|
|
|
if user == "q":
|
|
|
|
raise Exception("User quit")
|
|
|
|
print(f"User entered {user}", file=sys.stdout)
|
|
|
|
print(f"User entered {user}", file=sys.stderr)
|
|
|
|
time.sleep(1) # Wait for 1 second
|
|
|
|
c += 1
|
|
|
|
|
|
|
|
|
|
|
|
def show_run_vm(parser: argparse.ArgumentParser) -> None:
|
2023-12-19 16:51:43 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
2023-12-19 13:36:37 +00:00
|
|
|
from .executor import spawn
|
|
|
|
|
2023-12-19 16:51:43 +00:00
|
|
|
log_path = Path(".").resolve()
|
|
|
|
proc = spawn(wait_stdin_con=True, log_path=log_path, func=dummy_f, msg="Hello")
|
2023-12-19 15:44:26 +00:00
|
|
|
input("Press enter to kill process: ")
|
|
|
|
proc.kill_group()
|
2023-12-19 13:36:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def register_run_parser(parser: argparse.ArgumentParser) -> None:
|
|
|
|
parser.set_defaults(func=show_run_vm)
|