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

View file

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

View file

@ -1,6 +1,6 @@
import tkinter as tk import tkinter as tk
from tkinter import ttk 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.api.grpc import core_pb2
from core.gui.dialogs.dialog import Dialog from core.gui.dialogs.dialog import Dialog
@ -12,7 +12,7 @@ if TYPE_CHECKING:
class HookDialog(Dialog): 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) super().__init__(master, app, "Hook", modal=True)
self.name = tk.StringVar() self.name = tk.StringVar()
self.codetext = None self.codetext = None

View file

@ -1,6 +1,6 @@
import tkinter as tk import tkinter as tk
from tkinter import ttk from tkinter import ttk
from typing import TYPE_CHECKING from typing import TYPE_CHECKING, Any
import grpc import grpc
@ -69,7 +69,7 @@ class MobilityPlayer:
class MobilityPlayerDialog(Dialog): class MobilityPlayerDialog(Dialog):
def __init__( def __init__(
self, master: Dialog, app: "Application", canvas_node: "CanvasNode", config self, master: Any, app: "Application", canvas_node: "CanvasNode", config
): ):
super().__init__( super().__init__(
master, app, f"{canvas_node.core_node.name} Mobility Player", modal=False 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 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") logging.info("mac auto clicked")
if is_auto.get(): if is_auto.get():
logging.info("disabling mac") logging.info("disabling mac")
entry.var.set("") entry.delete(0, tk.END)
entry.insert(tk.END, "")
entry.config(state=tk.DISABLED) entry.config(state=tk.DISABLED)
else: 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) entry.config(state=tk.NORMAL)
class InterfaceData: 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.is_auto = is_auto
self.mac = mac self.mac = mac
self.ip4 = ip4 self.ip4 = ip4

View file

@ -3,7 +3,7 @@ core node services
""" """
import tkinter as tk import tkinter as tk
from tkinter import messagebox, ttk 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.dialog import Dialog
from core.gui.dialogs.serviceconfig import ServiceConfigDialog from core.gui.dialogs.serviceconfig import ServiceConfigDialog
@ -17,7 +17,11 @@ if TYPE_CHECKING:
class NodeServiceDialog(Dialog): class NodeServiceDialog(Dialog):
def __init__( 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" title = f"{canvas_node.core_node.name} Services"
super().__init__(master, app, title, modal=True) super().__init__(master, app, title, modal=True)

View file

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

View file

@ -1,6 +1,6 @@
import logging import logging
import tkinter as tk import tkinter as tk
from typing import TYPE_CHECKING, List, Optional, Tuple from typing import TYPE_CHECKING, List, Tuple
from PIL import Image, ImageTk from PIL import Image, ImageTk
@ -17,6 +17,7 @@ from core.gui.images import Images
from core.gui.nodeutils import NodeUtils from core.gui.nodeutils import NodeUtils
if TYPE_CHECKING: if TYPE_CHECKING:
from core.gui.app import Application
from core.gui.coreclient import CoreClient from core.gui.coreclient import CoreClient
ZOOM_IN = 1.1 ZOOM_IN = 1.1
@ -24,7 +25,9 @@ ZOOM_OUT = 0.9
class CanvasGraph(tk.Canvas): 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") super().__init__(master, highlightthickness=0, background="#cccccc")
self.app = master self.app = master
self.core = core self.core = core
@ -71,7 +74,7 @@ class CanvasGraph(tk.Canvas):
self.draw_canvas() self.draw_canvas()
self.draw_grid() 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: if self.grid is not None:
self.delete(self.grid) self.delete(self.grid)
if not dimensions: if not dimensions:
@ -398,7 +401,7 @@ class CanvasGraph(tk.Canvas):
node_dst.edges.add(edge) node_dst.edges.add(edge)
self.core.create_link(edge, node_src, node_dst) 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 create a bounding box when a node is selected
""" """
@ -478,7 +481,7 @@ class CanvasGraph(tk.Canvas):
self.selection.clear() self.selection.clear()
return nodes return nodes
def zoom(self, event: tk.Event, factor: Optional[float] = None): def zoom(self, event: tk.Event, factor: float = None):
if not factor: if not factor:
factor = ZOOM_IN if event.delta > 0 else ZOOM_OUT factor = ZOOM_IN if event.delta > 0 else ZOOM_OUT
event.x, event.y = self.canvasx(event.x), self.canvasy(event.y) event.x, event.y = self.canvasx(event.x), self.canvasy(event.y)
@ -685,10 +688,7 @@ class CanvasGraph(tk.Canvas):
return image return image
def draw_wallpaper( def draw_wallpaper(
self, self, image: ImageTk.PhotoImage, x: float = None, y: float = None
image: ImageTk.PhotoImage,
x: Optional[float] = None,
y: Optional[float] = None,
): ):
if x is None and y is None: if x is None and y is None:
x1, y1, x2, y2 = self.bbox(self.grid) 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.subnets = IPNetwork(f"{address}/{self.base_prefix}")
self.current_subnet = None self.current_subnet = None
def next_subnet(self): def next_subnet(self) -> IPNetwork:
# define currently used subnets # define currently used subnets
used_subnets = set() used_subnets = set()
for edge in self.app.core.links.values(): for edge in self.app.core.links.values():
@ -44,7 +44,7 @@ class InterfaceManager:
def reset(self): def reset(self):
self.current_subnet = None 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] ip4 = self.current_subnet[node_id]
ip6 = ip4.ipv6() ip6 = ip4.ipv6()
prefix = self.current_subnet.prefixlen prefix = self.current_subnet.prefixlen

View file

@ -6,7 +6,7 @@ import logging
import tkinter as tk import tkinter as tk
import webbrowser import webbrowser
from tkinter import filedialog, messagebox 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.appconfig import XMLS_PATH
from core.gui.dialogs.about import AboutDialog from core.gui.dialogs.about import AboutDialog
@ -26,21 +26,15 @@ if TYPE_CHECKING:
class MenuAction: class MenuAction:
"""
Actions performed when choosing menu items
"""
def __init__(self, app: "Application", master: tk.Tk): def __init__(self, app: "Application", master: tk.Tk):
self.master = master self.master = master
self.app = app self.app = app
self.canvas = app.canvas self.canvas = app.canvas
def cleanup_old_session(self, quitapp: Optional[bool] = False): def cleanup_old_session(self):
logging.info("cleaning up old session") logging.info("cleaning up old session")
self.app.core.stop_session() self.app.core.stop_session()
self.app.core.delete_session() self.app.core.delete_session()
# if quitapp:
# self.app.quit()
def prompt_save_running_session(self, quitapp: bool = False): def prompt_save_running_session(self, quitapp: bool = False):
""" """
@ -76,7 +70,7 @@ class MenuAction:
if file_path: if file_path:
self.app.core.save_xml(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()") logging.info("menuaction.py file_open_xml()")
file_path = filedialog.askopenfilename( file_path = filedialog.askopenfilename(
initialdir=str(XMLS_PATH), initialdir=str(XMLS_PATH),
@ -142,11 +136,11 @@ class MenuAction:
else: else:
self.app.core.cancel_throughputs() self.app.core.cancel_throughputs()
def copy(self, event: Optional[tk.Event] = None): def copy(self, event: tk.Event = None):
logging.debug("copy") logging.debug("copy")
self.app.canvas.copy() self.app.canvas.copy()
def paste(self, event: Optional[tk.Event] = None): def paste(self, event: tk.Event = None):
logging.debug("paste") logging.debug("paste")
self.app.canvas.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.api.grpc.core_pb2 import NodeType
from core.gui.images import ImageEnum, Images from core.gui.images import ImageEnum, Images
@ -7,13 +9,13 @@ ANTENNA_SIZE = 32
class NodeDraw: class NodeDraw:
def __init__(self): def __init__(self):
self.custom = False self.custom: bool = False
self.image = None self.image = None
self.image_enum = None self.image_enum = None
self.image_file = None self.image_file = None
self.node_type = None self.node_type = None
self.model = None self.model = None
self.services = set() self.services: Set[str] = set()
@classmethod @classmethod
def from_setup(cls, image_enum, node_type, label, model=None, tooltip=None): 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 functools import partial
from pathlib import PosixPath from pathlib import PosixPath
from tkinter import filedialog, font, ttk 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.api.grpc import core_pb2
from core.gui import themes from core.gui import themes
@ -32,7 +32,7 @@ def file_button_click(value: tk.StringVar, parent: tk.Widget):
class FrameScroll(ttk.Frame): 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) super().__init__(master, **kw)
self.app = app self.app = app
self.rowconfigure(0, weight=1) self.rowconfigure(0, weight=1)
@ -72,7 +72,7 @@ class FrameScroll(ttk.Frame):
class ConfigFrame(ttk.Notebook): class ConfigFrame(ttk.Notebook):
def __init__( def __init__(
self, self,
master: ttk.Widget, master: tk.Widget,
app: "Application", app: "Application",
config: Dict[str, core_pb2.ConfigOption], config: Dict[str, core_pb2.ConfigOption],
**kw **kw
@ -186,7 +186,7 @@ class ConfigFrame(ttk.Notebook):
class ListboxScroll(ttk.Frame): 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) super().__init__(master, **kw)
self.columnconfigure(0, weight=1) self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1) self.rowconfigure(0, weight=1)
@ -219,7 +219,7 @@ class CodeFont(font.Font):
class CodeText(ttk.Frame): class CodeText(ttk.Frame):
def __init__(self, master: ttk.Widget, **kwargs): def __init__(self, master: tk.Widget, **kwargs):
super().__init__(master, **kwargs) super().__init__(master, **kwargs)
self.rowconfigure(0, weight=1) self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1) self.columnconfigure(0, weight=1)
@ -243,7 +243,7 @@ class CodeText(ttk.Frame):
class Spinbox(ttk.Entry): 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) super().__init__(master, "ttk::spinbox", **kwargs)
def set(self, value): def set(self, value):