Skip to content

Commit

Permalink
refactor: tidy up node.py
Browse files Browse the repository at this point in the history
  • Loading branch information
eri24816 committed Mar 23, 2024
1 parent b88ba89 commit 34b1023
Showing 1 changed file with 14 additions and 30 deletions.
44 changes: 14 additions & 30 deletions backend/src/grapycal/sobjects/node.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import logging
logger = logging.getLogger(__name__)
from typing import TYPE_CHECKING, Awaitable, Callable, Literal, Self, TypeVar

from abc import ABCMeta
import asyncio
import enum
import io
from itertools import count
import logging
import random
from contextlib import contextmanager
import functools
import traceback

from grapycal.sobjects.controls.buttonControl import ButtonControl
from grapycal.sobjects.controls.imageControl import ImageControl
from grapycal.sobjects.controls.linePlotControl import LinePlotControl
from grapycal.sobjects.controls.nullControl import NullControl
from grapycal.sobjects.controls.optionControl import OptionControl

from grapycal.sobjects.controls.textControl import TextControl

logger = logging.getLogger(__name__)
from grapycal.stores import main_store
from grapycal.utils.logging import error_extension, user_logger, warn_extension
from contextlib import contextmanager
import functools
import traceback
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Generator, Literal, Self, TypeVar
from grapycal.extension.utils import NodeInfo
from grapycal.sobjects.controls.control import Control, ValuedControl
from grapycal.sobjects.edge import Edge
Expand All @@ -40,11 +39,12 @@
from objectsync.sobject import SObjectSerialized, WrappedTopic

if TYPE_CHECKING:
from grapycal.core.workspace import Workspace
from grapycal.extension.extension import Extension


def warn_no_control_name(control_type, node):
'''
Log a warning if the control does not have a name.
'''
node_type = node.get_type_name().split('.')[1]
warn_extension(
node,
Expand All @@ -53,6 +53,9 @@ def warn_no_control_name(control_type, node):
extra={"key": f"No control name {node_type}"},
)

'''
Decorator for node development.
'''

def singletonNode(auto_instantiate=True):
"""
Expand All @@ -77,22 +80,6 @@ class TestSingNode(Node):
T = TypeVar("T", bound=Node)

def wrapper(cls: type[T]):
# class WrapperClass(cls):
# instance: T
# def __init__(self,*args,**kwargs):
# super().__init__(*args,**kwargs)
# if hasattr(WrapperClass, "instance"):
# raise RuntimeError("Singleton node can only be instantiated once")
# WrapperClass.instance = self # type: ignore # strange complaint from linter

# def destroy(self) -> SObjectSerialized:
# del WrapperClass.instance
# return super().destroy()

# WrapperClass._is_singleton = True
# WrapperClass._auto_instantiate = auto_instantiate
# WrapperClass.__name__ = cls.__name__

def new_init(self, *args, **kwargs):
if hasattr(cls, "instance"):
raise RuntimeError("Singleton node can only be instantiated once")
Expand Down Expand Up @@ -144,7 +131,6 @@ def new_init_node(self: Node, **kwargs):

return wrapper


class NodeMeta(ABCMeta):
class_def_counter = count()
def_order = {}
Expand All @@ -157,7 +143,6 @@ def __init__(self, name, bases, attrs):
self._auto_instantiate = True # Used internally by the ExtensionManager.
return super().__init__(name, bases, attrs)


class RESTORE_FROM(enum.Enum):
SAME = 0

Expand All @@ -177,7 +162,6 @@ def get_def_order(cls):
return cls.def_order[cls.__name__]

def initialize(self, serialized:SObjectSerialized|None=None, *args, **kwargs):

self._already_restored_attributes = set()
self._already_restored_controls = set()
self.old_node_info = NodeInfo(serialized) if serialized is not None else None
Expand Down

0 comments on commit 34b1023

Please sign in to comment.