j
This commit is contained in:
114
SYS/config.py
114
SYS/config.py
@@ -126,8 +126,17 @@ def get_provider_block(config: Dict[str, Any], name: str) -> Dict[str, Any]:
|
||||
provider_cfg = config.get("provider")
|
||||
if not isinstance(provider_cfg, dict):
|
||||
return {}
|
||||
block = provider_cfg.get(str(name).strip().lower())
|
||||
return block if isinstance(block, dict) else {}
|
||||
normalized = _normalize_provider_name(name)
|
||||
if normalized:
|
||||
block = provider_cfg.get(normalized)
|
||||
if isinstance(block, dict):
|
||||
return block
|
||||
for key, block in provider_cfg.items():
|
||||
if not isinstance(block, dict):
|
||||
continue
|
||||
if _normalize_provider_name(key) == normalized:
|
||||
return block
|
||||
return {}
|
||||
|
||||
|
||||
def get_soulseek_username(config: Dict[str, Any]) -> Optional[str]:
|
||||
@@ -334,6 +343,77 @@ def resolve_debug_log(config: Dict[str, Any]) -> Optional[Path]:
|
||||
path = Path.cwd() / path
|
||||
return path
|
||||
|
||||
def _normalize_provider_name(value: Any) -> Optional[str]:
|
||||
candidate = str(value or "").strip().lower()
|
||||
return candidate if candidate else None
|
||||
|
||||
def _extract_api_key(value: Any) -> Optional[str]:
|
||||
if isinstance(value, dict):
|
||||
for key in ("api_key", "API_KEY", "apikey", "APIKEY"):
|
||||
candidate = value.get(key)
|
||||
if isinstance(candidate, str) and candidate.strip():
|
||||
return candidate.strip()
|
||||
elif isinstance(value, str):
|
||||
trimmed = value.strip()
|
||||
if trimmed:
|
||||
return trimmed
|
||||
return None
|
||||
|
||||
|
||||
def _sync_alldebrid_api_key(config: Dict[str, Any]) -> None:
|
||||
if not isinstance(config, dict):
|
||||
return
|
||||
|
||||
providers = config.get("provider")
|
||||
if not isinstance(providers, dict):
|
||||
providers = {}
|
||||
config["provider"] = providers
|
||||
|
||||
provider_entry = providers.get("alldebrid")
|
||||
provider_section: Dict[str, Any] | None = None
|
||||
provider_key = None
|
||||
if isinstance(provider_entry, dict):
|
||||
provider_section = provider_entry
|
||||
provider_key = _extract_api_key(provider_section)
|
||||
elif isinstance(provider_entry, str):
|
||||
provider_key = provider_entry.strip()
|
||||
if provider_key:
|
||||
provider_section = {"api_key": provider_key}
|
||||
providers["alldebrid"] = provider_section
|
||||
|
||||
store_block = config.get("store")
|
||||
if not isinstance(store_block, dict):
|
||||
store_block = {}
|
||||
config["store"] = store_block
|
||||
|
||||
debrid_block = store_block.get("debrid")
|
||||
store_key = None
|
||||
if isinstance(debrid_block, dict):
|
||||
service_entry = debrid_block.get("all-debrid")
|
||||
if isinstance(service_entry, dict):
|
||||
store_key = _extract_api_key(service_entry)
|
||||
elif isinstance(service_entry, str):
|
||||
store_key = service_entry.strip()
|
||||
if store_key:
|
||||
debrid_block["all-debrid"] = {"api_key": store_key}
|
||||
else:
|
||||
debrid_block = None
|
||||
|
||||
if provider_key:
|
||||
if debrid_block is None:
|
||||
debrid_block = {}
|
||||
store_block["debrid"] = debrid_block
|
||||
service_section = debrid_block.get("all-debrid")
|
||||
if not isinstance(service_section, dict):
|
||||
service_section = {}
|
||||
debrid_block["all-debrid"] = service_section
|
||||
service_section["api_key"] = provider_key
|
||||
elif store_key:
|
||||
if provider_section is None:
|
||||
provider_section = {}
|
||||
providers["alldebrid"] = provider_section
|
||||
provider_section["api_key"] = store_key
|
||||
|
||||
|
||||
def _flatten_config_entries(config: Dict[str, Any]) -> Dict[Tuple[str, str, str, str], Any]:
|
||||
entries: Dict[Tuple[str, str, str, str], Any] = {}
|
||||
@@ -372,6 +452,7 @@ def load_config() -> Dict[str, Any]:
|
||||
# Load strictly from database
|
||||
db_config = get_config_all()
|
||||
if db_config:
|
||||
_sync_alldebrid_api_key(db_config)
|
||||
_CONFIG_CACHE = db_config
|
||||
_LAST_SAVED_CONFIG = deepcopy(db_config)
|
||||
return db_config
|
||||
@@ -387,6 +468,7 @@ def reload_config() -> Dict[str, Any]:
|
||||
|
||||
def save_config(config: Dict[str, Any]) -> int:
|
||||
global _CONFIG_CACHE, _LAST_SAVED_CONFIG
|
||||
_sync_alldebrid_api_key(config)
|
||||
previous_config = deepcopy(_LAST_SAVED_CONFIG)
|
||||
changed_count = _count_changed_entries(previous_config, config)
|
||||
|
||||
@@ -398,17 +480,23 @@ def save_config(config: Dict[str, Any]) -> int:
|
||||
if key in ('store', 'provider', 'tool'):
|
||||
if isinstance(value, dict):
|
||||
for subtype, instances in value.items():
|
||||
if isinstance(instances, dict):
|
||||
if key == 'store':
|
||||
for name, settings in instances.items():
|
||||
if isinstance(settings, dict):
|
||||
for k, v in settings.items():
|
||||
save_config_value(key, subtype, name, k, v)
|
||||
count += 1
|
||||
else:
|
||||
for k, v in instances.items():
|
||||
save_config_value(key, subtype, "default", k, v)
|
||||
count += 1
|
||||
if not isinstance(instances, dict):
|
||||
continue
|
||||
if key == 'store':
|
||||
for name, settings in instances.items():
|
||||
if isinstance(settings, dict):
|
||||
for k, v in settings.items():
|
||||
save_config_value(key, subtype, name, k, v)
|
||||
count += 1
|
||||
else:
|
||||
normalized_subtype = subtype
|
||||
if key == 'provider':
|
||||
normalized_subtype = _normalize_provider_name(subtype)
|
||||
if not normalized_subtype:
|
||||
continue
|
||||
for k, v in instances.items():
|
||||
save_config_value(key, normalized_subtype, "default", k, v)
|
||||
count += 1
|
||||
else:
|
||||
if not key.startswith("_") and value is not None:
|
||||
save_config_value("global", "none", "none", key, value)
|
||||
|
||||
Reference in New Issue
Block a user