From ab432d628a1caae8abf1775a01954b5486fe044d Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Sun, 5 Feb 2023 16:02:53 +0100 Subject: [PATCH] import python code as raw to avoid reindent --- frontend/src/lib/components/SchemaForm.svelte | 1 - .../lib/init_scripts/python_failure_module.py | 10 ++ .../src/lib/init_scripts/python_init_code.py | 37 ++++ .../init_scripts/python_init_code_clear.py | 5 + .../init_scripts/python_init_code_trigger.py | 14 ++ frontend/src/lib/script_helpers.ts | 170 ++++++------------ 6 files changed, 122 insertions(+), 115 deletions(-) create mode 100644 frontend/src/lib/init_scripts/python_failure_module.py create mode 100644 frontend/src/lib/init_scripts/python_init_code.py create mode 100644 frontend/src/lib/init_scripts/python_init_code_clear.py create mode 100644 frontend/src/lib/init_scripts/python_init_code_trigger.py diff --git a/frontend/src/lib/components/SchemaForm.svelte b/frontend/src/lib/components/SchemaForm.svelte index ca136b6117..9a1054e8d7 100644 --- a/frontend/src/lib/components/SchemaForm.svelte +++ b/frontend/src/lib/components/SchemaForm.svelte @@ -47,7 +47,6 @@ if (!keys.includes(key)) { delete args[key] delete inputCheck[key] - console.log('DELETED', key) } }) } diff --git a/frontend/src/lib/init_scripts/python_failure_module.py b/frontend/src/lib/init_scripts/python_failure_module.py new file mode 100644 index 0000000000..6802529782 --- /dev/null +++ b/frontend/src/lib/init_scripts/python_failure_module.py @@ -0,0 +1,10 @@ +import os + +# flow is considered recovered and a success unless an exception is raised + + +def main(message: str, name: str): + flow_id = os.environ.get("WM_FLOW_JOB_ID") + print("message", message) + print("name", name) + return message, flow_id diff --git a/frontend/src/lib/init_scripts/python_init_code.py b/frontend/src/lib/init_scripts/python_init_code.py new file mode 100644 index 0000000000..39b01699e3 --- /dev/null +++ b/frontend/src/lib/init_scripts/python_init_code.py @@ -0,0 +1,37 @@ +import os +import wmill + +# You can import any package from PyPI, even if the assistant complains +# Ctrl+S to format the code. Autocompletion available. + + +def main( + no_default: str, + name="Nicolas Bourbaki", + age=42, + obj: dict = {"even": "dicts"}, + l: list = ["or", "lists!"], + file_: bytes = bytes(0), +): + + print(f"Hello World and a warm welcome especially to {name}") + print("and its acolytes..", age, obj, l, len(file_)) + + # retrieve variables, resources, states using the wmill client + try: + secret = wmill.get_variable("f/examples/secret") + except: + secret = "No secret yet at f/examples/secret !" + print(f"The variable at `f/examples/secret`: {secret}") + + # Get last state of this script execution by the same trigger/user + last_state = wmill.get_state() + new_state = {"foo": 42} if last_state is None else last_state + new_state["foo"] += 1 + wmill.set_state(new_state) + + # fetch context variables + user = os.environ.get("WM_USERNAME") + + # return value is converted to JSON + return {"splitted": name.split(), "user": user, "state": new_state} diff --git a/frontend/src/lib/init_scripts/python_init_code_clear.py b/frontend/src/lib/init_scripts/python_init_code_clear.py new file mode 100644 index 0000000000..32e567beca --- /dev/null +++ b/frontend/src/lib/init_scripts/python_init_code_clear.py @@ -0,0 +1,5 @@ +# import wmill + + +def main(x: str): + return x diff --git a/frontend/src/lib/init_scripts/python_init_code_trigger.py b/frontend/src/lib/init_scripts/python_init_code_trigger.py new file mode 100644 index 0000000000..3f552dc1a4 --- /dev/null +++ b/frontend/src/lib/init_scripts/python_init_code_trigger.py @@ -0,0 +1,14 @@ +import wmill + + +def main(): + # A common trigger script would follow this pattern: + # 1. Get the last saved state + # const state = wnmill.get_state() + # 2. Get the actual state from the external service + # const newState = ... + # 3. Compare the two states and update the internal state + # wmill.setState(newState) + # 4. Return the new rows + # return range from (state to newState) + return [1, 2, 3] diff --git a/frontend/src/lib/script_helpers.ts b/frontend/src/lib/script_helpers.ts index 2bd094ea3b..788ee81ab0 100644 --- a/frontend/src/lib/script_helpers.ts +++ b/frontend/src/lib/script_helpers.ts @@ -1,40 +1,12 @@ import type { Script } from "./gen" -export const PYTHON_INIT_CODE = `import os -import wmill +import PYTHON_INIT_CODE from '$lib/init_scripts/python_init_code.py?raw' +import PYTHON_INIT_CODE_CLEAR from '$lib/init_scripts/python_init_code_clear.py?raw' +import PYTHON_INIT_CODE_TRIGGER from '$lib/init_scripts/python_init_code_trigger.py?raw' +import PYTHON_FAILURE_MODULE_CODE from '$lib/init_scripts/python_failure_module.py?raw' -# You can import any package from PyPI, even if the assistant complains -# Ctrl+S to format the code. Autocompletion available. +export { PYTHON_INIT_CODE, PYTHON_INIT_CODE_CLEAR, PYTHON_INIT_CODE_TRIGGER, PYTHON_FAILURE_MODULE_CODE } -def main(no_default: str, - name = "Nicolas Bourbaki", - age = 42, - obj: dict = {"even": "dicts"}, - l: list = ["or", "lists!"], - file_: bytes = bytes(0)): - - print(f"Hello World and a warm welcome especially to {name}") - print("and its acolytes..", age, obj, l, len(file_)) - - # retrieve variables, resources, states using the wmill client - try: - secret = wmill.get_variable("f/examples/secret") - except: - secret = "No secret yet at f/examples/secret !" - print(f"The variable at \`f/examples/secret\`: {secret}") - - # Get last state of this script execution by the same trigger/user - last_state = wmill.get_state() - new_state = {"foo": 42} if last_state is None else last_state - new_state["foo"] += 1 - wmill.set_state(new_state) - - # fetch context variables - user = os.environ.get("WM_USERNAME") - - # return value is converted to JSON - return {"splitted": name.split(), "user": user, "state": new_state} -` export const DENO_INIT_CODE = `// Ctrl+. to cache dependencies on imports hover, Ctrl+S to format. // import { toWords } from "npm:number-to-words@1" @@ -105,21 +77,6 @@ export async function main(message: string, name: string) { } ` -export const PYTHON_INIT_CODE_CLEAR = `#import wmill - -def main(x: str): - return x -` - -export const PYTHON_FAILURE_MODULE_CODE = `import os -# flow is considered recovered and a success unless an exception is raised - -def main(message: str, name: str): - flow_id = os.environ.get("WM_FLOW_JOB_ID") - print("message", message) - print("name", name) - return message, flow_id -` export const POSTGRES_INIT_CODE = `import { pgSql, @@ -184,22 +141,6 @@ export async function main() { } ` -export const PYTHON_INIT_CODE_TRIGGER = `import wmill - -def main(): - # A common trigger script would follow this pattern: - # 1. Get the last saved state - # const state = wnmill.get_state() - # 2. Get the actual state from the external service - # const newState = ... - # 3. Compare the two states and update the internal state - # wmill.setState(newState) - # 4. Return the new rows - # return range from (state to newState) - return [1, 2, 3] -` - - export const GO_INIT_CODE_TRIGGER = `package inner import ( @@ -233,57 +174,58 @@ export async function main(approver?: string) { const ALL_INITIAL_CODE = [PYTHON_INIT_CODE, PYTHON_INIT_CODE_TRIGGER, DENO_INIT_CODE, POSTGRES_INIT_CODE, DENO_INIT_CODE_TRIGGER, DENO_INIT_CODE_CLEAR, PYTHON_INIT_CODE_CLEAR, DENO_INIT_CODE_APPROVAL, DENO_FAILURE_MODULE_CODE] export function isInitialCode(content: string): boolean { - for (const code of ALL_INITIAL_CODE) { - if (content === code) { - return true - } - } - return false + for (const code of ALL_INITIAL_CODE) { + if (content === code) { + return true + } + } + return false } export function initialCode(language: 'deno' | 'python3' | 'go' | 'bash', kind: Script.kind, subkind: 'pgsql' | 'mysql' | 'flow' | 'script' | undefined): string { - if (language === 'deno') { - if (kind === 'trigger') { - return DENO_INIT_CODE_TRIGGER - } else if (kind === 'script') { - if (subkind === 'flow') { - return DENO_INIT_CODE_CLEAR - } - else if (subkind === 'pgsql') { - return POSTGRES_INIT_CODE - } - else if (subkind === 'mysql') { - return MYSQL_INIT_CODE - } else { - return DENO_INIT_CODE - } - } else if (kind === 'failure') { - return DENO_FAILURE_MODULE_CODE - } else if (kind === 'approval') { - return DENO_INIT_CODE_APPROVAL - } - else { - return DENO_INIT_CODE - } - } else if (language === 'python3') { - if (subkind === 'flow') { - return PYTHON_INIT_CODE_CLEAR - } else if (kind === 'failure') { - return PYTHON_FAILURE_MODULE_CODE - } else if (kind === 'trigger') { - return PYTHON_INIT_CODE_TRIGGER - } else { - return PYTHON_INIT_CODE - } - } else if (language == 'bash') { - return BASH_INIT_CODE - } else { - if (kind === 'failure') { - return GO_FAILURE_MODULE_CODE - } else if (kind === 'trigger') { - return GO_INIT_CODE_TRIGGER - } else { - return GO_INIT_CODE - } - } + + if (language === 'deno') { + if (kind === 'trigger') { + return DENO_INIT_CODE_TRIGGER + } else if (kind === 'script') { + if (subkind === 'flow') { + return DENO_INIT_CODE_CLEAR + } + else if (subkind === 'pgsql') { + return POSTGRES_INIT_CODE + } + else if (subkind === 'mysql') { + return MYSQL_INIT_CODE + } else { + return DENO_INIT_CODE + } + } else if (kind === 'failure') { + return DENO_FAILURE_MODULE_CODE + } else if (kind === 'approval') { + return DENO_INIT_CODE_APPROVAL + } + else { + return DENO_INIT_CODE + } + } else if (language === 'python3') { + if (subkind === 'flow') { + return PYTHON_INIT_CODE_CLEAR + } else if (kind === 'failure') { + return PYTHON_FAILURE_MODULE_CODE + } else if (kind === 'trigger') { + return PYTHON_INIT_CODE_TRIGGER + } else { + return PYTHON_INIT_CODE + } + } else if (language == 'bash') { + return BASH_INIT_CODE + } else { + if (kind === 'failure') { + return GO_FAILURE_MODULE_CODE + } else if (kind === 'trigger') { + return GO_INIT_CODE_TRIGGER + } else { + return GO_INIT_CODE + } + } }