comparison 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
comparison
equal deleted inserted replaced
83:78139c3cecfa 84:95820b36d7e3
1 #!/usr/bin/env python
2
3 """
4 serve directory with decoupage, e.g.
5
6 ``decoupage --port 8080 /home/jhammel/tmp``
7
8 If the directory is not specified, the current working directory is used
9 """
10
11 import optparse
12 import os
13 import sys
14
15 from .web import Decoupage
16 from wsgiref import simple_server
17
18 here = os.path.dirname(os.path.realpath(__file__))
19
20 def DecoupageServer(object):
21 """serve locally with decoupage"""
22 def __init__(self):
23 raise NotImplementedError("Do I need this?")
24
25 def main(args=sys.argv[1:]):
26
27 # parse command line options
28 usage = '%prog [options]'
29 parser = optparse.OptionParser(usage=usage, description=__doc__)
30 parser.add_option('-p', '--port', dest='port',
31 type='int', default=1977,
32 help="port to serve on [DEFAULT: %default]")
33 parser.add_option('--no-reload', dest='auto_reload',
34 action='store_false', default=True,
35 help="do not dynamically refresh indices")
36 options, args = parser.parse_args(args)
37 if not args:
38 directory = os.getcwd()
39 elif len(args) == 1:
40 directory = args[0]
41 if len(args) > 1:
42 # TODO:
43 # allow multiple directories with mount points
44 # e.g. `decoupage [options] directory [directory2=/foo] [...]`
45 # This may be done by creating a temporary directory with appropriate
46 # symbolic links (on OSes that allow them)
47
48 parser.print_help()
49 parser.exit(1)
50
51 if not os.path.isdir(directory):
52 raise ("'%s' is not a directory" % directory)
53
54 # TODO:
55 # - allow CLI specification of formatters
56 # - template specification
57
58 # create WSGI app
59 app = Decoupage(directory=directory,
60 auto_reload=options.auto_reload)
61
62
63 # create server
64 # TODO: allow choice amongst server classes
65 address = '127.0.0.1'
66 server = simple_server.make_server(address, options.port, app)
67 print 'serving directory %s ( %s ) at \nhttp://%s:%d/' % (directory,
68 'file://' + directory, # XXX
69 address,
70 options.port)
71
72 try:
73 server.serve_forever()
74 except KeyboardInterrupt:
75 pass
76
77 if __name__ == '__main__':
78 main()