From d273574407859e95037e4c4e0b346442b98f5a46 Mon Sep 17 00:00:00 2001 From: Daniel Reis Date: Mon, 5 Jan 2015 11:52:00 +0000 Subject: [PATCH] Rebase from master with PR odoo/odoo#4531 --- shell/__init__.py | 2 +- shell/cli/shell.py | 52 ++++++++++++++++++++++++++++++---------------- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/shell/__init__.py b/shell/__init__.py index 9c019084d..8956315a3 100644 --- a/shell/__init__.py +++ b/shell/__init__.py @@ -1 +1 @@ -from cli import shell +from .cli import shell diff --git a/shell/cli/shell.py b/shell/cli/shell.py index b79bbce81..632afc304 100644 --- a/shell/cli/shell.py +++ b/shell/cli/shell.py @@ -16,12 +16,21 @@ # ############################################################################## +from __future__ import print_function import code +import os import signal +import sys import openerp +from openerp.api import Environment from openerp.cli import Command + +def raise_keyboard_interrupt(*a): + raise KeyboardInterrupt() + + class Console(code.InteractiveConsole): def __init__(self, locals=None, filename=""): code.InteractiveConsole.__init__(self, locals, filename) @@ -29,43 +38,50 @@ class Console(code.InteractiveConsole): import readline import rlcompleter except ImportError: - print 'readline or rlcompleter not available, autocomplete disabled.' + print('readline or rlcompleter not available,' + ' autocomplete disabled.') else: readline.set_completer(rlcompleter.Completer(locals).complete) readline.parse_and_bind("tab: complete") + class Shell(Command): """Start odoo in an interactive shell""" def init(self, args): openerp.tools.config.parse_config(args) openerp.cli.server.report_configuration() openerp.service.server.start(preload=[], stop=True) - self.locals = { - 'openerp': openerp - } + signal.signal(signal.SIGINT, raise_keyboard_interrupt) + + def console(self, local_vars): + if not os.isatty(sys.stdin.fileno()): + exec sys.stdin in local_vars + else: + if 'env' not in local_vars: + print('No environment set, use `odoo.py shell -d dbname`' + ' to get one.') + for i in sorted(local_vars): + print('%s: %s' % (i, local_vars[i])) + Console(locals=local_vars).interact() def shell(self, dbname): - signal.signal(signal.SIGINT, signal.SIG_DFL) - # TODO: Fix ctrl-c that doesnt seem to generate KeyboardInterrupt - with openerp.api.Environment.manage(): + local_vars = { + 'openerp': openerp + } + with Environment.manage(): if dbname: registry = openerp.modules.registry.RegistryManager.get(dbname) with registry.cursor() as cr: uid = openerp.SUPERUSER_ID - ctx = openerp.api.Environment(cr, uid, {})['res.users'].context_get() - env = openerp.api.Environment(cr, uid, ctx) - self.locals['env'] = env - self.locals['self'] = env.user - print 'Connected to %s,' % dbname - print ' env: Environement(cr, openerp.SUPERUSER_ID, %s).' % ctx - print ' self: %s.' % env.user - Console(locals=self.locals).interact() + ctx = Environment(cr, uid, {})['res.users'].context_get() + env = Environment(cr, uid, ctx) + local_vars['env'] = env + local_vars['self'] = env.user + self.console(local_vars) else: - print 'No evironement set, use `odoo.py shell -d dbname` to get one.' - Console(locals=self.locals).interact() + self.console(local_vars) def run(self, args): self.init(args) self.shell(openerp.tools.config['db_name']) return 0 -