# Released under the MIT License. See LICENSE for details.#"""Project related functionality."""from__future__importannotationsimportosimportjsonfrompathlibimportPathfromtypingimportTYPE_CHECKINGifTYPE_CHECKING:fromtypingimportLiteral,Any# Cache these since we may repeatedly fetch these in batch mode._g_project_configs:dict[str,dict[str,Any]]={}_g_local_configs:dict[str,dict[str,Any]]={}
[docs]defget_public_legal_notice(style:Literal['python','c++','makefile','raw'])->str:"""Return the license notice as used for our public facing stuff. 'style' arg can be 'python', 'c++', or 'makefile, or 'raw'. """# FIXME: Probably don't need style here for the minimal amount we're# doing with it now.ifstyle=='raw':return'Released under the MIT License. See LICENSE for details.'ifstyle=='python':return'# Released under the MIT License. See LICENSE for details.'ifstyle=='makefile':return'# Released under the MIT License. See LICENSE for details.'ifstyle=='c++':return'// Released under the MIT License. See LICENSE for details.'raiseRuntimeError(f'Invalid style: {style}')
[docs]defget_non_public_legal_notice()->str:"""Return the one line legal notice we expect private repo files to have."""# TODO: Move this to project config or somewhere not hard-coded.return'Copyright (c) 2011-2024 Eric Froemling'
[docs]defget_non_public_legal_notice_prev()->str:"""Allows us to auto-update."""# TODO: Move this to project config or somewhere not hard-coded.return'Copyright (c) 2011-2023 Eric Froemling'
[docs]defgetlocalconfig(projroot:Path|str)->dict[str,Any]:"""Return a project's localconfig contents (or default if missing)."""projrootstr=str(projroot)ifprojrootstrnotin_g_local_configs:localconfig:dict[str,Any]# Allow overriding path via env var.path=os.environ.get('EFRO_LOCALCONFIG_PATH')ifpathisNone:path='config/localconfig.json'try:withopen(Path(projroot,path),encoding='utf-8')asinfile:localconfig=json.loads(infile.read())exceptFileNotFoundError:localconfig={}_g_local_configs[projrootstr]=localconfigreturn_g_local_configs[projrootstr]
[docs]defgetprojectconfig(projroot:Path|str)->dict[str,Any]:"""Return a project's projectconfig contents (or default if missing)."""projrootstr=str(projroot)ifprojrootstrnotin_g_project_configs:config:dict[str,Any]try:withopen(Path(projroot,'config/projectconfig.json'),encoding='utf-8')asinfile:config=json.loads(infile.read())exceptFileNotFoundError:config={}_g_project_configs[projrootstr]=configreturn_g_project_configs[projrootstr]
[docs]defsetprojectconfig(projroot:Path|str,config:dict[str,Any])->None:"""Set the project config contents."""projrootstr=str(projroot)_g_project_configs[projrootstr]=configos.makedirs(Path(projroot,'config'),exist_ok=True)withPath(projroot,'config/projectconfig.json').open('w',encoding='utf-8')asoutfile:outfile.write(json.dumps(config,indent=2))