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-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__)
|
|
|
|
|
|
|
|
def get_db():
|
|
|
|
db = getattr(g, '_database', None)
|
|
|
|
if db is None:
|
|
|
|
db = g._database = sqlite3.connect('decorate.db')
|
|
|
|
return db
|
|
|
|
|
|
|
|
@app.teardown_appcontext
|
|
|
|
def close_connection(exception):
|
|
|
|
db = getattr(g, '_database', None)
|
|
|
|
if db is not None:
|
|
|
|
db.close()
|
|
|
|
|
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-09 11:21:52 +08:00
|
|
|
@app.route('/', methods=['GET'])
|
|
|
|
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)
|
|
|
|
|
|
|
|
@app.route('/<path:scriptPath>', methods=['GET'])
|
|
|
|
def scriptHandle(scriptPath):
|
|
|
|
# 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,
|
|
|
|
static_css = url_for('static', filename='site.css'),
|
|
|
|
static_js = url_for('static', filename='site.js'),
|
|
|
|
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
|
|
|
|
|
|
|
def run():
|
|
|
|
app.run()
|
|
|
|
|
2020-04-03 23:57:36 +08:00
|
|
|
|