From 6df4788cca5ac1e1159991400186ac8d64302e07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ben=C3=AD=C4=8Dek?= Date: Tue, 10 Dec 2024 15:55:18 +0100 Subject: [PATCH] Added variables system --- .env.sample | 3 +- .gitignore | 1 + src/main.py | 3 +- src/variables/models.py | 7 +++++ src/variables/router.py | 26 ++++++++++++++++ src/variables/service.py | 67 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 src/variables/models.py create mode 100644 src/variables/router.py create mode 100644 src/variables/service.py diff --git a/.env.sample b/.env.sample index 79a4785..f1b2700 100644 --- a/.env.sample +++ b/.env.sample @@ -1,3 +1,4 @@ MODULE_RTD8_LEVELS=01234567 #number of stack level (0-7) not separated MODULE_REL4HVI4_LEVELS=01234567 #number of stack level (0-7) not separated -MODULE_INDUSTRIALAUTOMATION_LEVELS=01234567 #number of stack level (0-7) not separated \ No newline at end of file +MODULE_INDUSTRIALAUTOMATION_LEVELS=01234567 #number of stack level (0-7) not separated +VARIABLES_SAVE_DIR=variables_storage #path to dir for saving variables \ No newline at end of file diff --git a/.gitignore b/.gitignore index 5d381cc..211d5d6 100644 --- a/.gitignore +++ b/.gitignore @@ -128,6 +128,7 @@ venv/ ENV/ env.bak/ venv.bak/ +variables_storage/ # Spyder project settings .spyderproject diff --git a/src/main.py b/src/main.py index f2a6911..44172aa 100644 --- a/src/main.py +++ b/src/main.py @@ -5,10 +5,11 @@ load_dotenv() from fastapi import FastAPI from src.modules.router import router as modulesrouter +from src.variables.router import router as variablesrouter app = FastAPI(root_path="/api") app.include_router(router=modulesrouter) - +app.include_router(router=variablesrouter) diff --git a/src/variables/models.py b/src/variables/models.py new file mode 100644 index 0000000..e3039a0 --- /dev/null +++ b/src/variables/models.py @@ -0,0 +1,7 @@ +from pydantic import BaseModel +from typing import Any + + +class SetVariable(BaseModel): + data: Any + default: bool \ No newline at end of file diff --git a/src/variables/router.py b/src/variables/router.py new file mode 100644 index 0000000..48cb5ec --- /dev/null +++ b/src/variables/router.py @@ -0,0 +1,26 @@ +from fastapi import APIRouter, status, HTTPException +import src.variables.service as service +from src.variables.models import SetVariable + + +router = APIRouter(prefix="/variables", tags=["Variables system"]) + + +@router.post("/{variable}", description="Set variable") +def set_variable(variable: str, params: SetVariable): + service.set_variable(variable=variable, data=params.data, default=params.default) + +@router.get("/{variable}", description="Get variable data", response_model=str) +def get_variable(variable: str): + try: + return service.get_variable(variable=variable) + except: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) + +@router.get("/", description="Get variables list", response_model=list) +def get_variables(): + return service.variables.keys() + +@router.delete("/{variable}", description="Remove variable") +def delete_variable(variable: str): + service.remove_variable(variable=variable) \ No newline at end of file diff --git a/src/variables/service.py b/src/variables/service.py new file mode 100644 index 0000000..1c51e5c --- /dev/null +++ b/src/variables/service.py @@ -0,0 +1,67 @@ +import os, re + + +variables: dict = {} + + +def __save_data(variable, data): + with open(variable, "w") as file: + file.write(data) + + +def __load_data(variable, type): + with open(variable, "r") as file: + return file.read() + +def __create_file_path(variable: str): + default_path: str = os.getenv("VARIABLES_SAVE_DIR", "variables_storage") + if not os.path.exists(default_path): + os.makedirs(default_path) + return f"{default_path}/{re.sub(r'[^\w\-_.]', '_', variable)}" + + +def set_variable(variable: str, data, default: bool): + """ + Set variable + """ + variables[variable] = data + + if default: + __save_data(variable=__create_file_path(variable), data=data) + + +def load_variable(variable: str) -> str: + """ + Load variable from File if exist + """ + try: + return __load_data(variable=__create_file_path(variable=variable)) + except FileNotFoundError: + raise FileNotFoundError(f"Unnable find variable: {variable}") + except: + raise ValueError((f"Error with loading variable: {variable}")) + + +def get_variable(variable: str) -> str | None: + """ + Get variable if exist + """ + if variable in variables: + return variables[variable] + else: + return None + + +def remove_variable(variable: str): + if variable in variables: + variables.pop(variable) + + filePath = __create_file_path(variable) + if os.path.exists(filePath): + os.remove(filePath) + + +#autoload variables from files +__create_file_path("none") +for file in os.listdir(os.getenv("VARIABLES_SAVE_DIR", "variables_storage")): + variables[file] = load_variable(file) \ No newline at end of file