Esempio n. 1
0
    def on_btn_export_clicked(self, *args):
        md = SkyTempleMessageDialog(
            MainController.window(),
            Gtk.DialogFlags.MODAL, Gtk.MessageType.INFO,
            Gtk.ButtonsType.OK,
            f"Export is done to a CSV file with the following specifications:\n"
            f"- Contains all strings in order, one per row\n"
            f"- Strings may be quoted with: \" and escaped with doube-quotes."
        )
        md.run()
        md.destroy()

        save_diag = Gtk.FileChooserNative.new(
            "Export strings as...",
            MainController.window(),
            Gtk.FileChooserAction.SAVE,
            None, None
        )

        add_dialog_csv_filter(save_diag)
        response = save_diag.run()
        fn = save_diag.get_filename()
        save_diag.destroy()

        if response == Gtk.ResponseType.ACCEPT:
            if '.' not in fn:
                fn += '.csv'
            with open_utf8(fn, 'w') as result_file:
                wr = csv.writer(result_file)
                wr.writerows([[x] for x in self._str.strings])
Esempio n. 2
0
    def on_btn_import_clicked(self, *args):
        md = SkyTempleMessageDialog(
            MainController.window(),
            Gtk.DialogFlags.MODAL, Gtk.MessageType.INFO,
            Gtk.ButtonsType.OK,
            f"Import is done from a CSV file with the following specifications:\n"
            f"- Has to contain all strings in order, one per row\n"
            f"- Strings may be quoted with: \" and escaped with doube-quotes."
        )
        md.run()
        md.destroy()
        save_diag = Gtk.FileChooserNative.new(
            "Import strings from...",
            MainController.window(),
            Gtk.FileChooserAction.OPEN,
            None, None
        )

        add_dialog_csv_filter(save_diag)
        response = save_diag.run()
        fn = save_diag.get_filename()
        save_diag.destroy()

        if response == Gtk.ResponseType.ACCEPT:
            try:
                with open_utf8(fn) as csv_file:
                    csv_reader = csv.reader(csv_file)
                    strings = []
                    for row in csv_reader:
                        if len(row) > 0:
                            strings.append(row[0])
                    if len(self._str.strings) != len(strings):
                        raise ValueError(f"The CSV file must contain exactly {len(self._str.strings)} strings, has {len(strings)}.")
                    self._str.strings = strings
                self.module.mark_as_modified(self.filename)
                MainController.reload_view()
            except BaseException as err:
                display_error(
                    sys.exc_info(),
                    str(err),
                    "Error exporting the strings."
                )