forked from ehoppmann/mlocate-web
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
executable file
·65 lines (57 loc) · 1.76 KB
/
app.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import flask
from subprocess import Popen, PIPE
from flask import send_from_directory
from socket import gethostname
from pipes import quote
MAX_RESULT_BYTES = 1000000 # set to -1 to disable limit
app = flask.Flask(__name__)
def getitem(obj, item, default):
if item not in obj:
return default
else:
return obj[item]
@app.route('/')
def main():
return flask.redirect('/index')
@app.route('/index')
def index():
# handle user args
args = flask.request.args
query = getitem(args, 'searchbox', '')
cs = getitem(args, 'caseSensitive', '')
hostname = gethostname()
if query == '':
resultslist = ''
results_truncated = False
else:
if cs != 'on':
cs = "-i "
else:
cs = ""
command = 'mlocate ' + cs + quote(query)
command = command.encode('utf-8')
with Popen(command, shell=True, stdout=PIPE) as proc:
outs = proc.stdout.read(MAX_RESULT_BYTES)
results = outs.splitlines()
results_truncated = len(outs) == MAX_RESULT_BYTES
if results_truncated:
results = results[:-1]
resultslist = ""
for entry in results:
resultslist = resultslist + "<li>" + entry.decode('utf-8') + "</li>"
html = flask.render_template(
'index.html',
results_truncated=results_truncated,
resultslist=resultslist,
hostname=hostname)
return html
@app.route('/css/<path:path>')
def send_css(path):
return send_from_directory('css', path)
if __name__ == '__main__':
port = 5000
app.run(debug=False,host='0.0.0.0',port=port)