2013-11-14 17:08:19 +01:00
|
|
|
'''
|
|
|
|
|
Created on 24.10.2013
|
|
|
|
|
|
|
|
|
|
@author: Philipp Rauch
|
2013-11-20 17:13:15 +01:00
|
|
|
@version: 0.6
|
2013-11-14 17:08:19 +01:00
|
|
|
'''
|
2014-01-30 16:06:14 +01:00
|
|
|
# from sys import stderr
|
2014-01-29 18:29:53 +01:00
|
|
|
from datetime import datetime
|
2014-03-24 16:27:09 +01:00
|
|
|
from flask import Flask, jsonify, make_response, request, Response
|
2014-01-29 18:29:53 +01:00
|
|
|
from socket import gethostname
|
2014-02-03 12:43:10 +01:00
|
|
|
from Config.parser import config
|
2014-01-29 18:29:53 +01:00
|
|
|
from ems import ems
|
|
|
|
|
from buffer import Buffer
|
2013-11-14 17:08:19 +01:00
|
|
|
|
2013-11-22 16:01:43 +01:00
|
|
|
### LOAD CONFIG ###
|
2014-02-03 12:43:10 +01:00
|
|
|
c = config()
|
2013-11-22 14:25:12 +01:00
|
|
|
conf = c.readConf()
|
2013-11-21 18:11:38 +01:00
|
|
|
|
|
|
|
|
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'])
|
2013-11-14 17:08:19 +01:00
|
|
|
|
2014-01-29 18:29:53 +01:00
|
|
|
#buf = Buffer()
|
2013-11-14 17:08:19 +01:00
|
|
|
|
2014-01-22 18:02:24 +01:00
|
|
|
class Request(object):
|
2014-03-17 17:25:53 +01:00
|
|
|
'''
|
|
|
|
|
request definition for buffer
|
|
|
|
|
'''
|
2014-01-29 18:29:53 +01:00
|
|
|
def __init__(self, path, time, req_id):
|
2014-01-22 18:02:24 +01:00
|
|
|
self.content = path
|
|
|
|
|
self.time = time
|
2014-01-29 18:29:53 +01:00
|
|
|
self.id = req_id
|
2014-01-22 18:02:24 +01:00
|
|
|
|
|
|
|
|
|
2014-01-30 18:42:36 +01:00
|
|
|
def REST_start():
|
|
|
|
|
api = REST()
|
2014-03-20 16:15:01 +01:00
|
|
|
if conf['config_debug']:
|
|
|
|
|
print 'REST-Thread:\t%s\n' % api
|
2014-01-30 16:06:14 +01:00
|
|
|
api.app.run(host = conf['flask_server'],
|
|
|
|
|
port = int(conf['flask_port']))
|
|
|
|
|
#debug = conf['flask_debug']
|
2014-01-22 18:02:24 +01:00
|
|
|
|
2014-01-30 18:42:36 +01:00
|
|
|
class REST(object):
|
2013-11-20 17:13:15 +01:00
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.app = Flask(__name__)
|
2014-01-30 16:06:14 +01:00
|
|
|
|
2014-01-30 11:36:00 +01:00
|
|
|
### Start Buffer ###
|
2014-01-29 18:29:53 +01:00
|
|
|
self.buf = Buffer()
|
2013-11-20 17:13:15 +01:00
|
|
|
|
2013-12-05 16:45:57 +01:00
|
|
|
### Start EMS thread ###
|
2014-01-29 18:29:53 +01:00
|
|
|
self.EMS = ems(self.buf)
|
2014-03-20 16:15:01 +01:00
|
|
|
self.EMS.start()
|
2013-12-05 16:45:57 +01:00
|
|
|
|
|
|
|
|
if conf['config_debug']:
|
|
|
|
|
print 'EMS-Thread:\t', self.EMS
|
2014-01-29 18:29:53 +01:00
|
|
|
print '\tAPI-BUFFER:\t', self.buf
|
2013-12-05 16:45:57 +01:00
|
|
|
|
2013-11-20 17:13:15 +01:00
|
|
|
### ADD URL RULES ###
|
2014-01-29 18:29:53 +01:00
|
|
|
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'])
|
2013-11-28 12:41:01 +01:00
|
|
|
self.app.add_url_rule('/<path:path>', 'get_catch_all',
|
2014-01-29 18:29:53 +01:00
|
|
|
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]:
|
2014-03-17 17:25:53 +01:00
|
|
|
args = request.args.to_dict(flat=False) # 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.
|
2014-01-29 18:29:53 +01:00
|
|
|
out = self.buf.foo(l, args)
|
2014-03-24 16:27:09 +01:00
|
|
|
elif 'point' == l[-1]:
|
|
|
|
|
del l[-1]
|
|
|
|
|
tmp = self.buf.get_point(l)
|
|
|
|
|
return Response(tmp, content_type='text/json')
|
2014-01-29 18:29:53 +01:00
|
|
|
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):
|
2014-03-17 17:25:53 +01:00
|
|
|
'''
|
|
|
|
|
build a request and add it to list
|
|
|
|
|
'''
|
2014-01-29 18:29:53 +01:00
|
|
|
notinlist = False
|
|
|
|
|
time = datetime.now()
|
|
|
|
|
req_id = Buffer._id
|
|
|
|
|
tmp = Request(path, time, req_id)
|
|
|
|
|
try:
|
2014-03-17 17:25:53 +01:00
|
|
|
Buffer.system['0_request'].index(tmp)
|
2014-01-29 18:29:53 +01:00
|
|
|
except ValueError:
|
|
|
|
|
notinlist = True
|
|
|
|
|
|
|
|
|
|
if notinlist:
|
2014-03-17 17:25:53 +01:00
|
|
|
Buffer.system['0_request'].insert(0, tmp)
|
2014-01-29 18:29:53 +01:00
|
|
|
return id
|
|
|
|
|
else:
|
|
|
|
|
return False
|