more type hinting, remove some Optional type, and fix a small bug at dialogs.nodeconfig.mac_auto()

This commit is contained in:
Huy Pham 2020-01-14 11:59:44 -08:00
parent 6c8a2526d9
commit c22f1680f7
12 changed files with 59 additions and 52 deletions

View file

@ -5,7 +5,7 @@ import json
import logging
import os
from pathlib import Path
from typing import TYPE_CHECKING, Dict, List, Optional
from typing import TYPE_CHECKING, Dict, List
import grpc
@ -40,14 +40,14 @@ OBSERVERS = {
class CoreServer:
def __init__(self, name, address, port):
def __init__(self, name: str, address: str, port: int):
self.name = name
self.address = address
self.port = port
class Observer:
def __init__(self, name, cmd):
def __init__(self, name: str, cmd: str):
self.name = name
self.cmd = cmd
@ -116,7 +116,7 @@ class CoreClient:
self.handling_events.cancel()
self.handling_events = None
def set_observer(self, value):
def set_observer(self, value: str):
self.observer = value
def read_config(self):
@ -388,7 +388,7 @@ class CoreClient:
except grpc.RpcError as e:
self.app.after(0, show_grpc_error, e)
def delete_session(self, session_id: Optional[int] = None):
def delete_session(self, session_id: int = None):
if session_id is None:
session_id = self.session_id
try:
@ -479,7 +479,7 @@ class CoreClient:
self.app.after(0, show_grpc_error, e)
return response
def stop_session(self, session_id: Optional[int] = None):
def stop_session(self, session_id: int = None) -> core_pb2.StartSessionResponse:
if not session_id:
session_id = self.session_id
response = core_pb2.StopSessionResponse(result=False)
@ -875,7 +875,7 @@ class CoreClient:
return config
def get_emane_model_config(
self, node_id: int, model: str, interface: Optional[int] = None
self, node_id: int, model: str, interface: int = None
) -> Dict[str, core_pb2.ConfigOption]:
logging.info("getting emane model config: %s %s %s", node_id, model, interface)
config = self.emane_model_configs.get((node_id, model, interface))
@ -893,7 +893,7 @@ class CoreClient:
node_id: int,
model: str,
config: Dict[str, core_pb2.ConfigOption],
interface: Optional[int] = None,
interface: int = None,
):
logging.info("setting emane model config: %s %s %s", node_id, model, interface)
self.emane_model_configs[(node_id, model, interface)] = config

View file

@ -190,7 +190,7 @@ class ColorPickerDialog(Dialog):
green = self.green_entry.get()
return "#%02x%02x%02x" % (int(red), int(green), int(blue))
def current_focus(self, focus):
def current_focus(self, focus: str):
self.focus = focus
def update_color(self, arg1=None, arg2=None, arg3=None):

View file

@ -1,6 +1,6 @@
import tkinter as tk
from tkinter import ttk
from typing import TYPE_CHECKING, Union
from typing import TYPE_CHECKING, Any
from core.api.grpc import core_pb2
from core.gui.dialogs.dialog import Dialog
@ -12,7 +12,7 @@ if TYPE_CHECKING:
class HookDialog(Dialog):
def __init__(self, master: Union[tk.Widget, Dialog], app: "Application"):
def __init__(self, master: Any, app: "Application"):
super().__init__(master, app, "Hook", modal=True)
self.name = tk.StringVar()
self.codetext = None

View file

@ -1,6 +1,6 @@
import tkinter as tk
from tkinter import ttk
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any
import grpc
@ -69,7 +69,7 @@ class MobilityPlayer:
class MobilityPlayerDialog(Dialog):
def __init__(
self, master: Dialog, app: "Application", canvas_node: "CanvasNode", config
self, master: Any, app: "Application", canvas_node: "CanvasNode", config
):
super().__init__(
master, app, f"{canvas_node.core_node.name} Mobility Player", modal=False

View file

@ -18,19 +18,27 @@ if TYPE_CHECKING:
from core.gui.graph.node import CanvasNode
def mac_auto(is_auto, entry):
def mac_auto(is_auto: tk.BooleanVar, entry: ttk.Entry):
logging.info("mac auto clicked")
if is_auto.get():
logging.info("disabling mac")
entry.var.set("")
entry.delete(0, tk.END)
entry.insert(tk.END, "")
entry.config(state=tk.DISABLED)
else:
entry.var.set("00:00:00:00:00:00")
entry.delete(0, tk.END)
entry.insert(tk.END, "00:00:00:00:00:00")
entry.config(state=tk.NORMAL)
class InterfaceData:
def __init__(self, is_auto, mac, ip4, ip6):
def __init__(
self,
is_auto: tk.BooleanVar,
mac: tk.StringVar,
ip4: tk.StringVar,
ip6: tk.StringVar,
):
self.is_auto = is_auto
self.mac = mac
self.ip4 = ip4

View file

@ -3,7 +3,7 @@ core node services
"""
import tkinter as tk
from tkinter import messagebox, ttk
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, Set
from core.gui.dialogs.dialog import Dialog
from core.gui.dialogs.serviceconfig import ServiceConfigDialog
@ -17,7 +17,11 @@ if TYPE_CHECKING:
class NodeServiceDialog(Dialog):
def __init__(
self, master: Any, app: "Application", canvas_node: "CanvasNode", services=None
self,
master: Any,
app: "Application",
canvas_node: "CanvasNode",
services: Set[str] = None,
):
title = f"{canvas_node.core_node.name} Services"
super().__init__(master, app, title, modal=True)

View file

@ -3,7 +3,7 @@ shape input dialog
"""
import tkinter as tk
from tkinter import font, ttk
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, List, Union
from core.gui.dialogs.colorpicker import ColorPickerDialog
from core.gui.dialogs.dialog import Dialog
@ -167,10 +167,9 @@ class ShapeDialog(Dialog):
self.add_text()
self.destroy()
def make_font(self):
def make_font(self) -> List[Union[int, str]]:
"""
create font for text or shape label
:return: list(font specifications)
"""
size = int(self.font_size.get())
text_font = [self.font.get(), size]

View file

@ -1,6 +1,6 @@
import logging
import tkinter as tk
from typing import TYPE_CHECKING, List, Optional, Tuple
from typing import TYPE_CHECKING, List, Tuple
from PIL import Image, ImageTk
@ -17,6 +17,7 @@ from core.gui.images import Images
from core.gui.nodeutils import NodeUtils
if TYPE_CHECKING:
from core.gui.app import Application
from core.gui.coreclient import CoreClient
ZOOM_IN = 1.1
@ -24,7 +25,9 @@ ZOOM_OUT = 0.9
class CanvasGraph(tk.Canvas):
def __init__(self, master, core: "CoreClient", width: int, height: int):
def __init__(
self, master: "Application", core: "CoreClient", width: int, height: int
):
super().__init__(master, highlightthickness=0, background="#cccccc")
self.app = master
self.core = core
@ -71,7 +74,7 @@ class CanvasGraph(tk.Canvas):
self.draw_canvas()
self.draw_grid()
def draw_canvas(self, dimensions: Optional[Tuple[int, int]] = None):
def draw_canvas(self, dimensions: Tuple[int, int] = None):
if self.grid is not None:
self.delete(self.grid)
if not dimensions:
@ -398,7 +401,7 @@ class CanvasGraph(tk.Canvas):
node_dst.edges.add(edge)
self.core.create_link(edge, node_src, node_dst)
def select_object(self, object_id: int, choose_multiple: Optional[bool] = False):
def select_object(self, object_id: int, choose_multiple: bool = False):
"""
create a bounding box when a node is selected
"""
@ -478,7 +481,7 @@ class CanvasGraph(tk.Canvas):
self.selection.clear()
return nodes
def zoom(self, event: tk.Event, factor: Optional[float] = None):
def zoom(self, event: tk.Event, factor: float = None):
if not factor:
factor = ZOOM_IN if event.delta > 0 else ZOOM_OUT
event.x, event.y = self.canvasx(event.x), self.canvasy(event.y)
@ -685,10 +688,7 @@ class CanvasGraph(tk.Canvas):
return image
def draw_wallpaper(
self,
image: ImageTk.PhotoImage,
x: Optional[float] = None,
y: Optional[float] = None,
self, image: ImageTk.PhotoImage, x: float = None, y: float = None
):
if x is None and y is None:
x1, y1, x2, y2 = self.bbox(self.grid)

View file

@ -24,7 +24,7 @@ class InterfaceManager:
self.subnets = IPNetwork(f"{address}/{self.base_prefix}")
self.current_subnet = None
def next_subnet(self):
def next_subnet(self) -> IPNetwork:
# define currently used subnets
used_subnets = set()
for edge in self.app.core.links.values():
@ -44,7 +44,7 @@ class InterfaceManager:
def reset(self):
self.current_subnet = None
def get_ips(self, node_id: int):
def get_ips(self, node_id: int) -> [str, str, int]:
ip4 = self.current_subnet[node_id]
ip6 = ip4.ipv6()
prefix = self.current_subnet.prefixlen

View file

@ -6,7 +6,7 @@ import logging
import tkinter as tk
import webbrowser
from tkinter import filedialog, messagebox
from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING
from core.gui.appconfig import XMLS_PATH
from core.gui.dialogs.about import AboutDialog
@ -26,21 +26,15 @@ if TYPE_CHECKING:
class MenuAction:
"""
Actions performed when choosing menu items
"""
def __init__(self, app: "Application", master: tk.Tk):
self.master = master
self.app = app
self.canvas = app.canvas
def cleanup_old_session(self, quitapp: Optional[bool] = False):
def cleanup_old_session(self):
logging.info("cleaning up old session")
self.app.core.stop_session()
self.app.core.delete_session()
# if quitapp:
# self.app.quit()
def prompt_save_running_session(self, quitapp: bool = False):
"""
@ -76,7 +70,7 @@ class MenuAction:
if file_path:
self.app.core.save_xml(file_path)
def file_open_xml(self, event: Optional[tk.Event] = None):
def file_open_xml(self, event: tk.Event = None):
logging.info("menuaction.py file_open_xml()")
file_path = filedialog.askopenfilename(
initialdir=str(XMLS_PATH),
@ -142,11 +136,11 @@ class MenuAction:
else:
self.app.core.cancel_throughputs()
def copy(self, event: Optional[tk.Event] = None):
def copy(self, event: tk.Event = None):
logging.debug("copy")
self.app.canvas.copy()
def paste(self, event: Optional[tk.Event] = None):
def paste(self, event: tk.Event = None):
logging.debug("paste")
self.app.canvas.paste()

View file

@ -1,3 +1,5 @@
from typing import Set
from core.api.grpc.core_pb2 import NodeType
from core.gui.images import ImageEnum, Images
@ -7,13 +9,13 @@ ANTENNA_SIZE = 32
class NodeDraw:
def __init__(self):
self.custom = False
self.custom: bool = False
self.image = None
self.image_enum = None
self.image_file = None
self.node_type = None
self.model = None
self.services = set()
self.services: Set[str] = set()
@classmethod
def from_setup(cls, image_enum, node_type, label, model=None, tooltip=None):

View file

@ -3,7 +3,7 @@ import tkinter as tk
from functools import partial
from pathlib import PosixPath
from tkinter import filedialog, font, ttk
from typing import TYPE_CHECKING, Dict, Optional
from typing import TYPE_CHECKING, Dict
from core.api.grpc import core_pb2
from core.gui import themes
@ -32,7 +32,7 @@ def file_button_click(value: tk.StringVar, parent: tk.Widget):
class FrameScroll(ttk.Frame):
def __init__(self, master: ttk.Widget, app: "Application", _cls=ttk.Frame, **kw):
def __init__(self, master: tk.Widget, app: "Application", _cls=ttk.Frame, **kw):
super().__init__(master, **kw)
self.app = app
self.rowconfigure(0, weight=1)
@ -72,7 +72,7 @@ class FrameScroll(ttk.Frame):
class ConfigFrame(ttk.Notebook):
def __init__(
self,
master: ttk.Widget,
master: tk.Widget,
app: "Application",
config: Dict[str, core_pb2.ConfigOption],
**kw
@ -186,7 +186,7 @@ class ConfigFrame(ttk.Notebook):
class ListboxScroll(ttk.Frame):
def __init__(self, master: Optional[ttk.Widget] = None, **kw):
def __init__(self, master: tk.Widget = None, **kw):
super().__init__(master, **kw)
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
@ -219,7 +219,7 @@ class CodeFont(font.Font):
class CodeText(ttk.Frame):
def __init__(self, master: ttk.Widget, **kwargs):
def __init__(self, master: tk.Widget, **kwargs):
super().__init__(master, **kwargs)
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
@ -243,7 +243,7 @@ class CodeText(ttk.Frame):
class Spinbox(ttk.Entry):
def __init__(self, master: Optional[ttk.Widget] = None, **kwargs):
def __init__(self, master: tk.Widget = None, **kwargs):
super().__init__(master, "ttk::spinbox", **kwargs)
def set(self, value):