forked from swill/kb_builder
-
Notifications
You must be signed in to change notification settings - Fork 6
/
kb_web
executable file
·132 lines (114 loc) · 4.27 KB
/
kb_web
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
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
# kb_builder builts keyboard plate and case CAD files using JSON input.
#
# Copyright (C) 2015 Will Stevens (swill)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import re
import hashlib
import json
import logging
import subprocess
import sys
import time
from flask import Flask, jsonify, render_template, request
# Setup the web config
sys.path.append('src')
from kb_builder.builder import KeyboardCase
# Setup Flask
DEBUG = True
SECRET_KEY = 'development key'
app = Flask(__name__)
app.config.from_object(__name__)
## Helpers
def render_page(page_name, **args):
"""Render a page.
"""
return render_template('%s.html' % page_name, enumerate=enumerate, len=len, sorted=sorted, **args)
@app.route('/', methods=['GET'])
def root_get():
"""Returns the front page.
"""
return render_page('index')
@app.route('/', methods=['POST'])
def root_post():
data = json.loads(request.get_data())
data_hash = hashlib.sha1(json.dumps(data, sort_keys=True)).hexdigest()
builder_args = {
'formats': ['js', 'json', 'dxf'],
'export_basename': data_hash,
'switch_type': unicode(data.get('switch-type')),
'stab_type': unicode(data.get('stab-type')),
'case_type': unicode(data.get('case-type')),
'mount_holes_num': int(data.get('mount-holes-num', 0)),
'mount_holes_size': float(data.get('mount-holes-size', 4)),
'width_padding': float(data.get('width-padding', 0)),
'height_padding': float(data.get('height-padding', 0)),
'pcb_width_padding': 0, # FIXME: Add to HTML form
'pcb_height_padding': 0, # FIXME: Add to HTML form
'corners': float(data.get('fillet', 0)),
'corner_type': 'round', # FIXME: Add ability to specify beveled
'thickness': float(data.get('thickness', 1.5)),
'kerf': float(data.get('kerf', 0)),
'reinforcing': False, # FIXME: Add ability to toggle this on
'usb_inner_width': 10,
'usb_outer_width': 15,
'usb_height': 10,
'usb_offset': 0,
'keyboard_layout': data.get('layout', ''),
'oversize': [], # FIXME: Add ability to specify this
'oversize_distance': 2, # FIXME: Add ability to specify this
}
build_start = time.time()
logging.info("Processing: %s" % (data_hash))
case = KeyboardCase(**builder_args)
# Create the shape based layers
layers = (
# (layer_name, create_function)
('simple', case.init_plate),
('bottom', case.create_bottom_layer),
('middle', case.create_middle_layer)
)
for layer, create_layer in layers:
if layer in case.layers:
plate = create_layer(layer)
case.export(plate, layer)
# Create the switch based layers
for layer in ('switch', 'reinforcing', 'top'):
if layer in case.layers:
plate = case.create_switch_layer(layer)
case.export(plate, layer)
logging.info("Finished: %s" % (data_hash))
logging.info("Processing took: {0:.2f} seconds".format(time.time()-build_start))
return jsonify({
'formats': case.formats,
'plates': case.layers,
'exports': case.exports,
'width': case.width,
'height': case.height
})
if __name__ == '__main__':
# Determine what our IP is
p = subprocess.Popen(["ifconfig"], stdout=subprocess.PIPE)
ifc_resp = p.communicate()
patt = re.compile(r'inet\s*\w*\S*:\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
resp = patt.findall(ifc_resp[0])
resp.remove('127.0.0.1')
print
for ip in resp:
print 'Try connecting to: http://%s:%s/' % (ip, 8080)
print
print
# Start the server
app.run(host='0.0.0.0', port=8080, debug=True)