Merge pull request #321 from pypeclub/feature/module_attributes_in_tray_menu

Feature/module attributes in tray menu
This commit is contained in:
Milan Kolar 2020-07-08 09:13:53 +02:00 committed by GitHub
commit 51bb034fb7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 69 additions and 39 deletions

View file

@ -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()

View file

@ -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):

View file

@ -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)

View file

@ -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)

View file

@ -1,4 +1,3 @@
from pype.api import Logger
from avalon import style
from Qt import QtCore, QtGui, QtWidgets

View file

@ -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'):