From b35d8d3a924624d2ccf213df40b4e0223e5dc2cd Mon Sep 17 00:00:00 2001 From: Petr Kalis Date: Tue, 16 Mar 2021 12:22:09 +0100 Subject: [PATCH] #21 - Change timers after task change --- pype/__init__.py | 9 ++- pype/lib/__init__.py | 6 +- pype/lib/avalon_context.py | 26 +++++++ pype/modules/timers_manager/timers_manager.py | 73 ++++++++++++++++++- 4 files changed, 110 insertions(+), 4 deletions(-) diff --git a/pype/__init__.py b/pype/__init__.py index 400bf4c7bb..fd0ba321ed 100644 --- a/pype/__init__.py +++ b/pype/__init__.py @@ -5,7 +5,8 @@ import functools import logging from .settings import get_project_settings -from .lib import Anatomy, filter_pyblish_plugins +from .lib import Anatomy, filter_pyblish_plugins, \ + change_timer_to_current_context pyblish = avalon = _original_discover = None @@ -132,6 +133,12 @@ def install(): log.info("Patching discovery") avalon.discover = patched_discover + avalon.on("taskChanged", _on_task_change) + + +def _on_task_change(*args): + change_timer_to_current_context() + @import_wrapper def uninstall(): diff --git a/pype/lib/__init__.py b/pype/lib/__init__.py index 62cd363d52..27dd93c1a1 100644 --- a/pype/lib/__init__.py +++ b/pype/lib/__init__.py @@ -63,7 +63,9 @@ from .avalon_context import ( BuildWorkfile, - get_creator_by_name + get_creator_by_name, + + change_timer_to_current_context ) from .applications import ( @@ -160,6 +162,8 @@ __all__ = [ "get_creator_by_name", + "change_timer_to_current_context", + "ApplicationLaunchFailed", "ApplictionExecutableNotFound", "ApplicationNotFound", diff --git a/pype/lib/avalon_context.py b/pype/lib/avalon_context.py index 3d3a288b10..6a85baa756 100644 --- a/pype/lib/avalon_context.py +++ b/pype/lib/avalon_context.py @@ -1,3 +1,4 @@ +"""Should be used only inside of hosts.""" import os import json import re @@ -1147,3 +1148,28 @@ def get_creator_by_name(creator_name, case_sensitive=False): if _creator_name == creator_name: return creator_plugin return None + + +@with_avalon +def change_timer_to_current_context(): + """Called after context change to change timers""" + webserver_url = os.environ.get("PYPE_WEBSERVER_URL") + if not webserver_url: + log.warning("Couldn't find webserver url") + return + + rest_api_url = "{}/timers_manager/start_timer".format(webserver_url) + try: + import requests + except Exception: + log.warning("Couldn't start timer") + return + data = { + "project_name": avalon.io.Session["AVALON_PROJECT"], + "asset_name": avalon.io.Session["AVALON_ASSET"], + "task_name": avalon.io.Session["AVALON_TASK"] + } + + requests.post(rest_api_url, json=data) + + diff --git a/pype/modules/timers_manager/timers_manager.py b/pype/modules/timers_manager/timers_manager.py index d5b65a7bd7..282aacdd2e 100644 --- a/pype/modules/timers_manager/timers_manager.py +++ b/pype/modules/timers_manager/timers_manager.py @@ -1,6 +1,8 @@ +import os from abc import ABCMeta, abstractmethod import six -from .. import PypeModule, ITrayService, IIdleManager +from .. import PypeModule, ITrayService, IIdleManager, IWebServerRoutes +from avalon.api import AvalonMongoDB @six.add_metaclass(ABCMeta) @@ -28,7 +30,7 @@ class ITimersManager: self.timer_manager_module.timer_stopped(self.id) -class TimersManager(PypeModule, ITrayService, IIdleManager): +class TimersManager(PypeModule, ITrayService, IIdleManager, IWebServerRoutes): """ Handles about Timers. Should be able to start/stop all timers at once. @@ -72,6 +74,52 @@ class TimersManager(PypeModule, ITrayService, IIdleManager): """Nothing special for TimersManager.""" return + def webserver_initialization(self, server_manager): + """Implementation of IWebServerRoutes interface.""" + if self.tray_initialized: + from .rest_api import TimersManagerModuleRestApi + self.rest_api_obj = TimersManagerModuleRestApi(self, + server_manager) + + def start_timer(self, project_name, asset_name, task_name): + """ + Start timer for 'project_name', 'asset_name' and 'task_name' + + Called from REST api by hosts. + + Args: + project_name (string) + asset_name (string) + task_name (string) + """ + dbconn = AvalonMongoDB() + dbconn.install() + dbconn.Session["AVALON_PROJECT"] = project_name + + asset_doc = dbconn.find_one({ + "type": "asset", "name": asset_name + }) + if not asset_doc: + raise ValueError("Uknown asset {}".format(asset_name)) + + task_type = '' + try: + task_type = asset_doc["data"]["tasks"][task_name]["type"] + except KeyError: + self.log.warning("Couldn't find task_type for {}".\ + format(task_name)) + + hierarchy = asset_doc["data"]["hierarchy"].split("\\") + hierarchy.append(asset_name) + + data = { + "project_name": project_name, + "task_name": task_name, + "task_type": task_type, + "hierarchy": hierarchy + } + self.timer_started(None, data) + def timer_started(self, source_id, data): for module in self.modules: if module.id != source_id: @@ -169,3 +217,24 @@ class TimersManager(PypeModule, ITrayService, IIdleManager): return if self.widget_user_idle.bool_is_showed is False: self.widget_user_idle.show() + + def change_timer_from_host(self, project_name, asset_name, task_name): + """Prepared method for calling change timers on REST api""" + webserver_url = os.environ.get("PYPE_WEBSERVER_URL") + if not webserver_url: + self.log.warning("Couldn't find webserver url") + return + + rest_api_url = "{}/timers_manager/start_timer".format(webserver_url) + try: + import requests + except Exception: + self.log.warning("Couldn't start timer") + return + data = { + "project_name": project_name, + "asset_name": asset_name, + "task_name": task_name + } + + requests.post(rest_api_url, json=data)