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 save_as(self): doc_file = self.current_doc.doc_file doc_file = doc_file or self.current_doc.doc_name if os.path.splitext(doc_file)[1] != "." + \ uc2const.FORMAT_EXTENSION[uc2const.SK2][0]: doc_file = os.path.splitext(doc_file)[0] + "." + \ uc2const.FORMAT_EXTENSION[uc2const.SK2][0] if not fsutils.lexists(os.path.dirname(doc_file)): doc_file = os.path.join(config.save_dir, os.path.basename(doc_file)) doc_file = dialogs.get_save_file_name(self.mw, doc_file, path_only=True) if doc_file: old_file = self.current_doc.doc_file old_name = self.current_doc.doc_name self.current_doc.set_doc_file(doc_file) try: self.make_backup(doc_file) self.current_doc.save() except Exception as e: self.current_doc.set_doc_file(old_file, old_name) first = _('Cannot save document:') msg = "%s\n'%s'." % (first, self.current_doc.doc_name) + '\n' msg += _('Please check file name and write permissions') dialogs.error_dialog(self.mw, self.appdata.app_name, msg) LOG.error('Cannot save file <%s> %s', doc_file, e) return False config.save_dir = str(os.path.dirname(doc_file)) self.history.add_entry(doc_file, appconst.SAVED) events.emit(events.DOC_SAVED, self.current_doc) events.emit(events.APP_STATUS, _('Document saved')) return True else: return False
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()
def import_file(self, doc_file=None, point=None): msg = _('Select file to import') if not doc_file: doc_file = dialogs.get_open_file_name(self.mw, config.import_dir, msg) if doc_file and fsutils.lexists(doc_file) and fsutils.isfile(doc_file): try: ret = self.current_doc.import_file(doc_file) if not ret: msg = _('Cannot import graphics from file:') msg = "%s\n'%s'" % (msg, doc_file) + '\n' msg += _('It seems the document is empty or ' 'contains unsupported objects.') dialogs.error_dialog(self.mw, self.appdata.app_name, msg) LOG.warn('Cannot import graphics from file <%s>', doc_file) elif point and self.current_doc.selection.bbox: x0, y0 = self.current_doc.canvas.win_to_doc(point) x1 = self.current_doc.selection.bbox[0] y1 = self.current_doc.selection.bbox[-1] dx = x0 - x1 dy = y0 - y1 self.current_doc.api.move_selected(dx, dy) config.import_dir = str(os.path.dirname(doc_file)) 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.warn('Cannot import file <%s>', doc_file, e)
def open(self, doc_file='', silent=False): doc_file = doc_file or \ dialogs.get_open_file_name(self.mw, config.open_dir) if doc_file and fsutils.lexists(doc_file) and fsutils.isfile(doc_file): try: doc = SK1Presenter(self, doc_file, silent) except RuntimeError: msg = _('Cannot open file:') msg = "%s\n'%s'" % (msg, doc_file) + '\n' msg += _('The file contains newer SK2 format.\n') msg += _('Try updating sK1 application from ' 'http://www.sk1project.net') dialogs.error_dialog(self.mw, self.appdata.app_name, msg) LOG.error('Cannot open file <%s>: newer SK2 format.', doc_file) return except Exception as e: msg = _('Cannot open 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 open file <%s> %s', doc_file, e) return self.docs.append(doc) config.open_dir = str(os.path.dirname(doc_file)) self.history.add_entry(doc_file) self.set_current_doc(doc) events.emit(events.APP_STATUS, _('Document opened'))
def save_selected(self): doc_file = self.current_doc.doc_file doc_file = doc_file or self.current_doc.doc_name if os.path.splitext(doc_file)[1] != "." + \ uc2const.FORMAT_EXTENSION[uc2const.SK2][0]: doc_file = os.path.splitext(doc_file)[0] + "." + \ uc2const.FORMAT_EXTENSION[uc2const.SK2][0] if not fsutils.lexists(os.path.dirname(doc_file)): doc_file = os.path.join(config.save_dir, os.path.basename(doc_file)) msg = _('Save selected objects only as...') doc_file = dialogs.get_save_file_name(self.mw, doc_file, msg, path_only=True) if doc_file: try: self.make_backup(doc_file) self.current_doc.save_selected(doc_file) self.history.add_entry(doc_file, appconst.SAVED) except Exception as e: first = _('Cannot save document:') msg = "%s\n'%s'." % (first, doc_file) + '\n' msg += _('Please check requested file format ' 'and write permissions') dialogs.error_dialog(self.mw, self.appdata.app_name, msg) LOG.error('Cannot save file <%s> %s', doc_file, e)
def get_loader(path, experimental=False, return_id=False): if not fsutils.lexists(path): return None if not fsutils.isfile(path): return None ret_id = None ext = get_file_extension(path) loader = None ld_formats = [] + uc2const.LOADER_FORMATS msg = 'Start to search for loader by file extension %s' % (ext.__str__()) events.emit(events.MESSAGES, msgconst.INFO, msg) if experimental: ld_formats += uc2const.EXPERIMENTAL_LOADERS for item in ld_formats: if ext in uc2const.FORMAT_EXTENSION[item]: checker = _get_checker(item) if checker and checker(path): loader = _get_loader(item) ret_id = item break if loader is None: msg = 'Loader is not found or not suitable for %s' % path events.emit(events.MESSAGES, msgconst.WARNING, msg) msg = 'Start to search loader by file content' events.emit(events.MESSAGES, msgconst.INFO, msg) for item in ld_formats: checker = _get_checker(item) if checker is not None: if checker(path): loader = _get_loader(item) ret_id = item break if loader is None: msg = 'By file content loader is not found for %s' % path events.emit(events.MESSAGES, msgconst.WARNING, msg) msg = 'Try using fallback loader' events.emit(events.MESSAGES, msgconst.INFO, msg) if fallback_check(path): loader = im_loader if loader is None: msg = 'Loader is not found for %s' % path events.emit(events.MESSAGES, msgconst.ERROR, msg) else: loader_name = loader.__str__().split(' ')[1] msg = 'Loader "%s" is found for %s' % (loader_name, path) events.emit(events.MESSAGES, msgconst.OK, msg) if return_id: return loader, ret_id return loader
def __init__(self, app, cfgdir='~'): # --- Init paths path = fsutils.expanduser(os.path.join(cfgdir, '.config', 'sk1-wx')) self.app_config_dir = path UCData.__init__(self, app, check=False) self.check_config_dirs() self.app_palette_dir = os.path.join(path, 'palettes') self.plugin_dir = os.path.join(path, 'sk1_custom_plugins') self.app_temp_dir = os.path.join(path, 'temp') # --- Check config directories paths = (self.app_palette_dir, self.plugin_dir, self.app_temp_dir) [fsutils.makedirs(item) for item in paths if not fsutils.lexists(item)] plugin_dir_init = os.path.join(self.plugin_dir, '__init__.py') if not fsutils.lexists(plugin_dir_init): fsutils.get_fileptr(plugin_dir_init, True).close()
def get_langs(): if not LANGS: LANGS.append(_('system')) path = os.path.join(config.resource_dir, 'locales') langs = ['en', ] if fsutils.lexists(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 check_config_dirs(self): if not fsutils.lexists(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.lexists(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.lexists(path): path = fsutils.get_sys_path(path) libcms.cms_save_default_profile(path, item)
def new_from_template(self): msg = _('Select Template') doc_file = dialogs.get_open_file_name(self.mw, config.template_dir, msg) if fsutils.lexists(doc_file) and fsutils.isfile(doc_file): try: doc = SK1Presenter(self, doc_file, template=True) except Exception as e: msg = _('Cannot parse 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) LOG.error('Cannot parse file <%s> %s', doc_file, e) return self.docs.append(doc) config.template_dir = str(os.path.dirname(doc_file)) self.set_current_doc(doc) events.emit(events.APP_STATUS, _('New document from template'))
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.lexists(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 load(self, filename=None): self.filename = filename if fsutils.lexists(filename): content_handler = XMLPrefReader(pref=self) error_handler = ErrorHandler() entity_resolver = EntityResolver() dtd_handler = DTDHandler() try: input_file = get_fileptr(filename) input_source = InputSource() input_source.setByteStream(input_file) xml_reader = xml.sax.make_parser() xml_reader.setContentHandler(content_handler) xml_reader.setErrorHandler(error_handler) xml_reader.setEntityResolver(entity_resolver) xml_reader.setDTDHandler(dtd_handler) xml_reader.parse(input_source) input_file.close() except Exception as e: LOG.error('Cannot read preferences from %s %s', filename, e)
def load(self, filename=None, fileptr=None): if filename and fsutils.lexists(filename): self.doc_file = filename elif not fileptr: msg = _('Error while loading:') + ' ' + _('No file') self.send_error(msg) raise IOError(msg) try: self.parsing_msg(0.03) self.send_info(_('Parsing in progress...')) self.model = self.loader.load(self, filename, fileptr) except Exception: self.close() LOG.error('Error loading %s', filename) raise model_name = uc2const.FORMAT_NAMES[self.cid] self.send_ok(_('<%s> document model is created') % model_name) self.update()
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 import_pattern(self, parent=None): parent = parent or self.mw file_types = uc2const.PATTERN_FORMATS img_file = dialogs.get_open_file_name(parent, config.import_dir, _('Select pattern to load'), file_types=file_types) if fsutils.lexists(img_file) and fsutils.isfile(img_file): first = _('Cannot load pattern for:') msg = "%s\n'%s'." % (first, self.current_doc.doc_name) + '\n' msg += _('The file may be corrupted or not supported format') msg += '\n' msg += _('Details see in application logs.') try: if libimg.check_image(img_file): config.import_dir = str(os.path.dirname(img_file)) return img_file else: dialogs.error_dialog(parent, self.appdata.app_name, msg) LOG.error('Cannot load pattern <%s>', img_file) except Exception as e: dialogs.error_dialog(parent, self.appdata.app_name, msg) LOG.error('Cannot load pattern <%s> %s', img_file, e)
def import_file(self): doc_file = dialogs.get_open_file_name(self.mw, config.import_dir, _('Select file to import')) if fsutils.lexists(doc_file) and fsutils.isfile(doc_file): try: ret = self.current_doc.import_file(doc_file) if not ret: msg = _('Cannot import graphics from file:') msg = "%s\n'%s'" % (msg, doc_file) + '\n' msg += _('It seems the document is empty or ' 'contains unsupported objects.') dialogs.error_dialog(self.mw, self.appdata.app_name, msg) LOG.warn('Cannot import graphics from file <%s>', doc_file) config.import_dir = str(os.path.dirname(doc_file)) 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.warn('Cannot import file <%s>', doc_file, e)
def save(self, doc=None): doc = doc or self.current_doc if not doc.doc_file: return self.save_as() ext = os.path.splitext(self.current_doc.doc_file)[1] if not ext == "." + uc2const.FORMAT_EXTENSION[uc2const.SK2][0]: return self.save_as() if not fsutils.lexists(os.path.dirname(self.current_doc.doc_file)): return self.save_as() try: self.make_backup(self.current_doc.doc_file) doc.save() self.history.add_entry(self.current_doc.doc_file, appconst.SAVED) events.emit(events.DOC_SAVED, doc) except Exception as e: msg = _('Cannot save file:') msg = "%s\n'%s'" % (msg, self.current_doc.doc_file) + '\n' msg += _('Please check file write permissions') dialogs.error_dialog(self.mw, self.appdata.app_name, msg) LOG.error('Cannot save file <%s> %s', self.current_doc.doc_file, e) return False events.emit(events.APP_STATUS, _('Document saved')) return True