def addServer(self): dialog = EditSageServerDlg(self) name_collision = True #The while loop needs to run at least once. while name_collision: if not dialog.exec_(): #The user clicked cancel. return #Fetch the data. new_server = dialog.getServerConfiguration() #Check to see if the name is in use. name_collision = ServerConfigurations.getServerByName(new_server["name"]) #If the user set the name to a new name that is already in use, name_collision will #not be None. The loop will continue and the dialog reopened. if name_collision: #The name is already in use. QMessageBox.critical(self, "Name already exists", "A server configuration already exists with that name. Choose a different name.") dialog.txtName.selectAll() dialog.txtName.setFocus() #Add the server configuration to the list. ServerConfigurations.addServer(new_server) item = QListWidgetItem(new_server["name"], self.ServerListView) self.ServerListView.setCurrentItem(item) if new_server["default"]: self.updateListViewDefault()
def deleteServer(self): #Which server configuration is selected? if not self.ServerListView.currentItem(): #Nothing selected, nothing to do. return name = self.ServerListView.currentItem().text() #Remove the corresponding server from the server_list. ServerConfigurations.removeServerByName(name) #And remove it from the ListView as well. self.removeSelectedItem()
def doActionSageServer(self): server_list_dialog = ServerListDlg(self) #Get a reference to the WorksheetController associated to this window. wsc = self.webViewController().worksheet_controller #Select the server associated to this window, if there is one. if wsc and wsc.server_configuration: server_list_dialog.selectServer(wsc.server_configuration) #Show the dialog. server_list_dialog.exec_() #It's possible that the user will delete all of the servers. It's not clear how to cleanly handle this case. #We choose to give the user a choice to fix the situation. while not ServerConfigurations.server_list: #No servers? message_text = "Guru needs a Sage server configured in order to evaluate cells. " \ "Add a Sage server configuration in the server configuration dialog?" response = QMessageBox.question(self, "Sage Not Configured", message_text, QMessageBox.Yes, QMessageBox.No) if response == QMessageBox.No: return server_list_dialog.exec_() #Execution only reaches this point if there exists a server. server_name = server_list_dialog.ServerListView.currentItem().text() if wsc: new_config = ServerConfigurations.getServerByName(server_name) try: wsc.useServerConfiguration(new_config) except Exception as e: QMessageBox.critical(self, "Error Switching Servers", "Could not switch servers:\n%s" % e.message)
def withWorksheetFile(webViewController, filename, server=None): if not server: server = ServerConfigurations.getDefault() wsc = WorksheetController(webViewController) wsc.server_configuration = server ws = wsc.notebook.import_worksheet(filename, wsc.notebook_username) SageProcessManager.setWorksheetProcessServer(ws, server) wsc.setWorksheet(ws) return wsc
def restoreSettings(self): settings = QSettings() #Restore recent files list. MainWindow.recentFiles = settings.value("RecentFiles") if MainWindow.recentFiles is None: MainWindow.recentFiles = [] self.updateRecentFilesMenu() #Restore window geometry self.restoreGeometry(settings.value("MainWindow/Geometry")) self.restoreState(settings.value("MainWindow/State")) #Populate the list of available Sage servers. sage_servers = settings.value("ServerConfigurations") if sage_servers is None: sage_servers = [] ServerConfigurations.restoreFromList(sage_servers)
def editServer(self): #Which server configuration is selected? if not self.ServerListView.currentItem(): #Nothing selected. return name = self.ServerListView.currentItem().text() #Find the corresponding server server_config = ServerConfigurations.getServerByName(name) #Create the dialog. It's only shown when we call dialog.exec_(). dialog = EditSageServerDlg(server_info=server_config) name_collision = True #The while loop needs to run at least once. while name_collision: if not dialog.exec_(): #User clicked cancel. return new_server = dialog.getServerConfiguration() name_collision = False #We check it with the 'if' below. #If the user changed the name to a new name that is already in use, the loop will continue #and the dialog reopened. if new_server["name"] != server_config["name"] and ServerConfigurations.getServerByName(new_server["name"]): #The name is already in use. name_collision = True QMessageBox.critical(self, "Name already exists", "A server configuration already exists with that name. Choose a different name.") dialog.txtName.selectAll() dialog.txtName.setFocus() #Update server_config if server_config != new_server: # Replace server_config with new_server. index = ServerConfigurations.server_list.index(server_config) ServerConfigurations.server_list[index] = new_server self.ServerListView.currentItem().setText(new_server["name"]) #When we set the "default" value, we need to also take care of the font of the item in the ListView. ServerConfigurations.setDefault(new_server, set=new_server["default"]) #Update the ListView to reflect our possibly new default server settings. self.updateListViewDefault()
def withNewWorksheet(webViewController, server=None): # server is a Sage server configuration that will determine the Sage process this # worksheet will use. if not server: server = ServerConfigurations.getDefault() wsc = WorksheetController(webViewController) wsc.server_configuration = server ws = guru_notebook.create_new_worksheet('Untitled', wsc.notebook_username) SageProcessManager.setWorksheetProcessServer(ws, server) wsc.setWorksheet(ws) return wsc
def restartSageProcess(worksheet): # The code to restart the Sage process in sagenb.notebook.worksheet.Worksheet makes assumptions # that may not hold for us. if ManagedWorksheets.has_key(worksheet): server_config = ManagedWorksheets[worksheet] else: # We're not managing this worksheet. server_config = ServerConfigurations.getDefault() #Turns out, the following is equivalent to restarting the process. setWorksheetProcessServer(worksheet, server_config)
def setWorksheetProcessServer(worksheet, server_config=None): # sagenb.notebook.worksheet.Worksheet was designed to get it's sage process # from its associated notebook, so we have to do some stuff that isn't # necessarily how one would normally design it if one were starting # from scratch. #We will need the worksheet to have it's _notebook property set. if (not hasattr(worksheet, "_notebook")) or (worksheet._notebook is None): worksheet._notebook = guru_notebook #If we aren't given a server config, use the default. if not server_config: server_config = ServerConfigurations.getDefault() # Note that some Sage interfaces do not require worksheet.initialize_sage(). # Seems to me initialize_sage() doesn't belong in Worksheet at all, but in # sagenb.interfaces.worksheet_process.WorksheetProcess, but I didn't write it. if server_config["type"]=="local": #For local servers, it's easy. worksheet = stopSageProcess(worksheet) worksheet._Worksheet__sage = getLocalProcess(server_config) worksheet.initialize_sage() elif server_config["type"]=="notebook server": worksheet = setNotebookProcessServer(worksheet, server_config) elif server_config["type"]=="cell server": pass elif server_config["type"]=="remote": pass else: #This should never execute. It just restarts sage using whatever the notebook gives it. worksheet.restart_sage() return worksheet
def newWorksheetFile(self, server=None): if not server: server = ServerConfigurations.getDefault() #Create a new worksheet. self.clear() self.worksheet_controller = WorksheetController.withNewWorksheet(self, server=server)
def openWorksheetFile(self, file_name, server=None): if not server: server = ServerConfigurations.getDefault() self.clear() self.worksheet_controller = WorksheetController.withWorksheetFile(self, file_name, server=server)