Mercurial > hg > decoupage
diff decoupage/cli.py @ 85:3262010f7f79
add command line serving
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Sat, 28 Dec 2013 21:56:06 -0800 |
parents | 95820b36d7e3 |
children | a9f5b60006ba |
line wrap: on
line diff
--- a/decoupage/cli.py Sat Dec 28 18:12:16 2013 -0800 +++ b/decoupage/cli.py Sat Dec 28 21:56:06 2013 -0800 @@ -10,17 +10,27 @@ import optparse import os +import socket import sys +from .formatters import Sort, Up from .web import Decoupage from wsgiref import simple_server here = os.path.dirname(os.path.realpath(__file__)) -def DecoupageServer(object): +class DecoupageServer(Decoupage): """serve locally with decoupage""" - def __init__(self): - raise NotImplementedError("Do I need this?") + # TODO: deprecate; move Decoupage to a few classes + # with more flexible formatters + def __init__(self, *args, **kwargs): + Decoupage.__init__(self, **kwargs) + # default formatters + # TODO: make configurable + self._formatters = [Sort(), Up('..')] + def get_formatters(self, path): + return self._formatters + def main(args=sys.argv[1:]): @@ -30,9 +40,15 @@ parser.add_option('-p', '--port', dest='port', type='int', default=1977, help="port to serve on [DEFAULT: %default]") + parser.add_option('-a', '--address', dest='address', + default='0.0.0.0', + help="address to serve on [DEFAULT: %default]") parser.add_option('--no-reload', dest='auto_reload', action='store_false', default=True, help="do not dynamically refresh indices") + parser.add_option('--no-print-ip', dest='print_ip', + action='store_false', default=True, + help="do not print resolvable IP address") options, args = parser.parse_args(args) if not args: directory = os.getcwd() @@ -47,28 +63,35 @@ parser.print_help() parser.exit(1) + if not os.path.isdir(directory): + raise OSError("'%s' is not a directory" % directory) - if not os.path.isdir(directory): - raise ("'%s' is not a directory" % directory) - + # create WSGI app # TODO: # - allow CLI specification of formatters # - template specification - - # create WSGI app - app = Decoupage(directory=directory, - auto_reload=options.auto_reload) + app = DecoupageServer(directory=directory, + auto_reload=options.auto_reload) # create server # TODO: allow choice amongst server classes - address = '127.0.0.1' - server = simple_server.make_server(address, options.port, app) + printable_address = '127.0.0.1' if options.address == '0.0.0.0' else options.address + server = simple_server.make_server(options.address, options.port, app) print 'serving directory %s ( %s ) at \nhttp://%s:%d/' % (directory, 'file://' + directory, # XXX - address, + printable_address, options.port) + if options.print_ip: + # from http://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib + hostname = "google.com" + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.connect((hostname,80)) + hostname = s.getsockname()[0] + print "http://%s:%s/" % (hostname, options.port) + s.close() + # serve directory contents via decoupage try: server.serve_forever() except KeyboardInterrupt: