-
Notifications
You must be signed in to change notification settings - Fork 0
/
chiptools.py
115 lines (82 loc) · 3.01 KB
/
chiptools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from datetime import datetime, timezone
import os
from shutil import rmtree
from tempfile import mkdtemp
from flask import Flask, redirect, request, render_template, make_response, session, url_for
from flask_wtf import FlaskForm, csrf
from flask_wtf.file import FileField, FileRequired
from flask_wtf.file import FileField, FileRequired
from wtforms import IntegerField, RadioField, StringField
from wtforms.validators import DataRequired, NumberRange, Optional
from werkzeug.utils import secure_filename
from flask_dropzone import Dropzone
from vwf2pwl import vwf2pwl
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
dropzone = Dropzone(app)
app.config.update(
UPLOADED_PATH = os.path.join(basedir, 'uploads'),
DELETE_TEMP_FILES = False,
# Flask-Dropzone config:
DROPZONE_ENABLE_CSRF = True,
DROPZONE_ALLOWED_FILE_CUSTOM = True,
DROPZONE_ALLOWED_FILE_TYPE = 'text/*, .vwf, .txt',
DROPZONE_REDIRECT_VIEW = 'uploaded',
)
app.secret_key = 'chiptoolskey'
csrf.CSRFProtect(app)
appname = __name__
@app.errorhandler(csrf.CSRFError)
def csrf_error(e):
return e.description, 400
@app.route('/')
def root():
return redirect(url_for('pwl'))
@app.route('/pwl', methods=('GET', 'POST'))
def pwl():
if request.method == 'POST':
f = request.files.get('file')
prefix = datetime.now(timezone.utc).strftime('%Y-%m-%dT%H%M%S.%f')
savename = prefix + '_' + secure_filename(f.filename)
filepath = os.path.join(app.config['UPLOADED_PATH'], savename)
f.save(filepath)
session['upload'] = (filepath, f.filename)
session['app'] = 'pwl'
response = 'ok'
print(f'Upload: {savename}')
else:
if 'upload' in session:
#cleanup stale session
session.pop('upload', None)
session.pop('app', None)
response = render_template('pwl.html')
return response
@app.route('/uploaded')
def uploaded():
referrer = session.pop('app', None)
(inpath, inname) = session.pop('upload', (None, None))
missing_context = ((referrer is None), (inpath is None), (inname is None))
if all(missing_context):
# not sure how we got here
return redirect(url_for('root'))
elif any(missing_context):
# anything missing is an application error
raise TypeError(f'Incomplete session referrer: {referrer}, upload: ({inpath}, {inname})')
# process the file according to the app
try:
if referrer == 'pwl':
# render from vwf to pwl file
outname = inname.replace('.vwf', '.pwl') # TODO: make better
outpath = vwf2pwl(inpath)
else:
raise TypeError(f'Unknown referrer: {referrer}')
except:
raise
doc = make_response(open(outpath, 'r').read())
doc.headers['Content-Disposition'] = "attachment; filename=%s" % outname
doc.mimetype = 'text/plain'
response = doc
if app.config['DELETE_TEMP_FILES']:
os.remove(inpath)
os.remove(outpath)
return response