Grammalecte  Check-in [944ec0bbde]

Overview
Comment:[server] GC options as parameters
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | server
Files: files | file ages | folders
SHA3-256: 944ec0bbde996bdc079768ce1522a9ba994ede7b63f1e2a2a9d34aadb10355d3
User & Date: olr on 2018-07-07 13:26:41
Other Links: manifest | tags
Context
2018-07-07
16:00
[fr] default options check-in: c11bedfccc user: olr tags: trunk, fr
13:26
[server] GC options as parameters check-in: 944ec0bbde user: olr tags: trunk, server
12:39
[server] don’t use locale ini file for grammar options check-in: 61ec0d2c15 user: olr tags: trunk, server
Changes

Modified grammalecte-cli.py from [75f47ce217] to [4a1caa6a4c].

150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
        xArgs.context = False
    if xArgs.concat_lines:
        xArgs.textformatter = False

    # grammar options
    oGrammarChecker.gce.setOptions({"html": True, "latex": True})
    if xArgs.opt_on:
        oGrammarChecker.gce.setOptions({ opt:True  for opt in xArgs.opt_on  if opt in oGrammarChecker.gce.getOptions() })
    if xArgs.opt_off:
        oGrammarChecker.gce.setOptions({ opt:False  for opt in xArgs.opt_off  if opt in oGrammarChecker.gce.getOptions() })

    # disable grammar rules
    if xArgs.rule_off:
        for sRule in xArgs.rule_off:
            oGrammarChecker.gce.ignoreRule(sRule)

    sFile = xArgs.file or xArgs.file_to_file







|

|







150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
        xArgs.context = False
    if xArgs.concat_lines:
        xArgs.textformatter = False

    # grammar options
    oGrammarChecker.gce.setOptions({"html": True, "latex": True})
    if xArgs.opt_on:
        oGrammarChecker.gce.setOptions({ opt:True  for opt in xArgs.opt_on })
    if xArgs.opt_off:
        oGrammarChecker.gce.setOptions({ opt:False  for opt in xArgs.opt_off })

    # disable grammar rules
    if xArgs.rule_off:
        for sRule in xArgs.rule_off:
            oGrammarChecker.gce.ignoreRule(sRule)

    sFile = xArgs.file or xArgs.file_to_file

Modified grammalecte-server.py from [a3f8e92dfa] to [46c6ee005c].

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
        return HOMEPAGE
        #return template("main", {})
    return SADLIFEOFAMACHINE

@app.route("/get_options/fr")
def listOptions ():
    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 ():
    #if len(lang) != 2 or lang != "fr":
    #    abort(404, "No grammar checker available for lang “" + str(lang) + "”")
    bComma = False
    dOptions = None
    sError = ""
    if request.cookies.user_id:
        if request.cookies.user_id in dUser:
            dOptions = 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:
            dOptions = dict(dServerGCOptions)  if not dOptions  else dict(dOptions)
            dOptions.update(json.loads(request.forms.options))
        except:
            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)
        sText = oGrammarChecker.generateParagraphAsJSON(i, sText, dOptions=dOptions, bEmptyIfNoErrors=True, bReturnText=bool(request.forms.tf))
        if sText:
            if bComma:
                sJSON += ",\n"
            sJSON += sText
            bComma = True
    sJSON += "\n]}\n"
    return sJSON

@app.route("/set_options/fr", method="POST")
def setOptions ():
    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))
            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:
            traceback.print_exc()







|



















|




















|







104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
        return HOMEPAGE
        #return template("main", {})
    return SADLIFEOFAMACHINE

@app.route("/get_options/fr")
def listOptions ():
    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 ():
    #if len(lang) != 2 or lang != "fr":
    #    abort(404, "No grammar checker available for lang “" + str(lang) + "”")
    bComma = False
    dOptions = None
    sError = ""
    if request.cookies.user_id:
        if request.cookies.user_id in dUser:
            dOptions = 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:
            dOptions = dict(dGCOptions)  if not dOptions  else dict(dOptions)
            dOptions.update(json.loads(request.forms.options))
        except:
            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)
        sText = oGrammarChecker.generateParagraphAsJSON(i, sText, dOptions=dOptions, bEmptyIfNoErrors=True, bReturnText=bool(request.forms.tf))
        if sText:
            if bComma:
                sJSON += ",\n"
            sJSON += sText
            bComma = True
    sJSON += "\n]}\n"
    return sJSON

@app.route("/set_options/fr", method="POST")
def setOptions ():
    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:
            traceback.print_exc()
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212


213
214
215
216







217

218
219
220
221
222
223
224
225
226
227
228
229
230








231
232
233
234
# initialisation
oGrammarChecker = grammalecte.GrammarChecker("fr", "Server")
oSpellChecker = oGrammarChecker.getSpellChecker()
oLexicographer = oGrammarChecker.getLexicographer()
oTextFormatter = oGrammarChecker.getTextFormatter()
gce = oGrammarChecker.getGCEngine()

echo("Grammalecte v{}".format(gce.version))
dServerGCOptions = gce.getOptions()
echo("Grammar options:\n" + " | ".join([ k + ": " + str(v)  for k, v in sorted(dServerGCOptions.items()) ]))
dUser = {}
userGenerator = genUserId()


def main (sHost="localhost", nPort=8080, bTestPage=False):
    # start server


    print("Python: " + sys.version)
    if bTestPage:
        global TESTPAGE
        TESTPAGE = True







    run(app, host=sHost, port=nPort)



if __name__ == '__main__':
    xParser = argparse.ArgumentParser()
    #xParser.add_argument("lang", type=str, nargs='+', help="lang project to generate (name of folder in /lang)")
    xParser.add_argument("-ht", "--host", help="host (default: localhost)", type=str)
    xParser.add_argument("-p", "--port", help="port (default: 8080)", type=int)
    xParser.add_argument("-t", "--test_page", help="page to test the server on /", action="store_true")
    #xParser.add_argument("-on", "--opt_on", nargs="+", help="activate options")
    #xParser.add_argument("-off", "--opt_off", nargs="+", help="deactivate options")
    #xParser.add_argument("-roff", "--rule_off", nargs="+", help="deactivate rules")
    xArgs = xParser.parse_args()









    sHost = xArgs.host  or  "localhost"
    nPort = xArgs.port  or  8080
    main(sHost, nPort, xArgs.test_page)








<
|
<




|

>
>
|

<

>
>
>
>
>
>
>

>








|
|
<


>
>
>
>
>
>
>
>


|
<
197
198
199
200
201
202
203

204

205
206
207
208
209
210
211
212
213
214

215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234

235
236
237
238
239
240
241
242
243
244
245
246
247

# initialisation
oGrammarChecker = grammalecte.GrammarChecker("fr", "Server")
oSpellChecker = oGrammarChecker.getSpellChecker()
oLexicographer = oGrammarChecker.getLexicographer()
oTextFormatter = oGrammarChecker.getTextFormatter()
gce = oGrammarChecker.getGCEngine()


dGCOptions = gce.getOptions()

dUser = {}
userGenerator = genUserId()


def main (sHost="localhost", nPort=8080, dOptions=None, bTestPage=False):
    # start server
    global dGCOptions
    global TESTPAGE

    if bTestPage:

        TESTPAGE = True
    if dOptions:
        oGrammarChecker.gce.setOptions(dOptions)
        dGCOptions = gce.getOptions()

    print("Python: " + sys.version)
    echo("Grammalecte v{}".format(gce.version))
    echo("Grammar options:\n" + " | ".join([ k + ": " + str(v)  for k, v in sorted(dGCOptions.items()) ]))
    run(app, host=sHost, port=nPort)



if __name__ == '__main__':
    xParser = argparse.ArgumentParser()
    #xParser.add_argument("lang", type=str, nargs='+', help="lang project to generate (name of folder in /lang)")
    xParser.add_argument("-ht", "--host", help="host (default: localhost)", type=str)
    xParser.add_argument("-p", "--port", help="port (default: 8080)", type=int)
    xParser.add_argument("-t", "--test_page", help="page to test the server on /", action="store_true")
    xParser.add_argument("-on", "--opt_on", nargs="+", help="activate options")
    xParser.add_argument("-off", "--opt_off", nargs="+", help="deactivate options")

    xArgs = xParser.parse_args()

    dOpt = None
    if xArgs.opt_on  or  xArgs.opt_off:
        dOpt = {}
        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)