Exemplo n.º 1
0
 def change_exteditor(self):
     """Change external editor path"""
     path, valid = QInputDialog.getText(
         self, _('External editor'), _('External editor executable path:'),
         QLineEdit.Normal, self.get_option('external_editor/path'))
     if valid:
         self.set_option('external_editor/path', to_text_string(path))
Exemplo n.º 2
0
 def create_new_folder(self, current_path, title, subtitle, is_package):
     """Create new folder"""
     if current_path is None:
         current_path = ''
     if osp.isfile(current_path):
         current_path = osp.dirname(current_path)
     name, valid = QInputDialog.getText(self, title, subtitle,
                                        QLineEdit.Normal, "")
     if valid:
         dirname = osp.join(current_path, to_text_string(name))
         try:
             os.mkdir(dirname)
         except EnvironmentError as error:
             QMessageBox.critical(self, title,
                                  _("<b>Unable "
                                    "to create folder <i>%s</i></b>"
                                    "<br><br>Error message:<br>%s"
                                    ) % (dirname, to_text_string(error)))
         finally:
             if is_package:
                 fname = osp.join(dirname, '__init__.py')
                 try:
                     with open(fname, 'wb') as f:
                         f.write(to_binary_string('#'))
                     return dirname
                 except EnvironmentError as error:
                     QMessageBox.critical(self, title,
                                          _("<b>Unable "
                                            "to create file <i>%s</i></b>"
                                            "<br><br>Error message:<br>%s"
                                            ) % (fname,
                                                 to_text_string(error)))
Exemplo n.º 3
0
 def edit_parameters(self):
     allaxes = self.canvas.figure.get_axes()
     if len(allaxes) == 1:
         axes = allaxes[0]
     elif len(allaxes) > 1:
         titles = []
         for axes in allaxes:
             title = axes.get_title()
             ylabel = axes.get_ylabel()
             if title:
                 text = title
                 if ylabel:
                     text += ": "+ylabel
                 text += " (%s)"
             elif ylabel:
                 text = ylabel+" (%s)"
             else:
                 text = "%s"
             titles.append(text % repr(axes))
         item, ok = QInputDialog.getItem(self, 'Customize',
                                         'Select axes:', titles,
                                         0, False)
         if ok:
             axes = allaxes[titles.index(unicode(item))]
         else:
             return
     else:
         return
     figure_edit(axes, self)
Exemplo n.º 4
0
 def rename_file(self, fname):
     """Rename file"""
     path, valid = QInputDialog.getText(self, _("Rename"), _("New name:"), QLineEdit.Normal, osp.basename(fname))
     if valid:
         path = osp.join(osp.dirname(fname), to_text_string(path))
         if path == fname:
             return
         if osp.exists(path):
             if (
                 QMessageBox.warning(
                     self,
                     _("Rename"),
                     _("Do you really want to rename <b>%s</b> and " "overwrite the existing file <b>%s</b>?")
                     % (osp.basename(fname), osp.basename(path)),
                     QMessageBox.Yes | QMessageBox.No,
                 )
                 == QMessageBox.No
             ):
                 return
         try:
             misc.rename_file(fname, path)
             self.parent_widget.renamed.emit(fname, path)
             return path
         except EnvironmentError as error:
             QMessageBox.critical(
                 self,
                 _("Rename"),
                 _("<b>Unable to rename file <i>%s</i></b>" "<br><br>Error message:<br>%s")
                 % (osp.basename(fname), to_text_string(error)),
             )
Exemplo n.º 5
0
 def create_new_folder(self, current_path, title, subtitle, is_package):
     """Create new folder"""
     if current_path is None:
         current_path = ""
     if osp.isfile(current_path):
         current_path = osp.dirname(current_path)
     name, valid = QInputDialog.getText(self, title, subtitle, QLineEdit.Normal, "")
     if valid:
         dirname = osp.join(current_path, to_text_string(name))
         try:
             os.mkdir(dirname)
         except EnvironmentError as error:
             QMessageBox.critical(
                 self,
                 title,
                 _("<b>Unable " "to create folder <i>%s</i></b>" "<br><br>Error message:<br>%s")
                 % (dirname, to_text_string(error)),
             )
         finally:
             if is_package:
                 fname = osp.join(dirname, "__init__.py")
                 try:
                     with open(fname, "wb") as f:
                         f.write(to_binary_string("#"))
                     return dirname
                 except EnvironmentError as error:
                     QMessageBox.critical(
                         self,
                         title,
                         _("<b>Unable " "to create file <i>%s</i></b>" "<br><br>Error message:<br>%s")
                         % (fname, to_text_string(error)),
                     )
Exemplo n.º 6
0
 def change_history_depth(self):
     "Change history max entries" ""
     depth, valid = QInputDialog.getInteger(
         self, _("History"), _("Maximum entries"), self.get_option("max_entries"), 10, 10000
     )
     if valid:
         self.set_option("max_entries", depth)
Exemplo n.º 7
0
 def rename_file(self, fname):
     """Rename file"""
     path, valid = QInputDialog.getText(self, _('Rename'),
                           _('New name:'), QLineEdit.Normal,
                           osp.basename(fname))
     if valid:
         path = osp.join(osp.dirname(fname), to_text_string(path))
         if path == fname:
             return
         if osp.exists(path):
             if QMessageBox.warning(self, _("Rename"),
                      _("Do you really want to rename <b>%s</b> and "
                        "overwrite the existing file <b>%s</b>?"
                        ) % (osp.basename(fname), osp.basename(path)),
                      QMessageBox.Yes|QMessageBox.No) == QMessageBox.No:
                 return
         try:
             misc.rename_file(fname, path)
             self.parent_widget.emit( \
                  SIGNAL("renamed(QString,QString)"), fname, path)
             return path
         except EnvironmentError as error:
             QMessageBox.critical(self, _("Rename"),
                         _("<b>Unable to rename file <i>%s</i></b>"
                           "<br><br>Error message:<br>%s"
                           ) % (osp.basename(fname), to_text_string(error)))
Exemplo n.º 8
0
 def get_arguments(self):
     arguments, valid = QInputDialog.getText(
         self, _("Arguments"), _("Command line arguments:"), QLineEdit.Normal, self.arguments
     )
     if valid:
         self.arguments = to_text_string(arguments)
     return valid
Exemplo n.º 9
0
 def change_max_line_count(self):
     "Change maximum line count" ""
     mlc, valid = QInputDialog.getInteger(
         self, _("Buffer"), _("Maximum line count"), self.get_option("max_line_count"), 0, 1000000
     )
     if valid:
         self.shell.setMaximumBlockCount(mlc)
         self.set_option("max_line_count", mlc)
Exemplo n.º 10
0
 def change_exteditor(self):
     """Change external editor path"""
     path, valid = QInputDialog.getText(self, _('External editor'),
                       _('External editor executable path:'),
                       QLineEdit.Normal,
                       self.get_option('external_editor/path'))
     if valid:
         self.set_option('external_editor/path', to_text_string(path))
Exemplo n.º 11
0
 def get_arguments(self):
     arguments, valid = QInputDialog.getText(self, _('Arguments'),
                                             _('Command line arguments:'),
                                             QLineEdit.Normal,
                                             self.arguments)
     if valid:
         self.arguments = to_text_string(arguments)
     return valid
Exemplo n.º 12
0
 def change_history_depth(self):
     "Change history max entries" ""
     depth, valid = QInputDialog.getInteger(self, _('History'),
                                            _('Maximum entries'),
                                            self.get_option('max_entries'),
                                            10, 10000)
     if valid:
         self.set_option('max_entries', depth)
Exemplo n.º 13
0
 def get_arguments(self):
     arguments, valid = QInputDialog.getText(self, _('Arguments'),
                                             _('Command line arguments:'),
                                             QLineEdit.Normal,
                                             self.arguments)
     if valid:
         self.arguments = unicode(arguments)
     return valid
Exemplo n.º 14
0
 def edit_filter(self):
     """Edit name filters"""
     filters, valid = QInputDialog.getText(
         self, _("Edit filename filters"), _("Name filters:"), QLineEdit.Normal, ", ".join(self.name_filters)
     )
     if valid:
         filters = [f.strip() for f in to_text_string(filters).split(",")]
         self.parent_widget.sig_option_changed.emit("name_filters", filters)
         self.set_name_filters(filters)
Exemplo n.º 15
0
 def change_max_line_count(self):
     "Change maximum line count"""
     mlc, valid = QInputDialog.getInteger(self, _('Buffer'),
                                        _('Maximum line count'),
                                        self.get_option('max_line_count'),
                                        0, 1000000)
     if valid:
         self.shell.setMaximumBlockCount(mlc)
         self.set_option('max_line_count', mlc)
Exemplo n.º 16
0
 def edit_filter(self):
     """Edit name filters"""
     filters, valid = QInputDialog.getText(self, _('Edit filename filters'),
                                           _('Name filters:'),
                                           QLineEdit.Normal,
                                           ", ".join(self.name_filters))
     if valid:
         filters = [f.strip() for f in unicode(filters).split(',')]
         self.parent_widget.sig_option_changed.emit('name_filters', filters)
         self.set_name_filters(filters)
Exemplo n.º 17
0
 def edit_filter(self):
     """Edit name filters"""
     filters, valid = QInputDialog.getText(self, _('Edit filename filters'),
                                           _('Name filters:'),
                                           QLineEdit.Normal,
                                           ", ".join(self.name_filters))
     if valid:
         filters = [f.strip() for f in to_text_string(filters).split(',')]
         self.parent_widget.sig_option_changed.emit('name_filters', filters)
         self.set_name_filters(filters)
Exemplo n.º 18
0
 def change_format(self):
     """Change display format"""
     format, valid = QInputDialog.getText(
         self, _("Format"), _("Float formatting"), QLineEdit.Normal, self.dataModel.get_format()
     )
     if valid:
         format = str(format)
         try:
             format % 1.1
         except:
             QMessageBox.critical(self, _("Error"), _("Format (%s) is incorrect") % format)
             return
         self.dataModel.set_format(format)
Exemplo n.º 19
0
 def change_format(self):
     """Change display format"""
     format, valid = QInputDialog.getText(self, _( 'Format'),
                              _( "Float formatting"),
                              QLineEdit.Normal, self.model.get_format())
     if valid:
         format = str(format)
         try:
             format % 1.1
         except:
             QMessageBox.critical(self, _("Error"),
                                  _("Format (%s) is incorrect") % format)
             return
         self.model.set_format(format)    
Exemplo n.º 20
0
 def create_new_folder(self, current_path, title, subtitle, is_package):
     """Create new folder"""
     if current_path is None:
         current_path = ''
     if osp.isfile(current_path):
         current_path = osp.dirname(current_path)
     name, valid = QInputDialog.getText(self, title, subtitle,
                                        QLineEdit.Normal, "")
     if valid:
         dirname = osp.join(current_path, unicode(name))
         try:
             os.mkdir(dirname)
         except EnvironmentError, error:
             QMessageBox.critical(self, title,
                                  _("<b>Unable "
                                    "to create folder <i>%s</i></b>"
                                    "<br><br>Error message:<br>%s"
                                    ) % (dirname, unicode(error)))
         finally:
Exemplo n.º 21
0
    def create_client_for_kernel(self):
        """Create a client connected to an existing kernel"""
        example = _("(for example: kernel-3764.json, or simply 3764)")
        while True:
            cf, valid = QInputDialog.getText(self, _('IPython'),
                          _('Provide an IPython kernel connection file:')+\
                          '\n'+example,
                          QLineEdit.Normal)
            if valid:
                cf = str(cf)
                match = re.match('(kernel-|^)([a-fA-F0-9-]+)(.json|$)', cf)
                if match is not None:
                    kernel_num = match.groups()[1]
                    if kernel_num:
                        cf = 'kernel-%s.json' % kernel_num
                        break
            else:
                return

        # Generating the client name and setting kernel_widget_id
        match = re.match('^kernel-([a-fA-F0-9-]+).json', cf)
        count = 0
        kernel_widget_id = None
        while True:
            client_name = match.groups()[0]
            if '-' in client_name:  # Avoid long names
                client_name = client_name.split('-')[0]
            client_name = client_name + '/' + chr(65+count)
            for cl in self.get_clients():
                if cl.name == client_name:
                    kernel_widget_id = cl.kernel_widget_id
                    break
            else:
                break
            count += 1
        
        # Trying to get kernel_widget_id from the currently opened kernels if
        # the previous procedure fails. This could happen when the first
        # client connected to a kernel is closed but the kernel is left open
        # and you try to connect new clients to it
        if kernel_widget_id is None:
            for sw in self.extconsole.shellwidgets:
                if sw.connection_file == cf:
                    kernel_widget_id = id(sw)

        # Verifying if the kernel exists
        try:
            find_connection_file(cf, profile='default')
        except (IOError, UnboundLocalError):
            QMessageBox.critical(self, _('IPython'),
                                 _("Unable to connect to IPython <b>%s") % cf)
            return
        
        # Verifying if frontend and kernel have compatible versions
        if not self.kernel_and_frontend_match(cf):
            QMessageBox.critical(self,
                                 _("Mismatch between kernel and frontend"),
                                 _("Your IPython frontend and kernel versions "
                                   "are <b>incompatible!!</b>"
                                   "<br><br>"
                                   "We're sorry but we can't create an IPython "
                                   "console for you."
                                ), QMessageBox.Ok)
            return
        
        # Creating the client
        client = IPythonClient(self, history_filename='history.py',
                               connection_file=cf,
                               kernel_widget_id=kernel_widget_id,
                               menu_actions=self.menu_actions)
        self.add_tab(client, name=client.get_name())
        self.register_client(client, client_name)
Exemplo n.º 22
0
    def import_data(self, filenames=None):
        """Import data from text file"""
        title = _("Import data")
        if filenames is None:
            if self.filename is None:
                basedir = getcwd()
            else:
                basedir = osp.dirname(self.filename)
            filenames, _selfilter = getopenfilenames(self, title, basedir,
                                                     iofunctions.load_filters)
            if not filenames:
                return
        elif is_text_string(filenames):
            filenames = [filenames]

        for filename in filenames:
            self.filename = to_text_string(filename)
            ext = osp.splitext(self.filename)[1].lower()

            if ext not in iofunctions.load_funcs:
                buttons = QMessageBox.Yes | QMessageBox.Cancel
                answer = QMessageBox.question(self, title,
                            _("<b>Unsupported file extension '%s'</b><br><br>"
                              "Would you like to import it anyway "
                              "(by selecting a known file format)?"
                              ) % ext, buttons)
                if answer == QMessageBox.Cancel:
                    return
                formats = list(iofunctions.load_extensions.keys())
                item, ok = QInputDialog.getItem(self, title,
                                                _('Open file as:'),
                                                formats, 0, False)
                if ok:
                    ext = iofunctions.load_extensions[to_text_string(item)]
                else:
                    return

            load_func = iofunctions.load_funcs[ext]
                
            # 'import_wizard' (self.setup_io)
            if is_text_string(load_func):
                # Import data with import wizard
                error_message = None
                try:
                    text, _encoding = encoding.read(self.filename)
                    if self.is_internal_shell:
                        self.editor.import_from_string(text)
                    else:
                        base_name = osp.basename(self.filename)
                        editor = ImportWizard(self, text, title=base_name,
                                      varname=fix_reference_name(base_name))
                        if editor.exec_():
                            var_name, clip_data = editor.get_data()
                            monitor_set_global(self._get_sock(),
                                               var_name, clip_data)
                except Exception as error:
                    error_message = str(error)
            else:
                QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
                QApplication.processEvents()
                if self.is_internal_shell:
                    namespace, error_message = load_func(self.filename)
                    interpreter = self.shellwidget.interpreter
                    for key in list(namespace.keys()):
                        new_key = fix_reference_name(key,
                                     blacklist=list(interpreter.namespace.keys()))
                        if new_key != key:
                            namespace[new_key] = namespace.pop(key)
                    if error_message is None:
                        interpreter.namespace.update(namespace)
                else:
                    error_message = monitor_load_globals(self._get_sock(),
                                                         self.filename, ext)
                QApplication.restoreOverrideCursor()
                QApplication.processEvents()
    
            if error_message is not None:
                QMessageBox.critical(self, title,
                                     _("<b>Unable to load '%s'</b>"
                                       "<br><br>Error message:<br>%s"
                                       ) % (self.filename, error_message))
            self.refresh_table()