Add defaults to `bin/cook.py --help` output, where appropriate.
[cookbook.git] / bin / cook.py
1 #!/usr/bin/python
2
3 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
4 #
5 # This file is part of Cookbook.
6 #
7 # Cookbook is free software: you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the
9 # Free Software Foundation, either version 3 of the License, or (at your
10 # option) any later version.
11 #
12 # Cookbook is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Cookbook.  If not, see <http://www.gnu.org/licenses/>.
19
20 import os.path
21
22 import cookbook
23 from cookbook import Cookbook
24 from cookbook.mom import MomParser
25 from cookbook.cookbook import test as test_cookbook
26
27 try:
28     import jinja2
29     import cherrypy
30     from cookbook.server import Server
31     from cookbook.server import test as test_server
32 except ImportError, e:
33     cherrypy = e
34     def test_server():
35         pass
36
37
38 if __name__ == '__main__':
39     import optparse
40     import sys
41
42     # HACK! to ensure we *always* get utf-8 output
43     p = optparse.OptionParser()
44     p.add_option('-t', '--test', dest='test', default=False,
45                  action='store_true', help='run internal tests and exit')
46     p.add_option('-m', '--mom', dest='mom', metavar='PATH',
47                  help="load Mom's cookbook")
48     p.add_option('-s', '--serve', dest='serve', default=False,
49                  action='store_true', help='serve cookbook')
50     p.add_option('-a', '--address', dest='address', default='127.0.0.1',
51                  metavar='ADDR',
52                  help='address that the server will bind to (%default)')
53     p.add_option('-p', '--port', dest='port', default='8080',
54                  metavar='PORT',
55                  help='port that the server will listen on (%default)')
56
57     options,arguments = p.parse_args()
58
59     if options.test == True:
60         test_cookbook()
61         test_server()
62         sys.exit(0)
63
64     if isinstance(cherrypy, ImportError):
65         raise cherrypy
66
67     sys.stderr.write('Loading cookbook\n')
68     if options.mom == None:
69         c = Cookbook()
70         c.load()
71     else:
72         p = MomParser()
73         c = p.parse(options.mom)
74
75     if options.serve == False:
76         sys.stderr.write('Saving cookbook\n')
77         c.save('new-recipe')
78     else:
79         #reload(sys)
80         #sys.setdefaultencoding('utf-8')
81         sys.stderr.write('Serving cookbook\n')
82         module_dir = os.path.dirname(os.path.abspath(cookbook.__file__))
83         static_dir = os.path.join(module_dir, 'static')
84         template_dir = os.path.join(module_dir, 'template')
85         config = os.path.join(module_dir, 'config')
86         s = Server(c, template_dir)
87         if cherrypy.__version__.startswith('3.'):
88             cherrypy.config.update({ # http://www.cherrypy.org/wiki/ConfigAPI
89                     'server.socket_host': options.address,
90                     'server.socket_port': int(options.port),
91                     'tools.encode.on': True,
92                     'tools.encode.encoding': 'utf8',
93                     'tools.staticdir.root': static_dir,
94                     })
95             app_config = { '/static': { 'tools.staticdir.on': True,
96                                         'tools.staticdir.dir': '', } }
97             cherrypy.quickstart(root=s, config=app_config)
98         elif cherrypy.__version__.startswith('2.'):
99             cherrypy.root = s
100             cherrypy.config.update({
101                     'server.environment': 'production',
102                     'server.socket_host': options.address,
103                     'server.socket_port': int(options.port),
104                     'encoding_filter.on': True,
105                     'encodinf_filter.encoding': 'utf8',
106                     'static_filter.on': True,
107                     'static_filter.dir': static_dir,
108                     })
109             import pprint
110             pprint.pprint(cherrypy.config.configs)
111             cherrypy.server.start()
112         s.cleanup()