shell.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # GNU MediaGoblin -- federated, autonomous media hosting
  2. # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import code
  17. from mediagoblin import mg_globals
  18. from mediagoblin.gmg_commands import util as commands_util
  19. from mediagoblin.tools.transition import DISABLE_GLOBALS
  20. def shell_parser_setup(subparser):
  21. subparser.add_argument(
  22. '--ipython', help='Use ipython',
  23. action="store_true")
  24. if DISABLE_GLOBALS:
  25. SHELL_BANNER = (
  26. "GNU MediaGoblin shell!\n"
  27. "----------------------\n"
  28. "Available vars:\n"
  29. " - app: instantiated mediagoblin application\n"
  30. " - db: database session\n"
  31. " - ctx: context object\n")
  32. else:
  33. SHELL_BANNER = (
  34. "GNU MediaGoblin shell!\n"
  35. "----------------------\n"
  36. "Available vars:\n"
  37. " - app: instantiated mediagoblin application\n"
  38. " - mg_globals: mediagoblin.globals\n"
  39. " - db: database instance\n"
  40. " - ctx: context object\n")
  41. def py_shell(**user_namespace):
  42. """
  43. Run a shell using normal python shell.
  44. """
  45. code.interact(
  46. banner=SHELL_BANNER,
  47. local=user_namespace)
  48. def ipython_shell(**user_namespace):
  49. """
  50. Run a shell for the user using ipython. Return False if there is no IPython
  51. """
  52. try:
  53. from IPython import embed
  54. except:
  55. return False
  56. embed(
  57. banner1=SHELL_BANNER,
  58. user_ns=user_namespace)
  59. return True
  60. def shell(args):
  61. """
  62. Setup a shell for the user either a normal Python shell or an IPython one
  63. """
  64. app = commands_util.setup_app(args)
  65. def run_shell(db, ctx):
  66. user_namespace = {
  67. 'mg_globals': mg_globals,
  68. 'app': app,
  69. 'db': db,
  70. "ctx": ctx}
  71. if args.ipython:
  72. ipython_shell(**user_namespace)
  73. else:
  74. # Try ipython_shell first and fall back if not available
  75. if not ipython_shell(**user_namespace):
  76. py_shell(**user_namespace)
  77. with app.gen_context() as ctx:
  78. db = ctx.db
  79. run_shell(db, ctx)