more on python type hint

This commit is contained in:
Huy Pham 2020-01-13 15:31:41 -08:00
parent eb5f2c5648
commit b9b8e3a5f1
32 changed files with 185 additions and 48 deletions

View file

@ -1,5 +1,6 @@
import tkinter as tk
from tkinter import ttk
from typing import Optional
from core.gui import appconfig, themes
from core.gui.coreclient import CoreClient
@ -17,7 +18,7 @@ HEIGHT = 800
class Application(tk.Frame):
def __init__(self, master=None):
def __init__(self, master: Optional[tk.Widget] = None):
super().__init__(master)
# load node icons
NodeUtils.setup()

View file

@ -1,9 +1,13 @@
import tkinter as tk
from tkinter import ttk
from typing import TYPE_CHECKING
from core.gui.dialogs.dialog import Dialog
from core.gui.widgets import CodeText
if TYPE_CHECKING:
from core.gui.app import Application
LICENSE = """\
Copyright (c) 2005-2020, the Boeing Company.
@ -31,7 +35,7 @@ THE POSSIBILITY OF SUCH DAMAGE.\
class AboutDialog(Dialog):
def __init__(self, master, app):
def __init__(self, master: tk.Widget, app: "Application"):
super().__init__(master, app, "About CORE", modal=True)
self.draw()

View file

@ -3,15 +3,19 @@ check engine light
"""
import tkinter as tk
from tkinter import ttk
from typing import TYPE_CHECKING
from core.api.grpc.core_pb2 import ExceptionLevel
from core.gui.dialogs.dialog import Dialog
from core.gui.themes import PADX, PADY
from core.gui.widgets import CodeText
if TYPE_CHECKING:
from core.gui.app import Application
class AlertsDialog(Dialog):
def __init__(self, master, app):
def __init__(self, master: tk.Widget, app: "Application"):
super().__init__(master, app, "Alerts", modal=True)
self.app = app
self.tree = None

View file

@ -3,15 +3,19 @@ size and scale
"""
import tkinter as tk
from tkinter import font, ttk
from typing import TYPE_CHECKING
from core.gui.dialogs.dialog import Dialog
from core.gui.themes import FRAME_PAD, PADX, PADY
if TYPE_CHECKING:
from core.gui.app import Application
PIXEL_SCALE = 100
class SizeAndScaleDialog(Dialog):
def __init__(self, master, app):
def __init__(self, master: "Application", app: "Application"):
"""
create an instance for size and scale object
"""

View file

@ -4,6 +4,7 @@ set wallpaper
import logging
import tkinter as tk
from tkinter import ttk
from typing import TYPE_CHECKING
from core.gui.appconfig import BACKGROUNDS_PATH
from core.gui.dialogs.dialog import Dialog
@ -11,9 +12,12 @@ from core.gui.images import Images
from core.gui.themes import PADX, PADY
from core.gui.widgets import image_chooser
if TYPE_CHECKING:
from core.gui.app import Application
class CanvasWallpaperDialog(Dialog):
def __init__(self, master, app):
def __init__(self, master: tk.Widget, app: "Application"):
"""
create an instance of CanvasWallpaper object

View file

@ -4,12 +4,21 @@ custom color picker
import logging
import tkinter as tk
from tkinter import ttk
from typing import TYPE_CHECKING, Optional
from core.gui.dialogs.dialog import Dialog
if TYPE_CHECKING:
from core.gui.app import Application
class ColorPickerDialog(Dialog):
def __init__(self, master, app, initcolor="#000000"):
def __init__(
self,
master: tk.Widget,
app: "Application",
initcolor: Optional[str] = "#000000",
):
super().__init__(master, app, "color picker", modal=True)
self.red_entry = None
self.blue_entry = None

View file

@ -5,15 +5,19 @@ copy service config dialog
import logging
import tkinter as tk
from tkinter import ttk
from typing import Tuple
from typing import TYPE_CHECKING, Tuple
from core.gui.dialogs.dialog import Dialog
from core.gui.themes import FRAME_PAD, PADX
from core.gui.widgets import CodeText
if TYPE_CHECKING:
from core.gui.app import Application
from core.gui.dialogs.serviceconfig import ServiceConfigDialog
class CopyServiceConfigDialog(Dialog):
def __init__(self, master, app, node_id):
def __init__(self, master: "ServiceConfigDialog", app: "Application", node_id: int):
super().__init__(master, app, f"Copy services to node {node_id}", modal=True)
self.parent = master
self.app = app

View file

@ -2,6 +2,7 @@ import logging
import tkinter as tk
from pathlib import Path
from tkinter import ttk
from typing import TYPE_CHECKING, Set
from core.gui import nodeutils
from core.gui.appconfig import ICONS_PATH
@ -11,9 +12,12 @@ from core.gui.nodeutils import NodeDraw
from core.gui.themes import FRAME_PAD, PADX, PADY
from core.gui.widgets import CheckboxList, ListboxScroll, image_chooser
if TYPE_CHECKING:
from core.gui.app import Application
class ServicesSelectDialog(Dialog):
def __init__(self, master, app, current_services):
def __init__(self, master, app: "Application", current_services: Set[str]):
super().__init__(master, app, "Node Services", modal=True)
self.groups = None
self.services = None
@ -96,7 +100,7 @@ class ServicesSelectDialog(Dialog):
class CustomNodesDialog(Dialog):
def __init__(self, master, app):
def __init__(self, master, app: "Application"):
super().__init__(master, app, "Custom Nodes", modal=True)
self.edit_button = None
self.delete_button = None

View file

@ -1,13 +1,22 @@
import tkinter as tk
from tkinter import ttk
from typing import Optional
from typing import TYPE_CHECKING, Optional
from core.gui.images import ImageEnum, Images
from core.gui.themes import DIALOG_PAD
if TYPE_CHECKING:
from core.gui.app import Application
class Dialog(tk.Toplevel):
def __init__(self, master, app, title, modal=False):
def __init__(
self,
master: tk.Widget,
app: "Application",
title: str,
modal: Optional[bool] = False,
):
super().__init__(master)
self.withdraw()
self.app = app

View file

@ -5,18 +5,24 @@ import logging
import tkinter as tk
import webbrowser
from tkinter import ttk
from typing import TYPE_CHECKING, Optional
import grpc
from core.api.grpc import core_pb2
from core.gui.dialogs.dialog import Dialog
from core.gui.errors import show_grpc_error
from core.gui.images import ImageEnum, Images
from core.gui.themes import PADX, PADY
from core.gui.widgets import ConfigFrame
if TYPE_CHECKING:
from core.gui.app import Application
from core.gui.graph.node import CanvasNode
class GlobalEmaneDialog(Dialog):
def __init__(self, master, app):
def __init__(self, master, app: "Application"):
super().__init__(master, app, "EMANE Configuration", modal=True)
self.config_frame = None
self.draw()
@ -47,7 +53,14 @@ class GlobalEmaneDialog(Dialog):
class EmaneModelDialog(Dialog):
def __init__(self, master, app, node, model, interface=None):
def __init__(
self,
master,
app: "Application",
node: core_pb2.Node,
model: str,
interface: Optional[int] = None,
):
super().__init__(master, app, f"{node.name} {model} Configuration", modal=True)
self.node = node
self.model = f"emane_{model}"
@ -91,7 +104,7 @@ class EmaneModelDialog(Dialog):
class EmaneConfigDialog(Dialog):
def __init__(self, master, app, canvas_node):
def __init__(self, master, app: "Application", canvas_node: "CanvasNode"):
super().__init__(
master, app, f"{canvas_node.core_node.name} EMANE Configuration", modal=True
)

View file

@ -1,14 +1,18 @@
import tkinter as tk
from tkinter import ttk
from typing import TYPE_CHECKING
from core.api.grpc import core_pb2
from core.gui.dialogs.dialog import Dialog
from core.gui.themes import PADX, PADY
from core.gui.widgets import CodeText, ListboxScroll
if TYPE_CHECKING:
from core.gui.app import Application
class HookDialog(Dialog):
def __init__(self, master, app):
def __init__(self, master, app: "Application"):
super().__init__(master, app, "Hook", modal=True)
self.name = tk.StringVar()
self.codetext = None
@ -84,7 +88,7 @@ class HookDialog(Dialog):
class HooksDialog(Dialog):
def __init__(self, master, app):
def __init__(self, master, app: "Application"):
super().__init__(master, app, "Hooks", modal=True)
self.listbox = None
self.edit_button = None

View file

@ -4,13 +4,17 @@ link configuration
import logging
import tkinter as tk
from tkinter import ttk
from typing import Union
from typing import TYPE_CHECKING, Union
from core.api.grpc import core_pb2
from core.gui.dialogs.colorpicker import ColorPickerDialog
from core.gui.dialogs.dialog import Dialog
from core.gui.themes import PADX, PADY
if TYPE_CHECKING:
from core.gui.app import Application
from core.gui.graph.edges import CanvasEdge
def get_int(var: tk.StringVar) -> Union[int, None]:
value = var.get()
@ -20,7 +24,7 @@ def get_int(var: tk.StringVar) -> Union[int, None]:
return None
def get_float(var: tk.StringVar) -> Union[int, None]:
def get_float(var: tk.StringVar) -> Union[float, None]:
value = var.get()
if value != "":
return float(value)
@ -29,7 +33,7 @@ def get_float(var: tk.StringVar) -> Union[int, None]:
class LinkConfigurationDialog(Dialog):
def __init__(self, master, app, edge):
def __init__(self, master, app: "Application", edge: "CanvasEdge"):
super().__init__(master, app, "Link Configuration", modal=True)
self.app = app
self.edge = edge

View file

@ -4,15 +4,21 @@ marker dialog
import tkinter as tk
from tkinter import ttk
from typing import TYPE_CHECKING, Optional
from core.gui.dialogs.colorpicker import ColorPickerDialog
from core.gui.dialogs.dialog import Dialog
if TYPE_CHECKING:
from core.gui.app import Application
MARKER_THICKNESS = [3, 5, 8, 10]
class MarkerDialog(Dialog):
def __init__(self, master, app, initcolor="#000000"):
def __init__(
self, master, app: "Application", initcolor: Optional[str] = "#000000"
):
super().__init__(master, app, "marker tool", modal=False)
self.app = app
self.color = initcolor

View file

@ -2,6 +2,7 @@
mobility configuration
"""
from tkinter import ttk
from typing import TYPE_CHECKING
import grpc
@ -10,9 +11,13 @@ from core.gui.errors import show_grpc_error
from core.gui.themes import PADX, PADY
from core.gui.widgets import ConfigFrame
if TYPE_CHECKING:
from core.gui.app import Application
from core.gui.graph.node import CanvasNode
class MobilityConfigDialog(Dialog):
def __init__(self, master, app, canvas_node):
def __init__(self, master, app: "Application", canvas_node: "CanvasNode"):
super().__init__(
master,
app,

View file

@ -1,5 +1,6 @@
import tkinter as tk
from tkinter import ttk
from typing import TYPE_CHECKING
import grpc
@ -9,11 +10,15 @@ from core.gui.errors import show_grpc_error
from core.gui.images import ImageEnum, Images
from core.gui.themes import PADX, PADY
if TYPE_CHECKING:
from core.gui.app import Application
from core.gui.graph.node import CanvasNode
ICON_SIZE = 16
class MobilityPlayer:
def __init__(self, master, app, canvas_node, config):
def __init__(self, master, app: "Application", canvas_node: "CanvasNode", config):
self.master = master
self.app = app
self.canvas_node = canvas_node

View file

@ -2,6 +2,7 @@ import logging
import tkinter as tk
from functools import partial
from tkinter import ttk
from typing import TYPE_CHECKING
from core.gui import nodeutils
from core.gui.appconfig import ICONS_PATH
@ -12,6 +13,10 @@ from core.gui.nodeutils import NodeUtils
from core.gui.themes import FRAME_PAD, PADX, PADY
from core.gui.widgets import ListboxScroll, image_chooser
if TYPE_CHECKING:
from core.gui.app import Application
from core.gui.graph.node import CanvasNode
def mac_auto(is_auto, entry: ttk.Entry):
logging.info("mac auto clicked")
@ -33,7 +38,7 @@ class InterfaceData:
class NodeConfigDialog(Dialog):
def __init__(self, master, app, canvas_node):
def __init__(self, master, app: "Application", canvas_node: "CanvasNode"):
"""
create an instance of node configuration

View file

@ -3,15 +3,22 @@ core node services
"""
import tkinter as tk
from tkinter import messagebox, ttk
from typing import TYPE_CHECKING
from core.gui.dialogs.dialog import Dialog
from core.gui.dialogs.serviceconfig import ServiceConfigDialog
from core.gui.themes import FRAME_PAD, PADX, PADY
from core.gui.widgets import CheckboxList, ListboxScroll
if TYPE_CHECKING:
from core.gui.app import Application
from core.gui.graph.node import CanvasNode
class NodeServiceDialog(Dialog):
def __init__(self, master, app, canvas_node, services=None):
def __init__(
self, master, app: "Application", canvas_node: "CanvasNode", services=None
):
title = f"{canvas_node.core_node.name} Services"
super().__init__(master, app, title, modal=True)
self.app = app

View file

@ -1,14 +1,18 @@
import tkinter as tk
from tkinter import ttk
from typing import TYPE_CHECKING
from core.gui.coreclient import Observer
from core.gui.dialogs.dialog import Dialog
from core.gui.themes import PADX, PADY
from core.gui.widgets import ListboxScroll
if TYPE_CHECKING:
from core.gui.app import Application
class ObserverDialog(Dialog):
def __init__(self, master, app):
def __init__(self, master, app: "Application"):
super().__init__(master, app, "Observer Widgets", modal=True)
self.observers = None
self.save_button = None

View file

@ -1,14 +1,18 @@
import logging
import tkinter as tk
from tkinter import ttk
from typing import TYPE_CHECKING
from core.gui import appconfig
from core.gui.dialogs.dialog import Dialog
from core.gui.themes import FRAME_PAD, PADX, PADY
if TYPE_CHECKING:
from core.gui.app import Application
class PreferencesDialog(Dialog):
def __init__(self, master, app):
def __init__(self, master, app: "Application"):
super().__init__(master, app, "Preferences", modal=True)
preferences = self.app.guiconfig["preferences"]
self.editor = tk.StringVar(value=preferences["editor"])

View file

@ -1,18 +1,22 @@
import tkinter as tk
from tkinter import ttk
from typing import TYPE_CHECKING
from core.gui.coreclient import CoreServer
from core.gui.dialogs.dialog import Dialog
from core.gui.themes import FRAME_PAD, PADX, PADY
from core.gui.widgets import ListboxScroll
if TYPE_CHECKING:
from core.gui.app import Application
DEFAULT_NAME = "example"
DEFAULT_ADDRESS = "127.0.0.1"
DEFAULT_PORT = 50051
class ServersDialog(Dialog):
def __init__(self, master, app):
def __init__(self, master, app: "Application"):
super().__init__(master, app, "CORE Servers", modal=True)
self.name = tk.StringVar(value=DEFAULT_NAME)
self.address = tk.StringVar(value=DEFAULT_ADDRESS)

View file

@ -1,7 +1,7 @@
"Service configuration dialog"
import tkinter as tk
from tkinter import ttk
from typing import List
from typing import TYPE_CHECKING, List
import grpc
@ -13,9 +13,12 @@ from core.gui.images import ImageEnum, Images
from core.gui.themes import FRAME_PAD, PADX, PADY
from core.gui.widgets import CodeText, ListboxScroll
if TYPE_CHECKING:
from core.gui.app import Application
class ServiceConfigDialog(Dialog):
def __init__(self, master, app, service_name, node_id):
def __init__(self, master, app: "Application", service_name: str, node_id: int):
title = f"{service_name} Service"
super().__init__(master, app, title, modal=True)
self.master = master

View file

@ -1,5 +1,6 @@
import logging
from tkinter import ttk
from typing import TYPE_CHECKING
import grpc
@ -8,9 +9,12 @@ from core.gui.errors import show_grpc_error
from core.gui.themes import PADX, PADY
from core.gui.widgets import ConfigFrame
if TYPE_CHECKING:
from core.gui.app import Application
class SessionOptionsDialog(Dialog):
def __init__(self, master, app):
def __init__(self, master, app: "Application"):
super().__init__(master, app, "Session Options", modal=True)
self.config_frame = None
self.config = self.get_config()

View file

@ -1,7 +1,7 @@
import logging
import tkinter as tk
from tkinter import ttk
from typing import Iterable
from typing import TYPE_CHECKING, Iterable
import grpc
@ -12,9 +12,12 @@ from core.gui.images import ImageEnum, Images
from core.gui.task import BackgroundTask
from core.gui.themes import PADX, PADY
if TYPE_CHECKING:
from core.gui.app import Application
class SessionsDialog(Dialog):
def __init__(self, master, app):
def __init__(self, master, app: "Application"):
super().__init__(master, app, "Sessions", modal=True)
self.selected = False
self.selected_id = None

View file

@ -3,6 +3,7 @@ shape input dialog
"""
import tkinter as tk
from tkinter import font, ttk
from typing import TYPE_CHECKING
from core.gui.dialogs.colorpicker import ColorPickerDialog
from core.gui.dialogs.dialog import Dialog
@ -10,12 +11,16 @@ from core.gui.graph import tags
from core.gui.graph.shapeutils import is_draw_shape, is_shape_text
from core.gui.themes import FRAME_PAD, PADX, PADY
if TYPE_CHECKING:
from core.gui.app import Application
from core.gui.graph.shape import Shape
FONT_SIZES = [8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72]
BORDER_WIDTH = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
class ShapeDialog(Dialog):
def __init__(self, master, app, shape):
def __init__(self, master, app: "Application", shape: "Shape"):
if is_draw_shape(shape.shape_type):
title = "Add Shape"
else:
@ -180,8 +185,6 @@ class ShapeDialog(Dialog):
def save_text(self):
"""
save info related to text or shape label
:return: nothing
"""
data = self.shape.shape_data
data.text = self.shape_text.get()
@ -195,8 +198,6 @@ class ShapeDialog(Dialog):
def save_shape(self):
"""
save info related to shape
:return: nothing
"""
data = self.shape.shape_data
data.fill_color = self.fill_color
@ -206,8 +207,6 @@ class ShapeDialog(Dialog):
def add_text(self):
"""
add text to canvas
:return: nothing
"""
text = self.shape_text.get()
text_font = self.make_font()

View file

@ -3,14 +3,18 @@ throughput dialog
"""
import tkinter as tk
from tkinter import ttk
from typing import TYPE_CHECKING
from core.gui.dialogs.colorpicker import ColorPickerDialog
from core.gui.dialogs.dialog import Dialog
from core.gui.themes import FRAME_PAD, PADX, PADY
if TYPE_CHECKING:
from core.gui.app import Application
class ThroughputDialog(Dialog):
def __init__(self, master, app):
def __init__(self, master, app: "Application"):
super().__init__(master, app, "Throughput Config", modal=False)
self.app = app
self.canvas = app.canvas

View file

@ -3,6 +3,7 @@ wlan configuration
"""
from tkinter import ttk
from typing import TYPE_CHECKING
import grpc
@ -11,9 +12,13 @@ from core.gui.errors import show_grpc_error
from core.gui.themes import PADX, PADY
from core.gui.widgets import ConfigFrame
if TYPE_CHECKING:
from core.gui.app import Application
from core.gui.graph.node import CanvasNode
class WlanConfigDialog(Dialog):
def __init__(self, master, app, canvas_node):
def __init__(self, master, app: "Application", canvas_node: "CanvasNode"):
super().__init__(
master, app, f"{canvas_node.core_node.name} Wlan Configuration", modal=True
)

View file

@ -1,5 +1,6 @@
import tkinter as tk
from tkinter import font
from typing import TYPE_CHECKING
import grpc
@ -16,11 +17,22 @@ from core.gui.graph import tags
from core.gui.graph.tooltip import CanvasTooltip
from core.gui.nodeutils import NodeUtils
if TYPE_CHECKING:
from core.gui.app import Application
from PIL.ImageTk import PhotoImage
NODE_TEXT_OFFSET = 5
class CanvasNode:
def __init__(self, app, x: float, y: float, core_node: core_pb2.Node, image):
def __init__(
self,
app: "Application",
x: float,
y: float,
core_node: core_pb2.Node,
image: "PhotoImage",
):
self.app = app
self.canvas = app.canvas
self.image = image

View file

@ -9,7 +9,7 @@ from core.gui.themes import Styles
class StatusBar(ttk.Frame):
def __init__(self, master, app, **kwargs):
def __init__(self, master: tk.Widget, app, **kwargs):
super().__init__(master, **kwargs)
self.app = app
self.status = None

View file

@ -1,9 +1,12 @@
import logging
import threading
from typing import Callable, Optional
class BackgroundTask:
def __init__(self, master, task, callback=None, args=()):
def __init__(
self, master, task: Callable, callback: Optional[Callable] = None, args=()
):
self.master = master
self.args = args
self.task = task

View file

@ -147,7 +147,7 @@ def theme_change_menu(event: tk.Event):
style_menu(event.widget)
def style_menu(widget: ttk.Widget):
def style_menu(widget: tk.Widget):
style = ttk.Style()
bg = style.lookup(".", "background")
fg = style.lookup(".", "foreground")

View file

@ -429,7 +429,7 @@ class Toolbar(ttk.Frame):
if not response.result:
messagebox.showerror("Stop Error", "Errors stopping session")
def update_annotation(self, image, shape_type: str):
def update_annotation(self, image: ImageTk.PhotoImage, shape_type: str):
logging.info("clicked annotation: ")
self.hide_pickers()
self.annotation_button.configure(image=image)

View file

@ -195,7 +195,7 @@ class CheckboxList(FrameScroll):
self.clicked = clicked
self.frame.columnconfigure(0, weight=1)
def add(self, name, checked):
def add(self, name: str, checked: bool):
var = tk.BooleanVar(value=checked)
func = partial(self.clicked, name, var)
checkbox = ttk.Checkbutton(self.frame, text=name, variable=var, command=func)