From f8475f6cb4d4d4df99002d07cf9583fb33b87876 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Sat, 28 Mar 2026 00:05:23 -0700 Subject: [PATCH] use xml and yaml libraries --- module.nix | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/module.nix b/module.nix index e9f247b..05d4c4c 100644 --- a/module.nix +++ b/module.nix @@ -332,6 +332,7 @@ let pythonEnv = pkgs.python3.withPackages ( ps: with ps; [ pyarr + pyyaml requests ] ); @@ -390,9 +391,9 @@ let import json import os - import re import sys import time + import xml.etree.ElementTree as ET import requests as http from pyarr import SonarrAPI @@ -410,12 +411,11 @@ let def read_api_key(config_xml_path): """Extract from a Servarr config.xml file.""" - with open(config_xml_path) as fh: - content = fh.read() - match = re.search(r"([^<]+)", content) - if not match: + tree = ET.parse(config_xml_path) + node = tree.find("ApiKey") + if node is None or not node.text: 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): @@ -793,11 +793,12 @@ let import json import os - import re import sys import time + import xml.etree.ElementTree as ET import requests as http + import yaml CONFIG = json.loads(${builtins.toJSON mkBazarrInitConfig}) @@ -805,23 +806,22 @@ let def read_api_key_yaml(config_yaml_path): """Extract the apikey from Bazarr's config.yaml (auth section).""" with open(config_yaml_path) as fh: - in_auth = False - for line in fh: - if line.strip().startswith("auth:"): - in_auth = True - elif in_auth and "apikey:" in line: - return line.split("apikey:")[-1].strip() - raise ValueError(f"Could not find apikey in {config_yaml_path}") + data = yaml.safe_load(fh) + try: + return data["auth"]["apikey"] + except (KeyError, TypeError) as exc: + raise ValueError( + f"Could not find auth.apikey in {config_yaml_path}" + ) from exc def read_api_key_xml(config_xml_path): """Extract from a Servarr config.xml file.""" - with open(config_xml_path) as fh: - content = fh.read() - match = re.search(r"([^<]+)", content) - if not match: + tree = ET.parse(config_xml_path) + node = tree.find("ApiKey") + if node is None or not node.text: 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):