def __init__(self, app, doc_file='', silent=False, template=False): self.app = app self.eventloop = EventLoop(self) self.selection = Selection(self) if doc_file and silent: loader = get_loader(doc_file) if not loader: raise IOError(_('Loader is not found for <%s>') % doc_file) self.doc_presenter = loader(app.appdata, doc_file) elif doc_file and not silent: pd = ProgressDialog(_('Opening file...'), self.app.mw) try: loader = get_loader(doc_file) if not loader: raise IOError(_('Loader is not found for <%s>') % doc_file) self.doc_presenter = pd.run(loader, [app.appdata, doc_file]) except Exception: raise finally: pd.destroy() if not template: self.doc_file = self.doc_presenter.doc_file self.doc_name = os.path.basename(self.doc_file) ext = uc2const.FORMAT_EXTENSION[uc2const.SK2][0] self.doc_name = change_file_extension(self.doc_name, ext) else: self.doc_name = self.app.get_new_docname() self.doc_presenter.doc_file = '' else: self.doc_presenter = SK2_Presenter(app.appdata) self.doc_name = self.app.get_new_docname() self.methods = self.doc_presenter.methods self.model = self.doc_presenter.model self.set_active_page() self.cms = self.doc_presenter.cms # self.app.default_cms.registry_cm(self.cms) self.api = PresenterAPI(self) self.corner = RulerCorner(self) self.hruler = Ruler(self, vertical=False) self.vruler = Ruler(self) self.canvas = AppCanvas(self) self.canvas.set_mode() self.eventloop.connect(self.eventloop.DOC_MODIFIED, self.modified) self.snap = SnapManager(self)
def _get_doc_form_file(self, filepath=None, win=None, title=None): doc = None wnd = win or self.wins[0] if wnd and not filepath: filepath = dialogs.get_open_file_name( wnd, config.open_dir, title, uc2const.PALETTE_LOADERS) if not filepath: return if os.path.isfile(filepath): try: loader = get_loader(filepath) if not loader: raise LookupError('Cannot find loader for %s' % filepath) doc = loader(self.appdata, filepath, convert=True) self._normalize_colors(doc) config.open_dir = str(os.path.dirname(filepath)) except Exception as e: msg = _('Cannot parse file:') msg = "%s\n'%s'" % (msg, filepath) + '\n' msg2 = _('The file may be corrupted or not supported format') wal.error_dialog(wnd, self.appdata.app_name, msg, msg2) LOG.exception('Cannot parse file <%s>' % filepath, e) return doc
def insert_doc(self, doc_file): doc_presenter = None loader = get_loader(doc_file) if loader is None: raise IOError(_('Unknown file format'), doc_file) pd = ProgressDialog(_('Importing file...'), self.app.mw) ret = pd.run(loader, [self.app.appdata, doc_file]) if ret == gtk.RESPONSE_OK: if pd.result is None: pd.destroy() raise IOError(*pd.error_info) doc_presenter = pd.result pd.destroy() res = simplify_doc(doc_presenter) if not res: first = _("File '%s' doesn't contain supported objects.") % ( doc_file) second = _( 'It seems all objects are either images or special groups.' ) ret = warning_dialog(self.app.mw, self.app.appdata.app_name, first, second, [(gtk.STOCK_OK, gtk.RESPONSE_OK)]) doc_presenter.close() return else: pd.destroy() raise IOError(_('Error while opening'), doc_file) if not doc_presenter is None: self.api.merge_doc(doc_presenter) doc_presenter.close()
def __init__(self, app, doc_file=''): self.app = app self.eventloop = EventLoop(self) self.doc_file = doc_file if doc_file: loader = get_loader(doc_file, True) if loader is None: raise IOError(_('Unknown file format'), doc_file) pd = ProgressDialog(_('Opening file...'), self.app.mw) ret = pd.run(loader, [app.appdata, doc_file, None, False]) if ret == gtk.RESPONSE_OK: if pd.result is None: pd.destroy() raise IOError(*pd.error_info) self.doc_presenter = pd.result pd.destroy() else: pd.destroy() raise IOError(_('Error while opening'), doc_file) self.doc_file = self.doc_presenter.doc_file self.doc_name = os.path.basename(self.doc_file) else: #FIXME: Here should be new model creation self.doc_name = self.app.get_new_docname() self.doc_presenter.update(True) self.selection = ModelSelection(self) self.docarea = DocArea(self.app, self) self.app.mw.add_tab(self.docarea) self.selection.set_root()
def __init__(self, app, doc_file='', silent=False): self.app = app self.eventloop = EventLoop(self) self.selection = Selection(self) if doc_file: loader = get_loader(doc_file) if loader is None: raise IOError(_('Unknown file format'), doc_file) if silent: self.doc_presenter = loader(app.appdata, doc_file) else: pd = ProgressDialog(_('Opening file...'), self.app.mw) ret = pd.run(loader, [app.appdata, doc_file]) if ret == gtk.RESPONSE_OK: if pd.result is None: pd.destroy() raise IOError(*pd.error_info) self.doc_presenter = pd.result pd.destroy() else: pd.destroy() raise IOError(_('Error while opening'), doc_file) self.doc_file = self.doc_presenter.doc_file self.doc_name = os.path.basename(self.doc_file) self.doc_name = change_file_extension( self.doc_name, uc2const.FORMAT_EXTENSION[uc2const.SK1][0]) else: self.doc_presenter = SK1_Presenter(app.appdata) self.doc_name = self.app.get_new_docname() self.methods = self.doc_presenter.methods self.model = self.doc_presenter.model self.set_active_page() self.cms = self.app.default_cms self.api = PresenterAPI(self) self.docarea = DocArea(self.app, self) self.canvas = self.docarea.canvas self.api.view = self.canvas self.app.mw.add_tab(self.docarea) self.eventloop.connect(self.eventloop.DOC_MODIFIED, self.modified) self.snap = SnapManager(self) self.traced_objects = [ self.doc_presenter, self.eventloop, self.api, self.docarea.hruler, self.docarea.vruler, self.docarea.corner, self.docarea, self.canvas.renderer, self.canvas, self.selection, self.snap, ]
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 load(doc_file=''): if not doc_file: raise ValueError('Empty doc_file path') elif not os.path.exists(doc_file) or not os.path.isfile(doc_file): ValueError('doc_file "%s" not found' % doc_file) loader = get_loader(doc_file, True) if loader is None: raise IOError('Unknown file format for %s' % doc_file) doc = loader(app.appdata, doc_file, translate=False, convert=False) doc.update(True) doc_id = hex(id(doc)) DOCS[doc_id] = doc doc.indexes = {} CD.append(doc) name = '%s document model' % doc.cid.upper() doc.json_model = dict(id=doc_id, name=name, info_column=False, binary=doc.model_type == uc2const.BINARY_MODEL, root=None, fileName=os.path.basename(doc_file), filePath=doc_file.replace(os.path.expanduser('~'), '~')) doc.json_model['root'] = _parse_model(doc.model) CD.remove(doc) return [doc_file, doc.json_model]
def insert_doc(self, doc_file): doc_presenter = None loader = get_loader(doc_file) if loader is None: raise IOError(_('Unknown file format'), doc_file) pd = ProgressDialog(_('Importing file...'), self.app.mw) ret = pd.run(loader, [self.app.appdata, doc_file]) if ret == gtk.RESPONSE_OK: if pd.result is None: pd.destroy() raise IOError(*pd.error_info) doc_presenter = pd.result pd.destroy() res = simplify_doc(doc_presenter) if not res: first = _("File '%s' doesn't contain supported objects.") % (doc_file) second = _('It seems all objects are either images or special groups.') ret = warning_dialog(self.app.mw, self.app.appdata.app_name, first, second, [(gtk.STOCK_OK, gtk.RESPONSE_OK)]) doc_presenter.close() return else: pd.destroy() raise IOError(_('Error while opening'), doc_file) if not doc_presenter is None: self.api.merge_doc(doc_presenter) doc_presenter.close()
def print_calibration(self, app, win, path, media=''): pd = ProgressDialog(_('Loading calibration page...'), win) try: loader = get_loader(path) doc_presenter = pd.run(loader, [app.appdata, path]) self.run_printdlg(win, Printout(doc_presenter)) except Exception: txt = _('Error while printing of calibration page!') txt += '\n' + _('Check your printer status and connection.') error_dialog(win, app.appdata.app_name, txt) finally: pd.destroy()
def __init__(self, app, doc_file='', silent=False, template=False): self.app = app self.eventloop = EventLoop(self) self.selection = Selection(self) if doc_file: loader = get_loader(doc_file) if silent: self.doc_presenter = loader(app.appdata, doc_file) else: pd = ProgressDialog(_('Opening file...'), self.app.mw) ret = pd.run(loader, [self.app.appdata, doc_file]) if ret: if pd.result is None: pd.destroy() raise IOError(*pd.error_info) self.doc_presenter = pd.result pd.destroy() else: pd.destroy() raise IOError(_('Error while opening'), doc_file) if not template: self.doc_file = self.doc_presenter.doc_file self.doc_name = os.path.basename(self.doc_file) self.doc_name = change_file_extension(self.doc_name, uc2const.FORMAT_EXTENSION[ uc2const.SK2][0]) else: self.doc_name = self.app.get_new_docname() self.doc_presenter.doc_file = '' else: self.doc_presenter = SK2_Presenter(app.appdata) self.doc_name = self.app.get_new_docname() self.methods = self.doc_presenter.methods self.model = self.doc_presenter.model self.set_active_page() self.cms = self.doc_presenter.cms # self.app.default_cms.registry_cm(self.cms) self.api = PresenterAPI(self) self.docarea = self.app.mdi.create_docarea(self) self.canvas = self.docarea.canvas self.canvas.set_mode() self.eventloop.connect(self.eventloop.DOC_MODIFIED, self.modified) self.snap = SnapManager(self) self.set_title()
def __init__(self, app, doc_file='', silent=False, template=False): self.app = app self.eventloop = EventLoop(self) self.selection = Selection(self) if doc_file: loader = get_loader(doc_file) if silent: self.doc_presenter = loader(app.appdata, doc_file) else: pd = ProgressDialog(_('Opening file...'), self.app.mw) ret = pd.run(loader, [self.app.appdata, doc_file]) if ret: if pd.result is None: pd.destroy() raise IOError(*pd.error_info) self.doc_presenter = pd.result pd.destroy() else: pd.destroy() raise IOError(_('Error while opening'), doc_file) if not template: self.doc_file = self.doc_presenter.doc_file self.doc_name = os.path.basename(self.doc_file) self.doc_name = change_file_extension(self.doc_name, uc2const.FORMAT_EXTENSION[uc2const.SK2][0]) else: self.doc_name = self.app.get_new_docname() self.doc_presenter.doc_file = '' else: self.doc_presenter = SK2_Presenter(app.appdata) self.doc_name = self.app.get_new_docname() self.methods = self.doc_presenter.methods self.model = self.doc_presenter.model self.set_active_page() self.cms = self.doc_presenter.cms #self.app.default_cms.registry_cm(self.cms) self.api = PresenterAPI(self) self.docarea = self.app.mdi.create_docarea(self) self.canvas = self.docarea.canvas self.canvas.set_mode() self.eventloop.connect(self.eventloop.DOC_MODIFIED, self.modified) self.snap = SnapManager(self) self.set_title()
def print_calibration(self, app, win, path, media=''): doc_presenter = None loader = get_loader(path) pd = ProgressDialog(_('Loading calibration page...'), win) ret = pd.run(loader, [app.appdata, path]) if ret and pd.result is not None: doc_presenter = pd.result if doc_presenter: try: self.printing(Printout(doc_presenter), media) except: doc_presenter = None pd.destroy() if not doc_presenter: txt = _('Error while printing of calibration page!') txt += '\n' + _('Check your printer status and connection.') error_dialog(win, app.appdata.app_name, txt)
def print_calibration(self, app, win, path, media=''): doc_presenter = None loader = get_loader(path) pd = ProgressDialog(_('Loading calibration page...'), win) ret = pd.run(loader, [app.appdata, path]) if ret and not pd.result is None: doc_presenter = pd.result pd.destroy() if doc_presenter: try: self.run_printdlg(win, Printout(doc_presenter)) except: doc_presenter = None if not doc_presenter: txt = _('Error while printing of calibration page!') txt += '\n' + _('Check your printer status and connection.') error_dialog(win, app.appdata.app_name, txt)
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
def import_file(self, doc_file): retval = True pd = ProgressDialog(_('Importing...'), self.app.mw) try: loader = get_loader(doc_file) if not loader: raise IOError(_('Loader is not found for <%s>') % doc_file) doc_presenter = pd.run(loader, [self.app.appdata, doc_file]) if not doc_presenter: LOG.error('Cannot load <%s>', doc_file) raise IOError(_('Cannot load <%s>') % doc_file) except Exception: raise finally: pd.destroy() pages = doc_presenter.methods.get_pages() if len(pages) == 1: page = doc_presenter.methods.get_page() objs = [] for layer in page.childs: for child in layer.childs: objs.append(child) layer.childs = [] if objs: self.api.paste_selected(objs) else: retval = False else: pages = doc_presenter.methods.get_pages() pages_obj = doc_presenter.methods.get_pages_obj() pages_obj.childs = [] if pages: self.api.add_pages(pages) else: retval = False doc_presenter.close() return retval
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=data.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 '%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
def import_file(self, doc_file): retval = True loader = get_loader(doc_file) pd = ProgressDialog(_('Opening file...'), self.app.mw) ret = pd.run(loader, [self.app.appdata, doc_file]) if ret: if pd.result is None: pd.destroy() raise IOError(*pd.error_info) doc_presenter = pd.result pd.destroy() else: pd.destroy() raise IOError(_('Error while opening'), doc_file) pages = doc_presenter.methods.get_pages() if len(pages) == 1: page = doc_presenter.methods.get_page() objs = [] for layer in page.childs: for child in layer.childs: objs.append(child) layer.childs = [] if objs: self.api.paste_selected(objs) else: retval = False else: pages = doc_presenter.methods.get_pages() pages_obj = doc_presenter.methods.get_pages_obj() pages_obj.childs = [] if pages: self.api.add_pages(pages) else: retval = False doc_presenter.close() return retval
def __init__(self, app, doc_file='', silent=False): self.app = app self.eventloop = EventLoop(self) self.selection = Selection(self) if doc_file: loader = get_loader(doc_file) if loader is None: raise IOError(_('Unknown file format'), doc_file) if silent: self.doc_presenter = loader(app.appdata, doc_file) simplify_doc(self.doc_presenter) else: pd = ProgressDialog(_('Opening file...'), self.app.mw) ret = pd.run(loader, [app.appdata, doc_file]) if ret == gtk.RESPONSE_OK: if pd.result is None: pd.destroy() raise IOError(*pd.error_info) self.doc_presenter = pd.result pd.destroy() res = simplify_doc(self.doc_presenter) if not res: first = _( "File '%s' doesn't contain supported objects.") % ( doc_file) second = _( 'It seems all objects are either images or special groups.' ) ret = warning_dialog(self.app.mw, self.app.appdata.app_name, first, second, [(gtk.STOCK_OK, gtk.RESPONSE_OK)]) else: pd.destroy() raise IOError(_('Error while opening'), doc_file) self.doc_file = self.doc_presenter.doc_file self.doc_name = os.path.basename(self.doc_file) self.doc_name = change_file_extension( self.doc_name, uc2const.FORMAT_EXTENSION[uc2const.PDXF][0]) else: self.doc_presenter = PDXF_Presenter(app.appdata) self.doc_presenter.new() self.doc_name = self.app.get_new_docname() self.methods = self.doc_presenter.methods self.model = self.doc_presenter.model self.set_active_page() self.cms = self.doc_presenter.cms self.cms.use_cms = False self.api = PresenterAPI(self) self.docarea = DocArea(self.app, self) self.canvas = self.docarea.canvas self.api.view = self.canvas self.app.mw.add_tab(self.docarea) self.eventloop.connect(self.eventloop.DOC_MODIFIED, self.modified) self.traced_objects = [ self.eventloop, self.api, self.docarea.hruler, self.docarea.vruler, self.docarea.corner, self.docarea, self.canvas.renderer, self.canvas, self.selection, self ]
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)
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)
from uc2 import uc2_init from uc2.formats import get_loader docs = [] def release(): for doc in docs: doc.close() docs.remove(doc) print 'released' uc_app = uc2_init() time.sleep(3) filepath = 'data/sample.pdxf' filepath2 = 'data/sample2.pdxf' loader = get_loader(filepath) i = 0 while i < 20: docs.append(loader(uc_app.appdata, filepath2)) release() docs.append(loader(uc_app.appdata, filepath)) release() docs.append(loader(uc_app.appdata, filepath2)) release() docs.append(loader(uc_app.appdata, filepath)) release() i += 1 print 'Done'
docs = [] def release(): for doc in docs: doc.close() docs.remove(doc) print 'released' uc_app = uc2_init() time.sleep(3) filepath = 'data/sample.pdxf' filepath2 = 'data/sample2.pdxf' loader = get_loader(filepath) i = 0 while i < 20: docs.append(loader(uc_app.appdata, filepath2)) release() docs.append(loader(uc_app.appdata, filepath)) release() docs.append(loader(uc_app.appdata, filepath2)) release() docs.append(loader(uc_app.appdata, filepath)) release() i += 1 print 'Done'
def __init__(self, app, doc_file='', silent=False): self.app = app self.eventloop = EventLoop(self) self.selection = Selection(self) if doc_file: loader = get_loader(doc_file) if loader is None: raise IOError(_('Unknown file format'), doc_file) if silent: self.doc_presenter = loader(app.appdata, doc_file) simplify_doc(self.doc_presenter) else: pd = ProgressDialog(_('Opening file...'), self.app.mw) ret = pd.run(loader, [app.appdata, doc_file]) if ret == gtk.RESPONSE_OK: if pd.result is None: pd.destroy() raise IOError(*pd.error_info) self.doc_presenter = pd.result pd.destroy() res = simplify_doc(self.doc_presenter) if not res: first = _("File '%s' doesn't contain supported objects.") % (doc_file) second = _('It seems all objects are either images or special groups.') ret = warning_dialog(self.app.mw, self.app.appdata.app_name, first, second, [(gtk.STOCK_OK, gtk.RESPONSE_OK)]) else: pd.destroy() raise IOError(_('Error while opening'), doc_file) self.doc_file = self.doc_presenter.doc_file self.doc_name = os.path.basename(self.doc_file) self.doc_name = change_file_extension(self.doc_name, uc2const.FORMAT_EXTENSION[uc2const.PDXF][0]) else: self.doc_presenter = PDXF_Presenter(app.appdata) self.doc_presenter.new() self.doc_name = self.app.get_new_docname() self.methods = self.doc_presenter.methods self.model = self.doc_presenter.model self.set_active_page() self.cms = self.doc_presenter.cms self.cms.use_cms = False self.api = PresenterAPI(self) self.docarea = DocArea(self.app, self) self.canvas = self.docarea.canvas self.api.view = self.canvas self.app.mw.add_tab(self.docarea) self.eventloop.connect(self.eventloop.DOC_MODIFIED, self.modified) self.traced_objects = [ self.eventloop, self.api, self.docarea.hruler, self.docarea.vruler, self.docarea.corner, self.docarea, self.canvas.renderer, self.canvas, self.selection, self ]
def run(self): if len(sys.argv) < 3 or '--help' in sys.argv: self.show_help() files = [] options_list = [] options = {} for item in sys.argv[1:]: if item[0] == '-': if item == '-verbose': events.connect(events.MESSAGES, self.verbose) else: options_list.append(item) else: files.append(item) if len(files) <> 2: self.show_help() if not os.path.lexists(files[0]): self.show_help() for item in options_list: result = item[1:].split('=') if not len(result) == 2: continue else: key, value = result if value == 'yes': value = True if value == 'no': value = False options[key] = value self.default_cms = cms.ColorManager() self.palettes = PaletteManager(self) print '' msg = _('Translation of') + ' "%s" ' % (files[0]) + _('into "%s"') % ( files[1]) events.emit(events.MESSAGES, msgconst.JOB, msg) saver = get_saver(files[1]) 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) sys.exit(1) loader = get_loader(files[0]) 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) sys.exit(1) try: doc = loader(self.appdata, files[0]) except: 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 = _('Translation is interrupted') events.emit(events.MESSAGES, msgconst.STOP, msg) print '\n', sys.exc_info()[1], sys.exc_info()[2] sys.exit(1) if doc is not None: try: saver(doc, files[1]) except: msg = _("Error while translation and saving '%s'") % (files[0]) events.emit(events.MESSAGES, msgconst.ERROR, msg) msg = _('Translation is interrupted') events.emit(events.MESSAGES, msgconst.STOP, msg) print '\n', sys.exc_info()[1], sys.exc_info()[2] sys.exit(1) else: msg = _("Error while model creating for '%s'") % (files[0]) events.emit(events.MESSAGES, msgconst.ERROR, msg) msg = _('Translation is interrupted') events.emit(events.MESSAGES, msgconst.STOP, msg) sys.exit(1) doc.close() events.emit(events.MESSAGES, msgconst.OK, _('Translation is successful')) print '' sys.exit(0)
def run(self): if len(sys.argv) < 3 or '--help' in sys.argv: self.show_help() files = [] options_list = [] options = {} for item in sys.argv[1:]: if item[0] == '-': if item == '-verbose': events.connect(events.MESSAGES, self.verbose) else: options_list.append(item) else: files.append(item) if len(files) <> 2: self.show_help() if not os.path.lexists(files[0]):self.show_help() for item in options_list: result = item[1:].split('=') if not len(result) == 2: continue else: key, value = result if value == 'yes':value = True if value == 'no':value = False options[key] = value self.default_cms = cms.ColorManager() self.palettes = PaletteManager(self) print '' msg = _('Translation of') + ' "%s" ' % (files[0]) + _('into "%s"') % (files[1]) events.emit(events.MESSAGES, msgconst.JOB, msg) saver = get_saver(files[1]) 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) sys.exit(1) loader = get_loader(files[0]) 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) sys.exit(1) try: doc = loader(self.appdata, files[0]) except: 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 = _('Translation is interrupted') events.emit(events.MESSAGES, msgconst.STOP, msg) print '\n', sys.exc_info()[1], sys.exc_info()[2] sys.exit(1) if doc is not None: try: saver(doc, files[1]) except: msg = _("Error while translation and saving '%s'") % (files[0]) events.emit(events.MESSAGES, msgconst.ERROR, msg) msg = _('Translation is interrupted') events.emit(events.MESSAGES, msgconst.STOP, msg) print '\n', sys.exc_info()[1], sys.exc_info()[2] sys.exit(1) else: msg = _("Error while model creating for '%s'") % (files[0]) events.emit(events.MESSAGES, msgconst.ERROR, msg) msg = _('Translation is interrupted') events.emit(events.MESSAGES, msgconst.STOP, msg) sys.exit(1) doc.close() events.emit(events.MESSAGES, msgconst.OK, _('Translation is successful')) print '' 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 __init__(self, app, doc_file='', silent=False): self.app = app self.eventloop = EventLoop(self) self.selection = Selection(self) if doc_file: loader = get_loader(doc_file) if loader is None: raise IOError(_('Unknown file format'), doc_file) if silent: self.doc_presenter = loader(app.appdata, doc_file) else: pd = ProgressDialog(_('Opening file...'), self.app.mw) ret = pd.run(loader, [app.appdata, doc_file]) if ret == gtk.RESPONSE_OK: if pd.result is None: pd.destroy() raise IOError(*pd.error_info) self.doc_presenter = pd.result pd.destroy() else: pd.destroy() raise IOError(_('Error while opening'), doc_file) self.doc_file = self.doc_presenter.doc_file self.doc_name = os.path.basename(self.doc_file) self.doc_name = change_file_extension(self.doc_name, uc2const.FORMAT_EXTENSION[uc2const.SK1][0]) else: self.doc_presenter = SK1_Presenter(app.appdata) self.doc_name = self.app.get_new_docname() self.methods = self.doc_presenter.methods self.model = self.doc_presenter.model self.set_active_page() self.cms = self.app.default_cms self.api = PresenterAPI(self) self.docarea = DocArea(self.app, self) self.canvas = self.docarea.canvas self.api.view = self.canvas self.app.mw.add_tab(self.docarea) self.eventloop.connect(self.eventloop.DOC_MODIFIED, self.modified) self.snap = SnapManager(self) self.traced_objects = [ self.doc_presenter, self.eventloop, self.api, self.docarea.hruler, self.docarea.vruler, self.docarea.corner, self.docarea, self.canvas.renderer, self.canvas, self.selection, self.snap, ]