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.

71 lines
2.9 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. import code
  19. import signal
  20. import openerp
  21. from openerp.cli import Command
  22. class Console(code.InteractiveConsole):
  23. def __init__(self, locals=None, filename="<console>"):
  24. code.InteractiveConsole.__init__(self, locals, filename)
  25. try:
  26. import readline
  27. import rlcompleter
  28. except ImportError:
  29. print 'readline or rlcompleter not available, autocomplete disabled.'
  30. else:
  31. readline.set_completer(rlcompleter.Completer(locals).complete)
  32. readline.parse_and_bind("tab: complete")
  33. class Shell(Command):
  34. """Start odoo in an interactive shell"""
  35. def init(self, args):
  36. openerp.tools.config.parse_config(args)
  37. openerp.cli.server.report_configuration()
  38. openerp.service.server.start(preload=[], stop=True)
  39. self.locals = {
  40. 'openerp': openerp
  41. }
  42. def shell(self, dbname):
  43. signal.signal(signal.SIGINT, signal.SIG_DFL)
  44. # TODO: Fix ctrl-c that doesnt seem to generate KeyboardInterrupt
  45. with openerp.api.Environment.manage():
  46. if dbname:
  47. registry = openerp.modules.registry.RegistryManager.get(dbname)
  48. with registry.cursor() as cr:
  49. uid = openerp.SUPERUSER_ID
  50. ctx = openerp.api.Environment(cr, uid, {})['res.users'].context_get()
  51. env = openerp.api.Environment(cr, uid, ctx)
  52. self.locals['env'] = env
  53. self.locals['self'] = env.user
  54. print 'Connected to %s,' % dbname
  55. print ' env: Environement(cr, openerp.SUPERUSER_ID, %s).' % ctx
  56. print ' self: %s.' % env.user
  57. Console(locals=self.locals).interact()
  58. else:
  59. print 'No evironement set, use `odoo.py shell -d dbname` to get one.'
  60. Console(locals=self.locals).interact()
  61. def run(self, args):
  62. self.init(args)
  63. self.shell(openerp.tools.config['db_name'])
  64. return 0