diff options
| author | Raphael Collet <rco@openerp.com> | 2016-09-02 15:58:39 +0200 |
|---|---|---|
| committer | Raphael Collet <rco@openerp.com> | 2016-09-02 17:28:12 +0200 |
| commit | 9e64f9f9514194d74e166b032add8511182f2677 (patch) | |
| tree | 5086710c04c6b923be18a6d1d97b3f0f3a2a9c4a /odoo/cli/shell.py | |
| parent | 11812b0b9e82b2b1b9041d8250ce01e7b4a5f7ee (diff) | |
[REF] openerp: move `openerp` to `odoo`
Diffstat (limited to 'odoo/cli/shell.py')
| -rw-r--r-- | odoo/cli/shell.py | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/odoo/cli/shell.py b/odoo/cli/shell.py new file mode 100644 index 00000000000..0d1ed59813e --- /dev/null +++ b/odoo/cli/shell.py @@ -0,0 +1,65 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import code +import os +import signal +import sys + +import openerp +from . import Command + +def raise_keyboard_interrupt(*a): + raise KeyboardInterrupt() + +class Console(code.InteractiveConsole): + def __init__(self, locals=None, filename="<console>"): + 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) + 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): + local_vars = { + 'openerp': openerp + } + with openerp.api.Environment.manage(): + if dbname: + registry = openerp.registry(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) + local_vars['env'] = env + local_vars['self'] = env.user + self.console(local_vars) + else: + self.console(local_vars) + + def run(self, args): + self.init(args) + self.shell(openerp.tools.config['db_name']) + return 0 |
