From a7510f40428d76f49b690a683d9592e9cedbabeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Wed, 31 Aug 2022 08:08:06 +0200 Subject: [PATCH] Cleanup: addon, split ping_manager() into two functions Split the `comms.ping_manager()` function into two: one that returns the version & config of the Manager, and the other that reports on it. This will make it possible to do the former without the latter in certain other situations where we want to refresh the manager info in the background. --- addon/flamenco/comms.py | 60 +++++++++++++++++++++++------------ addon/flamenco/operators.py | 2 +- addon/flamenco/preferences.py | 2 +- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/addon/flamenco/comms.py b/addon/flamenco/comms.py index 12d5161a..2948aeb7 100644 --- a/addon/flamenco/comms.py +++ b/addon/flamenco/comms.py @@ -3,7 +3,7 @@ # import logging -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Optional from urllib3.exceptions import HTTPError, MaxRetryError import bpy @@ -13,10 +13,16 @@ _log = logging.getLogger(__name__) if TYPE_CHECKING: from flamenco.manager import ApiClient as _ApiClient + from flamenco.manager.models import ( + FlamencoVersion as _FlamencoVersion, + ManagerConfiguration as _ManagerConfiguration, + ) from .preferences import FlamencoPreferences as _FlamencoPreferences else: _ApiClient = object _FlamencoPreferences = object + _FlamencoVersion = object + _ManagerConfiguration = object def flamenco_api_client(manager_url: str) -> _ApiClient: @@ -50,7 +56,9 @@ def discard_flamenco_data(): _flamenco_client = None -def ping_manager(context: bpy.types.Context, api_client: _ApiClient, prefs: _FlamencoPreferences) -> tuple[str, str]: +def ping_manager_with_report( + context: bpy.types.Context, api_client: _ApiClient, prefs: _FlamencoPreferences +) -> tuple[str, str]: """Ping the Manager, update preferences, and return a report as string. :returns: tuple (report, level). The report will be something like " @@ -59,36 +67,48 @@ def ping_manager(context: bpy.types.Context, api_client: _ApiClient, prefs: _Fla `Operator.report()`. """ + context.window_manager.flamenco_status_ping = "..." + + version, _, err = ping_manager(api_client, prefs) + if err: + context.window_manager.flamenco_status_ping = err + return err, "ERROR" + + assert version is not None + report = "%s version %s found" % (version.name, version.version) + context.window_manager.flamenco_status_ping = report + return report, "INFO" + + +def ping_manager( + api_client: _ApiClient, prefs: _FlamencoPreferences +) -> tuple[Optional[_FlamencoVersion], Optional[_ManagerConfiguration], str]: + """Fetch Manager config & version, and update preferences. + + :returns: tuple (version, config, error). + """ + # Do a late import, so that the API is only imported when actually used. from flamenco.manager import ApiException from flamenco.manager.apis import MetaApi from flamenco.manager.models import FlamencoVersion, ManagerConfiguration - context.window_manager.flamenco_status_ping = "..." - meta_api = MetaApi(api_client) try: version: FlamencoVersion = meta_api.get_version() config: ManagerConfiguration = meta_api.get_configuration() except ApiException as ex: - report = "Manager cannot be reached: %s" % ex - level = "ERROR" + return (None, None, "Manager cannot be reached: %s" % ex) except MaxRetryError as ex: # This is the common error, when for example the port number is - # incorrect and nothing is listening. - report = "Manager cannot be reached" - level = "WARNING" + # incorrect and nothing is listening. The exception text is not included + # because it's very long and confusing. + return (None, None, "Manager cannot be reached") except HTTPError as ex: - report = "Manager cannot be reached: %s" % ex - level = "ERROR" - else: - report = "%s version %s found" % (version.name, version.version) - level = "INFO" + return (None, None, "Manager cannot be reached: %s" % ex) - # Store whether this Manager supports the Shaman API. - prefs.is_shaman_enabled = config.shaman_enabled - prefs.job_storage = config.storage_location + # Store whether this Manager supports the Shaman API. + prefs.is_shaman_enabled = config.shaman_enabled + prefs.job_storage = config.storage_location - context.window_manager.flamenco_status_ping = report - - return report, level + return version, config, "" diff --git a/addon/flamenco/operators.py b/addon/flamenco/operators.py index 2af06c54..6b47ca90 100644 --- a/addon/flamenco/operators.py +++ b/addon/flamenco/operators.py @@ -86,7 +86,7 @@ class FLAMENCO_OT_ping_manager(FlamencoOpMixin, bpy.types.Operator): api_client = self.get_api_client(context) prefs = preferences.get(context) - report, level = comms.ping_manager(context, api_client, prefs) + report, level = comms.ping_manager_with_report(context, api_client, prefs) self.report({level}, report) return {"FINISHED"} diff --git a/addon/flamenco/preferences.py b/addon/flamenco/preferences.py index 437a6a06..fd06f3bd 100644 --- a/addon/flamenco/preferences.py +++ b/addon/flamenco/preferences.py @@ -31,7 +31,7 @@ def _manager_url_updated(prefs, context): # Warning, be careful what of the context to access here. Accessing / # changing too much can cause crashes, infinite loops, etc. - comms.ping_manager(context, api_client, prefs) + comms.ping_manager_with_report(context, api_client, prefs) class FlamencoPreferences(bpy.types.AddonPreferences):