This repository has been archived by the owner on May 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bowser.py
executable file
·136 lines (106 loc) · 4.17 KB
/
bowser.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
import bottle
import time
import json
import os
import argparse
from bottle import (
route, run,
static_file,
template
)
try:
from pygments import highlight
from pygments.lexers import get_lexer_for_filename
from pygments.formatters import HtmlFormatter
from pygments.styles import get_style_by_name
USE_PYGMENTS = True
except ImportError:
USE_PYGMENTS = False
bottle.debug = True
HOST = '0.0.0.0'
PORT = '8080'
STATIC_DIR = [ 'file', '/Users/jamie/dev/' ]
SITE_NAME = 'demo site'
SHOW_DOTFILES = False
@route('/')
def index():
return static_file('./bowser.html', root='./')
@route('/static/<path:path>')
def serve_static(path):
return static_file(path, root='./static')
# XXX REMOVE Non-JS listings
@route('/listing')
def index():
return jade_template(os.path.join(TEMPLATES_DIR,'index.tpl'), title=SITE_NAME)
@route("/{0}/<path:path>".format(STATIC_DIR[0]))
def serve_file(path):
return static_file(path, root=STATIC_DIR[1])
@route("/preview/<path:path>")
def preview_file(path):
path = path.replace('//', '/')
fullpath = os.path.join(STATIC_DIR[1], path)
code = open(fullpath, 'r').read()
if USE_PYGMENTS:
try:
lexer = get_lexer_for_filename(os.path.basename(fullpath))
formatter = HtmlFormatter(style="tango", full=True)
result = highlight(code, lexer, formatter)
return result
except:
return serve_file(path)
else:
html = " <script src=\"/static/js/google-code-prettify/prettify.js\"></script>"
html += " <script src=\"http://code.jquery.com/jquery-1.7.1.min.js\"></script>"
html += "<link href=\"/static/js/google-code-prettify/prettify.css\" type=\"text/css\" rel=\"stylesheet\" />"
html += " <script>$(function() { prettyPrint() }); </script>"
html += " <pre class=\"prettyprint\"> "
html += code
html += " </pre> "
return html
@route("/dir/<path:path>")
def serve_dir(path):
if path.startswith('/'):
path = path.rpartition('/')[2]
fullpath = os.path.join(STATIC_DIR[1],path)
files = os.listdir(fullpath)
filelist, dirs = [], []
for f in files:
if os.path.isdir(os.path.join(fullpath, f)):
if not (f.startswith('.') and SHOW_DOTFILES is False):
dirs.append({'name':f, \
'numfiles':len([name for name in os.listdir(os.path.join(fullpath,f)) if os.path.isfile(os.path.join(fullpath,f,name)) ]),
'numdirs':len([name for name in os.listdir(os.path.join(fullpath,f),) if os.path.isdir(os.path.join(fullpath,f,name)) ])
})
else:
if not (f.startswith('.') and SHOW_DOTFILES is False):
fmtime = time.strftime("%m/%d/%Y %I:%M:%S %p",time.localtime(os.path.getmtime(os.path.join(fullpath,f))))
filelist.append({ 'filename' : f, 'modtime' : fmtime, 'size' : round(float(os.path.getsize(os.path.join(fullpath,f))) / (1024 * 1024),2) })
parent_dir = '/'.join(path.split('/')[:-2])
if len(path.split('/')) > 2:
if not parent_dir.endswith('/'): parent_dir = parent_dir + '/'
if not parent_dir.startswith('/'): parent_dir = '/' + parent_dir
else:
parent_dir = '/'
parent_dir = parent_dir.replace('//', '/')
path = path.replace('//', '/')
return {'parentdir': parent_dir,'curdir': path, 'breadcrumbs' : breadcrumb(path), 'files': filelist, 'dirs': dirs}
def breadcrumb(path):
crumbs = []
a,b = '',''
for p in path.strip("/").split("/"):
b = p
p = os.path.join(a,p)
a = p
crumbs.append([p,b])
return crumbs
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Share a directory over HTTP instantly.')
parser.add_argument('-d', '--dir', default=".", help='The directory to share')
parser.add_argument('-p', '--port', default="8080", type=int, help='The local server port')
args = parser.parse_args()
if args.dir:
STATIC_DIR[1] = args.dir
if args.port:
PORT = args.port
run(host=HOST, port=PORT, reloader=True)