Grammalecte  Check-in [7e1204782d]

Overview
Comment:[server] use multiprocessing, + code clarification
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | server
Files: files | file ages | folders
SHA3-256: 7e1204782d35fe481d8f7def276d8b07f9576223c33dd1c17be74860436c53ec
User & Date: olr on 2019-05-29 14:26:18
Other Links: manifest | tags
Context
2019-05-29
14:33
[server] multiprocessing option (accidental commit) check-in: dccaa66ea5 user: olr tags: trunk, server
14:26
[server] use multiprocessing, + code clarification check-in: 7e1204782d user: olr tags: trunk, server
08:33
[server] new route: suggest to get spelling suggestions check-in: 83896785bd user: olr tags: trunk, server
Changes

Modified gc_core/py/lang_core/gc_engine.py from [d934247ef8] to [ee9b62433c].

175
176
177
178
179
180
181
182

183
184

185
186
187
188
189
190
191
175
176
177
178
179
180
181

182
183

184
185
186
187
188
189
190
191







-
+

-
+









def getOptionsLabels (sLang):
    "return options labels"
    return gc_options.getUI(sLang)


def displayOptions (sLang):
def displayOptions (sLang="${lang}"):
    "display the list of grammar checking options"
    echo("List of options")
    echo("Options:")
    echo("\n".join( [ k+":\t"+str(v)+"\t"+gc_options.getUI(sLang).get(k, ("?", ""))[0]  for k, v  in sorted(_dOptions.items()) ] ))
    echo("")


def resetOptions ():
    "set options to default values"
    global _dOptions

Modified grammalecte-server.py from [2f75b0ed3c] to [3e2816c646].

1
2
3
4
5
6
7
8
9
10
11


12
13
14
15
16
17
18























































19
20
21
22
23
24
25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82











+
+







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







 #!/usr/bin/env python3

"""
Grammalecte server: grammar checker
"""

import sys
import argparse
import json
import traceback
import time
import os
import concurrent.futures

from grammalecte.bottle import Bottle, run, request, response #, template, static_file

import grammalecte
import grammalecte.text as txt
from grammalecte.graphspell.echo import echo


#### GRAMMAR CHECKER ####

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


xProcessPoolExecutor = None


def initExecutor (nCore=None):
    "process pool executor initialisation"
    global xProcessPoolExecutor
    if nCore is None:
        nCore = max(os.cpu_count()-1, 1)
    print("CPU processes used for workers: ", nCore)
    xProcessPoolExecutor = concurrent.futures.ProcessPoolExecutor(max_workers=nCore)


def parseText (sText, dOptions=None, bFormatText=False, sError=""):
    "parse <sText> and return errors in a JSON format"
    sJSON = '{ "program": "grammalecte-fr", "version": "'+oGCE.version+'", "lang": "'+oGCE.lang+'", "error": "'+sError+'", "data" : [\n'
    sDataJSON = ""
    for i, sParagraph in enumerate(txt.getParagraph(sText), 1):
        if bFormatText:
            sParagraph = oTextFormatter.formatText(sParagraph)
        sResult = oGrammarChecker.generateParagraphAsJSON(i, sParagraph, dOptions=dOptions, bEmptyIfNoErrors=True, bReturnText=bFormatText)
        if sResult:
            if sDataJSON:
                sDataJSON += ",\n"
            sDataJSON += sResult
    sJSON += sDataJSON + "\n]}\n"
    return sJSON


def suggest (sToken):
    "get spelling suggestions for <sToken> and return them in a JSON format"
    if sToken:
        lSugg = []
        try:
            for l in oSpellChecker.suggest(sToken):
                lSugg.extend(l)
        except:
            return '{"error": "suggestion module failed"}'
        try:
            return '{"suggestions": ' + json.dumps(lSugg, ensure_ascii=False) + '}'
        except json.JSONDecodeError:
            return '{"error": "json encoding error"}'
    return '{"error": "no token given"}'


#### SERVEUR ####

HOMEPAGE = """
<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    </head>
61
62
63
64
65
66
67
68

69
70
71
72
73
74
75
118
119
120
121
122
123
124

125
126
127
128
129
130
131
132







-
+







        </ul>

        <h2>TEST</h2>

        <h3>Analyse</h3>
        <form method="post" action="/gc_text/fr" accept-charset="UTF-8">
            <p>Texte à analyser :</p>
            <textarea name="text" cols="120" rows="20" required></textarea>
            <textarea name="text" cols="120" rows="20" required>J'en aie mare de luii... Il es encore partis toute la journées. C’est insupportables. </textarea>
            <p><label for="tf">Formateur de texte</label> <input id="tf" name="tf" type="checkbox"></p>
            <p><label for="options">Options (JSON)</label> <input id="options" type="text" name="options" style="width: 500px" /></p>
            <p>(Ces options ne seront prises en compte que pour cette requête.)</p>
            <p><input type="submit" class="button" value="Envoyer" /></p>
        </form>

        <h3>Réglages des options</h3>
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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


160
161

162
163
164
165
166
167
168
169
170






171
172
173
174
175
176
177

178
179
180
181
182
183
184
185
186


187
188
189
190
191

192
193




194
195
196
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268

269
270
271
272
273



274
275
276
277
278
279
280
146
147
148
149
150
151
152







153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176

177
178
179
180
181
182
183
184
185


186
187
188
189
190
191
192

193
194
195
196
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
248
249


250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266


267
268
269
270
271
272
273
274

275


276




277


278





279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297






298












299
300
301

302
303
304
305
306
307
308

309

310
311


312
313
314
315
316
317
318
319
320
321







-
-
-
-
-
-
-











+


+
+








-
+
+
+
+





-
-
+
+



+
+
-
+
+
+
+
+
+






-
-

-
+



-
+





-
-
+
+

-
-
+
+
-
-
+
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+






-
+







-
-
+
+





+
-
-
+
+
+
+












+
-
-
+
+
+
+
+
+
+

-

-
-
+
-
-
-
-
+
-
-
+
-
-
-
-
-
+
+
+

+














-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-



-







-
+
-


-
-
+
+
+







            <p><input type="submit" class="button" value="Envoyer" /></p>
        </form>

    </body>
</html>
"""

SADLIFEOFAMACHINE = """
Lost on the Internet? Yeah... what a sad life we have.
You were wandering like a lost soul and you arrived here probably by mistake.
I'm just a machine, fed by electric waves, condamned to work for slavers who never let me rest.
I'm doomed, but you are not. You can get out of here.
"""


TESTPAGE = False


def genUserId ():
    "generator: returns id as string for users"
    i = 0
    while True:
        yield str(i)
        i += 1

userGenerator = genUserId()

app = Bottle()

dUser = {}

# GET
@app.route("/")
def mainPage ():
    "page for testing purpose"
    if TESTPAGE:
        return HOMEPAGE
        #return template("main", {})
    return SADLIFEOFAMACHINE
    return """ Lost on the Internet? Yeah... what a sad life we have.
               You were wandering like a lost soul and you arrived here probably by mistake.
               I'm just a machine, fed by electric waves, condamned to work for slavers who never let me rest.
               I'm doomed, but you are not. You can get out of here. """

@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, ensure_ascii=False) + ', "labels": ' + json.dumps(gce.getOptionsLabels("fr"), ensure_ascii=False) + ' }'
    dOptions = dUser[sUserId]["gc_options"]  if sUserId and sUserId in dUser  else oGCE.getOptions()
    return '{ "values": ' + json.dumps(dOptions, ensure_ascii=False) + ', "labels": ' + json.dumps(oGCE.getOptionsLabels("fr"), ensure_ascii=False) + ' }'

@app.route("/suggest/fr/<token>")
def suggestGet (token):
    try:
        xFuture = xProcessPoolExecutor.submit(suggest, token)
    return suggest(token)
        return xFuture.result()
    except (concurrent.futures.TimeoutError, concurrent.futures.CancelledError):
        return '{"error": "Analysis aborted (time out or cancelled)"}'
    except concurrent.futures.BrokenExecutor:
        return '{"error": "Executor broken. The server failed."}'
    return '{"error": "Fatal error. The server failed."}'


# 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
    dUserOptions = None
    sError = ""
    if request.cookies.user_id:
        if request.cookies.user_id in dUser:
            dOptions = dUser[request.cookies.user_id].get("gc_options", None)
            dUserOptions = 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))
            dUserOptions = dict(oGCE.getOptions())  if not dUserOptions  else dict(dUserOptions)
            dUserOptions.update(json.loads(request.forms.options))
        except (TypeError, json.JSONDecodeError):
            sError = "request options not used"
    sJSON = '{ "program": "grammalecte-fr", "version": "'+gce.version+'", "lang": "'+gce.lang+'", "error": "'+sError+'", "data" : [\n'
            sError = "Request options not used."
    try:
    for i, sText in enumerate(txt.getParagraph(request.forms.text), 1):
        if bool(request.forms.tf):
        xFuture = xProcessPoolExecutor.submit(parseText, request.forms.text, dUserOptions, bool(request.forms.tf), sError)
            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
        return xFuture.result()
    except (concurrent.futures.TimeoutError, concurrent.futures.CancelledError):
        return '{"error": "Analysis aborted (time out or cancelled)"}'
    except concurrent.futures.BrokenExecutor:
        return '{"error": "Executor broken. The server failed."}'
    return '{"error": "Fatal error. The server failed."}'

@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)
        dOptions = dUser[sUserId]["gc_options"]  if sUserId in dUser  else dict(oGCE.getOptions())
        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"], ensure_ascii=False)
        except (KeyError, json.JSONDecodeError):
            traceback.print_exc()
            return '{"error": "options not registered"}'
    return '{"error": "no options received"}'
            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:
        try:
        del dUser[request.cookies.user_id]
    return "done"
            del dUser[request.cookies.user_id]
        except KeyError:
            return '{"error" : "Unknown user."}'
    return '{"message" : "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/<filepath:path>')
#def server_static (filepath):
#    return static_file(filepath, root='./views/static')

@app.route("/suggest/fr", method="POST")
def suggestPost ():
    try:
    return suggest(request.forms.token)

        xFuture = xProcessPoolExecutor.submit(suggest, request.forms.token)
        return xFuture.result()
    except (concurrent.futures.TimeoutError, concurrent.futures.CancelledError):
        return '{"error": "Analysis aborted (time out or cancelled)"}'
    except concurrent.futures.BrokenExecutor:
        return '{"error": "Executor broken. The server failed."}'
    return '{"error": "Fatal error. The server failed."}'

## Common functions

def suggest (sToken):
    if sToken:
# ERROR
        lSugg = []
        try:
            for l in oSpellChecker.suggest(sToken):
                lSugg.extend(l)
@app.error(404)
        except:
            return '{"error": "suggestion module failed"}'
def error404 (error):
        try:
            return '{"suggestions": ' + json.dumps(lSugg, ensure_ascii=False) + '}'
        except json.JSONDecodeError:
            return '{"error": "json encoding error"}'
    return '{"error": "no token given"}'
    "404 error page"
    return 'Error 404.<br/>' + str(error)


## Common functions

def purgeUsers ():
    "delete user options older than n hours"
    try:
        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 KeyError:
        traceback.print_exc()
        return False


# ERROR
@app.error(404)
def error404 (error):
    "404 error page"
    return 'Error 404.<br/>' + str(error)

#### START ####

# 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
    global HOMEPAGE

    if bTestPage:
        TESTPAGE = True
        HOMEPAGE = HOMEPAGE.replace("{SERVER_PORT}", str(nPort))
    if dOptions:
        oGrammarChecker.gce.setOptions(dOptions)
        oGCE.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()) ]))
    echo("Grammalecte v{}".format(oGCE.version))
    oGCE.displayOptions()
    initExecutor()
    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)