-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.py
146 lines (121 loc) · 4.32 KB
/
main.py
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
from nged import Command, Shortcut, NetworkView, App, Editor, builtinGraphItemFactory, startApp
from nged.msghub import trace, debug, info, error
from dataview import DataView
from node import nodeFactory
from document import MyDocument
from edresponse import setupCallbacks
from evaluation import GraphEvaluationContext
import nodelib
try:
import libpandas
except ImportError:
pass
try:
import libnumpy
except ImportError:
pass
import dataview
import time
defaultLayout = '''
hsplit:
data:3:hide_tab_bar
vsplit:6
hsplit:7
network:5
inspector:3:hide_tab_bar
message:3:hide_tab_bar
'''.strip()
def makeSimpleCommand(onconfirm, name, longname, view='*', shortcut='', mayModifyGraph=True):
class SimpleCommand(Command):
def __init__(self):
Command.__init__(self, name, longname, view, Shortcut(shortcut))
self.mayModifyGraph = mayModifyGraph
def onConfirm(self, view):
onconfirm(view)
return SimpleCommand()
def evalSelectedNode(view):
assert isinstance(view, NetworkView)
doc = view.doc or view.graph.doc
ctx = doc.evalContext
selection = view.solySelectedNode
if selection:
trace(
f'----------- eval {selection.name} ({selection.uid}) ------------')
ctx.addDestiny(selection.id)
ctx.prepare(selection.graph)
ctx.getResult(selection)
def prepareGraph(view):
doc = view.doc or view.graph.doc
ctx = doc.evalContext
ctx.addDestiny(doc.root.outputnode.id)
ctx.prepare(doc.root)
def evalGraph(view):
doc = view.doc or view.graph.doc
ctx = doc.evalContext
ctx.addDestiny(doc.root.outputnode.id)
# test pickle
# import pickle
# ctx.prepare(doc.root)
# print(pickle.dumps(ctx))
ctx.evaluate(doc)
# ctx.prepare(doc.root)
# result = ctx.getResult(doc.root.outputnode)
# debug(f'root.output = {result}')
def resetEvalContext(view):
doc = view.doc or view.graph.doc
doc.evalContext = GraphEvaluationContext()
info('graph evaluation context is reset')
showDataCmd = makeSimpleCommand(lambda view: view.editor.addView(
view.doc, 'data'), 'View/Data', 'Show Datasheet', 'network', 'Ctrl+Alt+D', mayModifyGraph=False)
evalSelectedNodeCmd = makeSimpleCommand(
evalSelectedNode, 'Eval/Selected', 'Evaluate Selected Node', 'network', 'Ctrl+F5')
evalGraphCmd = makeSimpleCommand(
evalGraph, 'Eval/Graph', 'Evaluate Graph', 'network|inspector', 'F5')
prepareGraphCmd = makeSimpleCommand(
prepareGraph, 'Eval/PrepareGraph', 'Prepare Graph', 'network|inspector', 'F6')
resetEvalContextCmd = makeSimpleCommand(
resetEvalContext, 'Eval/ResetContext', 'Reset Evaluation Context', 'network|inspector', 'Alt+F5')
class MyApp(App):
def __init__(self):
App.__init__(self)
def agreeToQuit(self):
return self.editor.agreeToQuit()
def title(self):
return "Visual Python"
def init(self):
App.init(self)
self.editor = Editor(MyDocument, nodeFactory(),
builtinGraphItemFactory())
self.editor.registerView('data', DataView)
self.editor.defaultLayoutDesc = defaultLayout
self.editor.addCommand(showDataCmd)
self.editor.addCommand(evalSelectedNodeCmd)
self.editor.addCommand(evalGraphCmd)
self.editor.addCommand(prepareGraphCmd)
self.editor.addCommand(resetEvalContextCmd)
self.editor.newDoc()
setupCallbacks(self.editor)
self.timestamp = time.process_time()
def update(self):
now = time.monotonic()
deltaTime = now - self.timestamp
try:
dataview.syncDestinies(self.editor)
alldocs = set()
for view in self.editor.views():
if view.doc is not None:
alldocs.add(view.doc)
for doc in alldocs:
if not doc.evalContext.busy and len(doc.evalContext.dirtySources) > 0:
doc.evalContext.prepare(doc.root)
elif doc.evalContext.busy:
time.sleep(0.03) # give some time to eval thread
except Exception as e:
error(f'error preparing graph: {e}')
self.editor.update(deltaTime)
self.timestamp = time.monotonic()
self.editor.draw()
def quit(self):
self.editor = None
app = MyApp()
startApp(app)