-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebapp.py
executable file
·92 lines (66 loc) · 2.81 KB
/
webapp.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
#!/usr/bin/env python
# coding=utf-8
import json
from bottle import (get, request, run, route, default_app, app, static_file,
TEMPLATE_PATH, jinja2_template as template)
from utils import (build_diff_dict, with_same_pattern, get_instrument,
INSTRUMENT_CHOICES, normalize_chord)
TEMPLATE_PATH.append('./templates')
STATIC_DIR = './static'
@get('/')
def search():
return template('search.html', title='Chord search', instruments=INSTRUMENT_CHOICES)
@get('/reverse')
def reverse():
return template('rsearch.html', title='Reverse search', instruments=INSTRUMENT_CHOICES)
@get('/theory')
def links():
return template('theory.html', title='Chord theory')
@get('/search')
def search():
instrument, chord = request.query.get('instrument'), request.query.get('chord').strip()
max_fingers = request.query.get('max_fingers', None)
nchord = normalize_chord(chord.lower())
STRINGS, CHORDS = get_instrument(instrument)
by_diff = build_diff_dict(CHORDS)
matches = [(n, p) for n, p in CHORDS if any([n.lower() == nchord,
n[:len(chord)+1].lower() == nchord+'/'])]
if max_fingers:
matches = [(n, p) for n, p in matches
if len(tuple(filter(lambda x: x > 0, p))) <= int(max_fingers)]
matches = [(n, p, with_same_pattern(p, by_diff)) for n, p in matches]
return template(
'sresults.html', title='Chord search',
instrument=instrument, instruments=INSTRUMENT_CHOICES,
query=chord, max_fingers=max_fingers,
matches_json=json.dumps(matches), matches=matches,
strings_json=json.dumps(STRINGS)
)
@get('/rsearch')
def rsearch():
instrument, pattern = request.query.get('instrument'), request.query.get('pattern', '').strip()
STRINGS, CHORDS = get_instrument(instrument)
by_diff = build_diff_dict(CHORDS)
notes = tuple(map(lambda x: int(x) if x not in 'xX' else -1, pattern.split()))
matches = []
if len(notes) == len(STRINGS):
matches = [(name, patt) for name, patt in CHORDS if notes == patt]
matches = [(n, p, with_same_pattern(p, by_diff)) for n, p in matches]
return template(
'rresults.html', title='Reverse search',
instrument=instrument, instruments=INSTRUMENT_CHOICES,
query=pattern,
matches_json=json.dumps(matches), matches=matches,
strings_json=json.dumps(STRINGS)
)
@route('/static/<filename:path>')
def send_static(filename):
return static_file(filename, root=STATIC_DIR)
if __name__ == '__main__':
from werkzeug.debug import DebuggedApplication
app = app()
app.catchall = False
application = DebuggedApplication(app, evalex=True)
run(app=application, host='localhost', port=8080, reloader=True, debug=True)
else:
application = default_app()