Grammalecte  Diff

Differences From Artifact [a3f8e92dfa]:

To Artifact [46c6ee005c]:


   104    104           return HOMEPAGE
   105    105           #return template("main", {})
   106    106       return SADLIFEOFAMACHINE
   107    107   
   108    108   @app.route("/get_options/fr")
   109    109   def listOptions ():
   110    110       sUserId = request.cookies.user_id
   111         -    dOptions = dUser[sUserId]["gc_options"]  if sUserId and sUserId in dUser  else dServerGCOptions
          111  +    dOptions = dUser[sUserId]["gc_options"]  if sUserId and sUserId in dUser  else dGCOptions
   112    112       return '{ "values": ' + json.dumps(dOptions) + ', "labels": ' + json.dumps(gce.getOptionsLabels("fr"), ensure_ascii=False) + ' }'
   113    113   
   114    114   
   115    115   # POST
   116    116   @app.route("/gc_text/fr", method="POST")
   117    117   def gcText ():
   118    118       #if len(lang) != 2 or lang != "fr":
................................................................................
   124    124           if request.cookies.user_id in dUser:
   125    125               dOptions = dUser[request.cookies.user_id].get("gc_options", None)
   126    126               response.set_cookie("user_id", request.cookies.user_id, path="/", max_age=86400) # we renew cookie for 24h
   127    127           else:
   128    128               response.delete_cookie("user_id", path="/")
   129    129       if request.forms.options:
   130    130           try:
   131         -            dOptions = dict(dServerGCOptions)  if not dOptions  else dict(dOptions)
          131  +            dOptions = dict(dGCOptions)  if not dOptions  else dict(dOptions)
   132    132               dOptions.update(json.loads(request.forms.options))
   133    133           except:
   134    134               sError = "request options not used"
   135    135       sJSON = '{ "program": "grammalecte-fr", "version": "'+gce.version+'", "lang": "'+gce.lang+'", "error": "'+sError+'", "data" : [\n'
   136    136       for i, sText in enumerate(txt.getParagraph(request.forms.text), 1):
   137    137           if bool(request.forms.tf):
   138    138               sText = oTextFormatter.formatText(sText)
................................................................................
   145    145       sJSON += "\n]}\n"
   146    146       return sJSON
   147    147   
   148    148   @app.route("/set_options/fr", method="POST")
   149    149   def setOptions ():
   150    150       if request.forms.options:
   151    151           sUserId = request.cookies.user_id  if request.cookies.user_id  else next(userGenerator)
   152         -        dOptions = dUser[sUserId]["gc_options"]  if sUserId in dUser  else dict(dServerGCOptions)
          152  +        dOptions = dUser[sUserId]["gc_options"]  if sUserId in dUser  else dict(dGCOptions)
   153    153           try:
   154    154               dOptions.update(json.loads(request.forms.options))
   155    155               dUser[sUserId] = { "time": int(time.time()), "gc_options": dOptions }
   156    156               response.set_cookie("user_id", sUserId, path="/", max_age=86400) # 24h
   157    157               return json.dumps(dUser[sUserId]["gc_options"])
   158    158           except:
   159    159               traceback.print_exc()
................................................................................
   197    197   # initialisation
   198    198   oGrammarChecker = grammalecte.GrammarChecker("fr", "Server")
   199    199   oSpellChecker = oGrammarChecker.getSpellChecker()
   200    200   oLexicographer = oGrammarChecker.getLexicographer()
   201    201   oTextFormatter = oGrammarChecker.getTextFormatter()
   202    202   gce = oGrammarChecker.getGCEngine()
   203    203   
   204         -echo("Grammalecte v{}".format(gce.version))
   205         -dServerGCOptions = gce.getOptions()
   206         -echo("Grammar options:\n" + " | ".join([ k + ": " + str(v)  for k, v in sorted(dServerGCOptions.items()) ]))
          204  +dGCOptions = gce.getOptions()
   207    205   dUser = {}
   208    206   userGenerator = genUserId()
   209    207   
   210    208   
   211         -def main (sHost="localhost", nPort=8080, bTestPage=False):
          209  +def main (sHost="localhost", nPort=8080, dOptions=None, bTestPage=False):
   212    210       # start server
   213         -    print("Python: " + sys.version)
          211  +    global dGCOptions
          212  +    global TESTPAGE
          213  +
   214    214       if bTestPage:
   215         -        global TESTPAGE
   216    215           TESTPAGE = True
          216  +    if dOptions:
          217  +        oGrammarChecker.gce.setOptions(dOptions)
          218  +        dGCOptions = gce.getOptions()
          219  +
          220  +    print("Python: " + sys.version)
          221  +    echo("Grammalecte v{}".format(gce.version))
          222  +    echo("Grammar options:\n" + " | ".join([ k + ": " + str(v)  for k, v in sorted(dGCOptions.items()) ]))
   217    223       run(app, host=sHost, port=nPort)
          224  +
   218    225   
   219    226   
   220    227   if __name__ == '__main__':
   221    228       xParser = argparse.ArgumentParser()
   222    229       #xParser.add_argument("lang", type=str, nargs='+', help="lang project to generate (name of folder in /lang)")
   223    230       xParser.add_argument("-ht", "--host", help="host (default: localhost)", type=str)
   224    231       xParser.add_argument("-p", "--port", help="port (default: 8080)", type=int)
   225    232       xParser.add_argument("-t", "--test_page", help="page to test the server on /", action="store_true")
   226         -    #xParser.add_argument("-on", "--opt_on", nargs="+", help="activate options")
   227         -    #xParser.add_argument("-off", "--opt_off", nargs="+", help="deactivate options")
   228         -    #xParser.add_argument("-roff", "--rule_off", nargs="+", help="deactivate rules")
          233  +    xParser.add_argument("-on", "--opt_on", nargs="+", help="activate options")
          234  +    xParser.add_argument("-off", "--opt_off", nargs="+", help="deactivate options")
   229    235       xArgs = xParser.parse_args()
   230    236   
          237  +    dOpt = None
          238  +    if xArgs.opt_on  or  xArgs.opt_off:
          239  +        dOpt = {}
          240  +        if xArgs.opt_on:
          241  +            dOpt = { opt:True  for opt in xArgs.opt_on }
          242  +        if xArgs.opt_off:
          243  +            dOpt.update({ opt:False  for opt in xArgs.opt_off })
          244  +
   231    245       sHost = xArgs.host  or  "localhost"
   232    246       nPort = xArgs.port  or  8080
   233         -    main(sHost, nPort, xArgs.test_page)
   234         -
          247  +    main(sHost, nPort, dOpt, xArgs.test_page)