-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblocks.py
More file actions
177 lines (140 loc) · 4.04 KB
/
blocks.py
File metadata and controls
177 lines (140 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from __future__ import annotations
from enum import Enum, auto
from typing import Any
import log
import traceback
#log.Log.flags.add("EXCEPTION")
log.Log.flags.add("EVENT")
class Event:
"""Generic Event passed between blocks and nodes
Typically contains data to pass down a chain
field: t - type of event
"""
t:T
data:Any
source:Widget|None
dest:Widget
class T(Enum):
SEND = auto()
RECV = auto()
RESET = auto()
def __init__(self, t, dest:Widget, data=None, source:Widget|None=None) -> None:
self.t = t
self.data = data
self.source = source
self.dest = dest
def __str__(self) -> str:
return f"<Event {self.t}: {self.source} -> {self.dest}, data={self.data}>"
class Widget:
"""
Anything that can handle events
"""
evlog = log.Log("EVENT")
def handle(self, event:Event) -> None:
self.evlog(event)
class Port:
_val: Any
_has: bool
_repeat: bool
def __init__(self, repeat:bool=False) -> None:
self._val = None
self._has = False
self._repeat = repeat
def has(self) -> bool:
return self._has
def get(self) -> Any:
assert self._has
if self._repeat:
self._has = False
return self._val
def peek(self) -> Any|None:
if self._has:
return self._val
else:
return None
def set(self, value:Any) -> None:
assert not self._has
self._has = True
self._val = value
def reset(self) -> None:
self._val = None
self._has = False
class InPort(Port):
"""
Passes data into a block
Receives from OutputNode
"""
_link:OutPort|None
def __init__(self, repeat: bool = False) -> None:
super().__init__(repeat)
self._link = None
def try_pull(self) -> bool:
assert not self.has()
if self._link is not None:
self._link.try_push()
class OutPort(Port):
"""
Passes data out of a block
Sends to InputNode(s)
"""
_links:set[InPort]
def __init__(self, repeat: bool = False) -> None:
super().__init__(repeat)
self._links = set()
def link(self, target:InPort) -> None:
assert target not in self._links
assert target._link is None
self._links.add(target)
target._link = self
def unlink(self, target:InPort) -> None:
assert target in self._links
assert target._link is self
self._links.remove(target)
target._link = None
def unlink_all(self) -> None:
for target in self._links:
assert target._link is self
target._link = None
self._links = set()
def try_push(self) -> bool:
assert self.has()
if (not self._repeat) or all((not dest.has() for dest in self._links)):
v = self.get()
for dest in self._links:
dest.set(v)
return True
else:
return False
class Node(Widget):
inputs:dict[str, InPort]
outputs:dict[str, OutPort]
code:str
exlog = log.Log("EXCEPTION")
def __init__(self) -> None:
super().__init__()
self.inputs = {}
self.outputs = {}
self.code = str
def handle(self, event:Event) -> None:
super().handle(event)
def run(self) -> None:
exec_vars = {k:v.get() for k,v in self.inputs.items()}
try:
exec(self.code, globals(), exec_vars)
except Exception:
self.exlog(traceback.format_exc())
for k,v in exec_vars.items():
try:
self.outputs[k].set(v)
except KeyError:
pass
class Scene():
blocks:list[Node]
def __init__(self) -> None:
self.blocks = [basic_node_1i1o() for _ in range(3)]
def basic_node_1i1o():
b = Node()
b.inputs["i"] = InPort()
b.outputs["o"] = OutPort()
b.code = "o = i"
return b