gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.0KB

  1. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  2. import code
  3. import logging
  4. import os
  5. import signal
  6. import sys
  7. import threading
  8. from pathlib import Path
  9. import odoo
  10. from odoo.modules.registry import Registry
  11. from odoo.tools import config
  12. from . import Command
  13. _logger = logging.getLogger(__name__)
  14. """
  15. Shell exit behaviors
  16. ====================
  17. Legend:
  18. stop = The REPL main loop stop.
  19. raise = Exception raised.
  20. loop = Stay in REPL.
  21. Shell | ^D | exit() | quit() | sys.exit() | raise SystemExit()
  22. ----------------------------------------------------------------------
  23. python | stop | raise | raise | raise | raise
  24. ipython | stop | stop | stop | loop | loop
  25. ptpython | stop | raise | raise | raise | raise
  26. bpython | stop | stop | stop | stop | stop
  27. """
  28. def raise_keyboard_interrupt(*a):
  29. raise KeyboardInterrupt()
  30. class Console(code.InteractiveConsole):
  31. def __init__(self, locals=None, filename="<console>"):
  32. code.InteractiveConsole.__init__(self, locals, filename)
  33. try:
  34. import readline
  35. import rlcompleter
  36. except ImportError:
  37. print('readline or rlcompleter not available, autocomplete disabled.')
  38. else:
  39. readline.set_completer(rlcompleter.Completer(locals).complete)
  40. readline.parse_and_bind("tab: complete")
  41. class Shell(Command):
  42. """Start odoo in an interactive shell"""
  43. supported_shells = ['ipython', 'ptpython', 'bpython', 'python']
  44. def init(self, args):
  45. config.parser.prog = f'{Path(sys.argv[0]).name} {self.name}'
  46. config.parse_config(args, setup_logging=True)
  47. odoo.cli.server.report_configuration()
  48. odoo.service.server.start(preload=[], stop=True)
  49. signal.signal(signal.SIGINT, raise_keyboard_interrupt)
  50. def console(self, local_vars):
  51. if not os.isatty(sys.stdin.fileno()):
  52. local_vars['__name__'] = '__main__'
  53. exec(sys.stdin.read(), local_vars)
  54. else:
  55. if 'env' not in local_vars:
  56. print('No environment set, use `%s shell -d dbname` to get one.' % sys.argv[0])
  57. for i in sorted(local_vars):
  58. print('%s: %s' % (i, local_vars[i]))
  59. preferred_interface = config.options.get('shell_interface')
  60. if preferred_interface:
  61. shells_to_try = [preferred_interface, 'python']
  62. else:
  63. shells_to_try = self.supported_shells
  64. for shell in shells_to_try:
  65. try:
  66. return getattr(self, shell)(local_vars)
  67. except ImportError:
  68. pass
  69. except Exception:
  70. _logger.warning("Could not start '%s' shell." % shell)
  71. _logger.debug("Shell error:", exc_info=True)
  72. def ipython(self, local_vars):
  73. from IPython import start_ipython
  74. start_ipython(argv=[], user_ns=local_vars)
  75. def ptpython(self, local_vars):
  76. from ptpython.repl import embed
  77. embed({}, local_vars)
  78. def bpython(self, local_vars):
  79. from bpython import embed
  80. embed(local_vars)
  81. def python(self, local_vars):
  82. Console(locals=local_vars).interact()
  83. def shell(self, dbname):
  84. local_vars = {
  85. 'openerp': odoo,
  86. 'odoo': odoo,
  87. }
  88. if dbname:
  89. threading.current_thread().dbname = dbname
  90. registry = Registry(dbname)
  91. with registry.cursor() as cr:
  92. uid = odoo.SUPERUSER_ID
  93. ctx = odoo.api.Environment(cr, uid, {})['res.users'].context_get()
  94. env = odoo.api.Environment(cr, uid, ctx)
  95. local_vars['env'] = env
  96. local_vars['self'] = env.user
  97. self.console(local_vars)
  98. cr.rollback()
  99. else:
  100. self.console(local_vars)
  101. def run(self, args):
  102. self.init(args)
  103. self.shell(config['db_name'])
  104. return 0
上海开阖软件有限公司 沪ICP备12045867号-1