def get(lang): """Get the i18n of language lang and output dict.""" i18nkey = 'i18n:' + lang gval = g.get(i18nkey, None) if gval: return gval if redisconnection.exists(i18nkey): return json.loads(redisconnection.get(i18nkey)) data = {} fallbacklist = _create_fallback(lang) datafiles = _loadi18nfiles(fallbacklist) for key in datafiles['en']: if key == '@metadata': # @metadata is a dict not a string continue for code in fallbacklist: if key in datafiles.get(code, {}): data[key] = datafiles[code][key] # <'s and >'s aren't supposed to be here; # if the translation breaks due to double escaping, # oh well, why are you hacking this tool? # --XSS prevention data[key] = data[key].replace('<', '<') data[key] = data[key].replace('>', '>') break data['@lang'] = lang data['@dir'] = _dir(lang) setattr(g, i18nkey, data) redisconnection.setex(i18nkey, json.dumps(data), 60) return data
def get(lang): """Get the i18n of language lang and output dict.""" i18nkey = 'i18n:' + lang gval = g.get(i18nkey, None) if gval: return gval if redisconnection.exists(i18nkey): return json.loads(redisconnection.get(i18nkey)) data = {} fallbacklist = _create_fallback(lang) datafiles = _loadi18nfiles(fallbacklist) for key in datafiles['en']: if key == '@metadata': # @metadata is a dict not a string continue for code in fallbacklist: if key in datafiles.get(code, {}): data[key] = datafiles[code][key] # <'s and >'s aren't supposed to be here; # if the translation breaks due to double escaping, # oh well, why are you hacking this tool? # --XSS prevention data[key] = data[key].replace('<', '<') data[key] = data[key].replace('>', '>') break data['@dir'] = _dir(lang) setattr(g, i18nkey, data) redisconnection.setex(i18nkey, json.dumps(data), 60) return data
def _status(id): title = get_title_from_task(id) if not title: # task has been forgotten -- results should have been expired return None res = worker.main.AsyncResult(id) task = {'id': id, 'title': title} try: state = res.state except: task.update({ 'status': 'fail', 'text': 'The status of the task could not be retrieved.', 'traceback': traceback.format_exc() }) else: if state == 'PENDING': task.update({ 'status': 'progress', 'text': 'Your task is pending...', 'progress': -1 }) elif state == 'PROGRESS': task.update({ 'status': 'progress', 'text': res.result['text'], 'progress': res.result['percent'] }) elif state == 'SUCCESS': if isinstance(res.result, (list, tuple)): filename, wikifileurl = res.result task.update({ 'status': 'done', 'url': wikifileurl, 'text': filename }) elif isinstance(res.result, dict): if res.result['type'] == 'done': task.update({ 'status': 'done', 'url': res.result['url'], 'text': res.result['filename'] }) elif res.result['type'] == 'ssu': task.update({ 'status': 'needssu', 'filename': res.result['url'].rsplit('/', 1)[-1], 'url': res.result['url'], 'hashsum': res.result['hashsum'] }) elif state == 'FAILURE': e = res.result if e is False: task.update({ 'status': 'fail', 'text': res.traceback, 'restartable': True }) else: task.update({ 'status': 'fail', 'text': format_exception(e), 'restartable': ((not redisconnection.exists('restarted:' + id)) and redisconnection.exists('params:' + id)) }) elif state == 'RETRY': task.update({ 'status': 'progress', 'text': 'Your task is being rescheduled...', 'progress': -1 }) elif state == 'ABORTED': task.update({ 'status': 'abort', 'text': 'Your task is being aborted...' }) else: task.update({ 'status': 'fail', 'text': 'This task is in an unknown state. ' + 'Please file an issue in GitHub.' }) return task
def status(): """Get all visible task status for user.""" ids = get_tasks() goodids = [] values = [] ssus = [] hasrunning = False for id in ids: title = get_title_from_task(id) if not title: # task has been forgotten -- results should have been expired continue goodids.append(id) res = worker.main.AsyncResult(id) task = { 'id': id, 'title': title } try: state = res.state except: task.update({ 'status': 'fail', 'text': 'The status of the task could not be retrieved.', 'traceback': traceback.format_exc() }) else: if state == 'PENDING': task.update({ 'status': 'progress', 'text': 'Your task is pending...', 'progress': -1 }) hasrunning = True elif state == 'STARTED': task.update({ 'status': 'progress', 'text': 'Your task has been started; preprocessing...', 'progress': -1 }) hasrunning = True elif state == 'PROGRESS': task.update({ 'status': 'progress', 'text': res.result['text'], 'progress': res.result['percent'] }) hasrunning = True elif state == 'SUCCESS': filename, wikifileurl = res.result task.update({ 'status': 'done', 'url': wikifileurl, 'text': filename }) elif state == 'FAILURE': e = res.result if e is False: task.update({ 'status': 'fail', 'text': res.traceback, 'restartable': True }) elif isinstance(e, NeedServerSideUpload): task.update({ 'status': 'needssu', 'url': create_phab_url([e]) }) ssus.append(e) else: task.update({ 'status': 'fail', 'text': format_exception(e), 'restartable': ( (not redisconnection.exists('restarted:' + id)) and redisconnection.exists('params:' + id) ) }) elif state == 'ABORTED': task.update({ 'status': 'abort', 'text': 'Your task is being aborted...' }) hasrunning = True else: task.update({ 'status': 'fail', 'text': 'This task is in an unknown state. ' + 'Please file an issue in GitHub.' }) values.append(task) ssulink = create_phab_url(ssus) if ssus else '' return jsonify( ids=goodids, values=values, hasrunning=hasrunning, ssulink=ssulink )