From 3525e1c5541f52eb020f5fedfcc07e7ca93d90a4 Mon Sep 17 00:00:00 2001 From: Radouane Lahmidi Date: Tue, 2 Apr 2024 11:18:51 +0200 Subject: [PATCH] FIX client auto-connect * FIX background colors set --- pytvpaint/george/client/__init__.py | 16 +++++++++------- pytvpaint/george/grg_project.py | 7 ++++++- pytvpaint/project.py | 3 +-- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/pytvpaint/george/client/__init__.py b/pytvpaint/george/client/__init__.py index 4feb512..58b672c 100644 --- a/pytvpaint/george/client/__init__.py +++ b/pytvpaint/george/client/__init__.py @@ -24,19 +24,21 @@ def _connect_client( ) -> JSONRPCClient: host = os.getenv("PYTVPAINT_WS_HOST", host) port = int(os.getenv("PYTVPAINT_WS_PORT", port)) - startup_connect = bool(os.getenv("PYTVPAINT_WS_STARTUP_CONNECT", 1)) + startup_connect = bool(int(os.getenv("PYTVPAINT_WS_STARTUP_CONNECT", 1))) timeout = int(os.getenv("PYTVPAINT_WS_TIMEOUT", timeout)) - if timeout == 0: - timeout = -1 - rpc_client = JSONRPCClient(f"{host}:{port}", timeout) + if not startup_connect: + return rpc_client + start_time = time() wait_duration = 5 connection_successful = False - while startup_connect and ((time() - start_time) < timeout): + while True: + if timeout and (time() - start_time) > timeout: + break with contextlib.suppress(ConnectionRefusedError): rpc_client.connect() connection_successful = True @@ -45,7 +47,7 @@ def _connect_client( log.warning(f"Connection refused, trying again in {wait_duration} seconds...") sleep(wait_duration) - if startup_connect and not connection_successful: + if not connection_successful: # Connection could not be established after timeout if rpc_client.is_connected: rpc_client.disconnect() @@ -54,7 +56,7 @@ def _connect_client( "Could not establish connection with a tvpaint instance before timeout !" ) - if startup_connect and connection_successful: + if connection_successful: log.info(f"Connected to TVPaint on port {port}") return rpc_client diff --git a/pytvpaint/george/grg_project.py b/pytvpaint/george/grg_project.py index 2e2cd2a..d300e73 100644 --- a/pytvpaint/george/grg_project.py +++ b/pytvpaint/george/grg_project.py @@ -81,7 +81,12 @@ def tv_background_set( mode: BackgroundMode, color: tuple[RGBColor, RGBColor] | RGBColor | None = None, ) -> None: - """Set the background mode of the project.""" + """Set the background mode of the project. + + Args: + mode: color mode (None, checker or one color) + color: None for None mode, RBGColor for one color, and tuple of RGBColors for checker + """ args = [] if mode == BackgroundMode.CHECK and isinstance(color, tuple): diff --git a/pytvpaint/project.py b/pytvpaint/project.py index 0a59bc6..1e82150 100644 --- a/pytvpaint/project.py +++ b/pytvpaint/project.py @@ -266,8 +266,7 @@ def background_colors( self, colors: tuple[george.RGBColor, george.RGBColor] | george.RGBColor, ) -> None: - colors_args = colors if isinstance(colors, tuple) else [colors] - george.tv_background_set(self.background_mode, *colors_args) + george.tv_background_set(self.background_mode, colors) @set_as_current def clear_background(self) -> None: