diff --git a/pype/modules/rest_api/__init__.py b/pype/modules/rest_api/__init__.py index fbeec00c88..55253bc58b 100644 --- a/pype/modules/rest_api/__init__.py +++ b/pype/modules/rest_api/__init__.py @@ -2,6 +2,8 @@ from .rest_api import RestApiServer from .base_class import RestApi, abort, route, register_statics from .lib import RestMethods, CallbackResult +CLASS_DEFINIION = RestApiServer + def tray_init(tray_widget, main_widget): return RestApiServer() diff --git a/pype/modules/rest_api/rest_api.py b/pype/modules/rest_api/rest_api.py index 1c4db9f706..cc98b56a3b 100644 --- a/pype/modules/rest_api/rest_api.py +++ b/pype/modules/rest_api/rest_api.py @@ -6,7 +6,7 @@ from socketserver import ThreadingMixIn from http.server import HTTPServer from .lib import RestApiFactory, Handler from .base_class import route, register_statics -from pype.api import config, Logger +from pype.api import Logger log = Logger().get_logger("RestApiServer") @@ -85,20 +85,14 @@ class RestApiServer: Callback may return many types. For more information read docstring of `_handle_callback_result` defined in handler. """ + default_port = 8011 + exclude_ports = [] + def __init__(self): self.qaction = None self.failed_icon = None self._is_running = False - try: - self.presets = config.get_presets()["services"]["rest_api"] - except Exception: - self.presets = {"default_port": 8011, "exclude_ports": []} - log.debug(( - "There are not set presets for RestApiModule." - " Using defaults \"{}\"" - ).format(str(self.presets))) - port = self.find_port() self.rest_api_thread = RestApiThread(self, port) @@ -130,8 +124,8 @@ class RestApiServer: RestApiFactory.register_obj(obj) def find_port(self): - start_port = self.presets["default_port"] - exclude_ports = self.presets["exclude_ports"] + start_port = self.default_port + exclude_ports = self.exclude_ports found_port = None # port check takes time so it's lowered to 100 ports for port in range(start_port, start_port+100): diff --git a/pype/modules/timers_manager/__init__.py b/pype/modules/timers_manager/__init__.py index a6c4535f3d..a8a478d7ae 100644 --- a/pype/modules/timers_manager/__init__.py +++ b/pype/modules/timers_manager/__init__.py @@ -1,6 +1,8 @@ from .timers_manager import TimersManager from .widget_user_idle import WidgetUserIdle +CLASS_DEFINIION = TimersManager + def tray_init(tray_widget, main_widget): return TimersManager(tray_widget, main_widget) diff --git a/pype/modules/timers_manager/timers_manager.py b/pype/modules/timers_manager/timers_manager.py index cec730d007..16b6c658af 100644 --- a/pype/modules/timers_manager/timers_manager.py +++ b/pype/modules/timers_manager/timers_manager.py @@ -23,32 +23,33 @@ class TimersManager(metaclass=Singleton): If IdleManager is imported then is able to handle about stop timers when user idles for a long time (set in presets). """ - modules = [] - is_running = False - last_task = None + + # Presetable attributes + # - when timer will stop if idle manager is running (minutes) + full_time = 15 + # - how many minutes before the timer is stopped will popup the message + message_time = 0.5 def __init__(self, tray_widget, main_widget): self.log = Logger().get_logger(self.__class__.__name__) + + self.modules = [] + self.is_running = False + self.last_task = None + self.tray_widget = tray_widget self.main_widget = main_widget self.widget_user_idle = WidgetUserIdle(self) def set_signal_times(self): try: - timer_info = ( - config.get_presets() - .get('services') - .get('timers_manager') - .get('timer') - ) - full_time = int(float(timer_info['full_time'])*60) - message_time = int(float(timer_info['message_time'])*60) + full_time = int(self.full_time * 60) + message_time = int(self.message_time * 60) self.time_show_message = full_time - message_time self.time_stop_timer = full_time return True except Exception: - self.log.warning('Was not able to load presets for TimersManager') - return False + self.log.error("Couldn't set timer signals.", exc_info=True) def add_module(self, module): """ Adds module to context @@ -180,6 +181,7 @@ class SignalHandler(QtCore.QObject): signal_show_message = QtCore.Signal() signal_change_label = QtCore.Signal() signal_stop_timers = QtCore.Signal() + def __init__(self, cls): super().__init__() self.signal_show_message.connect(cls.show_message) diff --git a/pype/modules/timers_manager/widget_user_idle.py b/pype/modules/timers_manager/widget_user_idle.py index 697c0a04d9..ae7e19dde9 100644 --- a/pype/modules/timers_manager/widget_user_idle.py +++ b/pype/modules/timers_manager/widget_user_idle.py @@ -1,4 +1,3 @@ -from pype.api import Logger from avalon import style from Qt import QtCore, QtGui, QtWidgets diff --git a/pype/tools/tray/pype_tray.py b/pype/tools/tray/pype_tray.py index 436734e712..468a02c62e 100644 --- a/pype/tools/tray/pype_tray.py +++ b/pype/tools/tray/pype_tray.py @@ -56,22 +56,35 @@ class TrayManager: In this case `Statics Server` won't be used. """ - items = [] - # Get booleans is module should be used - for item in self.modules_imports: - import_path = item.get("import_path") - title = item.get("title") + # Backwards compatible presets loading + if isinstance(self.items, list): + items = self.items + else: + items = [] + # Get booleans is module should be used + usages = self.items.get("item_usage") or {} + attributes = self.items.get("attributes") or {} + for item in self.items.get("item_import", []): + import_path = item.get("import_path") + title = item.get("title") - item_usage = self.modules_usage.get(title) - if item_usage is None: - item_usage = self.modules_usage.get(import_path, True) + item_usage = usages.get(title) + if item_usage is None: + item_usage = usages.get(import_path, True) - if item_usage: + if not item_usage: + if not title: + title = import_path + self.log.debug("{} - Module ignored".format(title)) + continue + + _attributes = attributes.get(title) + if _attributes is None: + _attributes = attributes.get(import_path) + + if _attributes: + item["attributes"] = _attributes items.append(item) - else: - if not title: - title = import_path - self.log.info("{} - Module ignored".format(title)) if items: self.process_items(items, self.tray_widget.menu) @@ -148,11 +161,29 @@ class TrayManager: import_path = item.get('import_path', None) title = item.get('title', import_path) fromlist = item.get('fromlist', []) + attributes = item.get("attributes", {}) try: module = __import__( "{}".format(import_path), fromlist=fromlist ) + klass = getattr(module, "CLASS_DEFINIION", None) + if not klass and attributes: + self.log.error(( + "There are defined attributes for module \"{}\" but" + "module does not have defined \"CLASS_DEFINIION\"." + ).format(import_path)) + + elif klass and attributes: + for key, value in attributes.items(): + if hasattr(klass, key): + setattr(klass, key, value) + else: + self.log.error(( + "Module \"{}\" does not have attribute \"{}\"." + " Check your settings please." + ).format(import_path, key)) + obj = module.tray_init(self.tray_widget, self.main_window) name = obj.__class__.__name__ if hasattr(obj, 'tray_menu'):