import os from typing import Any import toml import datetime def findProjectRoot() -> tuple[bool, str]: [exists, projectFile] = findProjectFile() if not exists: return (False, "") return (True, "/".join(projectFile.split("/")[0:-1])) 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) return (True, parsedTOML) except: return (False, "Reading Project file failed, permissions may be set wrong") def writeProjectFile(projectDict: dict[str, Any]) -> tuple[bool, str]: [exists, path] = findProjectFile() if not exists: return (False, "") try: with open(path, "w") as f: toml.dump(projectDict, f) return (True, "") except: return (False, "Reading Project file failed, permissions may be set wrong") def removeLibraryInProject(lib: str) -> tuple[bool, str]: [exists, output] = loadProjectFile() if not exists: return (False, "Project doesn't exist.") projectDict = {} if isinstance(output, dict): projectDict = output else: return (False, "Output wasn't a dictionary") if "libraries" not in projectDict.keys(): return (False, "No libraries are declared in this project.") if lib in projectDict["libraries"].keys(): projectDict["libraries"].pop(lib) [wentWell, _] = writeProjectFile(projectDict) return (wentWell, "") return (False, "Library with this name is not declared") def addLibraryInProject(lib: str, std: str) -> tuple[bool, str]: [exists, output] = loadProjectFile() if not exists: return (False, "Project doesn't exist.") projectDict = {} if isinstance(output, dict): projectDict = output else: return (False, "Output wasn't a dictionary") if "libraries" not in projectDict.keys(): projectDict["libraries"] = {} if lib not in projectDict["libraries"].keys(): projectDict["libraries"][lib] = {} projectDict["libraries"][lib]["vhdl-version"] = std projectDict["libraries"][lib]["path"] = os.path.join(os.getcwd(), lib) [wentWell, _] = writeProjectFile(projectDict) return (wentWell, "") return (False, "Library with this name is already declared") def createProjectFileTemplate(projectName: str) -> str: return f""" title = "{projectName}" createdAt = "{datetime.date.today()}" maintainer = "" email = "" version = "0.0.1" """ if __name__ == "__main__": print(initProjectFile("test")) print(loadProjectFile()) print(findProjectFile()) print(findProjectRoot()) print(addLibraryInProject("ganimede", "93")) print(removeLibraryInProject("ganimede"))