view decoupage/cli.py @ 84:95820b36d7e3

cli client
author Jeff Hammel <jhammel@mozilla.com>
date Sat, 28 Dec 2013 18:12:16 -0800
parents
children 3262010f7f79
line wrap: on
line source

#!/usr/bin/env python

"""
serve directory with decoupage, e.g.

``decoupage --port 8080 /home/jhammel/tmp``

If the directory is not specified, the current working directory is used
"""

import optparse
import os
import sys

from .web import Decoupage
from wsgiref import simple_server

here = os.path.dirname(os.path.realpath(__file__))

def DecoupageServer(object):
    """serve locally with decoupage"""
    def __init__(self):
        raise NotImplementedError("Do I need this?")

def main(args=sys.argv[1:]):

    # parse command line options
    usage = '%prog [options]'
    parser = optparse.OptionParser(usage=usage, description=__doc__)
    parser.add_option('-p', '--port', dest='port',
                      type='int', default=1977,
                      help="port to serve on [DEFAULT: %default]")
    parser.add_option('--no-reload', dest='auto_reload',
                      action='store_false', default=True,
                      help="do not dynamically refresh indices")
    options, args = parser.parse_args(args)
    if not args:
        directory = os.getcwd()
    elif len(args) == 1:
        directory = args[0]
    if len(args) > 1:
            # TODO:
            # allow multiple directories with mount points
            #   e.g. `decoupage [options] directory [directory2=/foo] [...]`
            #   This may be done by creating a temporary directory with appropriate
            #   symbolic links (on OSes that allow them)

        parser.print_help()
        parser.exit(1)

    if not os.path.isdir(directory):
        raise ("'%s' is not a directory" % directory)

    # TODO:
    # - allow CLI specification of formatters
    # - template specification

    # create WSGI app
    app = Decoupage(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)
    print 'serving directory %s ( %s ) at \nhttp://%s:%d/' % (directory,
                                                              'file://' + directory, # XXX
                                                              address,
                                                              options.port)

    try:
        server.serve_forever()
    except KeyboardInterrupt:
        pass

if __name__ == '__main__':
    main()