Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import bottle | ||
|
||
app = bottle.Bottle() | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import sys, os | ||
from optparse import OptionParser | ||
import bottlecap.server as server | ||
|
||
def options_arguments_and_parser(): | ||
parser = OptionParser() | ||
parser.add_option("-p", "--port", dest="port", default=6969, | ||
type="int", | ||
help="PORT to listen on [default: %default]", metavar="PORT") | ||
parser.add_option("-a", "--addr", dest="addr", default='127.0.0.1', | ||
type="string", | ||
help="ADDR to listen on [default: %default]", metavar="ADDR") | ||
parser.add_option('-D', '--no-debug', dest="debug", default=True, | ||
action="store_false", | ||
help="Disable debug mode.") | ||
parser.add_option('-R', '--no-reload', dest="reload", default=True, | ||
action="store_false", | ||
help="Disable autoreloader") | ||
parser.add_option('-m', '--media', dest="media", | ||
default=os.path.join(os.getcwd()), | ||
action="store", type="string", | ||
help="The root of the media [default: %default]") | ||
parser.add_option('-P', '--pretend', default=False, | ||
action="store_true", | ||
help="Don't run the server, just process the arguments and report") | ||
(options, args) = parser.parse_args() | ||
return options.__dict__, args, parser | ||
|
||
|
||
def main(): | ||
options, args, parser = options_arguments_and_parser() | ||
host, port = options.pop('addr'), options.pop('port') | ||
pretend = options.pop('pretend') | ||
|
||
import bottlecap.routes.static | ||
|
||
if not pretend: | ||
import bottle | ||
from bottlecap.application import app | ||
|
||
bottle.debug(options.pop('debug', True)) | ||
app.config.update(media_root=options.pop('media')) | ||
|
||
server.run(host, port, **options) | ||
else: | ||
print "Would listen on %s:%s" % (host, port) | ||
print "-> Would mount / => %s" % options['media'] | ||
print "Options:", options | ||
|
||
|
||
if __name__ == '__main__': main() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import re, os, sys | ||
import operator | ||
|
||
import bottle | ||
|
||
from bottlecap.application import app | ||
|
||
indexes_re = [r'^index.htm.?$', r'^index.'] | ||
indexes = [re.compile(i) | ||
for i in indexes_re] | ||
|
||
def get_index_of(root): | ||
score = lambda m: m and operator.sub(*m.span()[::-1]) | ||
match_all = lambda f: (i.match(f) for i in indexes) | ||
nth = lambda n: lambda l: l[n] | ||
nth_to = lambda n, to: lambda obj: to(nth(n)(obj)) | ||
|
||
matches = [fname | ||
for (fname, score) in sorted([ (filename, max(match_all(filename))) for filename in os.listdir(root) | ||
if os.path.isfile(os.path.join(root, filename)) ], | ||
key=nth_to(1, score), reverse=True) | ||
if score] | ||
return matches and matches[0] | ||
|
||
def maybe_index(root, path): | ||
joined = os.path.join(root, path) | ||
if os.path.isdir(joined): | ||
path = get_index_of(joined) or path | ||
return path | ||
|
||
@app.get('/:path#.*?#', name='static') | ||
def static(path): | ||
root = os.path.join(app.config['media_root']) | ||
return bottle.static_file(maybe_index(root, path), | ||
root=root) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import bottle | ||
from bottlecap.application import app | ||
|
||
def run(addr, port, **kwargs): | ||
bottle.run(app=app, host=addr, port=port, | ||
**dict({'reloader': kwargs.pop('reload', True), | ||
'interval': kwargs.pop('reload_interval', 1)}, | ||
**kwargs)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env python | ||
import os, sys | ||
import errno | ||
import shutil | ||
import subprocess | ||
from setuptools import setup, find_packages | ||
from optparse import OptionParser | ||
|
||
BASEDIR = os.path.dirname(os.path.realpath(__file__)) | ||
|
||
setup( | ||
name='bottlecap', | ||
version='0.0.0', | ||
description = 'A tiny webserver', | ||
author = 'slava@hackinggibsons.com', | ||
url = 'https://github.com/sshirokov/bottlecap/', | ||
|
||
install_requires = ['bottle'], | ||
|
||
package_dir = {'': 'lib'}, | ||
packages = find_packages('lib'), | ||
|
||
entry_points = { | ||
'console_scripts': [ | ||
'bottlecap = bottlecap.bin.bcap:main', | ||
], | ||
}, | ||
) | ||
|