def _get_docs(self): docs = [] if len(sys.argv) > 1: for item in sys.argv[1:]: if os.path.exists(item): docs.append(fsutils.get_utf8_path(item)) return docs
def __init__(self, path='', cfgdir='~', check=True): self.path = path cfgdir = fsutils.expanduser(fsutils.get_utf8_path(cfgdir)) self.config = UCConfig() self.config.app = self self.appdata = UCData(self, cfgdir, check=check) setattr(uc2, 'config', self.config) setattr(uc2, 'appdata', self.appdata)
def write(self, msg): msg = fsutils.get_utf8_path(msg) if not msg.endswith('\n') and not msg.startswith(' '): if self.counter < 2: self.msg += msg self.counter += 1 else: self.logger(self.msg + msg) self.msg = '' self.counter = 0 else: self.msg += msg
def load(self, filename=None): self.filename = filename if fsutils.exists(filename): try: fileobj = fsutils.get_fileptr(filename) except Exception: return while True: line = fileobj.readline() if line.startswith('<?xml') or not line: break if line.startswith('#'): continue line = fsutils.get_utf8_path('self.%s' % line) try: code = compile(line, '<string>', 'exec') exec code except Exception as e: LOG.error('ERROR>>> %s\n%s', line, e) fileobj.close()
def call_after(self, *args): if config.make_font_cache_on_start: font_cache_update() if self.docs: return docs = [ fsutils.get_utf8_path(item) for item in sys.argv[1:] if os.path.exists(item) ] if config.new_doc_on_start and not docs: self.load_plugins() self.new() else: txt = _('To start, create new document or open existing') events.emit(events.APP_STATUS, txt) self.load_plugins() if not wal.IS_WX2: events.emit(events.NO_DOCS) self.update_actions() for item in docs: self.open(item)
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(fsutils.get_utf8_path(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) 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, exc_info=True) 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) if self.do_verbose: echo('') sys.exit(0)