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.
This commit is contained in:
Sybren A. Stüvel 2022-08-31 08:08:06 +02:00
parent 4761e3d76e
commit a7510f4042
3 changed files with 42 additions and 22 deletions

View File

@ -3,7 +3,7 @@
# <pep8 compliant> # <pep8 compliant>
import logging import logging
from typing import TYPE_CHECKING from typing import TYPE_CHECKING, Optional
from urllib3.exceptions import HTTPError, MaxRetryError from urllib3.exceptions import HTTPError, MaxRetryError
import bpy import bpy
@ -13,10 +13,16 @@ _log = logging.getLogger(__name__)
if TYPE_CHECKING: if TYPE_CHECKING:
from flamenco.manager import ApiClient as _ApiClient from flamenco.manager import ApiClient as _ApiClient
from flamenco.manager.models import (
FlamencoVersion as _FlamencoVersion,
ManagerConfiguration as _ManagerConfiguration,
)
from .preferences import FlamencoPreferences as _FlamencoPreferences from .preferences import FlamencoPreferences as _FlamencoPreferences
else: else:
_ApiClient = object _ApiClient = object
_FlamencoPreferences = object _FlamencoPreferences = object
_FlamencoVersion = object
_ManagerConfiguration = object
def flamenco_api_client(manager_url: str) -> _ApiClient: def flamenco_api_client(manager_url: str) -> _ApiClient:
@ -50,7 +56,9 @@ def discard_flamenco_data():
_flamenco_client = None _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. """Ping the Manager, update preferences, and return a report as string.
:returns: tuple (report, level). The report will be something like "<name> :returns: tuple (report, level). The report will be something like "<name>
@ -59,36 +67,48 @@ def ping_manager(context: bpy.types.Context, api_client: _ApiClient, prefs: _Fla
`Operator.report()`. `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. # Do a late import, so that the API is only imported when actually used.
from flamenco.manager import ApiException from flamenco.manager import ApiException
from flamenco.manager.apis import MetaApi from flamenco.manager.apis import MetaApi
from flamenco.manager.models import FlamencoVersion, ManagerConfiguration from flamenco.manager.models import FlamencoVersion, ManagerConfiguration
context.window_manager.flamenco_status_ping = "..."
meta_api = MetaApi(api_client) meta_api = MetaApi(api_client)
try: try:
version: FlamencoVersion = meta_api.get_version() version: FlamencoVersion = meta_api.get_version()
config: ManagerConfiguration = meta_api.get_configuration() config: ManagerConfiguration = meta_api.get_configuration()
except ApiException as ex: except ApiException as ex:
report = "Manager cannot be reached: %s" % ex return (None, None, "Manager cannot be reached: %s" % ex)
level = "ERROR"
except MaxRetryError as ex: except MaxRetryError as ex:
# This is the common error, when for example the port number is # This is the common error, when for example the port number is
# incorrect and nothing is listening. # incorrect and nothing is listening. The exception text is not included
report = "Manager cannot be reached" # because it's very long and confusing.
level = "WARNING" return (None, None, "Manager cannot be reached")
except HTTPError as ex: except HTTPError as ex:
report = "Manager cannot be reached: %s" % ex return (None, None, "Manager cannot be reached: %s" % ex)
level = "ERROR"
else:
report = "%s version %s found" % (version.name, version.version)
level = "INFO"
# Store whether this Manager supports the Shaman API. # Store whether this Manager supports the Shaman API.
prefs.is_shaman_enabled = config.shaman_enabled prefs.is_shaman_enabled = config.shaman_enabled
prefs.job_storage = config.storage_location prefs.job_storage = config.storage_location
context.window_manager.flamenco_status_ping = report return version, config, ""
return report, level

View File

@ -86,7 +86,7 @@ class FLAMENCO_OT_ping_manager(FlamencoOpMixin, bpy.types.Operator):
api_client = self.get_api_client(context) api_client = self.get_api_client(context)
prefs = preferences.get(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) self.report({level}, report)
return {"FINISHED"} return {"FINISHED"}

View File

@ -31,7 +31,7 @@ def _manager_url_updated(prefs, context):
# Warning, be careful what of the context to access here. Accessing / # Warning, be careful what of the context to access here. Accessing /
# changing too much can cause crashes, infinite loops, etc. # 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): class FlamencoPreferences(bpy.types.AddonPreferences):