1
- # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
1
import code
3
2
import logging
3
+ import optparse
4
4
import os
5
5
import signal
6
6
import sys
@@ -39,15 +39,15 @@ def raise_keyboard_interrupt(*a):
39
39
40
40
41
41
class Console (code .InteractiveConsole ):
42
- def __init__ (self , locals = None , filename = "<console>" ):
43
- code .InteractiveConsole .__init__ (self , locals , filename )
42
+ def __init__ (self , local_vars = None , filename = "<console>" ):
43
+ code .InteractiveConsole .__init__ (self , locals = local_vars , filename = filename )
44
44
try :
45
45
import readline
46
46
import rlcompleter
47
47
except ImportError :
48
48
print ('readline or rlcompleter not available, autocomplete disabled.' )
49
49
else :
50
- readline .set_completer (rlcompleter .Completer (locals ).complete )
50
+ readline .set_completer (rlcompleter .Completer (local_vars ).complete )
51
51
readline .parse_and_bind ("tab: complete" )
52
52
53
53
@@ -57,6 +57,19 @@ class Shell(Command):
57
57
58
58
def init (self , args ):
59
59
config .parser .prog = f'{ Path (sys .argv [0 ]).name } { self .name } '
60
+
61
+ group = optparse .OptionGroup (config .parser , "Shell options" )
62
+ group .add_option (
63
+ '--shell-file' , dest = 'shell_file' , type = 'string' , default = '' , my_default = '' ,
64
+ help = "Specify a python script to be run after the start of the shell. "
65
+ "Overrides the env variable PYTHONSTARTUP."
66
+ )
67
+ group .add_option (
68
+ '--shell-interface' , dest = 'shell_interface' , type = 'string' ,
69
+ help = "Specify a preferred REPL to use in shell mode. "
70
+ "Supported REPLs are: [ipython|ptpython|bpython|python]"
71
+ )
72
+ config .parser .add_option_group (group )
60
73
config .parse_config (args , setup_logging = True )
61
74
cli_server .report_configuration ()
62
75
server .start (preload = [], stop = True )
@@ -72,6 +85,8 @@ def console(self, local_vars):
72
85
for i in sorted (local_vars ):
73
86
print ('%s: %s' % (i , local_vars [i ]))
74
87
88
+ pythonstartup = config .options .get ('shell_file' ) or os .environ .get ('PYTHONSTARTUP' )
89
+
75
90
preferred_interface = config .options .get ('shell_interface' )
76
91
if preferred_interface :
77
92
shells_to_try = [preferred_interface , 'python' ]
@@ -80,27 +95,36 @@ def console(self, local_vars):
80
95
81
96
for shell in shells_to_try :
82
97
try :
83
- return getattr (self , shell )(local_vars )
98
+ shell_func = getattr (self , shell )
99
+ return shell_func (local_vars , pythonstartup )
84
100
except ImportError :
85
101
pass
86
102
except Exception :
87
- _logger .warning ("Could not start '%s' shell." % shell )
103
+ _logger .warning ("Could not start '%s' shell." , shell )
88
104
_logger .debug ("Shell error:" , exc_info = True )
89
105
90
- def ipython (self , local_vars ):
91
- from IPython import start_ipython
92
- start_ipython (argv = [], user_ns = local_vars )
93
-
94
- def ptpython (self , local_vars ):
95
- from ptpython .repl import embed
96
- embed ({}, local_vars )
97
-
98
- def bpython (self , local_vars ):
99
- from bpython import embed
100
- embed (local_vars )
101
-
102
- def python (self , local_vars ):
103
- Console (locals = local_vars ).interact ()
106
+ def ipython (self , local_vars , pythonstartup = None ):
107
+ from IPython import start_ipython # noqa: PLC0415
108
+ argv = (
109
+ ['--TerminalIPythonApp.display_banner=False' ]
110
+ + ([f'--TerminalIPythonApp.exec_files={ pythonstartup } ' ] if pythonstartup else [])
111
+ )
112
+ start_ipython (argv = argv , user_ns = local_vars )
113
+
114
+ def ptpython (self , local_vars , pythonstartup = None ):
115
+ from ptpython .repl import embed # noqa: PLC0415
116
+ embed ({}, local_vars , startup_paths = [pythonstartup ] if pythonstartup else False )
117
+
118
+ def bpython (self , local_vars , pythonstartup = None ):
119
+ from bpython import embed # noqa: PLC0415
120
+ embed (local_vars , args = ['-q' , '-i' , pythonstartup ] if pythonstartup else None )
121
+
122
+ def python (self , local_vars , pythonstartup = None ):
123
+ console = Console (local_vars )
124
+ if pythonstartup :
125
+ with open (pythonstartup , encoding = 'utf-8' ) as f :
126
+ console .runsource (f .read (), filename = pythonstartup , symbol = 'exec' )
127
+ console .interact (banner = '' )
104
128
105
129
def shell (self , dbname ):
106
130
local_vars = {
0 commit comments