allow use without installing tomllib

When ObsClient(host='...', port='...', password='...') are provided,
importing tomllib is not actually necessary.  Allow for tomllib to
not be installed at all, and only raise a tomllib ModuleNotFoundError
if (host, port, password) are not provided.
This commit is contained in:
Kamal Mostafa 2022-10-12 23:20:27 -07:00
parent ce6873f57a
commit 71c1e65483

View File

@ -6,9 +6,13 @@ from pathlib import Path
from random import randint from random import randint
try: try:
try:
import tomllib import tomllib
except ModuleNotFoundError: except ModuleNotFoundError:
import tomli as tomllib import tomli as tomllib
except ModuleNotFoundError:
# ObsClient(host='...', port='...', password='...') must be used
tomllib = None
import websocket import websocket
@ -21,7 +25,12 @@ class ObsClient:
def __init__(self, **kwargs): def __init__(self, **kwargs):
defaultkwargs = {"host": "localhost", "port": 4455, "password": None, "subs": 0} defaultkwargs = {"host": "localhost", "port": 4455, "password": None, "subs": 0}
if not any(key in kwargs for key in ("host", "port", "password")): if not any(key in kwargs for key in ("host", "port", "password")):
if tomllib:
kwargs |= self._conn_from_toml() kwargs |= self._conn_from_toml()
else:
raise ModuleNotFoundError(
"tomllib not installed; Perhaps use "
"ObsClient(host='...', port='...', password='...')")
kwargs = defaultkwargs | kwargs kwargs = defaultkwargs | kwargs
for attr, val in kwargs.items(): for attr, val in kwargs.items():
setattr(self, attr, val) setattr(self, attr, val)