def gcText(): "parse text and returns errors in a JSON text format" bComma = False dUserOptions = None sError = "" if request.cookies.user_id: if request.cookies.user_id in dUser: dUserOptions = dUser[request.cookies.user_id].get( "gc_options", None) response.set_cookie("user_id", request.cookies.user_id, path="/", max_age=86400) # we renew cookie for 24h else: response.delete_cookie("user_id", path="/") if request.forms.options: try: dUserOptions = dict( oGCE.getOptions()) if not dUserOptions else dict(dUserOptions) dUserOptions.update(json.loads(request.forms.options)) except (TypeError, json.JSONDecodeError): sError = "Request options not used." response.set_header("Content-Type", "application/json; charset=UTF-8") try: xFuture = xProcessPoolExecutor.submit(parseText, request.forms.text, dUserOptions, bool(request.forms.tf), sError) return xFuture.result() except (concurrent.futures.TimeoutError, concurrent.futures.CancelledError): return '{"error": "Analysis aborted (time out or cancelled)"}' except concurrent.futures.BrokenExecutor: return '{"error": "Executor broken. The server failed."}' return '{"error": "Fatal error. The server failed."}'
def resetOptions(): "default grammar options" response.set_header("Content-Type", "application/json; charset=UTF-8") if request.cookies.user_id and request.cookies.user_id in dUser: try: del dUser[request.cookies.user_id] except KeyError: return '{"error" : "Unknown user."}' return '{"message" : "Done."}'
def listOptions(): "returns grammar options in a text JSON format" sUserId = request.cookies.user_id dOptions = dUser[sUserId][ "gc_options"] if sUserId and sUserId in dUser else oGCE.getOptions() response.set_header("Content-Type", "application/json; charset=UTF-8") return '{ "values": ' + json.dumps( dOptions, ensure_ascii=False) + ', "labels": ' + json.dumps( oGCE.getOptionsLabels("fr"), ensure_ascii=False) + ' }'
def suggestPost (): response.set_header("Content-Type", "application/json; charset=UTF-8") try: xFuture = xProcessPoolExecutor.submit(suggest, request.forms.token) return xFuture.result() except (concurrent.futures.TimeoutError, concurrent.futures.CancelledError): return '{"error": "Analysis aborted (time out or cancelled)"}' except concurrent.futures.BrokenExecutor: return '{"error": "Executor broken. The server failed."}' return '{"error": "Fatal error. The server failed."}'
def setOptions (): "set grammar options for current user" response.set_header("Content-Type", "application/json; charset=UTF-8") if request.forms.options: sUserId = request.cookies.user_id if request.cookies.user_id else next(userGenerator) dOptions = dUser[sUserId]["gc_options"] if sUserId in dUser else dict(oGCE.getOptions()) try: dOptions.update(json.loads(request.forms.options)) dUser[sUserId] = { "time": int(time.time()), "gc_options": dOptions } response.set_cookie("user_id", sUserId, path="/", max_age=86400) # 24h return json.dumps(dUser[sUserId]["gc_options"], ensure_ascii=False) except (KeyError, json.JSONDecodeError): traceback.print_exc() return '{"error": "Options not registered."}' return '{"error": "No options received."}'