def __init__(self, path, cfgdir): self.path = path wal.Application.__init__(self) UCApplication.__init__(self, path, cfgdir, False) if wal.IS_WINXP: msg = _('WindowsXP platform is obsolete and not supported!') dialogs.error_dialog(self.mw, 'sK1', msg) sys.exit() self.appdata = AppData(self, cfgdir) log_level = config.log_level self.log_filepath = os.path.join(self.appdata.app_config_dir, 'sk1.log') mixutils.config_logging(fsutils.get_sys_path(self.log_filepath), log_level) sys.stderr = StreamLogger() LOG.info('Logging started') wal.SPIN['overlay'] = config.spin_overlay wal.SPIN['sep'] = config.spin_sep plg_dir = os.path.join(self.path, 'share', 'pd_plugins') custom_plg_dir = self.appdata.plugin_dir config.plugin_dirs = [plg_dir, custom_plg_dir] sys.path.insert(1, fsutils.get_sys_path(self.appdata.app_config_dir)) sys.path.insert(1, fsutils.get_sys_path(os.path.join(self.path, 'share'))) config.app = self LOG.info('Config is updated') self.history = AppHistoryManager(self) self.artprovider = create_artprovider() self.cursors = modes.get_cursors() self.proxy = AppProxy(self) self.insp = AppInspector(self) self.plugins = app_plugins.scan_plugins(self) self.actions = app_actions.create_actions(self) self.default_cms = AppColorManager(self) self.palettes = AppPaletteManager(self) self.clipboard = AppClipboard(self) self.mw = AppMainWindow(self) self.mw.set_global_shortcuts(self.actions) self.proxy.update() self.insp.update() LOG.info('Application is initialized') uc2.events.connect(uc2.events.MESSAGES, self.uc2_event_logging) events.connect(events.APP_STATUS, self.sk1_event_logging) self.fsw = AppFileWatcher(self, self.mw) if wal.IS_WX2: events.emit(events.NO_DOCS)
def make_backup(self, doc_file, export=False): if not export and not config.make_backup: return if export and not config.make_export_backup: return if fsutils.lexists(doc_file): if fsutils.lexists(doc_file + '~'): os.remove(get_sys_path(doc_file + '~')) os.rename(get_sys_path(doc_file), get_sys_path(doc_file + '~'))
def update(self): self.handles = {} self.clear_transforms() config = self.app.config profiles = [ config.cms_rgb_profile, config.cms_cmyk_profile, config.cms_lab_profile, config.cms_gray_profile, config.cms_display_profile ] profile_dicts = [ config.cms_rgb_profiles, config.cms_cmyk_profiles, config.cms_lab_profiles, config.cms_gray_profiles, config.cms_display_profiles ] index = 0 profile_dir = self.app.appdata.app_color_profile_dir for item in CS + [ COLOR_DISPLAY, ]: path = None profile = profiles[index] if profile and profile in profile_dicts[index]: profile_filename = profile_dicts[index][profile] path = os.path.join(profile_dir, profile_filename) if path: path = fsutils.get_sys_path(path) self.handles[item] = libcms.cms_open_profile_from_file(path) else: profile_dir = self.app.appdata.app_color_profile_dir filename = 'built-in_%s.icm' % item path = os.path.join(profile_dir, filename) path = fsutils.get_sys_path(path) self.handles[item] = libcms.cms_open_profile_from_file(path) index += 1 self.use_cms = config.cms_use self.use_display_profile = config.cms_use_display_profile self.rgb_intent = config.cms_rgb_intent self.cmyk_intent = config.cms_cmyk_intent self.flags = config.cms_flags self.proofing = config.cms_proofing self.alarm_codes = config.cms_alarmcodes self.gamutcheck = config.cms_gamutcheck if self.gamutcheck: libcms.cms_set_alarm_codes(*val_255(self.alarm_codes)) self.proof_for_spot = config.cms_proof_for_spot if self.proofing: self.flags = self.flags | uc2const.cmsFLAGS_SOFTPROOFING if self.gamutcheck: self.flags = self.flags | uc2const.cmsFLAGS_GAMUTCHECK if config.cms_bpc_flag: self.flags = self.flags | uc2const.cmsFLAGS_BLACKPOINTCOMPENSATION if config.cms_bpt_flag: self.flags = self.flags | uc2const.cmsFLAGS_PRESERVEBLACK
def load(self, presenter, path=None, fileptr=None): self.presenter = presenter self.model = presenter.model self.config = self.presenter.config if path: self.filepath = path self.file_size = os.path.getsize(get_sys_path(path)) self.fileptr = get_fileptr(path) elif fileptr: self.fileptr = fileptr self.fileptr.seek(0, 2) self.file_size = self.fileptr.tell() self.fileptr.seek(0) else: msg = _('There is no file for reading') raise IOError(errno.ENODATA, msg, '') try: self.init_load() except Exception: LOG.error('Error loading file content') raise self.fileptr.close() self.position = 0 return self.model
def __init__(self, path): self.path = path self.default_cms = cms.ColorManager() self.appdata = AppData(self) config.load(self.appdata.app_config) config.save(self.appdata.app_config) config.resource_dir = os.path.join(self.path, 'share') log_level = config.log_level self.log_filepath = os.path.join(self.appdata.app_config_dir, 'sword.log') config_logging(get_sys_path(self.log_filepath), log_level) sys.stderr = StreamLogger(self) LOG.info('Logging started') self.accelgroup = gtk.AccelGroup() self.actiongroup = gtk.ActionGroup('BasicAction') self.proxy = AppProxy(self) self.inspector = DocumentInspector(self) self.actions = create_actions(self) self.mw = MainWindow(self) self.proxy.update_references()
def remove_palette(self, palette_name): filepath = os.path.join(self.app.appdata.app_palette_dir, config.palette_files[palette_name]) if fsutils.isfile(filepath): os.remove(fsutils.get_sys_path(filepath)) del self.palettes[palette_name] del config.palette_files[palette_name]
def save(self, filename=None): if self.filename and filename is None: filename = self.filename if len(self.__dict__) == 0 or filename is None: return try: fileobj = fsutils.get_fileptr(filename, True) except Exception: return defaults = SerializedConfig.__dict__ items = self.__dict__.items() items.sort() for key, value in items: if key in defaults and defaults[key] == value: continue if key in ['filename', 'app']: continue try: line = fsutils.get_sys_path('%s = %s\n' % (key, value.__repr__())) except Exception as e: LOG.error('ERROR>>> %s', e) continue fileobj.write(line) fileobj.close()
def load_font(): fntdir = 'ruler-font%dpx' % config.ruler_font_size fntdir = os.path.join(config.resource_dir, 'fonts', fntdir) for char in '.,-0123456789': if char in '.,': file_name = os.path.join(fntdir, 'hdot.png') else: file_name = os.path.join(fntdir, 'h%s.png' % char) file_name = fsutils.get_sys_path(file_name) surface = cairo.ImageSurface.create_from_png(file_name) HFONT[char] = (surface.get_width(), surface) if char in '.,': file_name = os.path.join(fntdir, 'vdot.png') else: file_name = os.path.join(fntdir, 'v%s.png' % char) file_name = fsutils.get_sys_path(file_name) surface = cairo.ImageSurface.create_from_png(file_name) VFONT[char] = (surface.get_height(), surface)
def get_colored_surface(file_name, color): file_name = fsutils.get_sys_path(file_name) surface = cairo.ImageSurface.create_from_png(file_name) w, h = surface.get_width(), surface.get_height() res = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) cr = cairo.Context(res) cr.set_source_rgb(*color) cr.mask_surface(surface, 0, 0) cr.fill() return w, res
def import_profile(self): src = dialogs.get_open_file_name(self, config.profile_import_dir, _('Select profile to import'), file_types=[ICC, ICM]) if not src: return name = get_profile_name(src) title = self.app.appdata.app_name if name is None: msg = _('Cannot open profile') msg = "%s '%s'" % (msg, src) sec = _('The profile may be corrupted or in an unsupported format') wal.error_dialog(self, title, msg + '\n' + sec) return if name in self.pf_list: msg = _('Selected profile cannot be added to profile list:') msg = "%s '%s'" % (msg, name) sec = _('It seems like you have already imported this profile') wal.error_dialog(self, title, msg + '\n' + sec) return filename = os.path.basename(src) dst_dir = self.app.appdata.app_color_profile_dir dst = os.path.join(dst_dir, filename) if fsutils.exists(dst): msg = _('Selected file has been added to profile pool') msg = "%s '%s'" % (msg, src) sec = _('If you still wish to import the file, try renaming it') wal.error_dialog(self, title, msg + '\n' + sec) return try: shutil.copy(fsutils.get_sys_path(src), fsutils.get_sys_path(dst)) except Exception: msg = _('Cannot copy file') msg = "%s '%s'" % (msg, src) sec = _('Please check write permissions for config directory:') sec += '\n%s' % dst_dir wal.error_dialog(self, title, msg + '\n' + sec) return config.profile_import_dir = os.path.dirname(src) self.profiles[name] = filename self.apply_changes() self.viewer.set_active(self.pf_list.index(name))
def remove_profile(self): index = self.viewer.get_active() name = self.pf_list[index] filename = self.profiles[name] dst_dir = self.app.appdata.app_color_profile_dir dst = os.path.join(dst_dir, filename) if fsutils.isfile(dst): os.remove(fsutils.get_sys_path(dst)) self.profiles.pop(name) self.apply_changes() self.viewer.set_active(index - 1)
def get_langs(): if not LANGS: LANGS.append(_('system')) path = os.path.join(config.resource_dir, 'locales') langs = ['en', ] if fsutils.exists(path): langs += [item for item in os.listdir(fsutils.get_sys_path(path)) if fsutils.isdir(os.path.join(path, item))] langs.sort() for item in langs: LANGS.append(item)
def get_profile_info(filepath): """Returns profile info. If file is not suitable profile or doesn't exist returns None. """ try: filepath = fsutils.get_sys_path(filepath) profile = libcms.cms_open_profile_from_file(filepath) ret = libcms.cms_get_profile_info(profile) except Exception: ret = None return ret
def load_palettes(self): paldir = self.app.appdata.app_palette_dir loader = get_loader_by_id(uc2const.SKP) for item in config.palette_files.keys(): filepath = os.path.join(paldir, config.palette_files[item]) try: self.palettes[item] = loader(self.app.appdata, filepath, False, False, True) except Exception: if fsutils.isfile(filepath): os.remove(fsutils.get_sys_path(filepath)) del config.palette_files[item]
def get_profile_descr(filepath): """Returns profile description tuple (name, copyright, info). If file is not suitable profile or doesn't exist returns None. """ try: filepath = fsutils.get_sys_path(filepath) profile = libcms.cms_open_profile_from_file(filepath) ret = (libcms.cms_get_profile_name(profile), ) ret += (libcms.cms_get_profile_copyright(profile), ) ret += (libcms.cms_get_profile_info(profile), ) except Exception: ret = ('', '', '') return ret
def svgz_saver(sk2_doc, filename=None, fileptr=None, translate=True, cnf=None, **kw): cnf = merge_cnf(cnf, kw) if sk2_doc.cid == uc2const.SVG: translate = False path = get_sys_path(filename) fileptr = gzip.open(path, 'wb') if translate: svg_doc = SVG_Presenter(sk2_doc.appdata, cnf) svg_doc.translate_from_sk2(sk2_doc) svg_doc.save(None, fileptr) svg_doc.close() else: sk2_doc.save(None, fileptr)
def svgz_loader(appdata, filename=None, fileptr=None, translate=True, cnf=None, **kw): cnf = merge_cnf(cnf, kw) svg_doc = SVG_Presenter(appdata, cnf) path = get_sys_path(filename) fileptr = gzip.open(path, 'rb') svg_doc.load(None, fileptr) if translate: sk2_doc = SK2_Presenter(appdata, cnf) if filename: sk2_doc.doc_file = filename svg_doc.translate_to_sk2(sk2_doc) svg_doc.close() return sk2_doc return svg_doc
def close(self): filename = self.doc_file self.doc_file = '' if self.model is not None: self.model.destroy() self.model = None filename = filename.encode('utf-8') \ if isinstance(filename, unicode) else filename model_name = uc2const.FORMAT_NAMES[self.cid] self.send_ok( _('<%s> document model is destroyed for %s') % (model_name, filename)) if self.doc_dir and fsutils.lexists(self.doc_dir): try: fs.xremove_dir(fsutils.get_sys_path(self.doc_dir)) self.send_ok(_('Cache is cleared for') + ' %s' % filename) except Exception as e: msg = _('Cache clearing is unsuccessful') self.send_error(msg) LOG.warn(msg + ' %s', e)
def check_config_dirs(self): if not fsutils.exists(self.app_config_dir): fsutils.makedirs(self.app_config_dir) self.app_config = os.path.join(self.app_config_dir, 'preferences.cfg') # Check color profiles directory self.app_color_profile_dir = os.path.join(self.app_config_dir, 'profiles') if not fsutils.exists(self.app_color_profile_dir): fsutils.makedirs(self.app_color_profile_dir) from uc2.cms import libcms for item in uc2const.COLORSPACES + [uc2const.COLOR_DISPLAY, ]: filename = 'built-in_%s.icm' % item path = os.path.join(self.app_color_profile_dir, filename) if not fsutils.exists(path): path = fsutils.get_sys_path(path) libcms.cms_save_default_profile(path, item)