119 lines
3.7 KiB
Python
119 lines
3.7 KiB
Python
'''
|
|
Created on 24.10.2013
|
|
|
|
@author: Philipp Rauch
|
|
@version: 0.6
|
|
'''
|
|
# from sys import stderr
|
|
from datetime import datetime
|
|
from flask import Flask, jsonify, make_response, request
|
|
from socket import gethostname
|
|
from Config.config import Config
|
|
from ems import ems
|
|
from buffer import Buffer
|
|
|
|
### LOAD CONFIG ###
|
|
c = Config()
|
|
conf = c.readConf()
|
|
|
|
api_host = gethostname() if conf['flask_server'] == '0.0.0.0' else conf['flask_server']
|
|
api_url = 'http://%s:%s' % (api_host, conf['flask_port'])
|
|
|
|
#buf = Buffer()
|
|
|
|
class Request(object):
|
|
|
|
def __init__(self, path, time, req_id):
|
|
self.content = path
|
|
self.time = time
|
|
self.id = req_id
|
|
|
|
|
|
def REST_start():
|
|
api = REST()
|
|
print 'REST-Thread:\t%s\n' % api
|
|
api.app.run(host = conf['flask_server'],
|
|
port = int(conf['flask_port']))
|
|
#debug = conf['flask_debug']
|
|
|
|
class REST(object):
|
|
|
|
def __init__(self):
|
|
self.app = Flask(__name__)
|
|
|
|
### Start Buffer ###
|
|
self.buf = Buffer()
|
|
|
|
### Start EMS thread ###
|
|
self.EMS = ems(self.buf)
|
|
self.emsthread = self.EMS.start()
|
|
|
|
if conf['config_debug']:
|
|
print 'EMS-Thread:\t', self.EMS
|
|
print '\tAPI-BUFFER:\t', self.buf
|
|
|
|
### ADD URL RULES ###
|
|
self.app.error_handler_spec[None][400] = self.bad_reqest
|
|
self.app.error_handler_spec[None][404] = self.not_found
|
|
self.app.error_handler_spec[None][405] = self.not_allowed
|
|
self.app.error_handler_spec[None][500] = self.server_error
|
|
self.app.add_url_rule('/', 'get_root', self.get_root, methods = ['GET'])
|
|
self.app.add_url_rule('/<path:path>', 'get_catch_all',
|
|
self.get_catch_all, methods = ['GET'])
|
|
|
|
########## ERROR Handler ##########
|
|
def not_allowed(self, error):
|
|
return make_response(jsonify( { 'error': '405 Not Allowed' } ), 405)
|
|
|
|
def not_found(self, error):
|
|
return make_response(jsonify( { 'error': '404 Not Found' } ), 404)
|
|
|
|
def bad_reqest(self, error):
|
|
return make_response(jsonify( { 'error': '400 Bad Reqest' } ), 400)
|
|
|
|
def server_error(self, error):
|
|
return make_response(jsonify( { 'error': '500 Internal Server Error' } ), 500)
|
|
|
|
########## GET Handler ##########
|
|
def get_root(self):
|
|
self.buf.set_href(Buffer.system, api_url)
|
|
return jsonify( { 'system' : Buffer.system } )
|
|
|
|
def get_catch_all(self, path):
|
|
l = path.split('/')
|
|
l.insert(0, Buffer.system)
|
|
if '' == l[-1]: # if last element of list is empty
|
|
del l[-1] # remove it
|
|
out = self.buf.get_level(l)
|
|
elif 'dyn' == l[-1]:
|
|
args = request.args # returns a dictionary with a list of values for each key
|
|
# each value and each key is represented as a string
|
|
# to convert it to a dictionary use to_dict(flat=False)
|
|
# to_dict(flat=True) returns the key and the first item of the value list.
|
|
out = self.buf.foo(l, args)
|
|
else:
|
|
#req = add_reqest(path)
|
|
|
|
#while req not in Buffer.system['done']:
|
|
# pass
|
|
|
|
#Buffer.system['done'].remove(req)
|
|
out = self.buf.get_level(l)
|
|
|
|
return jsonify( out )
|
|
|
|
def add_reqest(self, path):
|
|
notinlist = False
|
|
time = datetime.now()
|
|
req_id = Buffer._id
|
|
tmp = Request(path, time, req_id)
|
|
try:
|
|
Buffer.system['request'].index(tmp)
|
|
except ValueError:
|
|
notinlist = True
|
|
|
|
if notinlist:
|
|
Buffer.system['request'].insert(0, tmp)
|
|
return id
|
|
else:
|
|
return False
|