Commit c1f81c19c8a00285153322e17df840e799f918f8

Authored by Erickson Silva
1 parent 5977687e
Exists in devel

[TranslationServer] Adiciona rota para regionalismo

Showing 1 changed file with 27 additions and 5 deletions   Show diff stats
src/TranslationServer.py
... ... @@ -7,13 +7,14 @@ from functools import wraps
7 7 from PortGlosa import traduzir
8 8 from subprocess import check_output
9 9 from threading import Lock
  10 +from time import sleep
10 11 from logging.handlers import RotatingFileHandler
11 12 import os, argparse, thread, logging, sys
12 13  
13 14 MySQLdb=None
14 15 RUN_MODE=None
15 16 BUNDLES_PATH=None
16   -BUNDLES_LIST=None
  17 +BUNDLES_LIST={}
17 18 conn=None
18 19 lock = Lock()
19 20 app=Flask(__name__, static_url_path="", static_folder="/var/www/")
... ... @@ -86,7 +87,21 @@ def init_mode(args):
86 87  
87 88 def list_bundles():
88 89 global BUNDLES_LIST
89   - BUNDLES_LIST = set(os.listdir(BUNDLES_PATH["STANDALONE"]))
  90 + 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["STANDALONE"]))
  92 + for state in states:
  93 + try:
  94 + BUNDLES_LIST[state] = set(os.listdir(os.path.join(BUNDLES_PATH["STANDALONE"], state)))
  95 + except OSError:
  96 + pass
  97 +
  98 +def list_files(path):
  99 + files = []
  100 + for fname in os.listdir(path):
  101 + path_mount = os.path.join(path, fname)
  102 + if not os.path.isdir(path_mount):
  103 + files.append(fname)
  104 + return files
90 105  
91 106 def check_database():
92 107 cursor = conn.cursor()
... ... @@ -241,16 +256,23 @@ def update_list_bundles():
241 256  
242 257 @app.route("/<platform>/<sign>", methods=['GET'])
243 258 @check_run_mode
244   -def get_sign(platform, sign):
  259 +def get_sign(platform, sign, state=None):
245 260 platform = platform.encode("UTF-8")
246 261 sign = sign.encode("UTF-8")
247 262 if " " in sign or platform not in BUNDLES_PATH: abort(400)
248   - file_exists = sign in BUNDLES_LIST
  263 + file_exists = sign in BUNDLES_LIST["DEFAULT"] if state == None else sign in BUNDLES_LIST[state]
249 264 thread.start_new_thread(update_database_statistic, (sign, platform, file_exists))
250   - if file_exists:
  265 + if file_exists and state is not None:
  266 + return send_from_directory(os.path.join(BUNDLES_PATH[platform], state), sign)
  267 + elif file_exists or (sign in BUNDLES_LIST["DEFAULT"] and state is not None):
251 268 return send_from_directory(BUNDLES_PATH[platform], sign)
252 269 abort(404)
253 270  
  271 +@app.route("/<platform>/<state>/<sign>", methods=['GET'])
  272 +@check_run_mode
  273 +def get_sign_localism(platform, state, sign):
  274 + return get_sign(platform, sign, state)
  275 +
254 276 if __name__ == "__main__":
255 277 parser = argparse.ArgumentParser(description='Translation server and signs download for VLibras.')
256 278 parser.add_argument('--port', help='Port where the server will be available.', default=3000)
... ...