Commit 9e06b9fd5cff9bf0a9edb7765db03bf86d707362
1 parent
62051eda
Exists in
devel
[TranslationServer] Adiciona endpoint para versão do dict e implementa nova form…
…a para obter a lista de sinais
Showing
1 changed file
with
45 additions
and
10 deletions
Show diff stats
src/TranslationServer.py
... | ... | @@ -5,14 +5,15 @@ from flask import Flask, request, abort, send_from_directory |
5 | 5 | from flask.ext.cors import CORS |
6 | 6 | from functools import wraps |
7 | 7 | from PortGlosa import traduzir |
8 | -from subprocess import check_output | |
8 | +from subprocess import check_output, Popen | |
9 | 9 | from threading import Lock |
10 | 10 | from time import sleep |
11 | 11 | from logging.handlers import RotatingFileHandler |
12 | -import os, argparse, thread, logging, sys | |
12 | +import Trie, os, argparse, json, thread, logging, sys | |
13 | 13 | |
14 | 14 | MySQLdb=None |
15 | 15 | RUN_MODE=None |
16 | +DICT_VERSION=None | |
16 | 17 | BUNDLES_PATH=None |
17 | 18 | BUNDLES_LIST={} |
18 | 19 | conn=None |
... | ... | @@ -39,6 +40,7 @@ def dict_mode(): |
39 | 40 | STANDALONE_SIGNS_PATH=os.path.join(SIGNS_PATH, "STANDALONE") |
40 | 41 | WEBGL_SIGNS_PATH=os.path.join(SIGNS_PATH, "WEBGL") |
41 | 42 | BUNDLES_PATH={"IOS":IOS_SIGNS_PATH, "ANDROID":ANDROID_SIGNS_PATH, "STANDALONE":STANDALONE_SIGNS_PATH, "WEBGL":WEBGL_SIGNS_PATH} |
43 | + check_version() | |
42 | 44 | list_bundles() |
43 | 45 | |
44 | 46 | def connect_database(): |
... | ... | @@ -72,6 +74,10 @@ def logger(): |
72 | 74 | log.setLevel(logging.DEBUG) |
73 | 75 | log.addHandler(handler) |
74 | 76 | |
77 | +def check_version(): | |
78 | + global DICT_VERSION | |
79 | + DICT_VERSION = check_output(["aptitude", "search", "dicionario-vlibras", "-F", "%V"]) | |
80 | + | |
75 | 81 | def init_mode(args): |
76 | 82 | global RUN_MODE |
77 | 83 | if args.logfile: logger() |
... | ... | @@ -88,12 +94,14 @@ def init_mode(args): |
88 | 94 | def list_bundles(): |
89 | 95 | global BUNDLES_LIST |
90 | 96 | states = ["AC", "AL", "AP", "AM", "BA", "CE", "DF", "ES", "GO", "MA", "MT", "MS", "MG", "PA", "PB", "PR", "PE", "PI", "RJ", "RN", "RS", "RO", "RR", "SC", "SP", "SE", "TO"] |
91 | - BUNDLES_LIST["DEFAULT"] = set(list_files(BUNDLES_PATH["ANDROID"])) | |
92 | - for state in states: | |
93 | - try: | |
94 | - BUNDLES_LIST[state] = set(os.listdir(os.path.join(BUNDLES_PATH["ANDROID"], state))) | |
95 | - except OSError: | |
96 | - BUNDLES_LIST[state] = set([]) | |
97 | + BUNDLES_LIST["DEFAULT"] = check_platform_files() | |
98 | + for platform, path in BUNDLES_PATH.iteritems(): | |
99 | + BUNDLES_LIST[platform] = {} | |
100 | + for state in states: | |
101 | + try: | |
102 | + BUNDLES_LIST[platform].update({state:set(os.listdir(os.path.join(path, state)))}) | |
103 | + except OSError: | |
104 | + BUNDLES_LIST[platform].update({state:set([])}) | |
97 | 105 | |
98 | 106 | def list_files(path): |
99 | 107 | files = [] |
... | ... | @@ -103,6 +111,15 @@ def list_files(path): |
103 | 111 | files.append(fname) |
104 | 112 | return files |
105 | 113 | |
114 | +def check_platform_files(): | |
115 | + android = set(list_files(BUNDLES_PATH["ANDROID"])) | |
116 | + ios = set(list_files(BUNDLES_PATH["IOS"])) | |
117 | + webgl = set(list_files(BUNDLES_PATH["WEBGL"])) | |
118 | + standalone = set(list_files(BUNDLES_PATH["STANDALONE"])) | |
119 | + if android == ios and ios == webgl and webgl == standalone: | |
120 | + return standalone | |
121 | + raise RuntimeError("Inconsistent signs. Check files.") | |
122 | + | |
106 | 123 | def check_database(): |
107 | 124 | cursor = conn.cursor() |
108 | 125 | cursor.execute('CREATE DATABASE IF NOT EXISTS signsdb;') |
... | ... | @@ -251,9 +268,27 @@ def load_statistics_page(): |
251 | 268 | |
252 | 269 | @app.route("/update", methods=['GET']) |
253 | 270 | def update_list_bundles(): |
271 | + try: | |
272 | + password = request.args.get('password').encode('utf-8') | |
273 | + except AttributeError: | |
274 | + return "Password not provided", 401 | |
275 | + | |
276 | + Popen('sudo -S <<< '+password+' apt-get update > /dev/null 2>&1', shell=True, stdin=None, stdout=None, stderr=None, executable="/bin/bash").wait() | |
277 | + check_version() | |
254 | 278 | list_bundles() |
255 | 279 | return "Successfully updated list.", 200 |
256 | 280 | |
281 | +@app.route("/version", methods=['GET']) | |
282 | +def get_version(): | |
283 | + return DICT_VERSION, 200 | |
284 | + | |
285 | +@app.route("/signs", methods=['GET']) | |
286 | +def get_signs_list(): | |
287 | + signs = list(BUNDLES_LIST["DEFAULT"]) | |
288 | + #return json.dumps(signs), 200 | |
289 | + trie = Trie.gen(signs) | |
290 | + return json.dumps(trie), 200 | |
291 | + | |
257 | 292 | @app.route("/<platform>/<sign>", methods=['GET']) |
258 | 293 | @check_run_mode |
259 | 294 | def get_sign(platform, sign): |
... | ... | @@ -273,9 +308,9 @@ def get_sign_state(platform, state, sign): |
273 | 308 | sign = sign.encode("UTF-8") |
274 | 309 | state = state.encode("UTF-8") |
275 | 310 | if " " in sign or platform not in BUNDLES_PATH: abort(400) |
276 | - file_exists = sign in BUNDLES_LIST[state] | |
277 | - thread.start_new_thread(update_database_statistic, (sign, platform, file_exists)) | |
311 | + file_exists = sign in BUNDLES_LIST[platform][state] | |
278 | 312 | if file_exists: |
313 | + thread.start_new_thread(update_database_statistic, (sign, platform, file_exists)) | |
279 | 314 | return send_from_directory(os.path.join(BUNDLES_PATH[platform], state), sign) |
280 | 315 | return get_sign(platform.decode("UTF-8"), sign.decode("UTF-8")) |
281 | 316 | ... | ... |