PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/main.py

https://github.com/eklitzke/toffee
Python | 50 lines | 40 code | 8 blank | 2 comment | 8 complexity | a8848667fd31f59fe7669d4fb5de918d MD5 | raw file
  1. import sys
  2. import os
  3. import optparse
  4. import yaml
  5. import sqlalchemy
  6. import tornado.httpserver
  7. import tornado.ioloop
  8. import tornado.web
  9. import toffee
  10. import toffee.db
  11. import toffee.uimodules
  12. if __name__ == '__main__':
  13. parser = optparse.OptionParser()
  14. parser.add_option('-p', '--port', dest='port', default=0, type='int', help='the port to listen on')
  15. parser.add_option('-t', '--threads', dest='threads', default=4, type='int', help='how many threads to spawn')
  16. parser.add_option('-c', '--config', dest='config', default='config.yaml', help='Configuration file to use')
  17. parser.add_option('--memory', dest='memory_db', action='store_true', default=False, help='Load a SQLite database from memory')
  18. opts, args = parser.parse_args()
  19. settings = {}
  20. if os.path.exists(opts.config):
  21. config = yaml.load(open(opts.config))
  22. else:
  23. print >> sys.stderr, 'no config file specified (or path did not exist), exiting'
  24. sys.exit(1)
  25. settings.update(config)
  26. settings['ui_modules'] = toffee.uimodules.all_uimodules
  27. # Set up the database. You may pass in --memory as a command line option to
  28. # load an empty SQLite database into memory.
  29. if opts.memory_db:
  30. engine = sqlalchemy.create_engine('sqlite:///:memory:', echo=settings['debug'])
  31. else:
  32. engine = sqlalchemy.create_engine(settings['sqlite_db'], echo=settings['debug'])
  33. toffee.db.bind_engine(engine)
  34. application = tornado.web.Application(toffee.routes, **settings)
  35. if opts.port:
  36. port = opts.port
  37. else:
  38. port = settings.get('port', 8000)
  39. for x in xrange(opts.threads):
  40. server = tornado.httpserver.HTTPServer(application)
  41. server.listen(port + x)
  42. tornado.ioloop.IOLoop.instance().start()