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.

87 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 os
  21. import signal
  22. import sys
  23. import openerp
  24. from openerp.api import Environment
  25. from openerp.cli import Command
  26. def raise_keyboard_interrupt(*a):
  27. raise KeyboardInterrupt()
  28. class Console(code.InteractiveConsole):
  29. def __init__(self, locals=None, filename="<console>"):
  30. code.InteractiveConsole.__init__(self, locals, filename)
  31. try:
  32. import readline
  33. import rlcompleter
  34. except ImportError:
  35. print('readline or rlcompleter not available,'
  36. ' autocomplete disabled.')
  37. else:
  38. readline.set_completer(rlcompleter.Completer(locals).complete)
  39. readline.parse_and_bind("tab: complete")
  40. class Shell(Command):
  41. """Start odoo in an interactive shell"""
  42. def init(self, args):
  43. openerp.tools.config.parse_config(args)
  44. openerp.cli.server.report_configuration()
  45. openerp.service.server.start(preload=[], stop=True)
  46. signal.signal(signal.SIGINT, raise_keyboard_interrupt)
  47. def console(self, local_vars):
  48. if not os.isatty(sys.stdin.fileno()):
  49. exec sys.stdin in local_vars
  50. else:
  51. if 'env' not in local_vars:
  52. print('No environment set, use `odoo.py shell -d dbname`'
  53. ' to get one.')
  54. for i in sorted(local_vars):
  55. print('%s: %s' % (i, local_vars[i]))
  56. Console(locals=local_vars).interact()
  57. def shell(self, dbname):
  58. local_vars = {
  59. 'openerp': openerp
  60. }
  61. with Environment.manage():
  62. if dbname:
  63. registry = openerp.modules.registry.RegistryManager.get(dbname)
  64. with registry.cursor() as cr:
  65. uid = openerp.SUPERUSER_ID
  66. ctx = Environment(cr, uid, {})['res.users'].context_get()
  67. env = Environment(cr, uid, ctx)
  68. local_vars['env'] = env
  69. local_vars['self'] = env.user
  70. self.console(local_vars)
  71. else:
  72. self.console(local_vars)
  73. def run(self, args):
  74. self.init(args)
  75. self.shell(openerp.tools.config['db_name'])
  76. return 0