type hinting

This commit is contained in:
Huy Pham 2020-01-14 14:02:37 -08:00
parent c22f1680f7
commit a0c04c0809
5 changed files with 30 additions and 18 deletions

View file

@ -772,7 +772,6 @@ class Session:
Broadcast node location to all listeners. Broadcast node location to all listeners.
:param core.nodes.base.NodeBase node: node to broadcast location for :param core.nodes.base.NodeBase node: node to broadcast location for
:return: nothing
""" """
node_data = NodeData( node_data = NodeData(
message_type=0, message_type=0,

View file

@ -22,7 +22,7 @@ class Images:
cls.images[image.stem] = str(image) cls.images[image.stem] = str(image)
@classmethod @classmethod
def get(cls, image_enum, width: int, height: int = None): def get(cls, image_enum: Enum, width: int, height: int = None):
file_path = cls.images[image_enum.value] file_path = cls.images[image_enum.value]
return cls.create(file_path, width, height) return cls.create(file_path, width, height)

View file

@ -1,8 +1,11 @@
from typing import Set from typing import TYPE_CHECKING, 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
if TYPE_CHECKING:
from core.api.grpc import core_pb2
ICON_SIZE = 48 ICON_SIZE = 48
ANTENNA_SIZE = 32 ANTENNA_SIZE = 32
@ -11,14 +14,21 @@ class NodeDraw:
def __init__(self): def __init__(self):
self.custom: bool = False self.custom: bool = False
self.image = None self.image = None
self.image_enum = None self.image_enum: ImageEnum = None
self.image_file = None self.image_file = None
self.node_type = None self.node_type: core_pb2.NodeType = None
self.model = None self.model: str = None
self.services: Set[str] = 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: ImageEnum,
node_type: "core_pb2.NodeType",
label: str,
model: str = None,
tooltip=None,
):
node_draw = NodeDraw() node_draw = NodeDraw()
node_draw.image_enum = image_enum node_draw.image_enum = image_enum
node_draw.image = Images.get(image_enum, ICON_SIZE) node_draw.image = Images.get(image_enum, ICON_SIZE)
@ -29,7 +39,7 @@ class NodeDraw:
return node_draw return node_draw
@classmethod @classmethod
def from_custom(cls, name, image_file, services): def from_custom(cls, name: str, image_file: str, services: str):
node_draw = NodeDraw() node_draw = NodeDraw()
node_draw.custom = True node_draw.custom = True
node_draw.image_file = image_file node_draw.image_file = image_file
@ -55,31 +65,31 @@ class NodeUtils:
ANTENNA_ICON = None ANTENNA_ICON = None
@classmethod @classmethod
def is_ignore_node(cls, node_type) -> bool: def is_ignore_node(cls, node_type: NodeType) -> bool:
return node_type in cls.IGNORE_NODES return node_type in cls.IGNORE_NODES
@classmethod @classmethod
def is_container_node(cls, node_type) -> bool: def is_container_node(cls, node_type: NodeType) -> bool:
return node_type in cls.CONTAINER_NODES return node_type in cls.CONTAINER_NODES
@classmethod @classmethod
def is_model_node(cls, node_type) -> bool: def is_model_node(cls, node_type: NodeType) -> bool:
return node_type == NodeType.DEFAULT return node_type == NodeType.DEFAULT
@classmethod @classmethod
def is_image_node(cls, node_type) -> bool: def is_image_node(cls, node_type: NodeType) -> bool:
return node_type in cls.IMAGE_NODES return node_type in cls.IMAGE_NODES
@classmethod @classmethod
def is_wireless_node(cls, node_type) -> bool: def is_wireless_node(cls, node_type: NodeType) -> bool:
return node_type in cls.WIRELESS_NODES return node_type in cls.WIRELESS_NODES
@classmethod @classmethod
def is_rj45_node(cls, node_type) -> bool: def is_rj45_node(cls, node_type: NodeType) -> bool:
return node_type in cls.RJ45_NODES return node_type in cls.RJ45_NODES
@classmethod @classmethod
def node_icon(cls, node_type, model: str) -> bool: def node_icon(cls, node_type: NodeType, model: str) -> bool:
if model == "": if model == "":
model = None model = None
return cls.NODE_ICONS[(node_type, model)] return cls.NODE_ICONS[(node_type, model)]

View file

@ -13,7 +13,7 @@ if TYPE_CHECKING:
class StatusBar(ttk.Frame): class StatusBar(ttk.Frame):
def __init__(self, master: tk.Widget, app: "Application", **kwargs): def __init__(self, master: "Application", app: "Application", **kwargs):
super().__init__(master, **kwargs) super().__init__(master, **kwargs)
self.app = app self.app = app
self.status = None self.status = None

View file

@ -1,11 +1,14 @@
import logging import logging
import threading import threading
from typing import Callable, Optional from typing import TYPE_CHECKING, Callable
if TYPE_CHECKING:
from core.gui.app import Application
class BackgroundTask: class BackgroundTask:
def __init__( def __init__(
self, master, task: Callable, callback: Optional[Callable] = None, args=() self, master: "Application", task: Callable, callback: Callable = None, args=()
): ):
self.master = master self.master = master
self.args = args self.args = args