Tornado stuff

This commit is contained in:
Emily 2019-03-08 11:12:47 +01:00
parent c542c66f7e
commit dc801464ed
7 changed files with 38 additions and 59 deletions

View File

@ -1,3 +1,4 @@
{ {
"python.pythonPath": "/usr/bin/python3.6" "python.pythonPath": "/usr/bin/python3.6",
"python.linting.pylintEnabled": true
} }

View File

@ -1,7 +0,0 @@
from . import missing
from os import listdir
versions = {}
for d in [d for d in listdir(__name__) if d not in ["__init__.py", "__pycache__", "missing.py"]]:
versions[d] = __import__("%s.%s" % (__name__, d), fromlist=[""])

View File

@ -1,7 +1,5 @@
from objects import glob from objects import glob
allowed_args = ["file_hash", "file_version", "timestamp"]
def handle(request): def handle(request):
print(request) print(request)

View File

@ -1,6 +0,0 @@
from os import listdir
handlers = {}
for d in [d.rstrip(".py") for d in listdir(__name__.replace(".", "/")) if d not in ["__init__.py", "__pycache__"]]:
handlers[d] = __import__("%s.%s" % (__name__, d), fromlist=[""])

View File

@ -1,36 +1,12 @@
import tornado.gen
import tornado.web
from objects import glob from objects import glob
allowed_args = ["file_hash", "file_version", "timestamp"] allowed_args = ["file_hash", "file_version", "timestamp"]
def handle(request): def handle(requestsManager.asyncRequestHandler):
if len([x for x in request.args if x in allowed_args]) == 0: return {}
return {
"error": "Missing valid args",
"allowed": allowed_args
}
for i in range(len(allowed_args)): # Gets the first valid argument and sets it as the method handler
method = request.args.get(allowed_args[i])
method_name = allowed_args[i]
if method is not None:
break
return callback(method_name, request.args.get(method_name))
def callback(method, data): def callback(method, data):
cur = glob.sql.cursor() return None
if method is "timestamp":
cur.execute("SELECT * FROM updates WHERE timestamp <= '{}' ORDER BY timestamp DESC LIMIT 1".format(
data
))
else:
query = "SELECT * FROM updates WHERE {} = {} LIMIT 1"
if method is "file_hash":
query = "SELECT * FROM updates WHERE {} = '{}' LIMIT 1"
cur.execute(query.format(
method,
data
))
return cur.fetchone()

35
main.py
View File

@ -6,29 +6,40 @@ import tornado.ioloop
import tornado.web import tornado.web
import tornado.netutil import tornado.netutil
def make_app(): def make_app():
routes = [] """
Make tornado application instance
:return: Tornado Application instance
"""
def get_files(dir): def get_files(dir):
"""
Get all file-/directory-names in a directory with a simple blacklist
:param str: directory to read
:return: String[] of file/directory
"""
return [d for d in listdir(dir) if not d.startswith("_")] return [d for d in listdir(dir) if not d.startswith("_")]
def map_out(dir): def map_routes(dir):
"""
Map out a directory array of modules in a given directory
:param str: directory to map out and import
:return: tuple( endpoint, module.handle )[] routes
"""
routes = []
apis = get_files(dir) apis = get_files(dir)
for api in apis: for api in apis:
endpoint = ""
if api.endswith(".py"): if api.endswith(".py"):
api = api.rstrip(".py") api = api.rstrip(".py")
routes.append( routes.append(
{ (
"/%s/%s" % (dir, api): __import__("%s.%s" % (dir.replace("/", "."), api), fromlist=[""]) r"/%s/%s" % (dir, api), __import__("%s.%s" % (dir.replace("/", "."), api), fromlist=[""]).handle
}) ))
else: else:
map_out("%s/%s" % (dir, api)) routes += map_routes("%s/%s" % (dir, api))
map_out("api")
return routes return routes
r"/api/%s/%s" routes = map_routes("api")
return tornado.web.Application(routes) return tornado.web.Application(routes)

6
web/asyncTornado.py Normal file
View File

@ -0,0 +1,6 @@
import tornado
import tornado.web
import tornado.gen
class asyncRequestHandler(tornado.web.RequestHandler):