Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def serialize(self, nodes, anchor=(0, 0)):
buff = []
buff.append({'version': Serializer.version})
for node in nodes:
if isinstance(node, Node):
buff.append(OrderedDict({'type': 'node',
'x': node.x - anchor[0],
'y': node.y - anchor[1],
'size': node.editor_size,
'color': node.color,
'code': node.code.split('\n'),
'connects': node.get_con_id(),
'id': node.id}))
elif isinstance(node, Field):
buff.append(OrderedDict({'type': 'field',
'x': node.x - anchor[0],
'y': node.y - anchor[1],
'size': (node.w, node.h),
'code': node.document.text.split('\n'),
'connects': node.get_con_id(),
'id': node.id}))
elif isinstance(node, Sub):
buff.append(OrderedDict({'type': 'sub',
'x': node.x - anchor[0],
'y': node.y - anchor[1],
'size': node.editor_size,
'color': node.color,
'code': node.code,
'connects': node.get_con_id(),
'id': node.id}))
buff = []
try:
for node in nodes:
if node['type'] == 'node':
buff.append(Node(self.window,
node['x'] + anchor[0],
node['y'] + anchor[1],
self.window.batch,
tuple(node['color']),
'\n'.join(node['code']) if version >= 0.3 else node['code'],
node['connects'],
node['size'],
node['id' if version >= 0.4 else 'parent']))
elif node['type'] == 'field':
buff.append(Field(self.window,
node['x'] + anchor[0],
node['y'] + anchor[1],
self.window.batch,
'\n'.join(node['code']) if version >= 0.3 else node['code'],
node['connects'],
node['size'],
node['id' if version >= 0.4 else 'parent']))
elif node['type'] == 'sub':
buff.append(Sub(self.window,
node['x'] + anchor[0],
node['y'] + anchor[1],
self.window.batch,
tuple(node['color']),
node['code'],
node['connects'],
node['size'],
si = node.selectedInput['name'] != 'none'
so = node.selectedOutput['name'] != 'none'
if si or so:
self.pointer = (x, y)
self.connection = True
if si:
self.disconnect_node(node)
elif so:
self.connecting_node = \
{'node': node,
'put': node.selectedOutput,
'mode': 'output'}
return
if isinstance(node, Node):
self.code_editor = CodeEditor(node)
elif isinstance(node, Field) and not self.field:
self.push_handlers(node)
self.field = node
elif isinstance(node, Sub):
self.code_editor = CodeEditor(node, highlighting=2)
if node.pwindow:
node.pwindow.set_visible(not node.pwindow.visible)
self.selected_nodes = [node]
self.node_drag = True
return
self.select = True
self.selectPoint = (x, y)
if modifiers == 2 and symbol == key.EQUAL:
# double zoom
self.pan_scale[1] = 2
if not (self.code_editor or self.field):
if symbol == key.N:
self.nodes.append(Node(self, self.pointer[0], self.pointer[1], self.batch,
random_node_color(), code=initialCode.node))
elif symbol == key.O:
self.nodes.append(Node(self, self.pointer[0], self.pointer[1], self.batch,
random_node_color(), code=initialCode.open_node))
elif symbol == key.F:
self.nodes.append(Field(self, self.pointer[0], self.pointer[1], self.batch))
elif symbol == key.S:
self.nodes.append(Sub(self, self.pointer[0], self.pointer[1], self.batch,
random_node_color()))
elif symbol == key.R:
for node in self.nodes:
if isinstance(node, Node):
node.reload()
if modifiers & key.MOD_CTRL:
if symbol == key.C:
self.copy_nodes()
elif symbol == key.V:
self.paste_nodes()
self.pwindow.close()
del self.pwindow
self.pwindow = pwin
except Exception as ex:
self.problem = ex # abusing since this resolves in most cases like "True"
self.er_label.text = repr(ex)
return
# window visibility handeled in window.py
self.pwindow.nodes_update()
inputs, outputs = [], []
self.input_nodes, self.output_nodes = {}, {}
for nid, node in enumerate(self.pwindow.nodes):
if not isinstance(node, Field):
continue
# print(node.inputs, node.outputs, node.connected_to, node.child, node.name)
# print(node, node.inputs, node.outputs, node.connected_to, node.child, node.code, node.gen_output)
# nid = node.id
# nid = hex(id(node))
if not node.connected_to: # fields with free inputs
for inp in node.inputs:
name = "%s_%i" % (inp, nid)
inputs.append(name)
self.input_nodes[name] = node # from self.pwindow.nodes
if not node.child: # fields with free outputs
for outp in node.outputs:
name = "%s_%i" % (outp, nid)
outputs.append(name)
self.output_nodes[name] = node # from self.pwindow.nodes