mirror of
https://github.com/bringout/oca-ocb-core.git
synced 2026-04-20 11:12:05 +02:00
19.0 vanilla
This commit is contained in:
parent
0a7ae8db93
commit
991d2234ca
416 changed files with 646602 additions and 300844 deletions
|
|
@ -1,16 +1,17 @@
|
|||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
import code
|
||||
import logging
|
||||
import optparse
|
||||
import os
|
||||
import signal
|
||||
import sys
|
||||
import threading
|
||||
from pathlib import Path
|
||||
|
||||
import odoo
|
||||
import odoo # to expose in the shell
|
||||
from odoo import api
|
||||
from odoo.modules.registry import Registry
|
||||
from odoo.service import server
|
||||
from odoo.tools import config
|
||||
from . import Command
|
||||
from . import Command, server as cli_server
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
|
@ -39,15 +40,15 @@ def raise_keyboard_interrupt(*a):
|
|||
|
||||
|
||||
class Console(code.InteractiveConsole):
|
||||
def __init__(self, locals=None, filename="<console>"):
|
||||
code.InteractiveConsole.__init__(self, locals, filename)
|
||||
def __init__(self, local_vars=None, filename="<console>"):
|
||||
code.InteractiveConsole.__init__(self, locals=local_vars, filename=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.set_completer(rlcompleter.Completer(local_vars).complete)
|
||||
readline.parse_and_bind("tab: complete")
|
||||
|
||||
|
||||
|
|
@ -56,10 +57,23 @@ class Shell(Command):
|
|||
supported_shells = ['ipython', 'ptpython', 'bpython', 'python']
|
||||
|
||||
def init(self, args):
|
||||
config.parser.prog = f'{Path(sys.argv[0]).name} {self.name}'
|
||||
config.parser.prog = self.prog
|
||||
|
||||
group = optparse.OptionGroup(config.parser, "Shell options")
|
||||
group.add_option(
|
||||
'--shell-file', dest='shell_file', type='string', my_default='',
|
||||
help="Specify a python script to be run after the start of the shell. "
|
||||
"Overrides the env variable PYTHONSTARTUP."
|
||||
)
|
||||
group.add_option(
|
||||
'--shell-interface', dest='shell_interface', type='string',
|
||||
help="Specify a preferred REPL to use in shell mode. "
|
||||
"Supported REPLs are: [ipython|ptpython|bpython|python]"
|
||||
)
|
||||
config.parser.add_option_group(group)
|
||||
config.parse_config(args, setup_logging=True)
|
||||
odoo.cli.server.report_configuration()
|
||||
odoo.service.server.start(preload=[], stop=True)
|
||||
cli_server.report_configuration()
|
||||
server.start(preload=[], stop=True)
|
||||
signal.signal(signal.SIGINT, raise_keyboard_interrupt)
|
||||
|
||||
def console(self, local_vars):
|
||||
|
|
@ -72,6 +86,8 @@ class Shell(Command):
|
|||
for i in sorted(local_vars):
|
||||
print('%s: %s' % (i, local_vars[i]))
|
||||
|
||||
pythonstartup = config.options.get('shell_file') or os.environ.get('PYTHONSTARTUP')
|
||||
|
||||
preferred_interface = config.options.get('shell_interface')
|
||||
if preferred_interface:
|
||||
shells_to_try = [preferred_interface, 'python']
|
||||
|
|
@ -80,27 +96,36 @@ class Shell(Command):
|
|||
|
||||
for shell in shells_to_try:
|
||||
try:
|
||||
return getattr(self, shell)(local_vars)
|
||||
shell_func = getattr(self, shell)
|
||||
return shell_func(local_vars, pythonstartup)
|
||||
except ImportError:
|
||||
pass
|
||||
except Exception:
|
||||
_logger.warning("Could not start '%s' shell." % shell)
|
||||
_logger.warning("Could not start '%s' shell.", shell)
|
||||
_logger.debug("Shell error:", exc_info=True)
|
||||
|
||||
def ipython(self, local_vars):
|
||||
from IPython import start_ipython
|
||||
start_ipython(argv=[], user_ns=local_vars)
|
||||
def ipython(self, local_vars, pythonstartup=None):
|
||||
from IPython import start_ipython # noqa: PLC0415
|
||||
argv = (
|
||||
['--TerminalIPythonApp.display_banner=False']
|
||||
+ ([f'--TerminalIPythonApp.exec_files={pythonstartup}'] if pythonstartup else [])
|
||||
)
|
||||
start_ipython(argv=argv, user_ns=local_vars)
|
||||
|
||||
def ptpython(self, local_vars):
|
||||
from ptpython.repl import embed
|
||||
embed({}, local_vars)
|
||||
def ptpython(self, local_vars, pythonstartup=None):
|
||||
from ptpython.repl import embed # noqa: PLC0415
|
||||
embed({}, local_vars, startup_paths=[pythonstartup] if pythonstartup else False)
|
||||
|
||||
def bpython(self, local_vars):
|
||||
from bpython import embed
|
||||
embed(local_vars)
|
||||
def bpython(self, local_vars, pythonstartup=None):
|
||||
from bpython import embed # noqa: PLC0415
|
||||
embed(local_vars, args=['-q', '-i', pythonstartup] if pythonstartup else None)
|
||||
|
||||
def python(self, local_vars):
|
||||
Console(locals=local_vars).interact()
|
||||
def python(self, local_vars, pythonstartup=None):
|
||||
console = Console(local_vars)
|
||||
if pythonstartup:
|
||||
with open(pythonstartup, encoding='utf-8') as f:
|
||||
console.runsource(f.read(), filename=pythonstartup, symbol='exec')
|
||||
console.interact(banner='')
|
||||
|
||||
def shell(self, dbname):
|
||||
local_vars = {
|
||||
|
|
@ -111,9 +136,9 @@ class Shell(Command):
|
|||
threading.current_thread().dbname = dbname
|
||||
registry = Registry(dbname)
|
||||
with registry.cursor() as cr:
|
||||
uid = odoo.SUPERUSER_ID
|
||||
ctx = odoo.api.Environment(cr, uid, {})['res.users'].context_get()
|
||||
env = odoo.api.Environment(cr, uid, ctx)
|
||||
uid = api.SUPERUSER_ID
|
||||
ctx = api.Environment(cr, uid, {})['res.users'].context_get()
|
||||
env = api.Environment(cr, uid, ctx)
|
||||
local_vars['env'] = env
|
||||
local_vars['self'] = env.user
|
||||
# context_get() has started the transaction already. Rollback to
|
||||
|
|
@ -127,5 +152,12 @@ class Shell(Command):
|
|||
|
||||
def run(self, args):
|
||||
self.init(args)
|
||||
self.shell(config['db_name'])
|
||||
|
||||
dbnames = config['db_name']
|
||||
if len(dbnames) > 1:
|
||||
sys.exit("-d/--database/db_name has multiple database, please provide a single one")
|
||||
if not dbnames:
|
||||
self.shell(None)
|
||||
else:
|
||||
self.shell(dbnames[0])
|
||||
return 0
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue