From d1c49f5116ce1db05625c7b6f9ca2412a07c0507 Mon Sep 17 00:00:00 2001 From: Daniel Reis Date: Mon, 22 Dec 2014 22:37:36 +0000 Subject: [PATCH] Backport v9 shell CLI command --- shell/__init__.py | 1 + shell/__openerp__.py | 6 ++++ shell/shell.py | 71 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 shell/__init__.py create mode 100644 shell/__openerp__.py create mode 100644 shell/shell.py diff --git a/shell/__init__.py b/shell/__init__.py new file mode 100644 index 000000000..c82a0d6eb --- /dev/null +++ b/shell/__init__.py @@ -0,0 +1 @@ +from . import shell diff --git a/shell/__openerp__.py b/shell/__openerp__.py new file mode 100644 index 000000000..5a9597430 --- /dev/null +++ b/shell/__openerp__.py @@ -0,0 +1,6 @@ +{ + 'name': 'Shell command backport', + 'description': 'Backport of the v9 shell CLI command.', + 'author': 'Daniel Reis', + 'version': '1.0', +} diff --git a/shell/shell.py b/shell/shell.py new file mode 100644 index 000000000..b79bbce81 --- /dev/null +++ b/shell/shell.py @@ -0,0 +1,71 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +import code +import signal + +import openerp +from openerp.cli import Command + +class Console(code.InteractiveConsole): + def __init__(self, locals=None, filename=""): + code.InteractiveConsole.__init__(self, locals, filename) + try: + import readline + import rlcompleter + except ImportError: + 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 + } + + 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(): + 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() + else: + print 'No evironement set, use `odoo.py shell -d dbname` to get one.' + Console(locals=self.locals).interact() + + def run(self, args): + self.init(args) + self.shell(openerp.tools.config['db_name']) + return 0 +