From c4e0d7b7484a79c5fbdbfb70aa557fedcda4c9b6 Mon Sep 17 00:00:00 2001 From: Joey Yu Date: Mon, 27 Nov 2023 16:18:32 -0500 Subject: [PATCH] chore: refactor import and block structure --- .../flojoy/control}/slider.py | 0 .../flojoy/math/arithmetic}/add.py | 0 .../flojoy/math/arithmetic}/subtract.py | 0 .../blocks => blocks/flojoy/math}/constant.py | 0 .../flojoy/visualization}/bignum.py | 0 captain/blocks/button.py | 6 --- captain/blocks/gamepad.py | 6 --- captain/controllers/reactive.py | 4 +- captain/types/flowchart.py | 6 ++- captain/utils/blocks.py | 45 +++++++++++++------ .../src/components/flow/BlockCard.tsx | 6 +-- .../src/components/flow/BlocksLibrary.tsx | 10 +++-- .../src/components/flow/FlowCanvas.tsx | 12 ++--- src/renderer/src/types/block.ts | 8 +++- 14 files changed, 62 insertions(+), 41 deletions(-) rename {captain/blocks => blocks/flojoy/control}/slider.py (100%) rename {captain/blocks => blocks/flojoy/math/arithmetic}/add.py (100%) rename {captain/blocks => blocks/flojoy/math/arithmetic}/subtract.py (100%) rename {captain/blocks => blocks/flojoy/math}/constant.py (100%) rename {captain/blocks => blocks/flojoy/visualization}/bignum.py (100%) delete mode 100644 captain/blocks/button.py delete mode 100644 captain/blocks/gamepad.py diff --git a/captain/blocks/slider.py b/blocks/flojoy/control/slider.py similarity index 100% rename from captain/blocks/slider.py rename to blocks/flojoy/control/slider.py diff --git a/captain/blocks/add.py b/blocks/flojoy/math/arithmetic/add.py similarity index 100% rename from captain/blocks/add.py rename to blocks/flojoy/math/arithmetic/add.py diff --git a/captain/blocks/subtract.py b/blocks/flojoy/math/arithmetic/subtract.py similarity index 100% rename from captain/blocks/subtract.py rename to blocks/flojoy/math/arithmetic/subtract.py diff --git a/captain/blocks/constant.py b/blocks/flojoy/math/constant.py similarity index 100% rename from captain/blocks/constant.py rename to blocks/flojoy/math/constant.py diff --git a/captain/blocks/bignum.py b/blocks/flojoy/visualization/bignum.py similarity index 100% rename from captain/blocks/bignum.py rename to blocks/flojoy/visualization/bignum.py diff --git a/captain/blocks/button.py b/captain/blocks/button.py deleted file mode 100644 index 6b53b31..0000000 --- a/captain/blocks/button.py +++ /dev/null @@ -1,6 +0,0 @@ -from captain.decorators import ui_input - - -@ui_input -def button(x): - return x diff --git a/captain/blocks/gamepad.py b/captain/blocks/gamepad.py deleted file mode 100644 index 31b4338..0000000 --- a/captain/blocks/gamepad.py +++ /dev/null @@ -1,6 +0,0 @@ -from captain.decorators import ui_input - - -@ui_input -def gamepad(x): - return x diff --git a/captain/controllers/reactive.py b/captain/controllers/reactive.py index 3940f02..8fb34f8 100644 --- a/captain/controllers/reactive.py +++ b/captain/controllers/reactive.py @@ -12,7 +12,7 @@ from captain.types.flowchart import BlockID, FCBlock, FlowChart from captain.utils.blocks import import_blocks, is_ui_input -BLOCKS_DIR = os.path.join("captain", "blocks") +BLOCKS_DIR = os.path.join("blocks") ZIPPED_BLOCKS = [] # TODO: I (sasha) am anti zip in all cases. @@ -183,6 +183,8 @@ def __init__( funcs = import_blocks(BLOCKS_DIR) + print(funcs, flush=True) + for block in flowchart.blocks: if is_ui_input(funcs[block.block_type]): logger.debug(f"Creating UI input for {block.id}") diff --git a/captain/types/flowchart.py b/captain/types/flowchart.py index ead5c8b..77fcbd8 100644 --- a/captain/types/flowchart.py +++ b/captain/types/flowchart.py @@ -4,7 +4,11 @@ # TODO: This is hardcoded for now BlockType = Literal[ - "slider", "gamepad", "button", "bignum", "add", "subtract", "constant" + "flojoy.control.slider", + "flojoy.visualization.bignum", + "flojoy.math.arithmetic.add", + "flojoy.math.arithmetic.subtract", + "flojoy.math.constant", ] BlockID: TypeAlias = str diff --git a/captain/utils/blocks.py b/captain/utils/blocks.py index 1a893cc..52592f3 100644 --- a/captain/utils/blocks.py +++ b/captain/utils/blocks.py @@ -2,30 +2,47 @@ import os from typing import Callable, Mapping +from captain.logging import logger + def import_blocks(blocks_dir: str) -> Mapping[str, Callable]: + # example key: "flojoy.math.arithmetic.add" functions = {} - for file in os.listdir(blocks_dir): - full_path = os.path.join(blocks_dir, file) - if not os.path.isfile(full_path) or not file.endswith(".py"): - continue - block_name = file.strip(".py") - spec = importlib.util.spec_from_file_location(block_name, full_path) + for root, _, files in os.walk(blocks_dir): + for file in files: + if not file.endswith(".py"): + continue + + block_name = file.strip(".py") + + block_path = os.path.join(root, file) + + # block_type is something like "flojoy.math.arithmetic.add" + block_type = os.path.relpath( + os.path.splitext(block_path)[0], blocks_dir + ).replace("/", ".") - if not spec: - raise ValueError(f"Invalid block spec from {full_path}") + spec = importlib.util.spec_from_file_location(block_name, block_path) - module = importlib.util.module_from_spec(spec) - if not spec.loader: - raise ValueError(f"Could not get loader from {full_path}") + if not spec: + raise ValueError(f"Invalid block spec from {block_path}") - spec.loader.exec_module(module) - func = getattr(module, block_name) - functions[block_name] = func + module = importlib.util.module_from_spec(spec) + if not spec.loader: + raise ValueError(f"Could not get loader from {block_path}") + + spec.loader.exec_module(module) + func = getattr(module, block_name) + + logger.info(f"importing {block_type}") + functions[block_type] = func return functions def is_ui_input(func: Callable) -> bool: return getattr(func, "ui_input", False) + + +import_blocks("blocks") diff --git a/src/renderer/src/components/flow/BlockCard.tsx b/src/renderer/src/components/flow/BlockCard.tsx index 5703e60..3960f92 100644 --- a/src/renderer/src/components/flow/BlockCard.tsx +++ b/src/renderer/src/components/flow/BlockCard.tsx @@ -3,11 +3,11 @@ import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '../ui/ type Props = { name: string; - block_id: string; + block_type: string; desc: string; }; -const BlockCard = ({ name, desc, block_id }: Props): JSX.Element => { +const BlockCard = ({ name, desc, block_type }: Props): JSX.Element => { const onDragStart = (event, nodeType) => { event.dataTransfer.setData('application/reactflow', nodeType); event.dataTransfer.effectAllowed = 'move'; @@ -24,7 +24,7 @@ const BlockCard = ({ name, desc, block_id }: Props): JSX.Element => { className="" variant="secondary" draggable - onDragStart={(event) => onDragStart(event, block_id)} + onDragStart={(event) => onDragStart(event, block_type)} >
{name}
diff --git a/src/renderer/src/components/flow/BlocksLibrary.tsx b/src/renderer/src/components/flow/BlocksLibrary.tsx index a97064a..1617fb8 100644 --- a/src/renderer/src/components/flow/BlocksLibrary.tsx +++ b/src/renderer/src/components/flow/BlocksLibrary.tsx @@ -13,9 +13,13 @@ const BlocksLibrary = () => {
- - - + + +
); diff --git a/src/renderer/src/components/flow/FlowCanvas.tsx b/src/renderer/src/components/flow/FlowCanvas.tsx index a686bb0..272b752 100644 --- a/src/renderer/src/components/flow/FlowCanvas.tsx +++ b/src/renderer/src/components/flow/FlowCanvas.tsx @@ -22,9 +22,9 @@ import useUndoRedo from '@/hooks/useUndoRedo'; // import useUndoRedo from '@/hooks/useUndoRedo'; const nodeTypes = { - slider: SliderBlock, - bignum: BigNumberBlock, - add: AddBlock + 'flojoy.control.slider': SliderBlock, + 'flojoy.visualization.bignum': BigNumberBlock, + 'flojoy.math.arithmetic.add': AddBlock }; const edgeTypes = { @@ -84,10 +84,10 @@ const FlowCanvas = () => { return; } - const block_id = event.dataTransfer.getData('application/reactflow'); + const block_type = event.dataTransfer.getData('application/reactflow'); // check if the dropped element is valid - if (typeof block_id === 'undefined' || !block_id) { + if (typeof block_type === 'undefined' || !block_type) { return; } @@ -99,7 +99,7 @@ const FlowCanvas = () => { y: event.clientY }); - addNode(block_id, position); + addNode(block_type, position); }, [reactFlowInstance] ); diff --git a/src/renderer/src/types/block.ts b/src/renderer/src/types/block.ts index 2cd2f17..3135fc0 100644 --- a/src/renderer/src/types/block.ts +++ b/src/renderer/src/types/block.ts @@ -1,6 +1,12 @@ import { NodeProps } from 'reactflow'; -export type BlockType = 'slider' | 'gamepad' | 'button' | 'bignum' | 'add' | 'subtract'; +// TODO: This should not be hardcoded +export type BlockType = + | 'flojoy.control.slider' + | 'flojoy.visualization.bignum' + | 'flojoy.math.arithmetic.add' + | 'flojoy.math.arithmetic.subtract' + | 'flojoy.math.constant'; export type BlockData = { block_type: BlockType;