2018-05-04 13:53:29 +02:00
|
|
|
#!/usr/bin/env python3
|
2014-08-06 11:43:40 +02:00
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
2014-08-14 17:55:18 +02:00
|
|
|
import redis
|
2018-04-17 16:06:32 +02:00
|
|
|
import configparser
|
2014-08-14 17:55:18 +02:00
|
|
|
import json
|
2016-07-21 13:44:22 +02:00
|
|
|
import datetime
|
2016-08-19 16:53:46 +02:00
|
|
|
import time
|
2016-08-13 15:24:57 +02:00
|
|
|
import calendar
|
2014-12-24 15:42:20 +01:00
|
|
|
from flask import Flask, render_template, jsonify, request
|
2014-08-06 11:43:40 +02:00
|
|
|
import flask
|
2017-04-19 11:02:03 +02:00
|
|
|
import importlib
|
2014-08-26 17:33:28 +02:00
|
|
|
import os
|
2017-04-19 11:02:03 +02:00
|
|
|
from os.path import join
|
2016-07-05 16:53:03 +02:00
|
|
|
import sys
|
|
|
|
sys.path.append(os.path.join(os.environ['AIL_BIN'], 'packages/'))
|
2017-04-19 11:02:03 +02:00
|
|
|
sys.path.append('./modules/')
|
2016-07-05 16:53:03 +02:00
|
|
|
import Paste
|
2016-07-21 13:44:22 +02:00
|
|
|
from Date import Date
|
2014-12-24 15:42:20 +01:00
|
|
|
|
2016-12-09 08:46:37 +01:00
|
|
|
# Import config
|
|
|
|
import Flask_config
|
2016-08-19 13:34:02 +02:00
|
|
|
|
2016-12-09 08:46:37 +01:00
|
|
|
# CONFIG #
|
|
|
|
cfg = Flask_config.cfg
|
2016-08-24 18:00:05 +02:00
|
|
|
|
2016-12-09 08:46:37 +01:00
|
|
|
Flask_config.app = Flask(__name__, static_url_path='/static/')
|
|
|
|
app = Flask_config.app
|
2014-08-26 17:33:28 +02:00
|
|
|
|
2017-04-25 12:18:08 +02:00
|
|
|
# ========= HEADER GENERATION ========
|
|
|
|
|
|
|
|
# Get headers items that should be ignored (not displayed)
|
|
|
|
toIgnoreModule = set()
|
|
|
|
try:
|
|
|
|
with open('templates/ignored_modules.txt', 'r') as f:
|
|
|
|
lines = f.read().splitlines()
|
|
|
|
for line in lines:
|
|
|
|
toIgnoreModule.add(line)
|
|
|
|
|
|
|
|
except IOError:
|
|
|
|
f = open('templates/ignored_modules.txt', 'w')
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
2017-04-19 15:14:20 +02:00
|
|
|
# Dynamically import routes and functions from modules
|
|
|
|
# Also, prepare header.html
|
|
|
|
to_add_to_header_dico = {}
|
2017-04-19 11:02:03 +02:00
|
|
|
for root, dirs, files in os.walk('modules/'):
|
2017-04-19 15:14:20 +02:00
|
|
|
sys.path.append(join(root))
|
2017-04-25 12:18:08 +02:00
|
|
|
|
|
|
|
# Ignore the module
|
|
|
|
curr_dir = root.split('/')[1]
|
|
|
|
if curr_dir in toIgnoreModule:
|
|
|
|
continue
|
|
|
|
|
2017-04-19 15:14:20 +02:00
|
|
|
for name in files:
|
|
|
|
module_name = root.split('/')[-2]
|
|
|
|
if name.startswith('Flask_') and name.endswith('.py'):
|
|
|
|
if name == 'Flask_config.py':
|
|
|
|
continue
|
|
|
|
name = name.strip('.py')
|
|
|
|
#print('importing {}'.format(name))
|
|
|
|
importlib.import_module(name)
|
|
|
|
elif name == 'header_{}.html'.format(module_name):
|
|
|
|
with open(join(root, name), 'r') as f:
|
|
|
|
to_add_to_header_dico[module_name] = f.read()
|
2014-08-26 17:33:28 +02:00
|
|
|
|
2017-04-19 15:14:20 +02:00
|
|
|
#create header.html
|
|
|
|
complete_header = ""
|
|
|
|
with open('templates/header_base.html', 'r') as f:
|
|
|
|
complete_header = f.read()
|
|
|
|
modified_header = complete_header
|
|
|
|
|
|
|
|
#Add the header in the supplied order
|
2018-04-17 16:06:32 +02:00
|
|
|
for module_name, txt in list(to_add_to_header_dico.items()):
|
2017-04-19 15:14:20 +02:00
|
|
|
to_replace = '<!--{}-->'.format(module_name)
|
|
|
|
if to_replace in complete_header:
|
|
|
|
modified_header = modified_header.replace(to_replace, txt)
|
|
|
|
del to_add_to_header_dico[module_name]
|
|
|
|
|
|
|
|
#Add the header for no-supplied order
|
|
|
|
to_add_to_header = []
|
|
|
|
for module_name, txt in to_add_to_header_dico.items():
|
|
|
|
to_add_to_header.append(txt)
|
|
|
|
|
|
|
|
modified_header = modified_header.replace('<!--insert here-->', '\n'.join(to_add_to_header))
|
|
|
|
|
|
|
|
#Write the header.html file
|
|
|
|
with open('templates/header.html', 'w') as f:
|
|
|
|
f.write(modified_header)
|
|
|
|
|
|
|
|
|
|
|
|
# ========= JINJA2 FUNCTIONS ========
|
2016-07-05 16:53:03 +02:00
|
|
|
def list_len(s):
|
|
|
|
return len(s)
|
|
|
|
app.jinja_env.filters['list_len'] = list_len
|
|
|
|
|
2016-08-04 11:55:38 +02:00
|
|
|
|
2016-08-09 11:59:36 +02:00
|
|
|
# ========= CACHE CONTROL ========
|
|
|
|
@app.after_request
|
|
|
|
def add_header(response):
|
|
|
|
"""
|
|
|
|
Add headers to both force latest IE rendering engine or Chrome Frame,
|
|
|
|
and also to cache the rendered page for 10 minutes.
|
|
|
|
"""
|
|
|
|
response.headers['X-UA-Compatible'] = 'IE=Edge,chrome=1'
|
|
|
|
response.headers['Cache-Control'] = 'public, max-age=0'
|
|
|
|
return response
|
2016-07-21 13:44:22 +02:00
|
|
|
|
2017-04-19 15:14:20 +02:00
|
|
|
# ========== ROUTES ============
|
|
|
|
@app.route('/searchbox/')
|
|
|
|
def searchbox():
|
|
|
|
return render_template("searchbox.html")
|
|
|
|
|
|
|
|
|
2016-12-09 08:46:37 +01:00
|
|
|
# ============ MAIN ============
|
2016-07-08 10:19:24 +02:00
|
|
|
|
2014-08-06 11:43:40 +02:00
|
|
|
if __name__ == "__main__":
|
2014-08-14 17:55:18 +02:00
|
|
|
app.run(host='0.0.0.0', port=7000, threaded=True)
|