Source code for efrotools.project

# Released under the MIT License. See LICENSE for details.
#
"""Project related functionality."""

from __future__ import annotations

import os
import json
from pathlib import Path
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from typing import Literal, 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] def getlocalconfig(projroot: Path | str) -> dict[str, Any]: """Return a project's localconfig contents (or default if missing).""" projrootstr = str(projroot) if projrootstr not in _g_local_configs: localconfig: dict[str, Any] # Allow overriding path via env var. path = os.environ.get('EFRO_LOCALCONFIG_PATH') if path is None: path = 'config/localconfig.json' try: with open(Path(projroot, path), encoding='utf-8') as infile: localconfig = json.loads(infile.read()) except FileNotFoundError: localconfig = {} _g_local_configs[projrootstr] = localconfig return _g_local_configs[projrootstr]
[docs] def getprojectconfig(projroot: Path | str) -> dict[str, Any]: """Return a project's projectconfig contents (or default if missing).""" projrootstr = str(projroot) if projrootstr not in _g_project_configs: config: dict[str, Any] try: with open( Path(projroot, 'config/projectconfig.json'), encoding='utf-8' ) as infile: config = json.loads(infile.read()) except FileNotFoundError: config = {} _g_project_configs[projrootstr] = config return _g_project_configs[projrootstr]
[docs] def setprojectconfig(projroot: Path | str, config: dict[str, Any]) -> None: """Set the project config contents.""" projrootstr = str(projroot) _g_project_configs[projrootstr] = config os.makedirs(Path(projroot, 'config'), exist_ok=True) with Path(projroot, 'config/projectconfig.json').open( 'w', encoding='utf-8' ) as outfile: outfile.write(json.dumps(config, indent=2))