Index: grammalecte-server.py ================================================================== --- grammalecte-server.py +++ grammalecte-server.py @@ -1,10 +1,11 @@ #!/usr/bin/env python3 -import sys -import os.path -import argparse +""" +GRAMMALECTE SERVER +""" + import json import traceback import configparser import time @@ -19,11 +20,11 @@ - +

Grammalecte · Serveur

INFORMATIONS

@@ -49,11 +50,11 @@

Remise à zéro de ses options

[adresse_serveur]:8080/reset_options/fr (POST)

TEST

- +

Analyse

Texte à analyser :

@@ -91,10 +92,11 @@ I'm doomed, but you are not. You can get out of here. """ def getServerOptions (): + "load server options in , returns server options as dictionary" xConfig = configparser.SafeConfigParser() try: xConfig.read("grammalecte-server-options._global.ini") dOpt = xConfig._sections['options'] except: @@ -101,11 +103,12 @@ echo("Options file [grammalecte-server-options._global.ini] not found or not readable") exit() return dOpt -def getConfigOptions (sLang): +def getLangConfigOptions (sLang): + "load options for language , returns grammar checker options as dictionary" xConfig = configparser.SafeConfigParser() try: xConfig.read("grammalecte-server-options." + sLang + ".ini") except: echo("Options file [grammalecte-server-options." + sLang + ".ini] not found or not readable") @@ -118,10 +121,11 @@ exit() return dGCOpt def genUserId (): + "generator: create a user id" i = 0 while True: yield str(i) i += 1 @@ -135,11 +139,11 @@ oTextFormatter = oGrammarChecker.getTextFormatter() gce = oGrammarChecker.getGCEngine() echo("Grammalecte v{}".format(gce.version)) dServerOptions = getServerOptions() - dGCOptions = getConfigOptions("fr") + dGCOptions = getLangConfigOptions("fr") if dGCOptions: gce.setOptions(dGCOptions) dServerGCOptions = gce.getOptions() echo("Grammar options:\n" + " | ".join([ k + ": " + str(v) for k, v in sorted(dServerGCOptions.items()) ])) dUser = {} @@ -148,25 +152,28 @@ app = Bottle() # GET @app.route("/") def mainPage (): + "show main page" if dServerOptions.get("testpage", False) == "True": return HOMEPAGE #return template("main", {}) return SADLIFEOFAMACHINE @app.route("/get_options/fr") def listOptions (): + "show language options as JSON string" sUserId = request.cookies.user_id dOptions = dUser[sUserId]["gc_options"] if sUserId and sUserId in dUser else dServerGCOptions 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 sent via POST, show result as a JSON string" #if len(lang) != 2 or lang != "fr": # abort(404, "No grammar checker available for lang “" + str(lang) + "”") bComma = False dOptions = None sError = "" @@ -195,10 +202,11 @@ sJSON += "\n]}\n" return sJSON @app.route("/set_options/fr", method="POST") def setOptions (): + "change options for user_id, returns options as a JSON string" 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(dServerGCOptions) try: dOptions.update(json.loads(request.forms.options)) @@ -210,16 +218,18 @@ return '{"error": "options not registered"}' return '{"error": "no options received"}' @app.route("/reset_options/fr", method="POST") def resetOptions (): + "erase options stored for user_id" 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 (): + "returns text modified via the text formatter" return oTextFormatter.formatText(request.forms.text) #@app.route('/static/') #def server_static (filepath): # return static_file(filepath, root='./views/static') @@ -234,19 +244,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 "done" - else: - return "no" + return "no" except: traceback.print_exc() return "error" # ERROR @app.error(404) def error404 (error): + "show error when error 404" return 'Error 404.
' + str(error) run(app, \ host=dServerOptions.get('host', 'localhost'), \ port=int(dServerOptions.get('port', 8080)))