From e19e6cda0cdc37769053b9fc2b01bc18333faea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20=C3=96rtenberg?= Date: Wed, 12 Mar 2025 13:17:00 +0100 Subject: [PATCH] added basic functionality for project files --- scripts/project_man.py | 63 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 scripts/project_man.py diff --git a/scripts/project_man.py b/scripts/project_man.py new file mode 100644 index 0000000..8283dee --- /dev/null +++ b/scripts/project_man.py @@ -0,0 +1,63 @@ +import os +from typing import Any +import toml +import datetime + +def findProjectFile() -> tuple[bool, str]: + cwd = os.getcwd().split("/") + for i in range(len(cwd) - 2): + searchPath = "/".join(cwd[0:len(cwd)-i]) + [exists, pathToProjectFile] = projectFileExists(searchPath) + if exists: + return (True, pathToProjectFile) + return (False, "") + +def projectFileExists(path: str) -> tuple[bool, str]: + files = os.listdir(path) + for file in files: + if "gantry.toml" in file: + return (True, os.path.join(path, file)) + return (False,"") + +def initProjectFile(projectName: str) -> tuple[bool, str]: + cwd = os.getcwd() + projectPath = os.path.join(cwd, "gantry.toml") + [exists, existingProjectPath] = projectFileExists(cwd) + if exists: + existingProjectName = existingProjectPath.split("/")[-1] + return (False, f"Project {existingProjectName} already exists, amend it to fit your intention or delete it to create a new project") + try: + with open(projectPath, "w") as f: + parsedTOML = toml.loads(createProjectFileTemplate(projectName)) + toml.dump(parsedTOML, f) + except: + return (False, "Creation of file failed, permissions may be set wrong") + + return (True, projectPath) + +def loadProjectFile() -> tuple[bool, str | dict[str, Any]] : + [exists, path] = findProjectFile() + if not exists: + return (False, "") + try: + with open(path, "r") as f: + toml.load + parsedTOML = toml.load(f) + print(parsedTOML) + return (True, parsedTOML) + except: + return (False, "Reading Project file failed, permissions may be set wrong") +def createProjectFileTemplate(projectName: str) -> str: + return f""" +title = "{projectName}" +createdAt = "{datetime.time()}" +maintainer = "" +email = "" +version = "0.0.1" +""" + + + +if __name__ == "__main__": + print(loadProjectFile()) + print(findProjectFile())