mirror of
https://github.com/ynput/ayon-core.git
synced 2026-01-01 16:34:53 +01:00
moved muster module
This commit is contained in:
parent
6291e01003
commit
3468a9a58b
4 changed files with 0 additions and 0 deletions
|
|
@ -1,6 +0,0 @@
|
|||
from .muster import MusterModule
|
||||
|
||||
|
||||
__all__ = (
|
||||
"MusterModule",
|
||||
)
|
||||
|
|
@ -1,157 +0,0 @@
|
|||
import os
|
||||
import json
|
||||
import appdirs
|
||||
import requests
|
||||
from openpype.modules import PypeModule
|
||||
from openpype_interfaces import (
|
||||
ITrayModule,
|
||||
IWebServerRoutes
|
||||
)
|
||||
|
||||
|
||||
class MusterModule(PypeModule, ITrayModule, IWebServerRoutes):
|
||||
"""
|
||||
Module handling Muster Render credentials. This will display dialog
|
||||
asking for user credentials for Muster if not already specified.
|
||||
"""
|
||||
cred_folder_path = os.path.normpath(
|
||||
appdirs.user_data_dir('pype-app', 'pype')
|
||||
)
|
||||
cred_filename = 'muster_cred.json'
|
||||
|
||||
name = "muster"
|
||||
|
||||
def initialize(self, modules_settings):
|
||||
muster_settings = modules_settings[self.name]
|
||||
self.enabled = muster_settings["enabled"]
|
||||
self.muster_url = muster_settings["MUSTER_REST_URL"]
|
||||
|
||||
self.cred_path = os.path.join(
|
||||
self.cred_folder_path, self.cred_filename
|
||||
)
|
||||
# Tray attributes
|
||||
self.widget_login = None
|
||||
self.action_show_login = None
|
||||
self.rest_api_obj = None
|
||||
|
||||
def get_global_environments(self):
|
||||
return {
|
||||
"MUSTER_REST_URL": self.muster_url
|
||||
}
|
||||
|
||||
def tray_init(self):
|
||||
from .widget_login import MusterLogin
|
||||
self.widget_login = MusterLogin(self)
|
||||
|
||||
def tray_start(self):
|
||||
"""Show login dialog if credentials not found."""
|
||||
# This should be start of module in tray
|
||||
cred = self.load_credentials()
|
||||
if not cred:
|
||||
self.show_login()
|
||||
|
||||
def tray_exit(self):
|
||||
"""Nothing special for Muster."""
|
||||
return
|
||||
|
||||
def connect_with_modules(self, *_a, **_kw):
|
||||
return
|
||||
|
||||
# Definition of Tray menu
|
||||
def tray_menu(self, parent):
|
||||
"""Add **change credentials** option to tray menu."""
|
||||
from Qt import QtWidgets
|
||||
|
||||
# Menu for Tray App
|
||||
menu = QtWidgets.QMenu('Muster', parent)
|
||||
menu.setProperty('submenu', 'on')
|
||||
|
||||
# Actions
|
||||
self.action_show_login = QtWidgets.QAction(
|
||||
"Change login", menu
|
||||
)
|
||||
|
||||
menu.addAction(self.action_show_login)
|
||||
self.action_show_login.triggered.connect(self.show_login)
|
||||
|
||||
parent.addMenu(menu)
|
||||
|
||||
def webserver_initialization(self, server_manager):
|
||||
"""Implementation of IWebServerRoutes interface."""
|
||||
if self.tray_initialized:
|
||||
from .rest_api import MusterModuleRestApi
|
||||
|
||||
self.rest_api_obj = MusterModuleRestApi(self, server_manager)
|
||||
|
||||
def load_credentials(self):
|
||||
"""
|
||||
Get credentials from JSON file
|
||||
"""
|
||||
credentials = {}
|
||||
try:
|
||||
file = open(self.cred_path, 'r')
|
||||
credentials = json.load(file)
|
||||
except Exception:
|
||||
file = open(self.cred_path, 'w+')
|
||||
file.close()
|
||||
|
||||
return credentials
|
||||
|
||||
def get_auth_token(self, username, password):
|
||||
"""
|
||||
Authenticate user with Muster and get authToken from server.
|
||||
"""
|
||||
if not self.muster_url:
|
||||
raise AttributeError("Muster REST API url not set")
|
||||
params = {
|
||||
'username': username,
|
||||
'password': password
|
||||
}
|
||||
api_entry = '/api/login'
|
||||
response = self._requests_post(
|
||||
self.muster_url + api_entry, params=params)
|
||||
if response.status_code != 200:
|
||||
self.log.error(
|
||||
'Cannot log into Muster: {}'.format(response.status_code))
|
||||
raise Exception('Cannot login into Muster.')
|
||||
|
||||
try:
|
||||
token = response.json()['ResponseData']['authToken']
|
||||
except ValueError as e:
|
||||
self.log.error('Invalid response from Muster server {}'.format(e))
|
||||
raise Exception('Invalid response from Muster while logging in.')
|
||||
|
||||
self.save_credentials(token)
|
||||
|
||||
def save_credentials(self, token):
|
||||
"""
|
||||
Save credentials to JSON file
|
||||
"""
|
||||
data = {
|
||||
'token': token
|
||||
}
|
||||
|
||||
file = open(self.cred_path, 'w')
|
||||
file.write(json.dumps(data))
|
||||
file.close()
|
||||
|
||||
def show_login(self):
|
||||
"""
|
||||
Show dialog to enter credentials
|
||||
"""
|
||||
if self.widget_login:
|
||||
self.widget_login.show()
|
||||
|
||||
def _requests_post(self, *args, **kwargs):
|
||||
""" Wrapper for requests, disabling SSL certificate validation if
|
||||
DONT_VERIFY_SSL environment variable is found. This is useful when
|
||||
Deadline or Muster server are running with self-signed certificates
|
||||
and their certificate is not added to trusted certificates on
|
||||
client machines.
|
||||
|
||||
WARNING: disabling SSL certificate validation is defeating one line
|
||||
of defense SSL is providing and it is not recommended.
|
||||
"""
|
||||
if 'verify' not in kwargs:
|
||||
kwargs['verify'] = False if os.getenv("OPENPYPE_DONT_VERIFY_SSL", True) else True # noqa
|
||||
return requests.post(*args, **kwargs)
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
from aiohttp.web_response import Response
|
||||
|
||||
|
||||
class MusterModuleRestApi:
|
||||
def __init__(self, user_module, server_manager):
|
||||
self.module = user_module
|
||||
self.server_manager = server_manager
|
||||
|
||||
self.prefix = "/muster"
|
||||
|
||||
self.register()
|
||||
|
||||
def register(self):
|
||||
self.server_manager.add_route(
|
||||
"GET",
|
||||
self.prefix + "/show_login",
|
||||
self.show_login_widget
|
||||
)
|
||||
|
||||
async def show_login_widget(self, request):
|
||||
self.module.action_show_login.trigger()
|
||||
return Response(status=200)
|
||||
|
|
@ -1,166 +0,0 @@
|
|||
import os
|
||||
from Qt import QtCore, QtGui, QtWidgets
|
||||
from openpype import resources, style
|
||||
|
||||
|
||||
class MusterLogin(QtWidgets.QWidget):
|
||||
|
||||
SIZE_W = 300
|
||||
SIZE_H = 150
|
||||
|
||||
loginSignal = QtCore.Signal(object, object, object)
|
||||
|
||||
def __init__(self, module, parent=None):
|
||||
|
||||
super(MusterLogin, self).__init__(parent)
|
||||
|
||||
self.module = module
|
||||
|
||||
# Icon
|
||||
icon = QtGui.QIcon(resources.pype_icon_filepath())
|
||||
self.setWindowIcon(icon)
|
||||
|
||||
self.setWindowFlags(
|
||||
QtCore.Qt.WindowCloseButtonHint |
|
||||
QtCore.Qt.WindowMinimizeButtonHint
|
||||
)
|
||||
|
||||
self._translate = QtCore.QCoreApplication.translate
|
||||
|
||||
# Font
|
||||
self.font = QtGui.QFont()
|
||||
self.font.setFamily("DejaVu Sans Condensed")
|
||||
self.font.setPointSize(9)
|
||||
self.font.setBold(True)
|
||||
self.font.setWeight(50)
|
||||
self.font.setKerning(True)
|
||||
|
||||
# Size setting
|
||||
self.resize(self.SIZE_W, self.SIZE_H)
|
||||
self.setMinimumSize(QtCore.QSize(self.SIZE_W, self.SIZE_H))
|
||||
self.setMaximumSize(QtCore.QSize(self.SIZE_W+100, self.SIZE_H+100))
|
||||
self.setStyleSheet(style.load_stylesheet())
|
||||
|
||||
self.setLayout(self._main())
|
||||
self.setWindowTitle('Muster login')
|
||||
|
||||
def _main(self):
|
||||
self.main = QtWidgets.QVBoxLayout()
|
||||
self.main.setObjectName("main")
|
||||
|
||||
self.form = QtWidgets.QFormLayout()
|
||||
self.form.setContentsMargins(10, 15, 10, 5)
|
||||
self.form.setObjectName("form")
|
||||
|
||||
self.label_username = QtWidgets.QLabel("Username:")
|
||||
self.label_username.setFont(self.font)
|
||||
self.label_username.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
|
||||
self.label_username.setTextFormat(QtCore.Qt.RichText)
|
||||
|
||||
self.input_username = QtWidgets.QLineEdit()
|
||||
self.input_username.setEnabled(True)
|
||||
self.input_username.setFrame(True)
|
||||
self.input_username.setPlaceholderText(
|
||||
self._translate("main", "e.g. John Smith")
|
||||
)
|
||||
|
||||
self.label_password = QtWidgets.QLabel("Password:")
|
||||
self.label_password.setFont(self.font)
|
||||
self.label_password.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
|
||||
self.label_password.setTextFormat(QtCore.Qt.RichText)
|
||||
|
||||
self.input_password = QtWidgets.QLineEdit()
|
||||
self.input_password.setEchoMode(QtWidgets.QLineEdit.Password)
|
||||
self.input_password.setEnabled(True)
|
||||
self.input_password.setFrame(True)
|
||||
self.input_password.setPlaceholderText(
|
||||
self._translate("main", "e.g. ********")
|
||||
)
|
||||
|
||||
self.error_label = QtWidgets.QLabel("")
|
||||
self.error_label.setFont(self.font)
|
||||
self.error_label.setStyleSheet('color: #FC6000')
|
||||
self.error_label.setWordWrap(True)
|
||||
self.error_label.hide()
|
||||
|
||||
self.form.addRow(self.label_username, self.input_username)
|
||||
self.form.addRow(self.label_password, self.input_password)
|
||||
self.form.addRow(self.error_label)
|
||||
|
||||
self.btn_group = QtWidgets.QHBoxLayout()
|
||||
self.btn_group.addStretch(1)
|
||||
self.btn_group.setObjectName("btn_group")
|
||||
|
||||
self.btn_ok = QtWidgets.QPushButton("Ok")
|
||||
self.btn_ok.clicked.connect(self.click_ok)
|
||||
|
||||
self.btn_cancel = QtWidgets.QPushButton("Cancel")
|
||||
QtWidgets.QShortcut(
|
||||
QtGui.QKeySequence(
|
||||
QtCore.Qt.Key_Escape), self).activated.connect(self.close)
|
||||
self.btn_cancel.clicked.connect(self.close)
|
||||
|
||||
self.btn_group.addWidget(self.btn_ok)
|
||||
self.btn_group.addWidget(self.btn_cancel)
|
||||
|
||||
self.main.addLayout(self.form)
|
||||
self.main.addLayout(self.btn_group)
|
||||
|
||||
return self.main
|
||||
|
||||
def keyPressEvent(self, key_event):
|
||||
if key_event.key() == QtCore.Qt.Key_Return:
|
||||
if self.input_username.hasFocus():
|
||||
self.input_password.setFocus()
|
||||
|
||||
elif self.input_password.hasFocus() or self.btn_ok.hasFocus():
|
||||
self.click_ok()
|
||||
|
||||
elif self.btn_cancel.hasFocus():
|
||||
self.close()
|
||||
else:
|
||||
super().keyPressEvent(key_event)
|
||||
|
||||
def setError(self, msg):
|
||||
self.error_label.setText(msg)
|
||||
self.error_label.show()
|
||||
|
||||
def invalid_input(self, entity):
|
||||
entity.setStyleSheet("border: 1px solid red;")
|
||||
|
||||
def click_ok(self):
|
||||
# all what should happen - validations and saving into appsdir
|
||||
username = self.input_username.text()
|
||||
password = self.input_password.text()
|
||||
# TODO: more robust validation. Password can be empty in muster?
|
||||
if not username:
|
||||
self.setError("Username cannot be empty")
|
||||
self.invalid_input(self.input_username)
|
||||
try:
|
||||
self.save_credentials(username, password)
|
||||
except Exception as e:
|
||||
self.setError(
|
||||
"<b>Cannot get auth token:</b>\n<code>{}</code>".format(e))
|
||||
else:
|
||||
self._close_widget()
|
||||
|
||||
def save_credentials(self, username, password):
|
||||
self.module.get_auth_token(username, password)
|
||||
|
||||
def showEvent(self, event):
|
||||
super(MusterLogin, self).showEvent(event)
|
||||
|
||||
# Make btns same width
|
||||
max_width = max(
|
||||
self.btn_ok.sizeHint().width(),
|
||||
self.btn_cancel.sizeHint().width()
|
||||
)
|
||||
self.btn_ok.setMinimumWidth(max_width)
|
||||
self.btn_cancel.setMinimumWidth(max_width)
|
||||
|
||||
def closeEvent(self, event):
|
||||
event.ignore()
|
||||
self._close_widget()
|
||||
|
||||
def _close_widget(self):
|
||||
self.hide()
|
||||
Loading…
Add table
Add a link
Reference in a new issue