use xml and yaml libraries

This commit is contained in:
2026-03-28 00:05:23 -07:00
parent c946150c81
commit f8475f6cb4

View File

@@ -332,6 +332,7 @@ let
pythonEnv = pkgs.python3.withPackages ( pythonEnv = pkgs.python3.withPackages (
ps: with ps; [ ps: with ps; [
pyarr pyarr
pyyaml
requests requests
] ]
); );
@@ -390,9 +391,9 @@ let
import json import json
import os import os
import re
import sys import sys
import time import time
import xml.etree.ElementTree as ET
import requests as http import requests as http
from pyarr import SonarrAPI from pyarr import SonarrAPI
@@ -410,12 +411,11 @@ let
def read_api_key(config_xml_path): def read_api_key(config_xml_path):
"""Extract <ApiKey> from a Servarr config.xml file.""" """Extract <ApiKey> from a Servarr config.xml file."""
with open(config_xml_path) as fh: tree = ET.parse(config_xml_path)
content = fh.read() node = tree.find("ApiKey")
match = re.search(r"<ApiKey>([^<]+)</ApiKey>", content) if node is None or not node.text:
if not match:
raise ValueError(f"Could not find ApiKey in {config_xml_path}") raise ValueError(f"Could not find ApiKey in {config_xml_path}")
return match.group(1) return node.text
def wait_for_api(base_url, api_key, timeout, name): def wait_for_api(base_url, api_key, timeout, name):
@@ -793,11 +793,12 @@ let
import json import json
import os import os
import re
import sys import sys
import time import time
import xml.etree.ElementTree as ET
import requests as http import requests as http
import yaml
CONFIG = json.loads(${builtins.toJSON mkBazarrInitConfig}) CONFIG = json.loads(${builtins.toJSON mkBazarrInitConfig})
@@ -805,23 +806,22 @@ let
def read_api_key_yaml(config_yaml_path): def read_api_key_yaml(config_yaml_path):
"""Extract the apikey from Bazarr's config.yaml (auth section).""" """Extract the apikey from Bazarr's config.yaml (auth section)."""
with open(config_yaml_path) as fh: with open(config_yaml_path) as fh:
in_auth = False data = yaml.safe_load(fh)
for line in fh: try:
if line.strip().startswith("auth:"): return data["auth"]["apikey"]
in_auth = True except (KeyError, TypeError) as exc:
elif in_auth and "apikey:" in line: raise ValueError(
return line.split("apikey:")[-1].strip() f"Could not find auth.apikey in {config_yaml_path}"
raise ValueError(f"Could not find apikey in {config_yaml_path}") ) from exc
def read_api_key_xml(config_xml_path): def read_api_key_xml(config_xml_path):
"""Extract <ApiKey> from a Servarr config.xml file.""" """Extract <ApiKey> from a Servarr config.xml file."""
with open(config_xml_path) as fh: tree = ET.parse(config_xml_path)
content = fh.read() node = tree.find("ApiKey")
match = re.search(r"<ApiKey>([^<]+)</ApiKey>", content) if node is None or not node.text:
if not match:
raise ValueError(f"Could not find ApiKey in {config_xml_path}") raise ValueError(f"Could not find ApiKey in {config_xml_path}")
return match.group(1) return node.text
def wait_for_api(base_url, api_key, timeout): def wait_for_api(base_url, api_key, timeout):