Files
nosys_libs/app/common/config.py
2026-01-25 13:55:46 +10:00

57 lines
1.7 KiB
Python

import json
import os
from pathlib import Path
from typing import Dict, Any
from collections import OrderedDict
from .paths import LIBS_DIR
class Config:
def __init__(self):
self.libs: Dict[str, Dict[str, Any]] = {}
self.load_libs_config()
def load_libs_config(self):
for lib_path in LIBS_DIR.iterdir():
if lib_path.is_dir():
info_path = lib_path / "info.json"
config_path = lib_path / "config.json"
lib_id = lib_path.name
info_data = {}
config_data = {}
if info_path.exists():
with open(info_path) as f:
info_data = json.load(f, object_pairs_hook=OrderedDict)
if config_path.exists():
with open(config_path) as f:
config_data = json.load(f, object_pairs_hook=OrderedDict)
config_data["info"] = info_data
self.libs[lib_id] = config_data
def get(self, lib: str, *keys, default=None):
"""
Retrieves values from the config.
Example:
config.get("app", "libs", "app", "update", "version", default="1.0")
Parameters:
- lib: the name of the library
- *keys: a sequence of keys to navigate through nested dictionaries
- default: the value to return if any key in the chain does not exist
Returns:
- The value found at the nested key path, or `default` if a key is missing.
"""
value = self.libs.get(lib, default)
for key in keys:
if isinstance(value, dict) and key in value:
value = value[key]
else:
return default
return value