pygui cleanup sessions dialog

This commit is contained in:
Blake Harnden 2020-04-19 23:02:25 -07:00
parent 3233d8ab58
commit b4de016a24
2 changed files with 78 additions and 85 deletions

View file

@ -283,6 +283,12 @@ class CoreClient:
self.session_id, self.handle_events self.session_id, self.handle_events
) )
# get session service defaults
response = self.client.get_service_defaults(self.session_id)
self.default_services = {
x.node_type: set(x.services) for x in response.defaults
}
# get location # get location
if query_location: if query_location:
response = self.client.get_session_location(self.session_id) response = self.client.get_session_location(self.session_id)
@ -483,10 +489,6 @@ class CoreClient:
else: else:
dialog = SessionsDialog(self.app, self.app, True) dialog = SessionsDialog(self.app, self.app, True)
dialog.show() dialog.show()
response = self.client.get_service_defaults(self.session_id)
self.default_services = {
x.node_type: set(x.services) for x in response.defaults
}
except grpc.RpcError as e: except grpc.RpcError as e:
show_grpc_error(e, self.app, self.app) show_grpc_error(e, self.app, self.app)
self.app.close() self.app.close()

View file

@ -1,7 +1,7 @@
import logging import logging
import tkinter as tk import tkinter as tk
from tkinter import ttk from tkinter import messagebox, ttk
from typing import TYPE_CHECKING, Iterable from typing import TYPE_CHECKING, List
import grpc import grpc
@ -19,35 +19,35 @@ if TYPE_CHECKING:
class SessionsDialog(Dialog): class SessionsDialog(Dialog):
def __init__( def __init__(
self, master: "Application", app: "Application", is_start_app: bool = False self, master: "Application", app: "Application", is_start_app: bool = False
): ) -> None:
super().__init__(master, app, "Sessions", modal=True) super().__init__(master, app, "Sessions", modal=True)
self.is_start_app = is_start_app self.is_start_app = is_start_app
self.selected = False self.selected_session = None
self.selected_id = None self.selected_id = None
self.tree = None self.tree = None
self.has_error = False
self.sessions = self.get_sessions() self.sessions = self.get_sessions()
if not self.has_error: self.connect_button = None
self.draw() self.delete_button = None
self.protocol("WM_DELETE_WINDOW", self.on_closing)
self.draw()
def get_sessions(self) -> Iterable[core_pb2.SessionSummary]: def get_sessions(self) -> List[core_pb2.SessionSummary]:
try: try:
response = self.app.core.client.get_sessions() response = self.app.core.client.get_sessions()
logging.info("sessions: %s", response) logging.info("sessions: %s", response)
return response.sessions return response.sessions
except grpc.RpcError as e: except grpc.RpcError as e:
show_grpc_error(e, self.app, self.app) show_grpc_error(e, self.app, self.app)
self.has_error = True
self.destroy() self.destroy()
def draw(self): def draw(self) -> None:
self.top.columnconfigure(0, weight=1) self.top.columnconfigure(0, weight=1)
self.top.rowconfigure(1, weight=1) self.top.rowconfigure(1, weight=1)
self.draw_description() self.draw_description()
self.draw_tree() self.draw_tree()
self.draw_buttons() self.draw_buttons()
def draw_description(self): def draw_description(self) -> None:
""" """
write a short description write a short description
""" """
@ -61,13 +61,16 @@ class SessionsDialog(Dialog):
) )
label.grid(pady=PADY) label.grid(pady=PADY)
def draw_tree(self): def draw_tree(self) -> None:
frame = ttk.Frame(self.top) frame = ttk.Frame(self.top)
frame.columnconfigure(0, weight=1) frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1) frame.rowconfigure(0, weight=1)
frame.grid(sticky="nsew", pady=PADY) frame.grid(sticky="nsew", pady=PADY)
self.tree = ttk.Treeview( self.tree = ttk.Treeview(
frame, columns=("id", "state", "nodes"), show="headings" frame,
columns=("id", "state", "nodes"),
show="headings",
selectmode=tk.BROWSE,
) )
self.tree.grid(sticky="nsew") self.tree.grid(sticky="nsew")
self.tree.column("id", stretch=tk.YES) self.tree.column("id", stretch=tk.YES)
@ -85,7 +88,7 @@ class SessionsDialog(Dialog):
text=str(session.id), text=str(session.id),
values=(session.id, state_name, session.nodes), values=(session.id, state_name, session.nodes),
) )
self.tree.bind("<Double-1>", self.on_selected) self.tree.bind("<Double-1>", self.double_click_join)
self.tree.bind("<<TreeviewSelect>>", self.click_select) self.tree.bind("<<TreeviewSelect>>", self.click_select)
yscrollbar = ttk.Scrollbar(frame, orient="vertical", command=self.tree.yview) yscrollbar = ttk.Scrollbar(frame, orient="vertical", command=self.tree.yview)
@ -96,9 +99,9 @@ class SessionsDialog(Dialog):
xscrollbar.grid(row=1, sticky="ew") xscrollbar.grid(row=1, sticky="ew")
self.tree.configure(xscrollcommand=xscrollbar.set) self.tree.configure(xscrollcommand=xscrollbar.set)
def draw_buttons(self): def draw_buttons(self) -> None:
frame = ttk.Frame(self.top) frame = ttk.Frame(self.top)
for i in range(5): for i in range(4):
frame.columnconfigure(i, weight=1) frame.columnconfigure(i, weight=1)
frame.grid(sticky="ew") frame.grid(sticky="ew")
@ -110,42 +113,37 @@ class SessionsDialog(Dialog):
b.grid(row=0, padx=PADX, sticky="ew") b.grid(row=0, padx=PADX, sticky="ew")
image = Images.get(ImageEnum.FILEOPEN, 16) image = Images.get(ImageEnum.FILEOPEN, 16)
b = ttk.Button( self.connect_button = ttk.Button(
frame, frame,
image=image, image=image,
text="Connect", text="Connect",
compound=tk.LEFT, compound=tk.LEFT,
command=self.click_connect, command=self.click_connect,
state=tk.DISABLED,
) )
b.image = image self.connect_button.image = image
b.grid(row=0, column=1, padx=PADX, sticky="ew") self.connect_button.grid(row=0, column=1, padx=PADX, sticky="ew")
image = Images.get(ImageEnum.SHUTDOWN, 16)
b = ttk.Button(
frame,
image=image,
text="Shutdown",
compound=tk.LEFT,
command=self.click_shutdown,
)
b.image = image
b.grid(row=0, column=2, padx=PADX, sticky="ew")
image = Images.get(ImageEnum.DELETE, 16) image = Images.get(ImageEnum.DELETE, 16)
b = ttk.Button( self.delete_button = ttk.Button(
frame, frame,
image=image, image=image,
text="Delete", text="Delete",
compound=tk.LEFT, compound=tk.LEFT,
command=self.click_delete, command=self.click_delete,
state=tk.DISABLED,
) )
b.image = image self.delete_button.image = image
b.grid(row=0, column=3, padx=PADX, sticky="ew") self.delete_button.grid(row=0, column=2, padx=PADX, sticky="ew")
image = Images.get(ImageEnum.CANCEL, 16) image = Images.get(ImageEnum.CANCEL, 16)
if self.is_start_app: if self.is_start_app:
b = ttk.Button( b = ttk.Button(
frame, image=image, text="Exit", compound=tk.LEFT, command=self.destroy frame,
image=image,
text="Exit",
compound=tk.LEFT,
command=self.click_exit,
) )
else: else:
b = ttk.Button( b = ttk.Button(
@ -156,42 +154,32 @@ class SessionsDialog(Dialog):
command=self.destroy, command=self.destroy,
) )
b.image = image b.image = image
b.grid(row=0, column=4, sticky="ew") b.grid(row=0, column=3, sticky="ew")
def click_new(self): def click_new(self) -> None:
self.app.core.create_new_session() self.app.core.create_new_session()
self.destroy() self.destroy()
def click_select(self, event: tk.Event): def click_select(self, _event: tk.Event = None) -> None:
item = self.tree.selection() item = self.tree.selection()
session_id = int(self.tree.item(item, "text")) if item:
self.selected = True self.selected_session = int(self.tree.item(item, "text"))
self.selected_id = session_id self.selected_id = item
self.delete_button.config(state=tk.NORMAL)
def click_connect(self): self.connect_button.config(state=tk.NORMAL)
"""
if no session is selected yet, create a new one else join that session
"""
if self.selected and self.selected_id is not None:
self.join_session(self.selected_id)
elif not self.selected and self.selected_id is None:
self.click_new()
else: else:
logging.error("sessions invalid state") self.selected_session = None
self.selected_id = None
self.delete_button.config(state=tk.DISABLED)
self.connect_button.config(state=tk.DISABLED)
logging.debug("selected session: %s", self.selected_session)
def click_shutdown(self): def click_connect(self) -> None:
""" if not self.selected_session:
if no session is currently selected create a new session else shut the selected return
session down. self.join_session(self.selected_session)
"""
if self.selected and self.selected_id is not None:
self.shutdown_session(self.selected_id)
elif not self.selected and self.selected_id is None:
self.click_new()
else:
logging.error("querysessiondrawing.py invalid state")
def join_session(self, session_id: int): def join_session(self, session_id: int) -> None:
if self.app.core.xml_file: if self.app.core.xml_file:
self.app.core.xml_file = None self.app.core.xml_file = None
self.app.statusbar.progress_bar.start(5) self.app.statusbar.progress_bar.start(5)
@ -199,26 +187,29 @@ class SessionsDialog(Dialog):
task.start() task.start()
self.destroy() self.destroy()
def on_selected(self, event: tk.Event): def double_click_join(self, _event: tk.Event) -> None:
item = self.tree.selection() item = self.tree.selection()
sid = int(self.tree.item(item, "text")) if item is None:
self.join_session(sid) return
session_id = int(self.tree.item(item, "text"))
self.join_session(session_id)
def shutdown_session(self, sid: int): def click_delete(self) -> None:
self.app.core.stop_session(sid) if not self.selected_session:
self.click_new() return
logging.debug("delete session: %s", self.selected_session)
# self.app.core.delete_session(self.selected_id, self.top)
self.tree.delete(self.selected_id)
self.app.core.delete_session(self.selected_session)
if self.selected_session == self.app.core.session_id:
self.click_new()
self.destroy()
self.click_select()
def click_exit(self) -> None:
self.destroy() self.destroy()
self.app.close()
def click_delete(self): def on_closing(self) -> None:
logging.debug("Click delete") if self.is_start_app and messagebox.askokcancel("Exit", "Quit?", parent=self):
item = self.tree.selection() self.click_exit()
if item:
sid = int(self.tree.item(item, "text"))
self.app.core.delete_session(sid, self.top)
self.tree.delete(item[0])
if sid == self.app.core.session_id:
self.click_new()
selections = self.tree.get_children()
if selections:
self.tree.focus(selections[0])
self.tree.selection_set(selections[0])