forked from prompt-toolkit/ptpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_ptpython.py
More file actions
74 lines (58 loc) · 2.16 KB
/
run_ptpython.py
File metadata and controls
74 lines (58 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
"""
ptpython: Interactive Python shell.
Usage:
ptpython [ --vi ]
[ --config-dir=<directory> ] [ --interactive=<filename> ]
[--] [ <arg>... ]
ptpython -h | --help
Options:
--vi : Use Vi keybindings instead of Emacs bindings.
--config-dir=<directory> : Pass config directory. By default '~/.ptpython/'.
-i, --interactive=<filename> : Start interactive shell after executing this file.
Other environment variables:
PYTHONSTARTUP: file executed on interactive startup (no default)
"""
from __future__ import absolute_import, unicode_literals
import docopt
import os
import six
import sys
from ptpython.repl import embed, enable_deprecation_warnings, run_config
def run():
a = docopt.docopt(__doc__)
vi_mode = bool(a['--vi'])
config_dir = os.path.expanduser(a['--config-dir'] or os.path.join('~', '.ptpython'))
# Create config directory.
if not os.path.isdir(config_dir) or not os.path.islink(config_dir):
os.mkdir(config_dir)
# Startup path
startup_paths = []
if 'PYTHONSTARTUP' in os.environ:
startup_paths.append(os.environ['PYTHONSTARTUP'])
# --interactive
if a['--interactive']:
startup_paths.append(a['--interactive'])
sys.argv = [a['--interactive']] + a['<arg>']
# Add the current directory to `sys.path`.
if sys.path[0] != '':
sys.path.insert(0, '')
# When a file has been given, run that, otherwise start the shell.
if a['<arg>'] and not a['--interactive']:
sys.argv = a['<arg>']
six.exec_(compile(open(a['<arg>'][0], "rb").read(), a['<arg>'][0], 'exec'))
# Run interactive shell.
else:
enable_deprecation_warnings()
# Apply config file
def configure(repl):
path = os.path.join(config_dir, 'config.py')
if os.path.exists(path):
run_config(repl, path)
embed(vi_mode=vi_mode,
history_filename=os.path.join(config_dir, 'history'),
configure=configure,
startup_paths=startup_paths,
title='Python REPL (ptpython)')
if __name__ == '__main__':
run()