64 lines
1.9 KiB
Python
64 lines
1.9 KiB
Python
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())
|