From f4507033a31344f207844143aa5eae8d7e6e19b2 Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Mon, 21 Nov 2022 19:04:14 +0100 Subject: [PATCH] feat(deno,python): get/set_shared_state --- deno-client/mod.ts | 17 ++++++ python-client/wmill/wmill/client.py | 92 +++++++++++++++++++++++------ 2 files changed, 90 insertions(+), 19 deletions(-) diff --git a/deno-client/mod.ts b/deno-client/mod.ts index beaee90e69..68c1aef66a 100644 --- a/deno-client/mod.ts +++ b/deno-client/mod.ts @@ -96,6 +96,23 @@ export async function setState(state: any): Promise { await setResource(state, undefined, 'state') } +/** + * Set the shared state + * @param state state to set + */ +export async function setSharedState(state: any, path = 'state.json'): Promise { + await Deno.writeTextFile(SHARED_FOLDER + '/' + path, JSON.stringify(state)) +} + +/** + * Get the shared state + * @param state state to set + */ +export async function getSharedState(path = 'state.json'): Promise { + return JSON.parse(await Deno.readTextFile(SHARED_FOLDER + '/' + path)) +} + + /** * Get the internal state * @deprecated use getState instead diff --git a/python-client/wmill/wmill/client.py b/python-client/wmill/wmill/client.py index 249ceed5a2..554004aeb9 100644 --- a/python-client/wmill/wmill/client.py +++ b/python-client/wmill/wmill/client.py @@ -1,34 +1,16 @@ from typing import Any, Union, Dict +from typing import Generic, TypeVar, TypeAlias import os from time import sleep from windmill_api.client import AuthenticatedClient -from windmill_api.api.settings import backend_version -from windmill_api.api.job import run_script_by_hash, get_job, get_completed_job - -from windmill_api.api.resource import get_resource as get_resource_api -from windmill_api.api.variable import get_variable as get_variable_api -from windmill_api.api.resource import exists_resource, update_resource, create_resource -from windmill_api.api.variable import exists_variable, update_variable, create_variable - -from windmill_api.models.update_resource_json_body import UpdateResourceJsonBody -from windmill_api.models.create_variable_json_body import CreateVariableJsonBody -from windmill_api.models.update_variable_json_body import UpdateVariableJsonBody -from windmill_api.models.create_resource_json_body import CreateResourceJsonBody -from windmill_api.models.get_job_response_200_type import GetJobResponse200Type - - -from windmill_api.models.run_script_by_hash_json_body import RunScriptByHashJsonBody - from enum import Enum from windmill_api.types import Unset -from typing import Generic, TypeVar, TypeAlias - S = TypeVar("S") @@ -82,6 +64,8 @@ def get_version() -> str: """ Returns the current version of the backend """ + from windmill_api.api.settings import backend_version + return backend_version.sync_detailed(client=create_client()).content.decode( "us-ascii" ) @@ -95,6 +79,10 @@ def run_script_async( """ Launch the run of a script and return immediately its job id """ + from windmill_api.api.job import run_script_by_hash + + from windmill_api.models.run_script_by_hash_json_body import RunScriptByHashJsonBody + return run_script_by_hash.sync_detailed( client=create_client(), workspace=get_workspace(), @@ -128,6 +116,9 @@ def get_job_status(job_id: str) -> JobStatus: """ Returns the status of a queued or completed job """ + from windmill_api.models.get_job_response_200_type import GetJobResponse200Type + from windmill_api.api.job import get_job + res = get_job.sync_detailed( client=create_client(), workspace=get_workspace(), id=job_id ).parsed @@ -150,6 +141,8 @@ def get_result(job_id: str) -> Dict[str, Any]: """ Returns the result of a completed job """ + from windmill_api.api.job import get_completed_job + res = get_completed_job.sync_detailed( client=create_client(), workspace=get_workspace(), id=job_id ).parsed @@ -165,6 +158,8 @@ def get_resource(path: str | None = None, none_if_undefined: bool = False) -> An """ Returns the resource at a given path """ + from windmill_api.api.resource import get_resource as get_resource_api + path = path or get_state_path() parsed = get_resource_api.sync_detailed( workspace=get_workspace(), path=path, client=create_client() @@ -197,6 +192,14 @@ def set_resource( """ Set the resource at a given path as a string, creating it if it does not exist """ + from windmill_api.models.create_resource_json_body import CreateResourceJsonBody + from windmill_api.api.resource import ( + exists_resource, + update_resource, + create_resource, + ) + from windmill_api.models.update_resource_json_body import UpdateResourceJsonBody + path = path or get_state_path() workspace = get_workspace() client = create_client() @@ -226,10 +229,52 @@ def set_state(value: Any) -> None: set_resource(value, None) +def set_shared_state_pickle(value: Any, path: str = "state.pickle") -> None: + """ + Set the state in the shared folder using pickle + """ + import pickle + + with open(f"/shared/{path}", "wb") as handle: + pickle.dump(value, handle, protocol=pickle.HIGHEST_PROTOCOL) + + +def get_shared_state_pickle(path: str = "state.pickle") -> Any: + """ + Get the state in the shared folder using pickle + """ + import pickle + + with open(f"/shared/{path}", "rb") as handle: + return pickle.load(handle) + + +def set_shared_state(value: Any, path: str = "state.json") -> None: + """ + Set the state in the shared folder using pickle + """ + import json + + with open(f"/shared/{path}", "w", encoding="utf-8") as f: + json.dump(value, f, ensure_ascii=False, indent=4) + + +def get_shared_state(path: str = "state.json") -> None: + """ + Set the state in the shared folder using pickle + """ + import json + + with open(f"/shared/{path}", "r", encoding="utf-8") as f: + return json.load(f) + + def get_variable(path: str) -> str: """ Returns the variable at a given path as a string """ + from windmill_api.api.variable import get_variable as get_variable_api + res = get_variable_api.sync_detailed( workspace=get_workspace(), path=path, client=create_client() ).parsed @@ -245,6 +290,15 @@ def set_variable(path: str, value: str) -> None: """ Set the variable at a given path as a string, creating it if it does not exist """ + from windmill_api.api.variable import ( + exists_variable, + update_variable, + create_variable, + ) + + from windmill_api.models.update_variable_json_body import UpdateVariableJsonBody + from windmill_api.models.create_variable_json_body import CreateVariableJsonBody + workspace = get_workspace() client = create_client() if not exists_variable.sync_detailed(