Skip to content

Commit

Permalink
[master] It works, I think?
Browse files Browse the repository at this point in the history
  • Loading branch information
sshirokov committed Nov 28, 2010
1 parent 417e9ae commit f69734b
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 0 deletions.
Empty file added lib/bottlecap/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions lib/bottlecap/application.py
@@ -0,0 +1,4 @@
import bottle

app = bottle.Bottle()

Empty file added lib/bottlecap/bin/__init__.py
Empty file.
51 changes: 51 additions & 0 deletions lib/bottlecap/bin/bcap.py
@@ -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.
35 changes: 35 additions & 0 deletions lib/bottlecap/routes/static.py
@@ -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)
9 changes: 9 additions & 0 deletions lib/bottlecap/server.py
@@ -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))

29 changes: 29 additions & 0 deletions setup.py
@@ -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',
],
},
)

0 comments on commit f69734b

Please sign in to comment.