Added libs
This commit is contained in:
56
app/common/config.py
Normal file
56
app/common/config.py
Normal file
@@ -0,0 +1,56 @@
|
||||
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
|
||||
Reference in New Issue
Block a user