Index: grammalecte-server.py ================================================================== --- grammalecte-server.py +++ grammalecte-server.py @@ -1,14 +1,18 @@ #!/usr/bin/env python3 + +""" +Grammalecte server: grammar checker +""" import sys import argparse import json import traceback import time -from grammalecte.bottle import Bottle, run, request, response, template, static_file +from grammalecte.bottle import Bottle, run, request, response #, template, static_file import grammalecte import grammalecte.text as txt from grammalecte.graphspell.echo import echo @@ -85,10 +89,11 @@ TESTPAGE = False def genUserId (): + "generator: returns id as string for users" i = 0 while True: yield str(i) i += 1 @@ -96,25 +101,28 @@ app = Bottle() # GET @app.route("/") def mainPage (): + "page for testing purpose" if TESTPAGE: return HOMEPAGE #return template("main", {}) return SADLIFEOFAMACHINE @app.route("/get_options/fr") 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 dGCOptions return '{ "values": ' + json.dumps(dOptions) + ', "labels": ' + json.dumps(gce.getOptionsLabels("fr"), ensure_ascii=False) + ' }' # POST @app.route("/gc_text/fr", method="POST") def gcText (): + "parse text and returns errors in a JSON text format" #if len(lang) != 2 or lang != "fr": # abort(404, "No grammar checker available for lang “" + str(lang) + "”") bComma = False dOptions = None sError = "" @@ -126,11 +134,11 @@ response.delete_cookie("user_id", path="/") if request.forms.options: try: dOptions = dict(dGCOptions) if not dOptions else dict(dOptions) dOptions.update(json.loads(request.forms.options)) - except: + except (TypeError, json.JSONDecodeError): sError = "request options not used" sJSON = '{ "program": "grammalecte-fr", "version": "'+gce.version+'", "lang": "'+gce.lang+'", "error": "'+sError+'", "data" : [\n' for i, sText in enumerate(txt.getParagraph(request.forms.text), 1): if bool(request.forms.tf): sText = oTextFormatter.formatText(sText) @@ -143,31 +151,34 @@ sJSON += "\n]}\n" return sJSON @app.route("/set_options/fr", method="POST") def setOptions (): + "set grammar options for current user" 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(dGCOptions) 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"]) - except: + except (KeyError, json.JSONDecodeError): traceback.print_exc() return '{"error": "options not registered"}' return '{"error": "no options received"}' @app.route("/reset_options/fr", method="POST") def resetOptions (): + "default grammar options" if request.cookies.user_id and request.cookies.user_id in dUser: del dUser[request.cookies.user_id] return "done" @app.route("/format_text/fr", method="POST") def formatText (): + "apply the text formatter and returns text" return oTextFormatter.formatText(request.forms.text) #@app.route('/static/') #def server_static (filepath): # return static_file(filepath, root='./views/static') @@ -179,18 +190,19 @@ nNowMinusNHours = int(time.time()) - (int(request.forms.hours) * 60 * 60) for nUserId, dValue in dUser.items(): if dValue["time"] < nNowMinusNHours: del dUser[nUserId] return True - except: + except KeyError: traceback.print_exc() return False # ERROR @app.error(404) def error404 (error): + "404 error page" return 'Error 404.
' + str(error) # initialisation oGrammarChecker = grammalecte.GrammarChecker("fr", "Server") @@ -203,11 +215,11 @@ dUser = {} userGenerator = genUserId() def main (sHost="localhost", nPort=8080, dOptions=None, bTestPage=False): - # start server + "start server" global dGCOptions global TESTPAGE global HOMEPAGE if bTestPage: @@ -239,8 +251,9 @@ if xArgs.opt_on: dOpt = { opt:True for opt in xArgs.opt_on } if xArgs.opt_off: dOpt.update({ opt:False for opt in xArgs.opt_off }) - sHost = xArgs.host or "localhost" - nPort = xArgs.port or 8080 - main(sHost, nPort, dOpt, xArgs.test_page) + main(xArgs.host or "localhost", \ + xArgs.port or 8080, \ + dOpt, + xArgs.test_page)