-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from nodedge/code-generation
Code generation
- Loading branch information
Showing
10 changed files
with
138 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
<ModuleName> module containing :class:`~nodedge.<Name>.<ClassName>` class. | ||
""" | ||
|
||
|
||
class MyClass: | ||
""":class:`~nodedge.<ModuleName>.<ClassName>` class .""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
<ModuleName> module containing :class:`~nodedge.<Name>.<ClassName>` class. | ||
""" | ||
|
||
|
||
class MyClass: | ||
""":class:`~nodedge.<ModuleName>.<ClassName>` class .""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
library,function,op_block_string,operation_name,block_name,operation_symbol | ||
operator,add,OP_NODE_ADD,add,Addition,+ | ||
operator,sub,OP_NODE_SUBTRACT,sub,Subtraction,- | ||
operator,mul,OP_NODE_MULTIPLY,mul,Multiplication,* | ||
operator,truediv,OP_NODE_DIVIDE,truediv,Division,/ | ||
operator,mod,OP_NODE_MODULO,mod,Modulo,% | ||
operator,pow,OP_NODE_POWER,pow,Power,** | ||
operator,lt,OP_NODE_LESS,lt,Less,< | ||
operator,le,OP_NODE_LESS_EQUAL,le,Less or Equal,<= | ||
operator,eq,OP_NODE_EQUAL,eq,Equal,== | ||
operator,gt,OP_NODE_GREATER,gt,Greater,> | ||
operator,ge,OP_NODE_GREATER_EQUAL,ge,Greater or Equal,>= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import csv | ||
import os.path | ||
from string import Template | ||
|
||
config_file = "block_config.csv" | ||
save_path = "../../nodedge/tools/block_generator/generated_blocks/" | ||
|
||
if __name__ == "__main__": | ||
|
||
# Read one line of the csv | ||
with open(config_file) as infile: | ||
reader = csv.DictReader(infile, delimiter=",") | ||
|
||
for row in reader: | ||
|
||
# Generate code | ||
with open("pi_block_template.txt") as template_file: | ||
input_data = template_file.read() | ||
template = Template(input_data) | ||
|
||
output_data = template.substitute(**row) | ||
file_name = f"{row['operation_name']}_block.py" | ||
file_path = os.path.join(save_path, file_name) | ||
output_file = open(file_path, "w") | ||
output_file.write(output_data) | ||
output_file.close() | ||
|
||
template_file.close() | ||
infile.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from ${library} import ${function} | ||
|
||
from nodedge.blocks.block import Block, EvaluationError | ||
from nodedge.blocks.block_config import BLOCKS_ICONS_PATH, registerNode | ||
try: | ||
from nodedge.blocks.block_config import ${op_block_string} | ||
except: | ||
op_block_string = -1 | ||
|
||
@registerNode(${op_block_string}) | ||
class ${block_name}Block(Block): | ||
icon = f"{BLOCKS_ICONS_PATH}/${operation_name}.png" | ||
operationCode = ${op_block_string} | ||
operationTitle = "${block_name}" | ||
contentLabel = "${operation_symbol}" | ||
contentLabelObjectName = "BlockBackground" | ||
evalString = "${operation_name}" | ||
|
||
def evalImplementation(self): | ||
inputs = [] | ||
for i in range(len(self.inputSockets)): | ||
inputs.append(self.inputNodeAt(i)) | ||
|
||
try: | ||
operation = f"{${block_name}Block.evalString}(" | ||
for curr_input in inputs: | ||
operation += f"{curr_input.eval()}," | ||
operation = operation[:-1] + f")" | ||
result = eval(operation) | ||
except TypeError as e: | ||
raise EvaluationError(e) | ||
|
||
self.value = result | ||
|
||
return self.value |