From 71c1e654834f859361ea4007f0bc1a8a7f2ed4f1 Mon Sep 17 00:00:00 2001 From: Kamal Mostafa Date: Wed, 12 Oct 2022 23:20:27 -0700 Subject: [PATCH 1/2] 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. --- obsws_python/baseclient.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/obsws_python/baseclient.py b/obsws_python/baseclient.py index 4b8ba2f..9f43704 100644 --- a/obsws_python/baseclient.py +++ b/obsws_python/baseclient.py @@ -6,9 +6,13 @@ from pathlib import Path from random import randint try: - import tomllib + try: + import tomllib + except ModuleNotFoundError: + import tomli as tomllib except ModuleNotFoundError: - import tomli as tomllib + # ObsClient(host='...', port='...', password='...') must be used + tomllib = None import websocket @@ -21,7 +25,12 @@ class ObsClient: def __init__(self, **kwargs): defaultkwargs = {"host": "localhost", "port": 4455, "password": None, "subs": 0} if not any(key in kwargs for key in ("host", "port", "password")): - kwargs |= self._conn_from_toml() + if tomllib: + kwargs |= self._conn_from_toml() + else: + raise ModuleNotFoundError( + "tomllib not installed; Perhaps use " + "ObsClient(host='...', port='...', password='...')") kwargs = defaultkwargs | kwargs for attr, val in kwargs.items(): setattr(self, attr, val) From 83afe31e04933be34fcc3252572a95867ac6b299 Mon Sep 17 00:00:00 2001 From: Onyx and Iris Date: Sun, 4 Dec 2022 19:34:55 +0000 Subject: [PATCH 2/2] Update baseclient.py lazy load tomli/tomllib as suggested in #17 --- obsws_python/baseclient.py | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/obsws_python/baseclient.py b/obsws_python/baseclient.py index 9f43704..273e96b 100644 --- a/obsws_python/baseclient.py +++ b/obsws_python/baseclient.py @@ -5,15 +5,6 @@ import logging from pathlib import Path from random import randint -try: - try: - import tomllib - except ModuleNotFoundError: - import tomli as tomllib -except ModuleNotFoundError: - # ObsClient(host='...', port='...', password='...') must be used - tomllib = None - import websocket from .error import OBSSDKError @@ -25,12 +16,7 @@ class ObsClient: def __init__(self, **kwargs): defaultkwargs = {"host": "localhost", "port": 4455, "password": None, "subs": 0} if not any(key in kwargs for key in ("host", "port", "password")): - if tomllib: - kwargs |= self._conn_from_toml() - else: - raise ModuleNotFoundError( - "tomllib not installed; Perhaps use " - "ObsClient(host='...', port='...', password='...')") + kwargs |= self._conn_from_toml() kwargs = defaultkwargs | kwargs for attr, val in kwargs.items(): setattr(self, attr, val) @@ -46,6 +32,10 @@ class ObsClient: self.server_hello = json.loads(self.ws.recv()) def _conn_from_toml(self) -> dict: + try: + import tomllib + except ModuleNotFoundError: + import tomli as tomllib conn = {} filepath = Path.cwd() / "config.toml" if filepath.exists():