def save_as_doc(self, doc, win=None): wnd = win or self.wins[0] doc_file = doc.doc_file doc_file = doc_file or doc.model.name if os.path.splitext(doc_file)[1] != "." + \ uc2const.FORMAT_EXTENSION[uc2const.SKP][0]: doc_file = os.path.splitext(doc_file)[0] + "." + \ uc2const.FORMAT_EXTENSION[uc2const.SKP][0] if not os.path.exists(os.path.dirname(doc_file)): doc_file = os.path.join(config.save_dir, os.path.basename(doc_file)) ret = dialogs.get_save_file_name( wnd, doc_file, file_types=uc2const.PALETTE_SAVERS) if ret and len(ret) == 2: try: doc_file, saver_id = ret saver = get_saver_by_id(saver_id) saver(doc, doc_file, translate=False, convert=True) LOG.info('Palette saved to %s', doc_file) return True except Exception as e: msg = _('Cannot save file:') msg = "%s\n'%s'" % (msg, doc_file) + '\n' msg2 = _('Details see in logs') wal.error_dialog(wnd, self.appdata.app_name, msg, msg2) LOG.exception('Cannot save file <%s>' % doc_file, e) return False
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'))
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] + "." + \ uc2const.FORMAT_EXTENSION[uc2const.SKP][0] doc_file = os.path.join(config.export_dir, os.path.basename(doc_file)) doc_file, index = dialogs.get_save_file_name(parent, self, doc_file, _('Export palette as...'), file_types=data.PALETTE_SAVERS) saver_id = data.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'))
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)
def run(self): if '--help' in sys.argv or '-help' in sys.argv or len(sys.argv) == 1: self.show_help() elif len(sys.argv) == 2: self.show_short_help(_('Not enough arguments!')) files = [] options_list = [] options = {} for item in sys.argv[1:]: if item.startswith('--'): options_list.append(item) elif item.startswith('-'): self.show_short_help(_('Unknown option "%s"') % item) else: files.append(item) if not files: self.show_short_help(_('File names are not provided!')) elif len(files) == 1: self.show_short_help(_('Destination file name is not provided!')) elif not os.path.lexists(files[0]): self.show_short_help( _('Source file "%s" is not found!') % files[0]) for item in options_list: result = item[2:].split('=') if not len(result) == 2: options[result[0]] = True else: key, value = result value = value.replace('"', '').replace("'", '') if value.lower() == 'yes': value = True if value.lower() == 'no': value = False options[key] = value self.do_verbose = options.get('verbose', False) events.connect(events.MESSAGES, self.verbose) log_level = options.get('log', self.config.log_level) self.log_filepath = os.path.join(self.appdata.app_config_dir, 'uc2.log') config_logging(self.log_filepath, log_level) self.default_cms = cms.ColorManager() self.palettes = PaletteManager(self) echo('') msg = _('Translation of "%s" into "%s"') % (files[0], files[1]) events.emit(events.MESSAGES, msgconst.JOB, msg) saver_ids = uc2const.PALETTE_SAVERS saver_ids += uc2const.MODEL_SAVERS + uc2const.BITMAP_SAVERS sid = options.get('format', '').lower() if sid and sid in saver_ids: saver_id = sid saver = get_saver_by_id(saver_id) else: saver, saver_id = get_saver(files[1], return_id=True) if saver is None: msg = _('Output file format of "%s" is unsupported.') % files[1] events.emit(events.MESSAGES, msgconst.ERROR, msg) msg = _('Translation is interrupted') events.emit(events.MESSAGES, msgconst.STOP, msg) loader, loader_id = get_loader(files[0], return_id=True) if loader is None: msg = _('Input file format of "%s" is unsupported.') % files[0] events.emit(events.MESSAGES, msgconst.ERROR, msg) msg = _('Translation is interrupted') events.emit(events.MESSAGES, msgconst.STOP, msg) doc = None try: if loader_id in uc2const.PALETTE_LOADERS and \ saver_id in uc2const.PALETTE_SAVERS: doc = loader(self.appdata, files[0], convert=True) else: doc = loader(self.appdata, files[0]) except Exception as e: msg = _('Error while loading "%s"') % files[0] msg += _( 'The file may be corrupted or contains unknown file format.') events.emit(events.MESSAGES, msgconst.ERROR, msg) msg = _('Loading is interrupted') LOG.error('%s %s', msg, e) events.emit(events.MESSAGES, msgconst.STOP, msg) if doc is not None: try: if loader_id in uc2const.PALETTE_LOADERS and \ saver_id in uc2const.PALETTE_SAVERS: saver(doc, files[1], translate=False, convert=True) else: saver(doc, files[1]) except Exception as e: msg = _('Error while translation and saving "%s"') % files[0] events.emit(events.MESSAGES, msgconst.ERROR, msg) msg = _('Translation is interrupted') LOG.error('%s %s', msg, e) events.emit(events.MESSAGES, msgconst.STOP, msg) else: msg = _('Error creating model for "%s"') % files[0] events.emit(events.MESSAGES, msgconst.ERROR, msg) msg = _('Translation is interrupted') events.emit(events.MESSAGES, msgconst.STOP, msg) doc.close() msg = _('Translation is successful') events.emit(events.MESSAGES, msgconst.OK, msg) echo('') sys.exit(0)
def convert(appdata, files, options): dry_run = bool(options.get('dry-run')) normalize_options(options) msg = 'Translation of "%s" into "%s"' % (files[0], files[1]) events.emit(events.MESSAGES, msgconst.JOB, msg) # Define saver ----------------------------------------- sid = options.get('format', '').lower() if sid and sid in SAVER_IDS: saver_id = sid saver = get_saver_by_id(saver_id) else: saver, saver_id = get_saver(files[1], return_id=True) if saver is None: msg = 'Output file format of "%s" is unsupported.' % files[1] events.emit(events.MESSAGES, msgconst.ERROR, msg) msg2 = 'Translation is interrupted' events.emit(events.MESSAGES, msgconst.STOP, msg2) raise Exception(msg) # Define loader ----------------------------------------- loader, loader_id = get_loader(files[0], return_id=True) if loader is None: msg = 'Input file format of "%s" is unsupported.' % files[0] events.emit(events.MESSAGES, msgconst.ERROR, msg) msg2 = 'Translation is interrupted' events.emit(events.MESSAGES, msgconst.STOP, msg2) raise Exception(msg) if dry_run: return # File loading ----------------------------------------- doc = None try: if loader_id in uc2const.PALETTE_LOADERS and \ saver_id in uc2const.PALETTE_SAVERS: doc = loader(appdata, files[0], convert=True, **options) else: doc = loader(appdata, files[0], **options) except Exception: msg = 'Error while loading "%s"' % files[0] msg += 'The file may be corrupted or contains unknown file format.' events.emit(events.MESSAGES, msgconst.ERROR, msg) LOG.exception(msg) msg = 'Loading is interrupted' events.emit(events.MESSAGES, msgconst.STOP, msg) raise # File saving ----------------------------------------- if doc is not None: try: if loader_id in uc2const.PALETTE_LOADERS and \ saver_id in uc2const.PALETTE_SAVERS: saver(doc, files[1], translate=False, convert=True, **options) else: saver(doc, files[1], **options) except Exception: msg = 'Error while translation and saving "%s"' % files[0] events.emit(events.MESSAGES, msgconst.ERROR, msg) LOG.exception(msg) msg2 = 'Translation is interrupted' events.emit(events.MESSAGES, msgconst.STOP, msg2) raise else: msg = 'Error creating model for "%s"' % files[0] events.emit(events.MESSAGES, msgconst.ERROR, msg) msg2 = 'Translation is interrupted' events.emit(events.MESSAGES, msgconst.STOP, msg2) raise Exception(msg) doc.close() msg = 'Translation is successful' events.emit(events.MESSAGES, msgconst.OK, msg)
def process_palette(self, dir_path, palette, pal_id): palette_name = palette.model.name palette_filename = palette_name.replace(' ', '_').replace('.', '_') pal_resources = '' for sid in saver_ids: saver = get_saver_by_id(sid) ext = '.' + FORMAT_EXTENSION[sid][0] if sid == SCRIBUS_PAL: ext = '(Scribus)' + ext if sid == COREL_PAL: ext = '(CorelDRAW)' + ext filename = palette_filename + ext doc_file = os.path.join(dir_path, filename) pal_resources += '\t"%s"=>"%s",\n' % (FORMAT_NAMES[sid], filename) saver(palette, doc_file, None, False, True) # saving for tarballs collection_path = os.path.dirname(dir_path) fmt_path = os.path.join(collection_path, FORMAT_NAMES[sid]) if not os.path.exists(fmt_path): os.makedirs(fmt_path) doc_file = os.path.join(fmt_path, filename) saver(palette, doc_file, None, False, True) sk2_doc = SK2_Presenter(self.app.appdata) palette.translate_to_sk2(sk2_doc) saver = get_saver_by_id(PNG) doc_file = os.path.join(dir_path, 'preview.png') saver(sk2_doc, doc_file, None, True, False, antialiasing=False) sk2_doc.close() # Thumbnail generation surface = cairo.ImageSurface(cairo.FORMAT_RGB24, int(514), int(20)) ctx = cairo.Context(surface) ctx.set_antialias(cairo.ANTIALIAS_NONE) ctx.set_source_rgb(0.0, 0.0, 0.0) ctx.fill() x = y = 1 w = h = 18 for color in palette.model.colors: r, g, b = self.app.default_cms.get_display_color(color) ctx.set_source_rgb(r, g, b) ctx.rectangle(x, y, w, h) ctx.fill() x += w + 1 ctx.set_source_rgb(1, 1, 1) ctx.rectangle(x, 0, 514, 20) ctx.fill() if len(palette.model.colors) > 22: lg = cairo.LinearGradient(418, 0, 513, 0) lg.add_color_stop_rgba(0, 1, 1, 1, 0) lg.add_color_stop_rgba(1, 1, 1, 1, 1) ctx.rectangle(418, 0, 513, 20) ctx.set_source(lg) ctx.fill() filename = os.path.join(dir_path, 'thumbnail.png') fileptr = open(filename, 'wb') surface.write_to_png(fileptr) fileptr.close() # index.php generation filename = os.path.join(dir_path, 'index.php') fileptr = open(filename, 'wb') content = '<?php\n' content += 'header("HTTP/1.1 301 Moved Permanently");\n' content += 'header( "Location: /palettes.php", true, 301);\n' content += 'exit;\n' content += '?>\n' fileptr.write(content) fileptr.close() # descriptor generation filename = os.path.join(dir_path, 'descriptor.php') fileptr = open(filename, 'wb') content = '<?php\n' content += '$descriptor = array(\n' content += '"id"=>"%s",\n' % pal_id content += '"name"=>"%s",\n' % palette_name content += '"source"=>"%s",\n' % palette.model.source comments = palette.model.comments.replace('\n', '<br>') content += '"comments"=>"%s",\n' % comments.replace('"', '\\"') content += '"ncolors"=>"%d",\n' % len(palette.model.colors) content += '"files"=>array(\n' content += pal_resources content += '\t),\n' content += ');\n' content += '?>\n' fileptr.write(content) fileptr.close()
def process_palette(self, dir_path, palette, pal_id): palette_name = palette.model.name palette_filename = palette_name.replace(' ', '_').replace('.', '_') pal_resources = '' for sid in saver_ids: saver = get_saver_by_id(sid) ext = '.' + FORMAT_EXTENSION[sid][0] if sid == SCRIBUS_PAL: ext = '(Scribus)' + ext if sid == COREL_PAL: ext = '(CorelDRAW)' + ext filename = palette_filename + ext doc_file = os.path.join(dir_path, filename) pal_resources += '\t"%s"=>"%s",\n' % (FORMAT_NAMES[sid], filename) saver(palette, doc_file, None, False, True) #saving for tarballs collection_path = os.path.dirname(dir_path) fmt_path = os.path.join(collection_path, FORMAT_NAMES[sid]) if not os.path.exists(fmt_path): os.makedirs(fmt_path) doc_file = os.path.join(fmt_path, filename) saver(palette, doc_file, None, False, True) sk2_doc = SK2_Presenter(self.app.appdata) palette.translate_to_sk2(sk2_doc) saver = get_saver_by_id(PNG) doc_file = os.path.join(dir_path, 'preview.png') saver(sk2_doc, doc_file, None, True, False, antialiasing=False) sk2_doc.close() #Thumbnail generation surface = cairo.ImageSurface(cairo.FORMAT_RGB24, int(514), int(20)) ctx = cairo.Context(surface) ctx.set_antialias(cairo.ANTIALIAS_NONE) ctx.set_source_rgb(0.0, 0.0, 0.0) ctx.fill() x = y = 1 w = h = 18 for color in palette.model.colors: r, g, b = self.app.default_cms.get_display_color(color) ctx.set_source_rgb(r, g, b) ctx.rectangle(x, y, w, h) ctx.fill() x += w + 1 ctx.set_source_rgb(1, 1, 1) ctx.rectangle(x, 0, 514, 20) ctx.fill() if len(palette.model.colors) > 22: lg = cairo.LinearGradient(418, 0, 513, 0) lg.add_color_stop_rgba(0, 1, 1, 1, 0) lg.add_color_stop_rgba(1, 1, 1, 1, 1) ctx.rectangle(418, 0, 513, 20) ctx.set_source(lg) ctx.fill() filename = os.path.join(dir_path, 'thumbnail.png') fileptr = open(filename, 'wb') surface.write_to_png(fileptr) fileptr.close() #index.php generation filename = os.path.join(dir_path, 'index.php') fileptr = open(filename, 'wb') content = '<?php\n' content += 'header("HTTP/1.1 301 Moved Permanently");\n' content += 'header( "Location: /palettes.php", true, 301);\n' content += 'exit;\n' content += '?>\n' fileptr.write(content) fileptr.close() #descriptor generation filename = os.path.join(dir_path, 'descriptor.php') fileptr = open(filename, 'wb') content = '<?php\n' content += '$descriptor = array(\n' content += '"id"=>"%s",\n' % pal_id content += '"name"=>"%s",\n' % palette_name content += '"source"=>"%s",\n' % palette.model.source comments = palette.model.comments.replace('\n', '<br>') content += '"comments"=>"%s",\n' % comments.replace('"', '\\"') content += '"ncolors"=>"%d",\n' % len(palette.model.colors) content += '"files"=>array(\n' content += pal_resources content += '\t),\n' content += ');\n' content += '?>\n' fileptr.write(content) fileptr.close()
def convert(appdata, files, options): dry_run = bool(options.get('dry-run')) normalize_options(options) msg = 'Translation of "%s" into "%s"' % (files[0], files[1]) events.emit(events.MESSAGES, msgconst.JOB, msg) # Define saver ----------------------------------------- sid = options.get('format', '').lower() if sid and sid in const.SAVER_IDS: saver_id = sid saver = get_saver_by_id(saver_id) else: saver, saver_id = get_saver(files[1], return_id=True) if saver is None: msg = 'Output file format of "%s" is unsupported.' % files[1] events.emit(events.MESSAGES, msgconst.ERROR, msg) msg2 = 'Translation is interrupted' events.emit(events.MESSAGES, msgconst.STOP, msg2) raise Exception(msg) # Define loader ----------------------------------------- loader, loader_id = get_loader(files[0], return_id=True) if loader is None: msg = 'Input file format of "%s" is unsupported.' % files[0] events.emit(events.MESSAGES, msgconst.ERROR, msg) msg2 = 'Translation is interrupted' events.emit(events.MESSAGES, msgconst.STOP, msg2) raise Exception(msg) if dry_run: return # File loading ----------------------------------------- try: if loader_id in uc2const.PALETTE_LOADERS and \ saver_id in uc2const.PALETTE_SAVERS: doc = loader(appdata, files[0], convert=True, **options) else: doc = loader(appdata, files[0], **options) except Exception: msg = 'Error while loading "%s"' % files[0] msg += 'The file may be corrupted or contains unknown file format.' events.emit(events.MESSAGES, msgconst.ERROR, msg) LOG.exception(msg) msg = 'Loading is interrupted' events.emit(events.MESSAGES, msgconst.STOP, msg) raise # Model transforming ---------------------------------- if doc and doc.cid == uc2const.SK2: for action in CLI_ACTIONS: if action == const.FIT_PAGE_TO_IMAGE: msg = 'ACTION: Fit page to image' LOG.info(msg) events.emit(events.MESSAGES, msgconst.JOB, msg) doc.methods.fit_pages_to_image() elif action == const.FIT_TO_PAGE: msg = 'ACTION: Fit drawing to page' LOG.info(msg) events.emit(events.MESSAGES, msgconst.JOB, msg) doc.methods.fit_to_pages() # File saving ----------------------------------------- if doc is not None: try: if loader_id in uc2const.PALETTE_LOADERS and \ saver_id in uc2const.PALETTE_SAVERS: saver(doc, files[1], translate=False, convert=True, **options) else: saver(doc, files[1], **options) except Exception: msg = 'Error while translation and saving "%s"' % files[0] events.emit(events.MESSAGES, msgconst.ERROR, msg) LOG.exception(msg) msg2 = 'Translation is interrupted' events.emit(events.MESSAGES, msgconst.STOP, msg2) raise else: msg = 'Error creating model for "%s"' % files[0] events.emit(events.MESSAGES, msgconst.ERROR, msg) msg2 = 'Translation is interrupted' events.emit(events.MESSAGES, msgconst.STOP, msg2) raise Exception(msg) doc.close() msg = 'Translation is successful' events.emit(events.MESSAGES, msgconst.OK, msg)