Ejemplo n.º 1
0
def save_plot(data, file_formats, filename=""):
    _LAST_DIR_KEY = "directories/last_graph_directory"
    _LAST_FILTER_KEY = "directories/last_graph_filter"
    settings = QSettings()
    start_dir = settings.value(_LAST_DIR_KEY, filename)
    if not start_dir or \
            (not os.path.exists(start_dir) and
             not os.path.exists(os.path.split(start_dir)[0])):
        start_dir = os.path.expanduser("~")
    last_filter = settings.value(_LAST_FILTER_KEY, "")
    filename, writer, filter = \
        filedialogs.open_filename_dialog_save(start_dir, last_filter, file_formats)
    if not filename:
        return
    try:
        writer.write(filename, data)
    except OSError as e:
        mb = QMessageBox(
            None,
            windowTitle="Error",
            text='Error occurred while saving file "{}": {}'.format(filename, e),
            detailedText=traceback.format_exc(),
            icon=QMessageBox.Critical)
        mb.exec_()
    else:
        settings.setValue(_LAST_DIR_KEY, os.path.split(filename)[0])
        settings.setValue(_LAST_FILTER_KEY, filter)
    def test_filename_with_fix_extension(self):

        def mock_fix_choice(ret):
            f = lambda *x: ret
            f.__dict__.update(fix_extension.__dict__)
            return f

        change_filter = iter([PickleReader, TabReader])

        for file_choice, fix in [
                [lambda *x: ("o.pickle", format_filter(TabReader)),
                 mock_fix_choice(fix_extension.CHANGE_EXT)],
                [lambda *x: ("o.tab", format_filter(PickleReader)),
                 mock_fix_choice(fix_extension.CHANGE_FORMAT)],
                [lambda *x: ("o.tab", format_filter(next(change_filter))),
                 mock_fix_choice(fix_extension.CANCEL)]
        ]:

            with patch("AnyQt.QtWidgets.QFileDialog.getSaveFileName", file_choice),\
                 patch("Orange.widgets.utils.filedialogs.fix_extension", fix):
                saved_filename, format, filter = \
                    open_filename_dialog_save(".", None, OWSave.get_writers(False))
                self.assertEqual(saved_filename, "o.tab")
                self.assertEqual(format, TabReader)
                self.assertEqual(filter, format_filter(TabReader))
Ejemplo n.º 3
0
 def save_file_as(self):
     file_name = self.filename or \
         os.path.join(self.last_dir or os.path.expanduser("~"),
                      getattr(self.data, 'name', ''))
     filename, writer, filter = filedialogs.open_filename_dialog_save(
         file_name, self.last_filter, self.get_writers(self.data.is_sparse()))
     if not filename:
         return
     self.filename = filename
     self.writer = writer
     self.unconditional_save_file()
     self.last_dir = os.path.split(self.filename)[0]
     self.last_filter = filter
     self.adjust_label()
Ejemplo n.º 4
0
 def save_file_as(self):
     file_name = self.filename or \
         os.path.join(self.last_dir or os.path.expanduser("~"),
                      getattr(self.data, 'name', ''))
     filename, writer, filter = filedialogs.open_filename_dialog_save(
         file_name, self.last_filter,
         self.get_writers(self.data.is_sparse()))
     if not filename:
         return
     self.filename = filename
     self.writer = writer
     self.unconditional_save_file()
     self.last_dir = os.path.split(self.filename)[0]
     self.last_filter = filter
     self.adjust_label()
Ejemplo n.º 5
0
    def save_file_as(self):
        file_name = self.remove_extensions(self.filename) or os.path.join(
            self.last_dir or os.path.expanduser("~"),
            getattr(self.data, 'name', ''))
        self.update_extension()
        writer = self.get_writer_selected()
        if not writer:
            return

        filename, writer, _ = filedialogs.open_filename_dialog_save(
            file_name, '', [writer],
        )
        if not filename:
            return

        self.filename = filename
        self.last_dir = os.path.split(self.filename)[0]
        self.basename = os.path.basename(self.remove_extensions(filename))
        self.unconditional_save_file()
        self.adjust_label()
Ejemplo n.º 6
0
    def save_file_as(self):
        file_name = self.remove_extensions(self.filename) or os.path.join(
            self.last_dir or os.path.expanduser("~"),
            getattr(self.data, 'name', ''))
        self.update_extension()
        writer = self.get_writer_selected()
        if not writer:
            return

        filename, writer, _ = filedialogs.open_filename_dialog_save(
            file_name,
            '',
            [writer],
        )
        if not filename:
            return

        self.filename = filename
        self.last_dir = os.path.split(self.filename)[0]
        self.basename = os.path.basename(self.remove_extensions(filename))
        self.unconditional_save_file()
        self.adjust_label()