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
|
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
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(dServerGCOptions) if not dOptions else dict(dOptions)
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(dServerGCOptions)
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
|
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()
echo("Grammalecte v{}".format(gce.version))
dServerGCOptions = gce.getOptions()
dGCOptions = 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):
def main (sHost="localhost", nPort=8080, dOptions=None, bTestPage=False):
# start server
global dGCOptions
global TESTPAGE
print("Python: " + sys.version)
if bTestPage:
global TESTPAGE
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")
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()
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, xArgs.test_page)
main(sHost, nPort, dOpt, xArgs.test_page)
|