manager.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # *************************************************************************
  2. # Copyright © 2015 JiangLin. All rights reserved.
  3. # File Name: db_create.py
  4. # Author:JiangLin
  5. # Mail:xiyang0807@gmail.com
  6. # Created Time: 2016-02-11 13:34:38
  7. # *************************************************************************
  8. # !/usr/bin/env python
  9. # -*- coding=UTF-8 -*-
  10. from flask_script import Manager
  11. from flask_migrate import Migrate, MigrateCommand
  12. from maple import app, db
  13. import os
  14. migrate = Migrate(app, db)
  15. manager = Manager(app)
  16. @manager.command
  17. def run():
  18. return app.run()
  19. @manager.command
  20. def init_db():
  21. """
  22. Drops and re-creates the SQL schema
  23. """
  24. db.drop_all()
  25. db.configure_mappers()
  26. db.create_all()
  27. db.session.commit()
  28. @manager.command
  29. def babel_init():
  30. pybabel = 'pybabel'
  31. os.system(pybabel +
  32. ' extract -F babel.cfg -k lazy_gettext -o messages.pot maple')
  33. os.system(pybabel + ' init -i messages.pot -d maple/LANG -l zh')
  34. os.unlink('messages.pot')
  35. @manager.command
  36. def babel_update():
  37. pybabel = 'pybabel'
  38. os.system(pybabel +
  39. ' extract -F babel.cfg -k lazy_gettext -o messages.pot maple')
  40. os.system(pybabel + ' update -i messages.pot -d maple/LANG')
  41. os.unlink('messages.pot')
  42. @manager.command
  43. def babel_compile():
  44. pybabel = 'pybabel'
  45. os.system(pybabel + ' compile -d maple/LANG')
  46. @manager.option('-h', '--host', dest='host', default='127.0.0.1')
  47. @manager.option('-p', '--port', dest='port', type=int, default=8000)
  48. @manager.option('-w', '--workers', dest='workers', type=int, default=2)
  49. def gunicorn(host, port, workers):
  50. """use gunicorn"""
  51. from gunicorn.app.base import Application
  52. class FlaskApplication(Application):
  53. def init(self, parser, opts, args):
  54. return {'bind': '{0}:{1}'.format(host, port), 'workers': workers}
  55. def load(self):
  56. return app
  57. application = FlaskApplication()
  58. return application.run()
  59. manager.add_command('db', MigrateCommand)
  60. if __name__ == '__main__':
  61. manager.run()