3 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
5 # This file is part of Cookbook.
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.
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.
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/>.
23 from cookbook import Cookbook
24 from cookbook.mom import MomParser
25 from cookbook.cookbook import test as test_cookbook
29 from cookbook.server import Server
30 from cookbook.server import test as test_server
31 except ImportError, e:
37 if __name__ == '__main__':
41 # HACK! to ensure we *always* get utf-8 output
42 p = optparse.OptionParser()
43 p.add_option('-t', '--test', dest='test', default=False,
44 action='store_true', help='run internal tests and exit')
45 p.add_option('-m', '--mom', dest='mom', metavar='PATH',
46 help="load Mom's cookbook")
47 p.add_option('-s', '--serve', dest='serve', default=False,
48 action='store_true', help='serve cookbook')
49 p.add_option('-a', '--address', dest='address', default='127.0.0.1',
50 metavar='ADDR', help='address that the server will bind to')
51 p.add_option('-p', '--port', dest='port', default='8080',
52 metavar='PORT', help='port that the server will listen on')
54 options,arguments = p.parse_args()
56 if options.test == True:
61 if isinstance(cherrypy, ImportError):
64 sys.stderr.write('Loading cookbook\n')
65 if options.mom == None:
70 c = p.parse(options.mom)
72 if options.serve == False:
73 sys.stderr.write('Saving cookbook\n')
77 #sys.setdefaultencoding('utf-8')
78 sys.stderr.write('Serving cookbook\n')
79 module_dir = os.path.dirname(os.path.abspath(cookbook.__file__))
80 static_dir = os.path.join(module_dir, 'static')
81 template_dir = os.path.join(module_dir, 'template')
82 config = os.path.join(module_dir, 'config')
83 s = Server(c, template_dir)
84 cherrypy.config.update({ # http://www.cherrypy.org/wiki/ConfigAPI
85 'server.socket_host': options.address,
86 'server.socket_port': int(options.port),
87 'tools.encode.on': True,
88 'tools.encode.encoding': 'utf8',
89 'tools.staticdir.root': static_dir
91 app_config = { '/static': { 'tools.staticdir.on': True,
92 'tools.staticdir.dir': '', } }
93 cherrypy.quickstart(root=s, config=app_config)
94 sys.stderr.write('Saving cookbook\n')