def multiple_convert(appdata, files, options): saver_ext = _get_saver_extension(options) verbose = bool(options.get('verbose')) verbose_short = bool(options.get('verbose-short')) filelist = files[:-1] dir_path = files[-1] for filepath in filelist: if not os.path.exists(filepath): msg = 'File "%s" is not found' % filepath events.emit(events.MESSAGES, msgconst.STOP, msg) continue filename = os.path.basename(filepath).split('.', 1)[0] out_filepath = os.path.join(dir_path, '%s.%s' % (filename, saver_ext)) kw = copy.deepcopy(options) try: convert(appdata, (filepath, out_filepath), kw) if verbose: echo() elif verbose_short: echo('Translation of "%s"' % filepath) echo('into "%s" ...[ OK ]\n' % out_filepath) except Exception: if verbose_short: echo('Translation of "%s"' % filepath) echo('into "%s" ...[ FAIL ]\n' % out_filepath)
def verbose(self, *args): status = msgconst.MESSAGES[args[0]] LOG_MAP[args[0]](args[1]) if self.do_verbose or args[0] in (msgconst.ERROR, msgconst.STOP): indent = ' ' * (msgconst.MAX_LEN - len(status)) echo('%s%s| %s' % (status, indent, args[1])) if args[0] == msgconst.STOP: echo('For details see logs: %s\n' % self.log_filepath)
def show_help(appdata): mark = '' if not appdata.build \ else ' build %s' % appdata.build app_name = '%s %s%s%s' % (appdata.app_name, appdata.version, appdata.revision, mark) echo(HELP_TEMPLATE % ( app_name, str(datetime.date.today().year), _get_infos(uc2const.PALETTE_LOADERS), _get_infos(uc2const.PALETTE_SAVERS), ))
def show_help(self): app_name = '%s %s%s' % ( self.appdata.app_name, self.appdata.version, self.appdata.revision) echo(HELP_TEMPLATE % (app_name, str(datetime.date.today().year), self._get_infos(uc2const.MODEL_LOADERS), self._get_infos(uc2const.PALETTE_LOADERS), self._get_infos(uc2const.BITMAP_LOADERS), self._get_infos(uc2const.MODEL_SAVERS), self._get_infos(uc2const.PALETTE_SAVERS), self._get_infos(uc2const.BITMAP_SAVERS),)) sys.exit(0)
def show_parts(appdata): echo() mark = '' if not appdata.build \ else ' build %s' % appdata.build app_name = '%s %s%s%s' % (appdata.app_name, appdata.version, appdata.revision, mark) echo('%s components:\n' % app_name) part = '' try: part = 'Python' _show_part(part, sys.version) part = 'LCMS' from uc2 import cms _show_part(part, cms.libcms.get_version()) part = 'Cairo' from uc2 import libcairo _show_part(part, libcairo.get_version()[0]) part = 'pycairo' _show_part(part, libcairo.get_version()[1]) part = 'Pillow' from uc2 import libimg _show_part(part, libimg.get_version()) part = 'ImageMagick' _show_part(part, libimg.get_magickwand_version()[0]) part = 'Pango' from uc2 import libpango _show_part(part, libpango.get_version()) part = 'Reportlab' import reportlab _show_part(part, reportlab.Version) except Exception as e: _show_part(part, 'FAIL') echo('Reason: %s' % str(e)) echo()
def show_parts(appdata): echo() mark = '' if not appdata.build \ else ' build %s' % appdata.build app_name = '%s %s%s%s' % (appdata.app_name, appdata.version, appdata.revision, mark) echo('%s components:\n' % app_name) part = '' try: part = 'Python' _show_part(part, sys.version) part = 'LCMS' from uc2 import cms _show_part(part, cms.libcms.get_version()) except Exception as e: _show_part(part, 'FAIL') echo('Reason: %s' % str(e)) echo()
def change_config(options): config = uc2.config if len(options) < 2: echo('Please provide configuration values to change.') return for key, value in options.items(): if key in BOOL_ATTRS: config.__dict__[key] = bool(value) elif key == 'log_level': if value in LEVELS: config.log_level = value elif key in INTENT_ATTRS: if isinstance(value, int) and value in INTENTS: config.__dict__[key] = value elif value in INTENTS: config.__dict__[key] = INTENTS[value] elif key in PROFILES and isinstance(value, str): if not value: config.__dict__[key] = '' continue cs = uc2const.COLORSPACES[PROFILES.index(key)] path = fsutils.normalize_path(value) if not fsutils.exists(path): echo('ERROR: file "%s" is not found!' % path) continue profile_name = cms.get_profile_name(path) if not profile_name: echo('ERROR: file "%s" is not valid color profile!' % path) continue profile_dir = config.app.appdata.app_color_profile_dir dest_path = os.path.join(profile_dir, '%s.icc' % cs) if fsutils.exists(dest_path): fsutils.remove(dest_path) fsutils.copy(path, dest_path) profile_dict = PROFILE_DICTS[PROFILES.index(key)] config.__dict__[profile_dict] = {profile_name: dest_path} config.__dict__[key] = profile_name
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 show_short_help(self, msg): echo('') echo(msg) echo(_('USAGE: uniconvertor [OPTIONS] [INPUT FILE] [OUTPUT FILE]')) echo(_('Use --help for more details.') + '\n') sys.exit(1)
def show_short_help(msg): echo() echo(msg) echo('USAGE: uniconvertor [OPTIONS] [INPUT FILE] [OUTPUT FILE]') echo('Use --help for more details.') echo('For detailed help visit https://sk1project.net/uc2/help/' + '\n')
def run(self, current_dir=None): if len(sys.argv) == 1: dt = self.appdata mark = '' if not dt.build else ' build %s' % dt.build msg = '%s %s%s%s\n' % (dt.app_name, dt.version, dt.revision, mark) cmds.show_short_help(msg) sys.exit(0) elif cmds.check_args(cmds.HELP_CMDS): cmds.show_help(self.appdata) sys.exit(0) elif cmds.check_args(cmds.PARTS_CMDS): cmds.show_parts(self.appdata) sys.exit(0) elif cmds.check_args(cmds.LOG_CMDS): log_filepath = os.path.join(self.appdata.app_config_dir, 'uc2.log') log_filepath = log_filepath.decode('utf-8') with open(log_filepath, 'rb') as fileptr: echo(fileptr.read()) sys.exit(0) elif cmds.check_args(cmds.DIR_CMDS): echo(os.path.dirname(os.path.dirname(__file__))) sys.exit(0) elif cmds.check_args(cmds.CFG_SHOW_CMDS): cmds.show_config() sys.exit(0) elif cmds.check_args(cmds.CONFIG_CMDS): options = cmds.parse_cmd_args(current_dir)[1] cmds.normalize_options(options) cmds.change_config(options) self.config.save() sys.exit(0) elif len(sys.argv) == 2: cmds.show_short_help('Not enough arguments!') sys.exit(1) self.do_verbose = cmds.check_args(cmds.VERBOSE_CMDS) current_dir = os.getcwdu() if current_dir is None else current_dir files, options = cmds.parse_cmd_args(current_dir) if not files: cmds.show_short_help('File names are not provided!') sys.exit(1) elif len(files) == 1: msg = 'Destination directory or file name is not provided!' cmds.show_short_help(msg) sys.exit(1) command = cmds.convert if any(['*' in files[0], '?' in files[0]]): command = cmds.wildcard_convert if os.path.exists(files[1]): if not os.path.isdir(files[1]): msg = 'Destination directory "%s" is not a directory!' cmds.show_short_help(msg % files[1]) sys.exit(1) else: os.makedirs(files[1]) elif len(files) > 2: command = cmds.multiple_convert if os.path.exists(files[-1]): if not os.path.isdir(files[-1]): msg = 'Destination directory "%s" is not a directory!' cmds.show_short_help(msg % files[-1]) sys.exit(1) else: os.makedirs(files[-1]) elif not fsutils.exists(files[0]): cmds.show_short_help('Source file "%s" is not found!' % files[0]) sys.exit(1) 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 = app_cms.AppColorManager(self) self.palettes = PaletteManager(self) # EXECUTION ---------------------------- status = 0 # noinspection PyBroadException try: command(self.appdata, files, options) except Exception: status = 1 if self.do_verbose: echo() sys.exit(status)
def _show_part(name, value): value = value.replace('\n', '') msg = ' ' * 4 + name + '.' * (ALIGNMENT - len(name)) + '[ %s ]' % value echo(msg)
def show_config(): config = uc2.config echo() echo('UniConvertor 2.0 preferences:\n') echo(' --log_level=%s' % config.log_level) echo() echo(' --cms_use=%s' % to_bool(config.cms_use)) echo(' --cms_rgb_profile="%s"' % (config.cms_rgb_profile or DEFAULT_RGB)) echo(' --cms_cmyk_profile="%s"' % (config.cms_cmyk_profile or DEFAULT_CMYK)) echo(' --cms_lab_profile="%s"' % (config.cms_lab_profile or DEFAULT_LAB)) echo(' --cms_gray_profile="%s"' % (config.cms_gray_profile or DEFAULT_GRAY)) echo() echo(' --cms_rgb_intent="%s"' % INTENTS[config.cms_rgb_intent]) echo(' --cms_cmyk_intent="%s"' % INTENTS[config.cms_cmyk_intent]) echo() echo(' --black_point_compensation=%s' % to_bool(config.cms_bpc_flag)) echo(' --black_preserving_transform=%s' % to_bool(config.cms_bpt_flag)) echo()