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.

89 lines
3.1 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  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
  6. # published by the Free Software Foundation, either version 3 of the
  7. # License, or (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. #
  17. ##############################################################################
  18. from __future__ import print_function
  19. import code
  20. import logging
  21. import os
  22. import signal
  23. import sys
  24. import openerp
  25. from openerp.api import Environment
  26. from openerp.cli import Command
  27. def raise_keyboard_interrupt(*a):
  28. raise KeyboardInterrupt()
  29. class Console(code.InteractiveConsole):
  30. def __init__(self, locals=None, filename="<console>"):
  31. code.InteractiveConsole.__init__(self, locals, filename)
  32. try:
  33. import readline
  34. import rlcompleter
  35. except ImportError:
  36. print('readline or rlcompleter not available,'
  37. ' 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. def init(self, args):
  44. openerp.tools.config.parse_config(args)
  45. openerp.cli.server.report_configuration()
  46. openerp.service.server.start(preload=[], stop=True)
  47. signal.signal(signal.SIGINT, raise_keyboard_interrupt)
  48. def console(self, local_vars):
  49. if not os.isatty(sys.stdin.fileno()):
  50. exec sys.stdin in local_vars
  51. else:
  52. if 'env' not in local_vars:
  53. print('No environment set, use `odoo.py shell -d dbname`'
  54. ' to get one.')
  55. for i in sorted(local_vars):
  56. print('%s: %s' % (i, local_vars[i]))
  57. logging.disable(logging.CRITICAL)
  58. Console(locals=local_vars).interact()
  59. def shell(self, dbname):
  60. local_vars = {
  61. 'openerp': openerp
  62. }
  63. with Environment.manage():
  64. if dbname:
  65. registry = openerp.modules.registry.RegistryManager.get(dbname)
  66. with registry.cursor() as cr:
  67. uid = openerp.SUPERUSER_ID
  68. ctx = Environment(cr, uid, {})['res.users'].context_get()
  69. env = Environment(cr, uid, ctx)
  70. local_vars['env'] = env
  71. local_vars['self'] = env.user
  72. self.console(local_vars)
  73. else:
  74. self.console(local_vars)
  75. def run(self, args):
  76. self.init(args)
  77. self.shell(openerp.tools.config['db_name'])
  78. return 0