pygui: fixed and changed custom service copy to focus only on copying the current file displayed from any other nodes with a customized version

This commit is contained in:
Blake Harnden 2020-06-23 16:11:39 -07:00
parent 60d9fe2026
commit 6490b5b9cb
2 changed files with 77 additions and 155 deletions

View file

@ -4,80 +4,58 @@ copy service config dialog
import tkinter as tk import tkinter as tk
from tkinter import ttk from tkinter import ttk
from typing import TYPE_CHECKING, Tuple from typing import TYPE_CHECKING, Dict, Optional
from core.gui.dialogs.dialog import Dialog from core.gui.dialogs.dialog import Dialog
from core.gui.themes import FRAME_PAD, PADX from core.gui.themes import PADX, PADY
from core.gui.widgets import CodeText from core.gui.widgets import CodeText, ListboxScroll
if TYPE_CHECKING: if TYPE_CHECKING:
from core.gui.app import Application from core.gui.app import Application
from core.gui.dialogs.serviceconfig import ServiceConfigDialog
class CopyServiceConfigDialog(Dialog): class CopyServiceConfigDialog(Dialog):
def __init__(self, master: tk.BaseWidget, app: "Application", node_id: int) -> None: def __init__(
super().__init__(app, f"Copy services to node {node_id}", master=master) self,
self.parent = master app: "Application",
self.node_id = node_id dialog: "ServiceConfigDialog",
self.service_configs = app.core.service_configs name: str,
self.file_configs = app.core.file_configs service: str,
self.tree = None file_name: str,
) -> None:
super().__init__(app, f"Copy Custom File to {name}", master=dialog)
self.dialog: "ServiceConfigDialog" = dialog
self.service: str = service
self.file_name: str = file_name
self.listbox: Optional[tk.Listbox] = None
self.nodes: Dict[str, int] = {}
self.draw() self.draw()
def draw(self) -> None: def draw(self) -> None:
self.top.columnconfigure(0, weight=1) self.top.columnconfigure(0, weight=1)
self.tree = ttk.Treeview(self.top) self.top.rowconfigure(1, weight=1)
self.tree.grid(row=0, column=0, sticky="ew", padx=PADX) label = ttk.Label(
self.tree["columns"] = () self.top, text=f"{self.service} - {self.file_name}", anchor=tk.CENTER
self.tree.column("#0", width=270, minwidth=270, stretch=tk.YES) )
self.tree.heading("#0", text="Service configuration items", anchor=tk.CENTER) label.grid(sticky="ew", pady=PADY)
custom_nodes = set(self.service_configs).union(set(self.file_configs))
for nid in custom_nodes: listbox_scroll = ListboxScroll(self.top)
treeid = self.tree.insert("", "end", text=f"n{nid}", tags="node") listbox_scroll.grid(sticky="nsew", pady=PADY)
services = self.service_configs.get(nid, None) self.listbox = listbox_scroll.listbox
files = self.file_configs.get(nid, None) for canvas_node in self.app.canvas.nodes.values():
tree_ids = {} file_configs = canvas_node.service_file_configs.get(self.service)
if services: if not file_configs:
for service, config in services.items(): continue
serviceid = self.tree.insert( data = file_configs.get(self.file_name)
treeid, "end", text=service, tags="service" if not data:
) continue
tree_ids[service] = serviceid name = canvas_node.core_node.name
cmdup = config.startup[:] self.nodes[name] = canvas_node.id
cmddown = config.shutdown[:] self.listbox.insert(tk.END, name)
cmdval = config.validate[:]
self.tree.insert(
serviceid,
"end",
text=f"cmdup=({str(cmdup)[1:-1]})",
tags=("cmd", "up"),
)
self.tree.insert(
serviceid,
"end",
text=f"cmddown=({str(cmddown)[1:-1]})",
tags=("cmd", "down"),
)
self.tree.insert(
serviceid,
"end",
text=f"cmdval=({str(cmdval)[1:-1]})",
tags=("cmd", "val"),
)
if files:
for service, configs in files.items():
if service in tree_ids:
serviceid = tree_ids[service]
else:
serviceid = self.tree.insert(
treeid, "end", text=service, tags="service"
)
tree_ids[service] = serviceid
for filename, data in configs.items():
self.tree.insert(serviceid, "end", text=filename, tags="file")
frame = ttk.Frame(self.top) frame = ttk.Frame(self.top)
frame.grid(row=1, column=0) frame.grid(sticky="ew")
for i in range(3): for i in range(3):
frame.columnconfigure(i, weight=1) frame.columnconfigure(i, weight=1)
button = ttk.Button(frame, text="Copy", command=self.click_copy) button = ttk.Button(frame, text="Copy", command=self.click_copy)
@ -85,118 +63,58 @@ class CopyServiceConfigDialog(Dialog):
button = ttk.Button(frame, text="View", command=self.click_view) button = ttk.Button(frame, text="View", command=self.click_view)
button.grid(row=0, column=1, sticky="ew", padx=PADX) button.grid(row=0, column=1, sticky="ew", padx=PADX)
button = ttk.Button(frame, text="Cancel", command=self.destroy) button = ttk.Button(frame, text="Cancel", command=self.destroy)
button.grid(row=0, column=2, sticky="ew", padx=PADX) button.grid(row=0, column=2, sticky="ew")
def click_copy(self) -> None: def click_copy(self) -> None:
selected = self.tree.selection() selection = self.listbox.curselection()
if selected: if not selection:
item = self.tree.item(selected[0]) return
if "file" in item["tags"]: name = self.listbox.get(selection)
filename = item["text"] canvas_node_id = self.nodes[name]
nid, service = self.get_node_service(selected) canvas_node = self.app.canvas.nodes[canvas_node_id]
data = self.file_configs[nid][service][filename] data = canvas_node.service_file_configs[self.service][self.file_name]
if service == self.parent.service_name: self.dialog.temp_service_files[self.file_name] = data
self.parent.temp_service_files[filename] = data self.dialog.modified_files.add(self.file_name)
self.parent.modified_files.add(filename) self.dialog.service_file_data.text.delete(1.0, tk.END)
if self.parent.filename_combobox.get() == filename: self.dialog.service_file_data.text.insert(tk.END, data)
self.parent.service_file_data.text.delete(1.0, "end")
self.parent.service_file_data.text.insert("end", data)
if "cmd" in item["tags"]:
nid, service = self.get_node_service(selected)
if service == self.master.service_name:
cmds = self.service_configs[nid][service]
if "up" in item["tags"]:
self.master.append_commands(
self.master.startup_commands,
self.master.startup_commands_listbox,
cmds.startup,
)
elif "down" in item["tags"]:
self.master.append_commands(
self.master.shutdown_commands,
self.master.shutdown_commands_listbox,
cmds.shutdown,
)
elif "val" in item["tags"]:
self.master.append_commands(
self.master.validate_commands,
self.master.validate_commands_listbox,
cmds.validate,
)
self.destroy() self.destroy()
def click_view(self) -> None: def click_view(self) -> None:
selected = self.tree.selection() selection = self.listbox.curselection()
data = "" if not selection:
if selected: return
item = self.tree.item(selected[0]) name = self.listbox.get(selection)
if "file" in item["tags"]: canvas_node_id = self.nodes[name]
nid, service = self.get_node_service(selected) canvas_node = self.app.canvas.nodes[canvas_node_id]
data = self.file_configs[nid][service][item["text"]] data = canvas_node.service_file_configs[self.service][self.file_name]
dialog = ViewConfigDialog( dialog = ViewConfigDialog(
self, self.app, nid, data, item["text"].split("/")[-1] self.app, self, name, self.service, self.file_name, data
) )
dialog.show() dialog.show()
if "cmd" in item["tags"]:
nid, service = self.get_node_service(selected)
cmds = self.service_configs[nid][service]
if "up" in item["tags"]:
data = f"({str(cmds.startup[:])[1:-1]})"
dialog = ViewConfigDialog(
self, self.app, self.node_id, data, "cmdup"
)
elif "down" in item["tags"]:
data = f"({str(cmds.shutdown[:])[1:-1]})"
dialog = ViewConfigDialog(
self, self.app, self.node_id, data, "cmdup"
)
elif "val" in item["tags"]:
data = f"({str(cmds.validate[:])[1:-1]})"
dialog = ViewConfigDialog(
self, self.app, self.node_id, data, "cmdup"
)
dialog.show()
def get_node_service(self, selected: Tuple[str]) -> Tuple[int, str]:
service_tree_id = self.tree.parent(selected[0])
service_name = self.tree.item(service_tree_id)["text"]
node_tree_id = self.tree.parent(service_tree_id)
node_id = int(self.tree.item(node_tree_id)["text"][1:])
return node_id, service_name
class ViewConfigDialog(Dialog): class ViewConfigDialog(Dialog):
def __init__( def __init__(
self, self,
master: tk.BaseWidget,
app: "Application", app: "Application",
node_id: int, master: tk.BaseWidget,
name: str,
service: str,
file_name: str,
data: str, data: str,
filename: str = None,
) -> None: ) -> None:
super().__init__(app, f"n{node_id} config data", master=master) title = f"{name} Service({service}) File({file_name})"
super().__init__(app, title, master=master)
self.data = data self.data = data
self.service_data = None self.service_data = None
self.filepath = tk.StringVar(value=f"/tmp/services.tmp-n{node_id}-{filename}")
self.draw() self.draw()
def draw(self) -> None: def draw(self) -> None:
self.top.columnconfigure(0, weight=1) self.top.columnconfigure(0, weight=1)
frame = ttk.Frame(self.top, padding=FRAME_PAD) self.top.rowconfigure(0, weight=1)
frame.columnconfigure(0, weight=1)
frame.columnconfigure(1, weight=10)
frame.grid(row=0, column=0, sticky="ew")
label = ttk.Label(frame, text="File: ")
label.grid(row=0, column=0, sticky="ew", padx=PADX)
entry = ttk.Entry(frame, textvariable=self.filepath)
entry.config(state="disabled")
entry.grid(row=0, column=1, sticky="ew")
self.service_data = CodeText(self.top) self.service_data = CodeText(self.top)
self.service_data.grid(row=1, column=0, sticky="nsew") self.service_data.grid(sticky="nsew", pady=PADY)
self.service_data.text.insert("end", self.data) self.service_data.text.insert(tk.END, self.data)
self.service_data.text.config(state="disabled") self.service_data.text.config(state=tk.DISABLED)
button = ttk.Button(self.top, text="Close", command=self.destroy) button = ttk.Button(self.top, text="Close", command=self.destroy)
button.grid(row=2, column=0, sticky="ew", padx=PADX) button.grid(sticky="ew")

View file

@ -563,7 +563,11 @@ class ServiceConfigDialog(Dialog):
self.current_service_color("") self.current_service_color("")
def click_copy(self) -> None: def click_copy(self) -> None:
dialog = CopyServiceConfigDialog(self, self.app, self.node_id) file_name = self.filename_combobox.get()
name = self.canvas_node.core_node.name
dialog = CopyServiceConfigDialog(
self.app, self, name, self.service_name, file_name
)
dialog.show() dialog.show()
@classmethod @classmethod