Merge pull request #399 from coreemu/coretk-enhance/execute-xml-script
Coretk enhance/execute xml script
This commit is contained in:
commit
d076229973
4 changed files with 99 additions and 1 deletions
|
@ -16,6 +16,7 @@ MOBILITY_PATH = HOME_PATH.joinpath("mobility")
|
||||||
XMLS_PATH = HOME_PATH.joinpath("xmls")
|
XMLS_PATH = HOME_PATH.joinpath("xmls")
|
||||||
CONFIG_PATH = HOME_PATH.joinpath("gui.yaml")
|
CONFIG_PATH = HOME_PATH.joinpath("gui.yaml")
|
||||||
LOG_PATH = HOME_PATH.joinpath("gui.log")
|
LOG_PATH = HOME_PATH.joinpath("gui.log")
|
||||||
|
SCRIPT_PATH = HOME_PATH.joinpath("scripts")
|
||||||
|
|
||||||
# local paths
|
# local paths
|
||||||
DATA_PATH = Path(__file__).parent.joinpath("data")
|
DATA_PATH = Path(__file__).parent.joinpath("data")
|
||||||
|
@ -67,6 +68,7 @@ def check_directory():
|
||||||
ICONS_PATH.mkdir()
|
ICONS_PATH.mkdir()
|
||||||
MOBILITY_PATH.mkdir()
|
MOBILITY_PATH.mkdir()
|
||||||
XMLS_PATH.mkdir()
|
XMLS_PATH.mkdir()
|
||||||
|
SCRIPT_PATH.mkdir()
|
||||||
|
|
||||||
copy_files(LOCAL_ICONS_PATH, ICONS_PATH)
|
copy_files(LOCAL_ICONS_PATH, ICONS_PATH)
|
||||||
copy_files(LOCAL_BACKGROUND_PATH, BACKGROUNDS_PATH)
|
copy_files(LOCAL_BACKGROUND_PATH, BACKGROUNDS_PATH)
|
||||||
|
|
|
@ -1063,3 +1063,9 @@ class CoreClient:
|
||||||
|
|
||||||
def service_been_modified(self, node_id: int) -> bool:
|
def service_been_modified(self, node_id: int) -> bool:
|
||||||
return node_id in self.modified_service_nodes
|
return node_id in self.modified_service_nodes
|
||||||
|
|
||||||
|
def execute_script(self, script):
|
||||||
|
response = self.client.execute_script(script)
|
||||||
|
logging.info("execute python script %s", response)
|
||||||
|
if response.session_id != -1:
|
||||||
|
self.join_session(response.session_id)
|
||||||
|
|
85
daemon/core/gui/dialogs/executepython.py
Normal file
85
daemon/core/gui/dialogs/executepython.py
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
import logging
|
||||||
|
import tkinter as tk
|
||||||
|
from tkinter import filedialog, ttk
|
||||||
|
|
||||||
|
from core.gui.appconfig import SCRIPT_PATH
|
||||||
|
from core.gui.dialogs.dialog import Dialog
|
||||||
|
from core.gui.themes import FRAME_PAD, PADX
|
||||||
|
|
||||||
|
|
||||||
|
class ExecutePythonDialog(Dialog):
|
||||||
|
def __init__(self, master, app):
|
||||||
|
super().__init__(master, app, "Execute Python Script", modal=True)
|
||||||
|
self.app = app
|
||||||
|
self.with_options = tk.IntVar(value=0)
|
||||||
|
self.options = tk.StringVar(value="")
|
||||||
|
self.option_entry = None
|
||||||
|
self.file_entry = None
|
||||||
|
self.draw()
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
i = 0
|
||||||
|
frame = ttk.Frame(self.top, padding=FRAME_PAD)
|
||||||
|
frame.columnconfigure(0, weight=1)
|
||||||
|
frame.columnconfigure(1, weight=1)
|
||||||
|
frame.grid(row=i, column=0, sticky="nsew")
|
||||||
|
i = i + 1
|
||||||
|
var = tk.StringVar(value="")
|
||||||
|
self.file_entry = ttk.Entry(frame, textvariable=var)
|
||||||
|
self.file_entry.grid(row=0, column=0, sticky="ew")
|
||||||
|
button = ttk.Button(frame, text="...", command=self.select_file)
|
||||||
|
button.grid(row=0, column=1, sticky="ew")
|
||||||
|
|
||||||
|
self.top.columnconfigure(0, weight=1)
|
||||||
|
button = ttk.Checkbutton(
|
||||||
|
self.top,
|
||||||
|
text="With Options",
|
||||||
|
variable=self.with_options,
|
||||||
|
command=self.add_options,
|
||||||
|
)
|
||||||
|
button.grid(row=i, column=0, sticky="ew")
|
||||||
|
i = i + 1
|
||||||
|
|
||||||
|
label = ttk.Label(
|
||||||
|
self.top, text="Any command-line options for running the Python script"
|
||||||
|
)
|
||||||
|
label.grid(row=i, column=0, sticky="ew")
|
||||||
|
i = i + 1
|
||||||
|
self.option_entry = ttk.Entry(
|
||||||
|
self.top, textvariable=self.options, state="disabled"
|
||||||
|
)
|
||||||
|
self.option_entry.grid(row=i, column=0, sticky="ew")
|
||||||
|
i = i + 1
|
||||||
|
|
||||||
|
frame = ttk.Frame(self.top, padding=FRAME_PAD)
|
||||||
|
frame.columnconfigure(0, weight=1)
|
||||||
|
frame.columnconfigure(1, weight=1)
|
||||||
|
frame.grid(row=i, column=0)
|
||||||
|
button = ttk.Button(frame, text="Execute", command=self.script_execute)
|
||||||
|
button.grid(row=0, column=0, sticky="ew", padx=PADX)
|
||||||
|
button = ttk.Button(frame, text="Cancel", command=self.destroy)
|
||||||
|
button.grid(row=0, column=1, sticky="ew", padx=PADX)
|
||||||
|
|
||||||
|
def add_options(self):
|
||||||
|
if self.with_options.get():
|
||||||
|
self.option_entry.configure(state="normal")
|
||||||
|
else:
|
||||||
|
self.option_entry.configure(state="disabled")
|
||||||
|
|
||||||
|
def select_file(self):
|
||||||
|
file = filedialog.askopenfilename(
|
||||||
|
parent=self.top,
|
||||||
|
initialdir=str(SCRIPT_PATH),
|
||||||
|
title="Open python script",
|
||||||
|
filetypes=((".py Files", "*.py"), ("All Files", "*")),
|
||||||
|
)
|
||||||
|
if file:
|
||||||
|
self.file_entry.delete(0, "end")
|
||||||
|
self.file_entry.insert("end", file)
|
||||||
|
|
||||||
|
def script_execute(self):
|
||||||
|
file = self.file_entry.get()
|
||||||
|
options = self.option_entry.get()
|
||||||
|
logging.info("Execute %s with options %s", file, options)
|
||||||
|
self.app.core.execute_script(file)
|
||||||
|
self.destroy()
|
|
@ -6,6 +6,7 @@ from typing import TYPE_CHECKING
|
||||||
|
|
||||||
import core.gui.menuaction as action
|
import core.gui.menuaction as action
|
||||||
from core.gui.coreclient import OBSERVERS
|
from core.gui.coreclient import OBSERVERS
|
||||||
|
from core.gui.dialogs.executepython import ExecutePythonDialog
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from core.gui.app import Application
|
from core.gui.app import Application
|
||||||
|
@ -68,7 +69,7 @@ class Menubar(tk.Menu):
|
||||||
menu.add_cascade(label="Recent files", menu=self.recent_menu)
|
menu.add_cascade(label="Recent files", menu=self.recent_menu)
|
||||||
menu.add_separator()
|
menu.add_separator()
|
||||||
menu.add_command(label="Export Python script...", state=tk.DISABLED)
|
menu.add_command(label="Export Python script...", state=tk.DISABLED)
|
||||||
menu.add_command(label="Execute XML or Python script...", state=tk.DISABLED)
|
menu.add_command(label="Execute Python script...", command=self.execute_python)
|
||||||
menu.add_command(
|
menu.add_command(
|
||||||
label="Execute Python script with options...", state=tk.DISABLED
|
label="Execute Python script with options...", state=tk.DISABLED
|
||||||
)
|
)
|
||||||
|
@ -442,6 +443,10 @@ class Menubar(tk.Menu):
|
||||||
else:
|
else:
|
||||||
self.menuaction.file_save_as_xml()
|
self.menuaction.file_save_as_xml()
|
||||||
|
|
||||||
|
def execute_python(self):
|
||||||
|
dialog = ExecutePythonDialog(self.app, self.app)
|
||||||
|
dialog.show()
|
||||||
|
|
||||||
def change_menubar_item_state(self, is_runtime: bool):
|
def change_menubar_item_state(self, is_runtime: bool):
|
||||||
for i in range(self.edit_menu.index("end")):
|
for i in range(self.edit_menu.index("end")):
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Add table
Reference in a new issue