2020-04-25 12:28:57 +08:00
|
|
|
import CustomConfig
|
|
|
|
|
2020-04-09 11:21:52 +08:00
|
|
|
from flask import Flask
|
|
|
|
from flask import g
|
|
|
|
from flask import render_template
|
|
|
|
from flask import url_for
|
|
|
|
from flask import request
|
2020-04-13 15:36:37 +08:00
|
|
|
from flask import abort
|
2020-04-18 12:49:48 +08:00
|
|
|
from flask import redirect
|
2020-04-09 11:21:52 +08:00
|
|
|
|
2020-04-11 22:51:03 +08:00
|
|
|
from functools import reduce
|
2020-04-09 11:21:52 +08:00
|
|
|
import sqlite3
|
2020-04-11 22:51:03 +08:00
|
|
|
import json
|
2020-04-09 11:21:52 +08:00
|
|
|
import ServerStruct as ss
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
2020-04-18 12:49:48 +08:00
|
|
|
# =============================================database
|
2020-04-09 11:21:52 +08:00
|
|
|
def get_db():
|
|
|
|
db = getattr(g, '_database', None)
|
|
|
|
if db is None:
|
2020-04-25 12:28:57 +08:00
|
|
|
db = g._database = sqlite3.connect(CustomConfig.decorated_db)
|
2020-04-09 11:21:52 +08:00
|
|
|
return db
|
|
|
|
|
2020-08-11 13:08:20 +08:00
|
|
|
def get_env():
|
|
|
|
env = getattr(g, '_envDatabase', None)
|
|
|
|
if env is None:
|
|
|
|
env = g._envDatabase = sqlite3.connect(CustomConfig.env_db)
|
2020-08-11 14:48:04 +08:00
|
|
|
env.text_factory = lambda x: x.decode(CustomConfig.database_encoding, errors="ignore")
|
2020-08-11 13:08:20 +08:00
|
|
|
return env
|
|
|
|
|
2020-04-09 11:21:52 +08:00
|
|
|
@app.teardown_appcontext
|
|
|
|
def close_connection(exception):
|
|
|
|
db = getattr(g, '_database', None)
|
|
|
|
if db is not None:
|
|
|
|
db.close()
|
|
|
|
|
2020-04-18 12:49:48 +08:00
|
|
|
# =============================================template func
|
2020-04-11 22:51:03 +08:00
|
|
|
@app.template_global(name = 'pinDecoder')
|
2020-04-12 16:02:20 +08:00
|
|
|
def block_pin_decoder(target):
|
2020-04-12 15:19:13 +08:00
|
|
|
return json.loads(target)
|
2020-04-11 22:51:03 +08:00
|
|
|
|
2020-04-12 16:02:20 +08:00
|
|
|
@app.template_global(name = 'pinDecoder2')
|
|
|
|
def block_pin_decoder2(target):
|
|
|
|
vab = json.loads(target)
|
|
|
|
return [vab['name'], vab['type']]
|
|
|
|
|
2020-04-18 12:49:48 +08:00
|
|
|
# =============================================route
|
2020-08-11 13:08:20 +08:00
|
|
|
|
|
|
|
# =========== default
|
|
|
|
|
2020-04-09 11:21:52 +08:00
|
|
|
@app.route('/', methods=['GET'])
|
2020-04-18 12:49:48 +08:00
|
|
|
def nospecHandle():
|
|
|
|
return redirect(url_for('indexHandle'))
|
|
|
|
|
2020-08-11 13:08:20 +08:00
|
|
|
# =========== misc page
|
|
|
|
|
2020-04-18 12:49:48 +08:00
|
|
|
@app.route('/help', methods=['GET'])
|
2020-08-10 11:45:43 +08:00
|
|
|
def helpMainHandle():
|
2020-04-18 12:49:48 +08:00
|
|
|
return render_template("help.html")
|
|
|
|
|
2020-08-11 13:08:20 +08:00
|
|
|
@app.route('/about', methods=['GET'])
|
|
|
|
def aboutHandle():
|
|
|
|
return render_template("about.html", static_icon = url_for('static', filename='icon.png'))
|
|
|
|
|
|
|
|
# =========== help page
|
|
|
|
|
2020-08-10 11:45:43 +08:00
|
|
|
@app.route('/help/<path:scriptPath>', methods=['GET'])
|
|
|
|
def helpHandle(scriptPath):
|
|
|
|
if scriptPath == 'converter':
|
|
|
|
return render_template("help/converter.html",
|
|
|
|
tabcontrol_css = url_for('static', filename='tabcontrol.css'),
|
|
|
|
tabcontrol_js = url_for('static', filename='tabcontrol.js'),
|
|
|
|
converter_js = url_for('static', filename='converter.js'))
|
2020-08-10 23:00:44 +08:00
|
|
|
elif scriptPath == 'env':
|
|
|
|
return render_template("help/env.html",
|
|
|
|
tabcontrol_css = url_for('static', filename='tabcontrol.css'),
|
|
|
|
tabcontrol_js = url_for('static', filename='tabcontrol.js'),
|
|
|
|
env_js = url_for('static', filename='env.js'),
|
|
|
|
env_css = url_for('static', filename='env.css'),
|
|
|
|
database_data = ss.envDatabaseList)
|
2020-08-10 11:45:43 +08:00
|
|
|
else:
|
|
|
|
abort(404)
|
|
|
|
|
2020-08-11 13:08:20 +08:00
|
|
|
@app.route('/help/env', methods=['POST'])
|
|
|
|
def envQueryHandle():
|
|
|
|
basicReturn = {
|
|
|
|
"status": False,
|
|
|
|
"overflow": False,
|
|
|
|
"data": []
|
|
|
|
}
|
|
|
|
|
|
|
|
# check tag
|
|
|
|
queryTag = request.form['tag'];
|
|
|
|
if queryTag not in ss.legalEnvQueryKey:
|
|
|
|
return basicReturn
|
|
|
|
|
|
|
|
cur = get_env().cursor()
|
|
|
|
#try:
|
|
|
|
readyData = json.loads(request.form['data'])
|
|
|
|
fieldLength = len(readyData.keys())
|
|
|
|
if fieldLength == 0:
|
|
|
|
cur.execute("SELECT * FROM {}".format(queryTag))
|
|
|
|
else:
|
2020-08-11 14:48:04 +08:00
|
|
|
whereStatement = 'AND'.join(map(lambda x: "([" + x + "] = ?)", readyData.keys()))
|
2020-08-11 13:08:20 +08:00
|
|
|
cur.execute("SELECT * FROM {} WHERE ({})".format(queryTag, whereStatement), list(readyData.values()))
|
|
|
|
|
|
|
|
# iterate
|
|
|
|
counter = 0
|
|
|
|
for i in cur.fetchall():
|
|
|
|
if counter == 100:
|
|
|
|
basicReturn['overflow'] = True
|
|
|
|
break
|
|
|
|
basicReturn['data'].append(i)
|
|
|
|
counter+=1
|
|
|
|
|
|
|
|
basicReturn['status'] = True
|
|
|
|
#except Exception as ex:
|
|
|
|
# basicReturn['status'] = False
|
|
|
|
# basicReturn['overflow'] = False
|
|
|
|
# basicReturn['data'] = []
|
|
|
|
|
|
|
|
return basicReturn
|
|
|
|
|
|
|
|
# =========== index
|
2020-04-18 12:49:48 +08:00
|
|
|
|
|
|
|
@app.route('/index', methods=['GET'])
|
2020-04-09 11:21:52 +08:00
|
|
|
def indexHandle():
|
|
|
|
cur = get_db().cursor()
|
|
|
|
cur.execute("SELECT [graph], [graph_name], [belong_to] FROM graph WHERE [index] != -1 ORDER BY [belong_to], [index] ASC;")
|
|
|
|
data = {}
|
|
|
|
for i in cur.fetchall():
|
|
|
|
if i[2] not in data.keys():
|
|
|
|
data[i[2]] = [ss.ScriptItem(i[1], i[0])]
|
|
|
|
else:
|
|
|
|
data[i[2]].append(ss.ScriptItem(i[1], i[0]))
|
|
|
|
|
|
|
|
return render_template('index.html', scripts = data)
|
|
|
|
|
2020-08-11 13:08:20 +08:00
|
|
|
# =========== viewer
|
|
|
|
|
2020-04-18 12:49:48 +08:00
|
|
|
@app.route('/viewer/<path:scriptPath>', methods=['GET'])
|
|
|
|
def viewerHandle(scriptPath):
|
2020-04-13 15:36:37 +08:00
|
|
|
|
2020-04-09 11:21:52 +08:00
|
|
|
# comput hamburger
|
|
|
|
pathSlice = scriptPath.split('/')
|
|
|
|
cur = get_db().cursor()
|
|
|
|
cur.execute("SELECT [graph], [graph_name] FROM graph WHERE [graph] IN ({});".format(','.join(pathSlice)))
|
|
|
|
hamburgerMap = {}
|
|
|
|
hamburger = []
|
|
|
|
for i in cur.fetchall():
|
2020-04-11 22:51:03 +08:00
|
|
|
hamburgerMap[str(i[0])] = i[1]
|
|
|
|
currentHamburger = hamburgerMap[pathSlice[-1]]
|
|
|
|
|
|
|
|
for i in range(len(pathSlice) - 1):
|
|
|
|
hamburger.append(ss.HamburgerItem(hamburgerMap[pathSlice[i]], reduce(lambda x,y: x + '/' + y, pathSlice[0:i + 1], '')))
|
2020-04-09 11:21:52 +08:00
|
|
|
|
2020-04-12 15:19:13 +08:00
|
|
|
# gei w/h
|
|
|
|
cur.execute('SELECT [width], [height] FROM graph WHERE [graph] == ?', (pathSlice[-1], ))
|
|
|
|
cache = cur.fetchone()
|
|
|
|
width = cache[0]
|
|
|
|
height = cache[1]
|
|
|
|
|
2020-04-09 11:21:52 +08:00
|
|
|
# get blocks
|
2020-04-12 22:38:10 +08:00
|
|
|
cur.execute('SELECT * FROM block WHERE [belong_to_graph] == ?', (pathSlice[-1], ))
|
2020-04-09 11:21:52 +08:00
|
|
|
dbBlocks = cur.fetchall()
|
|
|
|
|
2020-04-12 15:19:13 +08:00
|
|
|
# get cells
|
2020-04-12 22:38:10 +08:00
|
|
|
cur.execute("SELECT * FROM cell WHERE [belong_to_graph] == ?", (pathSlice[-1], ))
|
2020-04-12 15:19:13 +08:00
|
|
|
dbCells = cur.fetchall()
|
|
|
|
|
|
|
|
# get links
|
2020-04-12 22:38:10 +08:00
|
|
|
cur.execute("SELECT * FROM link WHERE [belong_to_graph] == ?", (pathSlice[-1], ))
|
|
|
|
dbLinks = cur.fetchall()
|
2020-04-09 11:21:52 +08:00
|
|
|
|
|
|
|
# render
|
|
|
|
return render_template('viewer.html',
|
2020-04-11 22:51:03 +08:00
|
|
|
currentPath = scriptPath,
|
2020-04-12 15:19:13 +08:00
|
|
|
gWidth = width,
|
|
|
|
gHeight = height,
|
2020-04-09 11:21:52 +08:00
|
|
|
hamburgerHistory = hamburger,
|
2020-08-10 11:45:43 +08:00
|
|
|
viewer_css = url_for('static', filename='viewer.css'),
|
|
|
|
tabcontrol_css = url_for('static', filename='tabcontrol.css'),
|
|
|
|
viewer_js = url_for('static', filename='viewer.js'),
|
|
|
|
tabcontrol_js = url_for('static', filename='tabcontrol.js'),
|
2020-04-09 11:21:52 +08:00
|
|
|
hamburgerCurrent = currentHamburger,
|
2020-04-12 15:19:13 +08:00
|
|
|
blocks = dbBlocks,
|
2020-04-12 22:38:10 +08:00
|
|
|
cells = dbCells,
|
|
|
|
links = dbLinks)
|
2020-04-09 11:21:52 +08:00
|
|
|
|
2020-04-18 12:49:48 +08:00
|
|
|
@app.route('/viewer/<path:scriptPath>', methods=['POST'])
|
|
|
|
def actionHandle(scriptPath):
|
2020-04-13 12:35:41 +08:00
|
|
|
cache = request.form['operation']
|
|
|
|
if cache == "info":
|
2020-08-12 12:06:55 +08:00
|
|
|
return infoHandle(request.form['target'], request.form['tag'])
|
2020-04-13 12:35:41 +08:00
|
|
|
elif cache == "move":
|
|
|
|
return moveHandle(request.form['target'])
|
|
|
|
else:
|
|
|
|
abort(400)
|
|
|
|
|
2020-08-12 12:06:55 +08:00
|
|
|
def infoHandle(target, tag):
|
2020-04-13 12:35:41 +08:00
|
|
|
cur = get_db().cursor()
|
2020-08-12 12:06:55 +08:00
|
|
|
|
2020-04-13 12:35:41 +08:00
|
|
|
data = {}
|
2020-08-12 12:06:55 +08:00
|
|
|
existedSet = set()
|
2020-08-12 15:07:41 +08:00
|
|
|
if tag == '0':
|
2020-08-12 12:06:55 +08:00
|
|
|
# call from cell
|
|
|
|
cur.execute("SELECT * FROM info WHERE [target] == ?", (target, ))
|
2020-08-12 15:07:41 +08:00
|
|
|
elif tag == '1':
|
2020-08-12 12:06:55 +08:00
|
|
|
# call from bb
|
|
|
|
cur.execute("SELECT * FROM info WHERE [attach_bb] == ?", (target, ))
|
|
|
|
else:
|
2020-08-12 15:07:41 +08:00
|
|
|
return {}
|
2020-08-12 12:06:55 +08:00
|
|
|
# get data
|
2020-04-13 12:35:41 +08:00
|
|
|
for i in cur.fetchall():
|
2020-08-12 12:06:55 +08:00
|
|
|
if i[0] in existedSet:
|
|
|
|
data[i[0]]['data'].append((i[4], i[5]))
|
|
|
|
else:
|
|
|
|
existedSet.add(i[0])
|
|
|
|
data[i[0]] = {
|
|
|
|
'name': i[3],
|
|
|
|
'is_setting': True if i[2] != 0 else False,
|
2020-08-12 15:07:41 +08:00
|
|
|
'data': [(i[4], i[5])]
|
2020-08-12 12:06:55 +08:00
|
|
|
}
|
2020-04-13 12:35:41 +08:00
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
def moveHandle(target):
|
|
|
|
return {}
|
|
|
|
|
2020-04-09 11:21:52 +08:00
|
|
|
def run():
|
|
|
|
app.run()
|
2020-08-11 13:08:20 +08:00
|
|
|
|