Source code for schrodinger.application.pldb.login

from schrodinger import get_maestro
from schrodinger.application.pldb.packages.pldb_gui_shell_dir import \
    client_shell
from schrodinger.application.pldb.packages.pldb_gui_shell_dir import utils_shell
from schrodinger.Qt.QtCore import Qt
from schrodinger.ui.qt import basewidgets
from schrodinger.ui.qt.appframework2 import settings

from . import login_model
from . import login_ui

maestro = get_maestro()


[docs]class PLDBLoginPanel(basewidgets.Panel): """ A panel that prompts the user for login credentials to a PLDB server, then attempts to connect to the server in order to validate those credentials. After a successful connection, the valid credentials are stored in this singleton panel and can be accessed by a few "getter" methods. """ model_class = login_model.LoginModel ui_module = login_ui SUMMARY_TEXT = 'Please enter your PLDB credentials' TITLE_TEXT = 'Connect to PLDB'
[docs] def initSetOptions(self): super().initSetOptions() self._panel_prefkey = self.__class__.__name__ self._session_host = None self._session_username = None self._session_password = None self._pldb_client = None MC = self.model_class self._persistent_params = [MC.host, MC.username] self.setWindowModality(Qt.WindowModal)
[docs] def initSetUp(self): super().initSetUp() self.setWindowTitle(self.TITLE_TEXT) ui = self.ui ui.login_btn.clicked.connect(self._onLogInClicked) # Activate the login button if the user presses Enter in any line edit for le in (ui.host_le, ui.username_le, ui.password_le): le.returnPressed.connect(ui.login_btn.click) ui.login_btn.setDefault(True) ui.login_btn.setAutoDefault(True) ui.summary_lbl.setText(self.SUMMARY_TEXT) if maestro: maestro.project_close_callback_add(self._onProjectClosed)
[docs] def initSetDefaults(self): super().initSetDefaults() self._pldb_client = None self._readPersistentValues() self._validateCredentials(False)
[docs] def defineMappings(self): ui, MC = self.ui, self.model_class return [(ui.host_le, MC.host), (ui.username_le, MC.username), (ui.password_le, MC.password)]
def _validateCredentials(self, valid): """ Validate or invalidate the credentials used in this panel. Do this by storing the contents of the line edits if they are valid, or storing `None` otherwise. :param valid: whether the values entered into this panel are valid :type valid: bool """ if valid: model = self.model self._session_host = model.host self._session_username = model.username self._session_password = model.password else: self._session_host = None self._session_username = None self._session_password = None def _onLogInClicked(self): """ Attempt to log into the specified PLDB server. If successful, close this panel. """ self.standardizeHost() connection_successful = self._connectToPLDB() self._validateCredentials(connection_successful) if connection_successful: self._writePersistentValues() self.close() def _connectToPLDB(self): """ Attempt to connect to a PLDB server with the supplied credentials. :return: Whether the connection is successful :rtype: bool """ model = self.model self._pldb_client = client_shell.PLDBSearchClientShell( model.host, model.username, model.password, widget_parent=self) try: self._pldb_client.get_server_settings() except utils_shell.HttpError as exc: self._pldb_client = None msg = f'Connection to PLDB server failed with error:\n\n{exc}' self.error(msg) return False return True
[docs] def standardizeHost(self): """ Apply a standard formatting to the host string. """ host = self.model.host while host.endswith('/'): host = host[:-1] if not host.startswith('https://'): host = f'https://{host}' self.model.host = host
def _getPrefkey(self, abstract_param): """ Generate and return a preference key for persistent storage unique to this panel and to the supplied abstract parameter. :param abstract_param: an abstract parameter for which the value should be stored persistently using this prefkey :type abstract_param: parameters.Param :return: a unique prefkey for this panel and the supplied parameter :rtype: str """ # Use the string representaton of the abstract parameter, but strip out # unusable characters param_str = f'{abstract_param}'.strip('<').strip('>') param_str = param_str[len('Descriptor:'):] return f'{self._panel_prefkey}.{param_str}' def _readPersistentValues(self): """ Read and apply persistent values to the model. """ for abstract_param in self._persistent_params: prefkey = self._getPrefkey(abstract_param) value = settings.get_persistent_value(prefkey, '') abstract_param.setParamValue(self.model, value) def _writePersistentValues(self): """ Write data from the model to persistent storage. """ for abstract_param in self._persistent_params: prefkey = self._getPrefkey(abstract_param) value = abstract_param.getParamValue(self.model) settings.set_persistent_value(prefkey, value) def _onProjectClosed(self): """ If current project is closed, then all saved credentials will be reset. """ self.initSetDefaults()
[docs] def getPLDBClient(self): """ :return: the PLDB client for this session, if valid :rtype: client_shell.PLDBSearchClientShell """ return self._pldb_client
[docs] def getSessionHost(self): """ :return: the validated PLDB host for the current session, if possible :rtype: str or None """ return self._session_host
[docs] def getSessionUsername(self): """ :return: the validated username for the current session, if possible :rtype: str or None """ return self._session_username
[docs] def getSessionPassword(self): """ :return: the validated password for the current session, if possible :rtype: str or None """ return self._session_password
[docs] def connectionIsValid(self): """ :return: whether the current session is validated :rtype: bool """ credentials = [ self._session_host, self._session_username, self._session_password ] return all([val is not None for val in credentials])
[docs]def panel(): return PLDBLoginPanel.panel()
if __name__ == "__main__": panel()