added more helper functions
This commit is contained in:
parent
2f77dba953
commit
07576d14a4
@ -1,8 +1,16 @@
|
||||
import os
|
||||
import re
|
||||
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):
|
||||
@ -43,14 +51,48 @@ def loadProjectFile() -> tuple[bool, str | dict[str, Any]] :
|
||||
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 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 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.time()}"
|
||||
createdAt = "{datetime.date.today()}"
|
||||
maintainer = ""
|
||||
email = ""
|
||||
version = "0.0.1"
|
||||
@ -59,5 +101,8 @@ version = "0.0.1"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(initProjectFile("test"))
|
||||
print(loadProjectFile())
|
||||
print(findProjectFile())
|
||||
print(findProjectRoot())
|
||||
print(addLibraryInProject("ganimede", "93"))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user