forked from primeosu/apg_challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppParser.py
120 lines (94 loc) · 3.72 KB
/
AppParser.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
115
116
117
118
119
120
__author__ = "DMcHale"
import os
import csv
from sqlite3 import connect
from flask import Flask, render_template, request, flash
from flask_bootstrap import Bootstrap
from werkzeug import secure_filename
UPLOAD_FOLDER = './uploads/'
ALLOWED_EXTENSIONS = set(['csv'])
app = Flask(__name__)
Bootstrap(app)
app.secret_key = 'baseKey64'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/')
def index():
definitionsList = []
classificationTypes = []
try:
conn = connect('example.db', check_same_thread=False)
except Exception as e:
print "Unable to connect to 'example.db'"
c = conn.cursor()
try:
for row in c.execute('SELECT * FROM definitions ORDER BY classificationType'):
definitionsList.append(row)
except Exception as e:
print('Could not retrieve definitions from definitions table')
print(e)
try:
for row in c.execute(
'SELECT classificationType, COUNT(classificationType) FROM definitions GROUP BY classificationType'):
classificationTypes.append(row)
except Exception as e:
print(e)
return render_template('index.html',
definitionsList=definitionsList,
classificationTypes=classificationTypes)
@app.route('/handleUpload', methods=['POST'])
def handle_upload():
error = None
if request.method == 'POST':
file = request.files['file']
definitions = []
definitionsList = []
classificationTypes = []
try:
conn = connect('example.db', check_same_thread=False)
except Exception as e:
print "Unable to connect to 'example.db'"
c = conn.cursor()
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
with open('./uploads/' + file.filename, 'rb') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row.has_key(''):
row.pop('')
definitions.append(row)
flash('Definitions successfully ingested.')
else:
flash('Invalid file type, please upload a .csv file.')
for entry in definitions:
definitionsList.append((entry['MD5'], entry['ClassificationName'], entry['ClassificationType'],
entry['Size'], entry['FileType']))
try:
c.executemany('INSERT INTO definitions VALUES (?,?,?,?,?)', definitionsList)
conn.commit()
except Exception as e:
print('Could not insert definitions into definitions table')
print(e)
try:
for row in c.execute('SELECT * FROM definitions ORDER BY classificationType'):
definitionsList.append(row)
except Exception as e:
print('Could not retrieve definitions from definitions table')
print(e)
try:
for row in c.execute(
'SELECT classificationType, COUNT(classificationType) FROM definitions GROUP BY classificationType'):
classificationTypes.append(row)
except Exception as e:
print(e)
return render_template('index.html',
definitionsList=definitionsList,
classificationTypes=classificationTypes)
else:
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)