From 91ba90056c104e6219fd8678ecff41495bca861c Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Thu, 9 Mar 2023 01:34:44 +0000 Subject: [PATCH] adds get_filepath traverses a list of paths for config.toml --- obsws_python/baseclient.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/obsws_python/baseclient.py b/obsws_python/baseclient.py index e1dfc52..10e9d12 100644 --- a/obsws_python/baseclient.py +++ b/obsws_python/baseclient.py @@ -4,6 +4,7 @@ import json import logging from pathlib import Path from random import randint +from typing import Optional import websocket @@ -36,11 +37,26 @@ class ObsClient: import tomllib except ModuleNotFoundError: import tomli as tomllib + + def get_filepath() -> Optional[Path]: + """ + traverses a list of paths for a 'config.toml' + returns the first config file found or None. + """ + filepaths = [ + Path.cwd() / "config.toml", + Path.home() / "config.toml", + Path.home() / ".config" / "obsws-python" / "config.toml", + ] + for filepath in filepaths: + if filepath.exists(): + return filepath + conn = {} - filepath = Path.cwd() / "config.toml" - if filepath.exists(): + if filepath := get_filepath(): with open(filepath, "rb") as f: conn = tomllib.load(f) + self.logger.info(f"loading config from {filepath}") return conn["connection"] if "connection" in conn else conn def authenticate(self):