From b360545aa61e730f6176e255c2aa98adfa061c7e Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Fri, 27 Oct 2023 17:29:53 +0100 Subject: [PATCH] adds a configurable timeout for login() readme, changelog updated fixes #9 --- CHANGELOG.md | 7 +++++++ README.md | 1 + pyproject.toml | 2 +- voicemeeterlib/factory.py | 1 + voicemeeterlib/remote.py | 19 ++++++++++++++----- 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 466ee4a..03afd70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,13 @@ Before any major/minor/patch bump all unit tests will be run to verify they pass - [x] +## [2.5.0] + +### Fixed + +- {Remote}.login() now has a configuratble timeout. Use timeout kwarg to set it. Defaults to 2 seconds. +- Remote class section in README updated to include timeout kwarg. + ## [2.4.8] - 2023-08-13 ### Added diff --git a/README.md b/README.md index 6cc7f84..b266ab5 100644 --- a/README.md +++ b/README.md @@ -812,6 +812,7 @@ You may pass the following optional keyword arguments: - `mdirty`: boolean=False, macrobutton updates - `midi`: boolean=False, midi updates - `ldirty`: boolean=False, level updates +- `timeout`: float=2.0, maximum time to wait for a successful login in seconds Access to lower level Getters and Setters are provided with these functions: diff --git a/pyproject.toml b/pyproject.toml index baac996..dfb16f6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "voicemeeter-api" -version = "2.4.13" +version = "2.5.0" description = "A Python wrapper for the Voiceemeter API" authors = ["onyx-and-iris "] license = "MIT" diff --git a/voicemeeterlib/factory.py b/voicemeeterlib/factory.py index ddfe54b..18b4e86 100644 --- a/voicemeeterlib/factory.py +++ b/voicemeeterlib/factory.py @@ -114,6 +114,7 @@ class FactoryBase(Remote): "mdirty": False, "midi": False, "ldirty": False, + "timeout": 2, } if "subs" in kwargs: defaultkwargs |= kwargs.pop("subs") # for backwards compatibility diff --git a/voicemeeterlib/remote.py b/voicemeeterlib/remote.py index 6f9a1bf..7e45f0e 100644 --- a/voicemeeterlib/remote.py +++ b/voicemeeterlib/remote.py @@ -75,11 +75,21 @@ class Remote(CBindings): "Voicemeeter engine running but GUI not launched. Launching the GUI now." ) self.run_voicemeeter(self.kind.name) - time.sleep(0.1) + start = time.time() + while True: + try: + time.sleep(0.1) # ensure at least 0.1 delay before clearing dirty + self.logger.info( + f"{type(self).__name__}: Successfully logged into {self} version {self.version}" + ) + elapsed = time.time() - start + self.logger.debug(f"login time: {round(elapsed, 2)}") + break + except CAPIError: + if time.time() > start + self.timeout: + raise VMError("Timeout logging into the api") + continue self.clear_dirty() - self.logger.info( - f"{type(self).__name__}: Successfully logged into {self} version {self.version}" - ) def run_voicemeeter(self, kind_id: str) -> None: if kind_id not in (kind.name.lower() for kind in KindId): @@ -89,7 +99,6 @@ class Remote(CBindings): else: value = KindId[kind_id.upper()].value self.call(self.bind_run_voicemeeter, value) - time.sleep(1) @property def type(self) -> str: