Exemplo n.º 1
0
    def import_palette(self, parent=None):
        parent = parent or self.mw
        file_types = uc2const.PALETTE_LOADERS
        doc_file = dialogs.get_open_file_name(parent,
                                              config.import_dir,
                                              _('Select palette to import'),
                                              file_types=file_types)
        if fsutils.lexists(doc_file) and fsutils.isfile(doc_file):

            pd = dialogs.ProgressDialog(_('Opening file...'), parent)
            try:
                loader = get_loader(doc_file)
                if not loader:
                    raise IOError(_('Loader is not found for <%s>') % doc_file)
                palette = pd.run(loader,
                                 [self.appdata, doc_file, None, False, True])
                if not palette:
                    raise IOError(_('Error while opening'), doc_file)
                self.palettes.add_palette(palette)
                config.import_dir = str(os.path.dirname(doc_file))
                msg = _('Palette is successfully imported')
                events.emit(events.APP_STATUS, msg)
                return palette.model.name
            except Exception as e:
                msg = _('Cannot import file:')
                msg = "%s\n'%s'" % (msg, doc_file) + '\n'
                msg += _('The file may be corrupted or not supported format.')
                msg += '\n'
                msg += _('Details see in application logs.')
                dialogs.error_dialog(self.mw, self.appdata.app_name, msg)
                LOG.error('Cannot import file <%s> %s', doc_file, e)
            finally:
                pd.destroy()
Exemplo n.º 2
0
    def export_palette(self, palette, parent=None):
        if not parent: parent = self.mw
        doc_file = '' + palette.model.name
        doc_file = os.path.splitext(doc_file)[0]
        doc_file = os.path.join(config.export_dir, os.path.basename(doc_file))
        ret = dialogs.get_save_file_name(parent,
                                         self,
                                         doc_file,
                                         _('Export palette as...'),
                                         file_types=PALETTE_SAVERS)
        if not ret: return
        doc_file, index = ret
        saver_id = PALETTE_SAVERS[index]

        if doc_file:

            if not os.path.splitext(doc_file)[1] == "." + \
               uc2const.FORMAT_EXTENSION[saver_id][0]:
                doc_file = os.path.splitext(doc_file)[0] + "." + \
                  uc2const.FORMAT_EXTENSION[saver_id][0]

            try:
                saver = get_saver_by_id(saver_id)
                if saver is None:
                    raise IOError(
                        _('Unknown file format is requested for export!'),
                        doc_file)

                self.make_backup(doc_file, True)

                pd = dialogs.ProgressDialog(_('Exporting...'), parent)
                ret = pd.run(saver, [palette, doc_file, None, False, True],
                             False)
                if ret:
                    if not pd.error_info is None:
                        pd.destroy()
                        raise IOError(*pd.error_info)
                    pd.destroy()
                else:
                    pd.destroy()
                    raise IOError(_('Error while exporting'), doc_file)

            except IOError:
                raise IOError(*sys.exc_info())

            config.export_dir = str(os.path.dirname(doc_file))
            events.emit(events.APP_STATUS,
                        _('Palette is successfully exported'))
Exemplo n.º 3
0
    def export_palette(self, palette, parent=None):
        if not parent:
            parent = self.mw
        doc_file = palette.model.name
        doc_file = os.path.splitext(doc_file)[0]
        doc_file = os.path.join(config.export_dir, os.path.basename(doc_file))
        ret = dialogs.get_save_file_name(parent,
                                         doc_file,
                                         _('Export palette as...'),
                                         file_types=uc2const.PALETTE_SAVERS)
        if not ret:
            return
        doc_file, index = ret
        saver_id = uc2const.PALETTE_SAVERS[index]

        if doc_file:
            if not os.path.splitext(doc_file)[1] == "." + \
                   uc2const.FORMAT_EXTENSION[saver_id][0]:
                doc_file = os.path.splitext(doc_file)[0] + "." + \
                           uc2const.FORMAT_EXTENSION[saver_id][0]

            pd = dialogs.ProgressDialog(_('Exporting...'), parent)
            try:
                saver = get_saver_by_id(saver_id)
                if saver is None:
                    msg = _('Unknown file format is requested for export <%s>')
                    raise IOError(msg % doc_file)
                self.make_backup(doc_file, True)
                pd.run(saver, [palette, doc_file, None, False, True])
            except Exception as e:
                msg = _('Cannot export palette:')
                msg = "%s\n'%s'." % (msg, doc_file) + '\n'
                msg += _('Please check file name and write permissions')
                dialogs.error_dialog(self.mw, self.appdata.app_name, msg)
                LOG.error('Cannot save bitmap in <%s>', doc_file, e)
                return
            finally:
                pd.destroy()

            config.export_dir = str(os.path.dirname(doc_file))
            msg = _('Palette is successfully exported')
            events.emit(events.APP_STATUS, msg)
Exemplo n.º 4
0
    def import_palette(self, parent=None):
        if not parent: parent = self.mw
        doc_file = dialogs.get_open_file_name(parent,
                                              self,
                                              config.import_dir,
                                              _('Select palette to import'),
                                              file_types=PALETTE_LOADERS)
        if os.path.lexists(doc_file) and os.path.isfile(doc_file):
            try:
                palette = None
                loader = get_loader(doc_file)
                pd = dialogs.ProgressDialog(_('Opening file...'), parent)
                ret = pd.run(loader,
                             [self.appdata, doc_file, None, False, True])
                if ret:
                    if pd.result is None:
                        pd.destroy()
                        raise IOError(*pd.error_info)

                    palette = pd.result
                    ret = True
                    pd.destroy()
                else:
                    pd.destroy()
                    raise IOError(_('Error while opening'), doc_file)

                if palette:
                    self.palettes.add_palette(palette)
                    config.import_dir = str(os.path.dirname(doc_file))
                    msg = _('Palette is successfully imported')
                    events.emit(events.APP_STATUS, msg)
                    return palette.model.name

            except:
                msg = _('Cannot import file:')
                msg = "%s\n'%s'" % (msg, doc_file) + '\n'
                msg += _('The file may be corrupted or not supported format')
                dialogs.error_dialog(self.mw, self.appdata.app_name, msg)
                self.print_stacktrace()
        return None